Search in sources :

Example 6 with MultiblockCapability

use of com.lowdragmc.multiblocked.api.capability.MultiblockCapability in project Multiblocked by Low-Drag-MC.

the class PredicateAnyCapability method getConfigWidget.

@Override
public List<WidgetGroup> getConfigWidget(List<WidgetGroup> groups) {
    super.getConfigWidget(groups);
    WidgetGroup group = new WidgetGroup(0, 0, 100, 20);
    groups.add(group);
    MultiblockCapability<?> current = MbdCapabilities.get(capability);
    group.addWidget(new SelectorWidget(0, 0, 120, 20, MbdCapabilities.CAPABILITY_REGISTRY.values().stream().map(MultiblockCapability::getUnlocalizedName).collect(Collectors.toList()), -1).setValue(current == null ? "" : current.getUnlocalizedName()).setOnChanged(capability -> {
        this.capability = capability.replace("multiblocked.capability.", "");
        buildPredicate();
    }).setButtonBackground(ResourceBorderTexture.BUTTON_COMMON).setBackground(new ColorRectTexture(0xffaaaaaa)).setHoverTooltips("multiblocked.gui.predicate.capability"));
    return groups;
}
Also used : MultiblockCapability(com.lowdragmc.multiblocked.api.capability.MultiblockCapability) SelectorWidget(com.lowdragmc.lowdraglib.gui.widget.SelectorWidget) WidgetGroup(com.lowdragmc.lowdraglib.gui.widget.WidgetGroup) ColorRectTexture(com.lowdragmc.lowdraglib.gui.texture.ColorRectTexture)

Example 7 with MultiblockCapability

use of com.lowdragmc.multiblocked.api.capability.MultiblockCapability in project Multiblocked by Low-Drag-MC.

the class ComponentTileEntity method load.

@Override
public void load(@Nonnull BlockState blockState, @Nonnull CompoundNBT compound) {
    super.load(blockState, compound);
    if (needAlwaysUpdate()) {
        MultiblockWorldSavedData.getOrCreate(level).addLoading(this);
    }
    if (compound.contains("owner")) {
        this.owner = compound.getUUID("owner");
    }
    CompoundNBT traitTag = compound.getCompound("trait");
    for (Map.Entry<MultiblockCapability<?>, CapabilityTrait> entry : traits.entrySet()) {
        entry.getValue().readFromNBT(traitTag.getCompound(entry.getKey().name));
    }
    if (compound.contains("persisted")) {
        persistedData = compound.get("persisted");
    }
}
Also used : CompoundNBT(net.minecraft.nbt.CompoundNBT) MultiblockCapability(com.lowdragmc.multiblocked.api.capability.MultiblockCapability) CapabilityTrait(com.lowdragmc.multiblocked.api.capability.trait.CapabilityTrait) Map(java.util.Map) HashMap(java.util.HashMap)

Example 8 with MultiblockCapability

use of com.lowdragmc.multiblocked.api.capability.MultiblockCapability in project Multiblocked by Low-Drag-MC.

the class ControllerTileEntity method save.

@Nonnull
@Override
public CompoundNBT save(@Nonnull CompoundNBT compound) {
    super.save(compound);
    if (!asyncRecipeSearching) {
        compound.putBoolean("ars", false);
    }
    if (recipeLogic != null)
        compound.put("recipeLogic", recipeLogic.writeToNBT(new CompoundNBT()));
    if (capabilities != null) {
        ListNBT tagList = new ListNBT();
        for (Table.Cell<IO, MultiblockCapability<?>, Long2ObjectOpenHashMap<CapabilityProxy<?>>> cell : capabilities.cellSet()) {
            IO io = cell.getRowKey();
            MultiblockCapability<?> cap = cell.getColumnKey();
            Long2ObjectOpenHashMap<CapabilityProxy<?>> value = cell.getValue();
            if (io != null && cap != null && value != null) {
                for (Map.Entry<Long, CapabilityProxy<?>> entry : value.entrySet()) {
                    CompoundNBT tag = new CompoundNBT();
                    tag.putInt("io", io.ordinal());
                    tag.putInt("facing", entry.getValue().facing.ordinal());
                    tag.putString("cap", cap.name);
                    tag.putLong("pos", entry.getKey());
                    tagList.add(tag);
                }
            }
        }
        compound.put("capabilities", tagList);
    }
    return compound;
}
Also used : Table(com.google.common.collect.Table) CompoundNBT(net.minecraft.nbt.CompoundNBT) MultiblockCapability(com.lowdragmc.multiblocked.api.capability.MultiblockCapability) IO(com.lowdragmc.multiblocked.api.capability.IO) Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap) ListNBT(net.minecraft.nbt.ListNBT) Object2ObjectOpenHashMap(it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap) Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap) Map(java.util.Map) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap) CapabilityProxy(com.lowdragmc.multiblocked.api.capability.proxy.CapabilityProxy) Nonnull(javax.annotation.Nonnull)

Example 9 with MultiblockCapability

use of com.lowdragmc.multiblocked.api.capability.MultiblockCapability in project Multiblocked by Low-Drag-MC.

the class ControllerTileEntity method onStructureFormed.

/**
 * Called when its formed, server side only.
 */
public void onStructureFormed() {
    if (recipeLogic == null) {
        recipeLogic = new RecipeLogic(this);
    }
    if (status.equals("unformed")) {
        setStatus("idle");
    }
    // init capabilities
    Map<Long, EnumMap<IO, Set<MultiblockCapability<?>>>> capabilityMap = state.getMatchContext().get("capabilities");
    if (capabilityMap != null) {
        capabilities = Tables.newCustomTable(new EnumMap<>(IO.class), Object2ObjectOpenHashMap::new);
        for (Map.Entry<Long, EnumMap<IO, Set<MultiblockCapability<?>>>> entry : capabilityMap.entrySet()) {
            TileEntity tileEntity = level.getBlockEntity(BlockPos.of(entry.getKey()));
            if (tileEntity != null) {
                if (settings != null) {
                    Map<MultiblockCapability<?>, Tuple<IO, Direction>> caps = settings.get(entry.getKey());
                    if (caps != null) {
                        for (Map.Entry<MultiblockCapability<?>, Tuple<IO, Direction>> ioEntry : caps.entrySet()) {
                            MultiblockCapability<?> capability = ioEntry.getKey();
                            Tuple<IO, Direction> tuple = ioEntry.getValue();
                            if (tuple == null || capability == null)
                                continue;
                            IO io = tuple.getA();
                            Direction facing = tuple.getB();
                            if (capability.isBlockHasCapability(io, tileEntity)) {
                                if (!capabilities.contains(io, capability)) {
                                    capabilities.put(io, capability, new Long2ObjectOpenHashMap<>());
                                }
                                CapabilityProxy<?> proxy = capability.createProxy(io, tileEntity);
                                proxy.facing = facing;
                                capabilities.get(io, capability).put(entry.getKey().longValue(), proxy);
                            }
                        }
                    }
                } else {
                    entry.getValue().forEach((io, set) -> {
                        for (MultiblockCapability<?> capability : set) {
                            if (capability.isBlockHasCapability(io, tileEntity)) {
                                if (!capabilities.contains(io, capability)) {
                                    capabilities.put(io, capability, new Long2ObjectOpenHashMap<>());
                                }
                                CapabilityProxy<?> proxy = capability.createProxy(io, tileEntity);
                                capabilities.get(io, capability).put(entry.getKey().longValue(), proxy);
                            }
                        }
                    });
                }
            }
        }
    }
    settings = null;
    // init parts
    parts = state.getMatchContext().get("parts");
    if (parts != null) {
        for (Long pos : parts) {
            TileEntity tileEntity = level.getBlockEntity(BlockPos.of(pos));
            if (tileEntity instanceof IPartComponent) {
                ((IPartComponent) tileEntity).addedToController(this);
            }
        }
    }
    writeCustomData(-1, this::writeState);
    if (Multiblocked.isKubeJSLoaded()) {
        new StructureFormedEvent(this).post(ScriptType.SERVER, StructureFormedEvent.ID, getSubID());
    }
}
Also used : IPartComponent(com.lowdragmc.multiblocked.api.tile.part.IPartComponent) MultiblockCapability(com.lowdragmc.multiblocked.api.capability.MultiblockCapability) IO(com.lowdragmc.multiblocked.api.capability.IO) Direction(net.minecraft.util.Direction) TileEntity(net.minecraft.tileentity.TileEntity) RecipeLogic(com.lowdragmc.multiblocked.api.recipe.RecipeLogic) EnumMap(java.util.EnumMap) Object2ObjectOpenHashMap(it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap) Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap) Map(java.util.Map) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap) Tuple(net.minecraft.util.Tuple)

Example 10 with MultiblockCapability

use of com.lowdragmc.multiblocked.api.capability.MultiblockCapability in project Multiblocked by Low-Drag-MC.

the class Recipe method handleRecipe.

@SuppressWarnings("ALL")
public boolean handleRecipe(IO io, ICapabilityProxyHolder holder, ImmutableMap<MultiblockCapability<?>, ImmutableList<Tuple<Object, Float>>> contents) {
    Table<IO, MultiblockCapability<?>, Long2ObjectOpenHashMap<CapabilityProxy<?>>> capabilityProxies = holder.getCapabilitiesProxy();
    for (Map.Entry<MultiblockCapability<?>, ImmutableList<Tuple<Object, Float>>> entry : contents.entrySet()) {
        Set<CapabilityProxy<?>> used = new HashSet<>();
        List content = new ArrayList<>();
        for (Tuple<Object, Float> tuple : entry.getValue()) {
            if (tuple.getB() == 1 || Multiblocked.RNG.nextFloat() < tuple.getB()) {
                // chance input
                content.add(tuple.getA());
            }
        }
        if (content.isEmpty())
            continue;
        if (capabilityProxies.contains(io, entry.getKey())) {
            for (CapabilityProxy<?> proxy : capabilityProxies.get(io, entry.getKey()).values()) {
                // search same io type
                if (used.contains(proxy))
                    continue;
                used.add(proxy);
                content = proxy.handleRecipe(io, this, content);
                if (content == null)
                    break;
            }
        }
        if (content == null)
            continue;
        if (capabilityProxies.contains(IO.BOTH, entry.getKey())) {
            for (CapabilityProxy<?> proxy : capabilityProxies.get(IO.BOTH, entry.getKey()).values()) {
                // search both type
                if (used.contains(proxy))
                    continue;
                used.add(proxy);
                content = proxy.handleRecipe(io, this, content);
                if (content == null)
                    break;
            }
        }
        if (content != null) {
            Multiblocked.LOGGER.warn("io error while handling a recipe {} outputs. holder: {}", uid, holder);
            return false;
        }
    }
    return true;
}
Also used : MultiblockCapability(com.lowdragmc.multiblocked.api.capability.MultiblockCapability) IO(com.lowdragmc.multiblocked.api.capability.IO) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ImmutableMap(com.google.common.collect.ImmutableMap) Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap) Map(java.util.Map) CapabilityProxy(com.lowdragmc.multiblocked.api.capability.proxy.CapabilityProxy) HashSet(java.util.HashSet)

Aggregations

MultiblockCapability (com.lowdragmc.multiblocked.api.capability.MultiblockCapability)14 Map (java.util.Map)9 IO (com.lowdragmc.multiblocked.api.capability.IO)8 Long2ObjectOpenHashMap (it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap)7 HashMap (java.util.HashMap)6 ArrayList (java.util.ArrayList)5 EnumMap (java.util.EnumMap)5 Direction (net.minecraft.util.Direction)5 Tuple (net.minecraft.util.Tuple)4 CapabilityProxy (com.lowdragmc.multiblocked.api.capability.proxy.CapabilityProxy)3 PatternStringError (com.lowdragmc.multiblocked.api.pattern.error.PatternStringError)3 ControllerTileEntity (com.lowdragmc.multiblocked.api.tile.ControllerTileEntity)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Set (java.util.Set)3 TileEntity (net.minecraft.tileentity.TileEntity)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Table (com.google.common.collect.Table)2 JsonObject (com.google.gson.JsonObject)2