Skip to content

Commit

Permalink
Only send update packets once a second
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Dec 11, 2023
1 parent f95996f commit 0d96ff2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -62,6 +64,7 @@ public class ContainerScreenTerminalScripting extends ContainerScreenExtended<Co
private WidgetTextArea textArea;
private ButtonText buttonCreateFile;
private WidgetDialog pendingScriptRemovalDialog;
private int lastClientSyncTick;

public ContainerScreenTerminalScripting(ContainerTerminalScripting container, Inventory inventory, Component title) {
super(container, inventory, title);
Expand All @@ -73,6 +76,7 @@ public ContainerScreenTerminalScripting(ContainerTerminalScripting container, In
this.inventoryLabelX = 88;
this.inventoryLabelY = this.imageHeight - 94;
this.firstRow = 0;
this.lastClientSyncTick = 0;
}

@Override
Expand Down Expand Up @@ -118,6 +122,32 @@ public int getTotalRows() {
protected void containerTick() {
super.containerTick();
textArea.tick();

if (this.lastClientSyncTick == 0) {
// Send modified scripts from client to server
syncDirtyScripts();
}
this.lastClientSyncTick = (this.lastClientSyncTick + 1) % GeneralConfig.terminalScriptingClientSyncTickInterval;
}

protected void syncDirtyScripts() {
for (Pair<Integer, Path> entry : getMenu().getClientScriptsDirty()) {
Map<Path, String> 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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,6 +47,7 @@ public class ContainerTerminalScripting extends InventoryContainer {
private final Level world;
private final Optional<INetwork> network;
private final Optional<IScriptingNetwork> scriptingNetwork;
private final Set<Pair<Integer, Path>> clientScriptsDirty;

private final Int2ObjectMap<Map<Path, String>> lastScripts = new Int2ObjectAVLTreeMap<>();
private IntList availableDisks;
Expand All @@ -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);
Expand Down Expand Up @@ -119,12 +122,16 @@ public void setActiveDisk(int activeDisk) {
this.activeDisk = activeDisk;
}

public Set<Pair<Integer, Path>> 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<Path, String> scriptsNew = ScriptingNetworkHelpers.getScriptingData().getScripts(disk);
Expand Down Expand Up @@ -247,9 +254,10 @@ public void setActiveScript(String scriptNew) {
if (path != null && disk >= 0) {
Map<Path, String> 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));
}
}
}
}
Expand Down

0 comments on commit 0d96ff2

Please sign in to comment.