Search in sources :

Example 11 with MultiblockCapability

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

Example 12 with MultiblockCapability

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

the class IOPageWidget method refresh.

private void refresh() {
    List<MultiblockCapability<?>> values = new ArrayList<>(capabilitySettings.keySet());
    for (int i = index; i < index + 3; i++) {
        MultiblockCapability<?> capability = null;
        if (i < values.size()) {
            capability = values.get(i);
            labels[i - index].setBackgroundColor(capability.color).updateText(capability.getUnlocalizedName());
            if (capabilitySettings.get(capability) != null && capabilitySettings.get(capability).getA() != null) {
                selectors[i - index].setValue(capabilitySettings.get(capability).getB().name()).setVisible(true);
            } else {
                selectors[i - index].setVisible(false);
            }
        } else {
            labels[i - index].setBackgroundColor(0).updateText("Empty");
            selectors[i - index].setVisible(false);
        }
        IO io = capabilitySettings.get(capability) == null ? null : capabilitySettings.get(capability).getA();
        lines[0][i - index].setImage(LINE_0_MAP.get(io));
        lines[1][i - index].setImage(LINE_1_MAP.get(io));
    }
}
Also used : MultiblockCapability(com.lowdragmc.multiblocked.api.capability.MultiblockCapability) IO(com.lowdragmc.multiblocked.api.capability.IO) ArrayList(java.util.ArrayList)

Example 13 with MultiblockCapability

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

the class IOPageWidget method click.

@SuppressWarnings("ConstantConditions")
private void click(ClickData clickData, int index, IO io) {
    if (clickData.isRemote) {
        EnumMap<IO, Set<MultiblockCapability<?>>> enumMap;
        if (pos != null && capabilityMap.containsKey(pos.asLong())) {
            enumMap = capabilityMap.get(pos.asLong());
        } else {
            return;
        }
        List<MultiblockCapability<?>> values = new ArrayList<>(capabilitySettings.keySet());
        if (index >= 0 && index < values.size()) {
            MultiblockCapability<?> capability = values.get(index);
            Tuple<IO, Direction> tuple = capabilitySettings.get(capability);
            IO originalIO = tuple == null ? null : tuple.getA();
            Direction originalFacing = tuple == null ? Direction.UP : tuple.getB();
            boolean find = false;
            if (enumMap.get(io) != null && enumMap.get(io).contains(capability)) {
                find = true;
            }
            if (enumMap.get(IO.BOTH) != null && enumMap.get(IO.BOTH).contains(capability)) {
                find = true;
            }
            if (!find)
                return;
            if (originalIO == null) {
                capabilitySettings.put(capability, new Tuple<>(io, originalFacing));
            } else if (originalIO == io) {
                capabilitySettings.put(capability, new Tuple<>(null, originalFacing));
            } else if (originalIO == IO.BOTH) {
                capabilitySettings.put(capability, new Tuple<>(io == IO.IN ? IO.OUT : IO.IN, originalFacing));
            } else {
                capabilitySettings.put(capability, new Tuple<>(IO.BOTH, originalFacing));
            }
            writeClientAction(-1, buffer -> {
                buffer.writeUtf(capability.name);
                buffer.writeBoolean(originalIO != null);
                if (originalIO != null) {
                    buffer.writeEnum(originalIO);
                }
                IO newIO = capabilitySettings.get(capability).getA();
                buffer.writeBoolean(newIO != null);
                if (newIO != null) {
                    buffer.writeEnum(newIO);
                    buffer.writeEnum(capabilitySettings.get(capability).getB());
                }
            });
            refresh();
        }
    }
}
Also used : Set(java.util.Set) MultiblockCapability(com.lowdragmc.multiblocked.api.capability.MultiblockCapability) IO(com.lowdragmc.multiblocked.api.capability.IO) ArrayList(java.util.ArrayList) Direction(net.minecraft.util.Direction) Tuple(net.minecraft.util.Tuple)

Example 14 with MultiblockCapability

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

the class PredicateAnyCapability method checkCapability.

private static boolean checkCapability(IO io, MultiblockCapability<?> capability, MultiblockState state) {
    if (io != null) {
        TileEntity tileEntity = state.getTileEntity();
        if (tileEntity != null && capability.isBlockHasCapability(io, tileEntity)) {
            Map<Long, EnumMap<IO, Set<MultiblockCapability<?>>>> capabilities = state.getMatchContext().getOrCreate("capabilities", Long2ObjectOpenHashMap::new);
            capabilities.computeIfAbsent(state.getPos().asLong(), l -> new EnumMap<>(IO.class)).computeIfAbsent(io, x -> new HashSet<>()).add(capability);
            return true;
        }
    }
    state.setError(new PatternStringError(LocalizationUtils.format("multiblocked.pattern.error.capability", LocalizationUtils.format(capability.getUnlocalizedName()), io == null ? "NULL" : io.name())));
    return false;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) JSONUtils(net.minecraft.util.JSONUtils) JsonObject(com.google.gson.JsonObject) LocalizationUtils(com.lowdragmc.lowdraglib.utils.LocalizationUtils) EnumMap(java.util.EnumMap) MultiblockState(com.lowdragmc.multiblocked.api.pattern.MultiblockState) Set(java.util.Set) MultiblockCapability(com.lowdragmc.multiblocked.api.capability.MultiblockCapability) ResourceBorderTexture(com.lowdragmc.lowdraglib.gui.texture.ResourceBorderTexture) Collectors(java.util.stream.Collectors) Blocks(net.minecraft.block.Blocks) MbdCapabilities(com.lowdragmc.multiblocked.api.registry.MbdCapabilities) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) SelectorWidget(com.lowdragmc.lowdraglib.gui.widget.SelectorWidget) BlockInfo(com.lowdragmc.lowdraglib.utils.BlockInfo) List(java.util.List) ColorRectTexture(com.lowdragmc.lowdraglib.gui.texture.ColorRectTexture) Gson(com.google.gson.Gson) Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap) Map(java.util.Map) IO(com.lowdragmc.multiblocked.api.capability.IO) TileEntity(net.minecraft.tileentity.TileEntity) WidgetGroup(com.lowdragmc.lowdraglib.gui.widget.WidgetGroup) PatternStringError(com.lowdragmc.multiblocked.api.pattern.error.PatternStringError) MultiblockCapability(com.lowdragmc.multiblocked.api.capability.MultiblockCapability) IO(com.lowdragmc.multiblocked.api.capability.IO) Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap) PatternStringError(com.lowdragmc.multiblocked.api.pattern.error.PatternStringError) EnumMap(java.util.EnumMap) 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