From f4f66324c044984354f380adfd95666979d02477 Mon Sep 17 00:00:00 2001 From: Des Herriott Date: Thu, 29 Feb 2024 15:17:27 +0000 Subject: [PATCH] fixed some networking problems --- build.gradle | 2 +- .../container/BulkItemFilterMenu.java | 5 +++++ .../container/handler/BaseModuleHandler.java | 13 +++++++++--- .../network/ClientPayloadHandler.java | 16 +++++--------- .../network/NetworkHandler.java | 21 ++++++++++--------- .../network/ServerPayloadHandler.java | 20 +++++++----------- .../messages/RouterSettingsMessage.java | 4 ++-- .../modularrouters/util/SetofItemStack.java | 2 +- 8 files changed, 42 insertions(+), 41 deletions(-) diff --git a/build.gradle b/build.gradle index cfba2a5d..075abcb2 100644 --- a/build.gradle +++ b/build.gradle @@ -106,7 +106,7 @@ dependencies { // exclude group: "net.minecraftforge", module: "forge" //} - implementation "mcjty.theoneprobe:theoneprobe:${top_version}" + compileOnly "mcjty.theoneprobe:theoneprobe:${top_version}" implementation "curse.maven:jade-324717:${jade_curse_id}" diff --git a/src/main/java/me/desht/modularrouters/container/BulkItemFilterMenu.java b/src/main/java/me/desht/modularrouters/container/BulkItemFilterMenu.java index ed8d1107..329c8a76 100644 --- a/src/main/java/me/desht/modularrouters/container/BulkItemFilterMenu.java +++ b/src/main/java/me/desht/modularrouters/container/BulkItemFilterMenu.java @@ -61,9 +61,11 @@ public BulkItemFilterMenu(int windowId, Inventory invPlayer, MFLocator loc) { } public void clearSlots() { + handler.setAutoSave(false); for (int i = 0; i < handler.getSlots(); i++) { handler.setStackInSlot(i, ItemStack.EMPTY); } + handler.setAutoSave(true); handler.save(); if (getRouter() != null && !getRouter().getLevel().isClientSide) { @@ -86,12 +88,15 @@ public int mergeInventory(IItemHandler srcInv, Filter.Flags flags, boolean clear } int slot = 0; + handler.setAutoSave(false); for (ItemStack stack : stacks.sortedList()) { handler.setStackInSlot(slot++, stack); } while (slot < handler.getSlots()) { handler.setStackInSlot(slot++, ItemStack.EMPTY); } + + handler.setAutoSave(true); handler.save(); if (getRouter() != null && !getRouter().getLevel().isClientSide) { diff --git a/src/main/java/me/desht/modularrouters/container/handler/BaseModuleHandler.java b/src/main/java/me/desht/modularrouters/container/handler/BaseModuleHandler.java index cab95afd..7850e3c9 100644 --- a/src/main/java/me/desht/modularrouters/container/handler/BaseModuleHandler.java +++ b/src/main/java/me/desht/modularrouters/container/handler/BaseModuleHandler.java @@ -16,6 +16,7 @@ public abstract class BaseModuleHandler extends GhostItemHandler { private final ItemStack holderStack; protected final ModularRouterBlockEntity router; private final String tagName; + private boolean autoSave = true; public BaseModuleHandler(ItemStack holderStack, ModularRouterBlockEntity router, int size, String tagName) { super(size); @@ -26,6 +27,10 @@ public BaseModuleHandler(ItemStack holderStack, ModularRouterBlockEntity router, deserializeNBT(ModuleHelper.validateNBT(holderStack).getCompound(tagName)); } + public void setAutoSave(boolean autoSave) { + this.autoSave = autoSave; + } + /** * Get the itemstack which holds this filter. Could be a module, could be a bulk item filter... * @@ -37,10 +42,12 @@ public ItemStack getHolderStack() { @Override protected void onContentsChanged(int slot) { - save(); + if (autoSave) { + save(); - if (router != null) { - router.recompileNeeded(ModularRouterBlockEntity.COMPILE_MODULES); + if (router != null) { + router.recompileNeeded(ModularRouterBlockEntity.COMPILE_MODULES); + } } } diff --git a/src/main/java/me/desht/modularrouters/network/ClientPayloadHandler.java b/src/main/java/me/desht/modularrouters/network/ClientPayloadHandler.java index 50ecf942..e4f1d415 100644 --- a/src/main/java/me/desht/modularrouters/network/ClientPayloadHandler.java +++ b/src/main/java/me/desht/modularrouters/network/ClientPayloadHandler.java @@ -13,14 +13,8 @@ import net.minecraft.world.phys.Vec3; import net.neoforged.neoforge.network.handling.PlayPayloadContext; -public enum ClientPayloadHandler { - INSTANCE; - - public static ClientPayloadHandler getInstance() { - return INSTANCE; - } - - public void handleData(GuiSyncMessage message, PlayPayloadContext context) { +public class ClientPayloadHandler { + public static void handleData(GuiSyncMessage message, PlayPayloadContext context) { context.workHandler().submitAsync(() -> { if (Minecraft.getInstance().screen instanceof IResyncableGui syncable) { syncable.resync(message.newStack()); @@ -28,13 +22,13 @@ public void handleData(GuiSyncMessage message, PlayPayloadContext context) { }); } - public void handleData(ItemBeamMessage message, PlayPayloadContext context) { + public static void handleData(ItemBeamMessage message, PlayPayloadContext context) { context.workHandler().submitAsync(() -> Minecraft.getInstance().level.getBlockEntity(message.pos(), ModBlockEntities.MODULAR_ROUTER.get()) .ifPresent(te -> message.beams().forEach(te::addItemBeam))); } - public void handleData(PushEntityMessage message, PlayPayloadContext context) { + public static void handleData(PushEntityMessage message, PlayPayloadContext context) { context.workHandler().submitAsync(() -> { Entity entity = Minecraft.getInstance().level.getEntity(message.entityId()); if (entity != null) { @@ -47,7 +41,7 @@ public void handleData(PushEntityMessage message, PlayPayloadContext context) { }); } - public void handleData(RouterUpgradesSyncMessage message, PlayPayloadContext context) { + public static void handleData(RouterUpgradesSyncMessage message, PlayPayloadContext context) { context.workHandler().submitAsync(() -> { Level level = Minecraft.getInstance().level; if (level != null && level.isLoaded(message.pos())) { diff --git a/src/main/java/me/desht/modularrouters/network/NetworkHandler.java b/src/main/java/me/desht/modularrouters/network/NetworkHandler.java index 03939d8e..58932aa8 100644 --- a/src/main/java/me/desht/modularrouters/network/NetworkHandler.java +++ b/src/main/java/me/desht/modularrouters/network/NetworkHandler.java @@ -14,6 +14,7 @@ public class NetworkHandler { private static final String NETWORK_VERSION = "1.0"; + @SuppressWarnings("Convert2MethodRef") @SubscribeEvent public static void register(final RegisterPayloadHandlerEvent event) { final IPayloadRegistrar registrar = event.registrar(ModularRouters.MODID) @@ -21,27 +22,27 @@ public static void register(final RegisterPayloadHandlerEvent event) { // clientbound registrar.play(GuiSyncMessage.ID, GuiSyncMessage::new, handler -> handler - .client(ClientPayloadHandler.getInstance()::handleData)); + .client((message, context) -> ClientPayloadHandler.handleData(message, context))); registrar.play(ItemBeamMessage.ID, ItemBeamMessage::fromNetwork, handler -> handler - .client(ClientPayloadHandler.getInstance()::handleData)); + .client((message, context) -> ClientPayloadHandler.handleData(message, context))); registrar.play(PushEntityMessage.ID, PushEntityMessage::new, handler -> handler - .client(ClientPayloadHandler.getInstance()::handleData)); + .client((message, context) -> ClientPayloadHandler.handleData(message, context))); registrar.play(RouterUpgradesSyncMessage.ID, RouterUpgradesSyncMessage::new, handler -> handler - .client(ClientPayloadHandler.getInstance()::handleData)); + .client((message, context) -> ClientPayloadHandler.handleData(message, context))); // serverbound registrar.play(FilterSettingsMessage.ID, FilterSettingsMessage::new, handler -> handler - .server(ServerPayloadHandler.getInstance()::handleData)); + .server((message, context) -> ServerPayloadHandler.handleData(message, context))); registrar.play(ModuleFilterMessage.ID, ModuleFilterMessage::new, handler -> handler - .server(ServerPayloadHandler.getInstance()::handleData)); + .server((message, context) -> ServerPayloadHandler.handleData(message, context))); registrar.play(ModuleSettingsMessage.ID, ModuleSettingsMessage::new, handler -> handler - .server(ServerPayloadHandler.getInstance()::handleData)); + .server((message, context) -> ServerPayloadHandler.handleData(message, context))); registrar.play(OpenGuiMessage.ID, OpenGuiMessage::new, handler -> handler - .server(ServerPayloadHandler.getInstance()::handleData)); + .server((message, context) -> ServerPayloadHandler.handleData(message, context))); registrar.play(SyncUpgradeSettingsMessage.ID, SyncUpgradeSettingsMessage::new, handler -> handler - .server(ServerPayloadHandler.getInstance()::handleData)); + .server((message, context) -> ServerPayloadHandler.handleData(message, context))); registrar.play(ValidateModuleMessage.ID, ValidateModuleMessage::new, handler -> handler - .server(ServerPayloadHandler.getInstance()::handleData)); + .server((message, context) -> ServerPayloadHandler.handleData(message, context))); // bidirectional registrar.play(RouterSettingsMessage.ID, RouterSettingsMessage::new, handler -> handler diff --git a/src/main/java/me/desht/modularrouters/network/ServerPayloadHandler.java b/src/main/java/me/desht/modularrouters/network/ServerPayloadHandler.java index 33555295..67d391f6 100644 --- a/src/main/java/me/desht/modularrouters/network/ServerPayloadHandler.java +++ b/src/main/java/me/desht/modularrouters/network/ServerPayloadHandler.java @@ -22,14 +22,8 @@ import java.util.Objects; -public enum ServerPayloadHandler { - INSTANCE; - - public static ServerPayloadHandler getInstance() { - return INSTANCE; - } - - public void handleData(FilterSettingsMessage message, PlayPayloadContext context) { +public class ServerPayloadHandler { + public static void handleData(FilterSettingsMessage message, PlayPayloadContext context) { context.player().ifPresent(player -> context.workHandler().submitAsync(() -> { MFLocator locator = message.locator(); ItemStack moduleStack = locator.getModuleStack(player); @@ -60,7 +54,7 @@ public void handleData(FilterSettingsMessage message, PlayPayloadContext context })); } - public void handleData(ModuleFilterMessage message, PlayPayloadContext context) { + public static void handleData(ModuleFilterMessage message, PlayPayloadContext context) { context.player().ifPresent(player -> context.workHandler().submitAsync(() -> { AbstractContainerMenu c = player.containerMenu; int slot = message.slot(); @@ -74,7 +68,7 @@ private static boolean isValidContainer(AbstractContainerMenu c) { return c instanceof ModuleMenu || c instanceof BulkItemFilterMenu; } - public void handleData(ModuleSettingsMessage message, PlayPayloadContext context) { + public static void handleData(ModuleSettingsMessage message, PlayPayloadContext context) { context.player().ifPresent(player -> context.workHandler().submitAsync(() -> { MFLocator locator = message.locator(); CompoundTag payload = message.payload(); @@ -95,7 +89,7 @@ public void handleData(ModuleSettingsMessage message, PlayPayloadContext context })); } - public void handleData(OpenGuiMessage message, PlayPayloadContext context) { + public static void handleData(OpenGuiMessage message, PlayPayloadContext context) { context.player().ifPresent(player -> context.workHandler().submitAsync(() -> { MFLocator locator = message.locator(); switch (message.op()) { @@ -121,7 +115,7 @@ public void handleData(OpenGuiMessage message, PlayPayloadContext context) { })); } - public void handleData(SyncUpgradeSettingsMessage message, PlayPayloadContext context) { + public static void handleData(SyncUpgradeSettingsMessage message, PlayPayloadContext context) { context.player().ifPresent(player -> context.workHandler().submitAsync(() -> { ItemStack held = player.getItemInHand(message.hand()); if (held.getItem() instanceof SyncUpgrade) { @@ -130,7 +124,7 @@ public void handleData(SyncUpgradeSettingsMessage message, PlayPayloadContext co })); } - public void handleData(ValidateModuleMessage message, PlayPayloadContext context) { + public static void handleData(ValidateModuleMessage message, PlayPayloadContext context) { context.player().ifPresent(player -> context.workHandler().submitAsync(() -> { ItemStack stack = player.getItemInHand(message.hand()); if (stack.getItem() instanceof ModuleItem moduleItem && player instanceof ServerPlayer sp) { diff --git a/src/main/java/me/desht/modularrouters/network/messages/RouterSettingsMessage.java b/src/main/java/me/desht/modularrouters/network/messages/RouterSettingsMessage.java index 9beda174..00035a64 100644 --- a/src/main/java/me/desht/modularrouters/network/messages/RouterSettingsMessage.java +++ b/src/main/java/me/desht/modularrouters/network/messages/RouterSettingsMessage.java @@ -29,10 +29,10 @@ public RouterSettingsMessage(FriendlyByteBuf buffer) { @Override public void write(FriendlyByteBuf buffer) { - buffer.writeBlockPos(pos); - buffer.writeByte(redstoneBehaviour.ordinal()); buffer.writeBoolean(ecoMode); + buffer.writeByte(redstoneBehaviour.ordinal()); buffer.writeEnum(energyDirection); + buffer.writeBlockPos(pos); } @Override diff --git a/src/main/java/me/desht/modularrouters/util/SetofItemStack.java b/src/main/java/me/desht/modularrouters/util/SetofItemStack.java index da62923c..189ba851 100644 --- a/src/main/java/me/desht/modularrouters/util/SetofItemStack.java +++ b/src/main/java/me/desht/modularrouters/util/SetofItemStack.java @@ -46,7 +46,7 @@ public static SetofItemStack fromItemHandler(IItemHandler handler, Flags filterF for (int i = 0; i < handler.getSlots(); i++) { ItemStack stack = handler.getStackInSlot(i); if (!stack.isEmpty()) { - itemStacks.add(stack); + itemStacks.add(stack.copy()); } } return new SetofItemStack(itemStacks, filterFlags);