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);
}
}
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));
}
}
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();
}
}
}
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;
}
Aggregations