Search in sources :

Example 1 with CapabilityProxy

use of com.lowdragmc.multiblocked.api.capability.proxy.CapabilityProxy 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 2 with CapabilityProxy

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

the class ControllerTileEntity method asyncThreadLogic.

@Override
public void asyncThreadLogic(long periodID) {
    if (!isFormed() && getDefinition().catalyst == null && (getOffset() + periodID) % 4 == 0) {
        BlockPattern pattern = getPattern();
        if (pattern != null && pattern.checkPatternAt(new MultiblockState(level, worldPosition), false)) {
            ServerLifecycleHooks.getCurrentServer().execute(() -> {
                if (state == null)
                    state = new MultiblockState(level, worldPosition);
                if (checkPattern()) {
                    // formed
                    MultiblockWorldSavedData.getOrCreate(level).addMapping(state);
                    onStructureFormed();
                }
            });
        }
    }
    try {
        if (hasProxies()) {
            // should i add locks for proxies?
            for (Long2ObjectOpenHashMap<CapabilityProxy<?>> map : getCapabilitiesProxy().values()) {
                if (map != null) {
                    for (CapabilityProxy<?> proxy : map.values()) {
                        if (proxy != null) {
                            proxy.updateChangedState(periodID);
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        Multiblocked.LOGGER.error("something run while checking proxy changes");
    }
}
Also used : BlockPattern(com.lowdragmc.multiblocked.api.pattern.BlockPattern) MultiblockState(com.lowdragmc.multiblocked.api.pattern.MultiblockState) CapabilityProxy(com.lowdragmc.multiblocked.api.capability.proxy.CapabilityProxy)

Example 3 with CapabilityProxy

use of com.lowdragmc.multiblocked.api.capability.proxy.CapabilityProxy 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)

Example 4 with CapabilityProxy

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

the class IOPageWidget method handleClientAction.

@Override
public void handleClientAction(int id, PacketBuffer buffer) {
    if (id == -1) {
        MultiblockCapability<?> capability = MbdCapabilities.get(buffer.readUtf());
        Table<IO, MultiblockCapability<?>, Long2ObjectOpenHashMap<CapabilityProxy<?>>> capabilities = controller.getCapabilitiesProxy();
        if (buffer.readBoolean()) {
            IO io = buffer.readEnum(IO.class);
            capabilities.get(io, capability).remove(pos.asLong());
            if (capabilities.get(io, capability).isEmpty()) {
                capabilities.remove(io, capability);
            }
        }
        if (buffer.readBoolean()) {
            IO io = buffer.readEnum(IO.class);
            TileEntity entity = controller.getLevel().getBlockEntity(pos);
            if (entity != null && capability.isBlockHasCapability(io, entity)) {
                if (!capabilities.contains(io, capability)) {
                    capabilities.put(io, capability, new Long2ObjectOpenHashMap<>());
                }
                CapabilityProxy<?> proxy = capability.createProxy(io, entity);
                proxy.facing = buffer.readEnum(Direction.class);
                capabilities.get(io, capability).put(pos.asLong(), proxy);
            }
        }
        controller.markAsDirty();
    } else if (id == -2) {
        MultiblockCapability<?> capability = MbdCapabilities.get(buffer.readUtf());
        Table<IO, MultiblockCapability<?>, Long2ObjectOpenHashMap<CapabilityProxy<?>>> capabilities = controller.getCapabilitiesProxy();
        IO io = buffer.readEnum(IO.class);
        TileEntity entity = controller.getLevel().getBlockEntity(pos);
        if (entity != null && capability.isBlockHasCapability(io, entity)) {
            if (!capabilities.contains(io, capability)) {
                capabilities.put(io, capability, new Long2ObjectOpenHashMap<>());
            }
            CapabilityProxy<?> proxy = capability.createProxy(io, entity);
            proxy.facing = buffer.readEnum(Direction.class);
            capabilities.get(io, capability).put(pos.asLong(), proxy);
        }
        controller.markAsDirty();
    } else {
        super.handleClientAction(id, buffer);
    }
}
Also used : ControllerTileEntity(com.lowdragmc.multiblocked.api.tile.ControllerTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) Table(com.google.common.collect.Table) MultiblockCapability(com.lowdragmc.multiblocked.api.capability.MultiblockCapability) IO(com.lowdragmc.multiblocked.api.capability.IO) Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap) Direction(net.minecraft.util.Direction) CapabilityProxy(com.lowdragmc.multiblocked.api.capability.proxy.CapabilityProxy)

Aggregations

CapabilityProxy (com.lowdragmc.multiblocked.api.capability.proxy.CapabilityProxy)4 IO (com.lowdragmc.multiblocked.api.capability.IO)3 MultiblockCapability (com.lowdragmc.multiblocked.api.capability.MultiblockCapability)3 Long2ObjectOpenHashMap (it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap)3 Table (com.google.common.collect.Table)2 Map (java.util.Map)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 BlockPattern (com.lowdragmc.multiblocked.api.pattern.BlockPattern)1 MultiblockState (com.lowdragmc.multiblocked.api.pattern.MultiblockState)1 ControllerTileEntity (com.lowdragmc.multiblocked.api.tile.ControllerTileEntity)1 Object2ObjectOpenHashMap (it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap)1 ArrayList (java.util.ArrayList)1 EnumMap (java.util.EnumMap)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Nonnull (javax.annotation.Nonnull)1 CompoundNBT (net.minecraft.nbt.CompoundNBT)1 ListNBT (net.minecraft.nbt.ListNBT)1