From 0d96ff283a9358a08aceae9822d1ab35cf57b50d Mon Sep 17 00:00:00 2001 From: Ruben Taelman Date: Mon, 11 Dec 2023 13:08:30 +0100 Subject: [PATCH] Only send update packets once a second --- .../integratedscripting/GeneralConfig.java | 2 ++ .../ContainerScreenTerminalScripting.java | 30 +++++++++++++++++++ .../container/ContainerTerminalScripting.java | 16 +++++++--- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/cyclops/integratedscripting/GeneralConfig.java b/src/main/java/org/cyclops/integratedscripting/GeneralConfig.java index 575b5bbd..3b9e2848 100644 --- a/src/main/java/org/cyclops/integratedscripting/GeneralConfig.java +++ b/src/main/java/org/cyclops/integratedscripting/GeneralConfig.java @@ -19,6 +19,8 @@ public class GeneralConfig extends DummyConfig { public static int scriptingDriveBaseConsumption = 2; @ConfigurableProperty(category = "general", comment = "The base energy usage for the scripting terminal.", minimalValue = 0, configLocation = ModConfig.Type.SERVER) public static int terminalScriptingBaseConsumption = 1; + @ConfigurableProperty(category = "general", comment = "The minimum number of ticks inbetween sending a script change packet from client to server.", minimalValue = 0, configLocation = ModConfig.Type.SERVER) + public static int terminalScriptingClientSyncTickInterval = 20; public GeneralConfig() { super(IntegratedScripting._instance, "general"); diff --git a/src/main/java/org/cyclops/integratedscripting/client/gui/container/ContainerScreenTerminalScripting.java b/src/main/java/org/cyclops/integratedscripting/client/gui/container/ContainerScreenTerminalScripting.java index 6db1e1de..11483746 100644 --- a/src/main/java/org/cyclops/integratedscripting/client/gui/container/ContainerScreenTerminalScripting.java +++ b/src/main/java/org/cyclops/integratedscripting/client/gui/container/ContainerScreenTerminalScripting.java @@ -20,6 +20,7 @@ import org.cyclops.cyclopscore.helper.Helpers; import org.cyclops.cyclopscore.helper.L10NHelpers; import org.cyclops.cyclopscore.helper.RenderHelpers; +import org.cyclops.integratedscripting.GeneralConfig; import org.cyclops.integratedscripting.IntegratedScripting; import org.cyclops.integratedscripting.Reference; import org.cyclops.integratedscripting.api.language.ILanguageHandler; @@ -28,6 +29,7 @@ import org.cyclops.integratedscripting.core.language.LanguageHandlers; import org.cyclops.integratedscripting.inventory.container.ContainerTerminalScripting; import org.cyclops.integratedscripting.network.packet.TerminalScriptingDeleteScriptPacket; +import org.cyclops.integratedscripting.network.packet.TerminalScriptingModifiedScriptPacket; import org.lwjgl.glfw.GLFW; import javax.annotation.Nullable; @@ -62,6 +64,7 @@ public class ContainerScreenTerminalScripting extends ContainerScreenExtended entry : getMenu().getClientScriptsDirty()) { + Map diskScripts = getMenu().getLastScripts().get(entry.getLeft()); + if (diskScripts != null) { + String script = diskScripts.get(entry.getRight()); + if (script != null) { + IntegratedScripting._instance.getPacketHandler() + .sendToServer(new TerminalScriptingModifiedScriptPacket(entry.getLeft(), entry.getRight(), script)); + } + } + } + getMenu().getClientScriptsDirty().clear(); + } + + @Override + public void onClose() { + this.syncDirtyScripts(); + super.onClose(); } public void removed() { diff --git a/src/main/java/org/cyclops/integratedscripting/inventory/container/ContainerTerminalScripting.java b/src/main/java/org/cyclops/integratedscripting/inventory/container/ContainerTerminalScripting.java index 6bd26060..74ff3c9c 100644 --- a/src/main/java/org/cyclops/integratedscripting/inventory/container/ContainerTerminalScripting.java +++ b/src/main/java/org/cyclops/integratedscripting/inventory/container/ContainerTerminalScripting.java @@ -11,6 +11,7 @@ import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.Level; +import org.apache.commons.lang3.tuple.Pair; import org.cyclops.cyclopscore.inventory.container.InventoryContainer; import org.cyclops.integrateddynamics.api.network.INetwork; import org.cyclops.integrateddynamics.api.part.IPartContainer; @@ -46,6 +47,7 @@ public class ContainerTerminalScripting extends InventoryContainer { private final Level world; private final Optional network; private final Optional scriptingNetwork; + private final Set> clientScriptsDirty; private final Int2ObjectMap> lastScripts = new Int2ObjectAVLTreeMap<>(); private IntList availableDisks; @@ -68,6 +70,7 @@ public ContainerTerminalScripting(int id, Inventory playerInventory, this.network = NetworkHelpers.getNetwork(getTarget().getCenter()).resolve(); this.scriptingNetwork = this.network.flatMap(network -> ScriptingNetworkHelpers.getScriptingNetwork(network).resolve()); + this.clientScriptsDirty = Sets.newHashSet(); this.availableDisks = initData.getAvailableDisks(); this.activeDisk = this.availableDisks.isEmpty() ? -1 : this.availableDisks.getInt(0); @@ -119,12 +122,16 @@ public void setActiveDisk(int activeDisk) { this.activeDisk = activeDisk; } + public Set> getClientScriptsDirty() { + return clientScriptsDirty; + } + @Override public void broadcastChanges() { super.broadcastChanges(); - // Send disk contents to clients if (!this.getLevel().isClientSide()) { + // Send disk contents to clients this.getScriptingNetwork().ifPresent(scriptingNetwork -> { for (Integer disk : this.getAvailableDisks()) { Map scriptsNew = ScriptingNetworkHelpers.getScriptingData().getScripts(disk); @@ -247,9 +254,10 @@ public void setActiveScript(String scriptNew) { if (path != null && disk >= 0) { Map diskScripts = getLastScripts().get(disk); if (diskScripts != null) { - diskScripts.put(path, scriptNew); - IntegratedScripting._instance.getPacketHandler() - .sendToServer(new TerminalScriptingModifiedScriptPacket(disk, path, scriptNew)); + String scriptOld = diskScripts.put(path, scriptNew); + if (!Objects.equals(scriptOld, scriptNew)) { + this.clientScriptsDirty.add(Pair.of(disk, path)); + } } } }