Skip to content

Commit

Permalink
Continuously send script data to terminal clients
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Oct 28, 2023
1 parent 518c3c5 commit 5711456
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.cyclops.integratedscripting.core.network.ScriptingNetworkHelpers;
import org.cyclops.integratedscripting.inventory.container.ContainerScriptingDrive;
import org.cyclops.integratedscripting.item.ItemScriptingDisk;
import org.cyclops.integratedscripting.network.ScriptingDriveNetworkElement;
import org.cyclops.integratedscripting.core.network.ScriptingDriveNetworkElement;

import javax.annotation.Nullable;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.cyclops.integratedscripting.network;
package org.cyclops.integratedscripting.core.network;

import net.minecraft.resources.ResourceLocation;
import org.cyclops.cyclopscore.datastructure.DimPos;
Expand All @@ -9,7 +9,6 @@
import org.cyclops.integratedscripting.GeneralConfig;
import org.cyclops.integratedscripting.Reference;
import org.cyclops.integratedscripting.blockentity.BlockEntityScriptingDrive;
import org.cyclops.integratedscripting.core.network.ScriptingNetworkHelpers;

import java.util.function.Supplier;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package org.cyclops.integratedscripting.inventory.container;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import it.unimi.dsi.fastutil.ints.Int2ObjectAVLTreeMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.IntList;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
Expand All @@ -12,14 +17,20 @@
import org.cyclops.integrateddynamics.api.part.PartTarget;
import org.cyclops.integrateddynamics.core.helper.NetworkHelpers;
import org.cyclops.integrateddynamics.core.helper.PartHelpers;
import org.cyclops.integratedscripting.IntegratedScripting;
import org.cyclops.integratedscripting.RegistryEntries;
import org.cyclops.integratedscripting.api.network.IScriptingData;
import org.cyclops.integratedscripting.api.network.IScriptingNetwork;
import org.cyclops.integratedscripting.core.network.ScriptingNetworkHelpers;
import org.cyclops.integratedscripting.network.packet.TerminalScriptingModifiedScriptPacket;
import org.cyclops.integratedscripting.part.PartTypeTerminalScripting;

import javax.annotation.Nullable;
import java.nio.file.Path;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

/**
* Container for the crafting jobs overview gui.
Expand All @@ -34,6 +45,7 @@ public class ContainerTerminalScripting extends InventoryContainer {
private final Optional<INetwork> network;
private final Optional<IScriptingNetwork> scriptingNetwork;

private final Int2ObjectMap<Map<Path, String>> lastScripts = new Int2ObjectAVLTreeMap<>();
private IntList availableDisks;
private int activeDisk;

Expand Down Expand Up @@ -111,8 +123,42 @@ public void broadcastChanges() {
if (!this.getLevel().isClientSide()) {
this.getScriptingNetwork().ifPresent(scriptingNetwork -> {
for (Integer disk : this.getAvailableDisks()) {
Map<Path, String> scripts = ScriptingNetworkHelpers.getScriptingData().getScripts(disk);
// TODO
Map<Path, String> scriptsNew = ScriptingNetworkHelpers.getScriptingData().getScripts(disk);
Map<Path, String> scriptsOld = lastScripts.get((int) disk);

// Determine files (union of new and old)
Set<Path> files;
if (scriptsOld == null) {
scriptsOld = Maps.newHashMap();
lastScripts.put((int) disk, scriptsOld);
files = scriptsNew.keySet();
} else {
files = Sets.newHashSet(scriptsNew.keySet());
files.addAll(scriptsOld.keySet());
}

// Determine changed files
for (Path file : files) {
String scriptNew = scriptsNew.get(file);
String scriptOld = scriptsOld.get(file);
if (!Objects.equals(scriptNew, scriptOld)) {
// Send separate packet for each modified file
IntegratedScripting._instance.getPacketHandler()
.sendToPlayer(new TerminalScriptingModifiedScriptPacket(disk, file, scriptNew), (ServerPlayer) player);

// Update the next old value
if (scriptNew != null) {
scriptsOld.put(file, scriptNew);
} else {
scriptsOld.remove(file);
}

// Cleanup if we have no scripts anymore for a certain disk
if (scriptsOld.isEmpty()) {
lastScripts.remove((int) disk);
}
}
}
}
});
}
Expand All @@ -123,6 +169,30 @@ protected int getSizeInventory() {
return 0;
}

public void setLastScript(int disk, Path path, @Nullable String script) {
Map<Path, String> lastScriptsDisk = lastScripts.get(disk);
if (lastScriptsDisk == null) {
lastScriptsDisk = Maps.newHashMap();
lastScripts.put(disk, lastScriptsDisk);
}

// Store or delete script
if (script != null) {
lastScriptsDisk.put(path, script);
} else {
lastScriptsDisk.remove(path);
}

// Cleanup if we have no scripts anymore for a certain disk
if (lastScriptsDisk.isEmpty()) {
lastScripts.remove(disk);
}
}

public void setServerScript(int disk, Path path, @Nullable String script) {
ScriptingNetworkHelpers.getScriptingData().setScript(disk, path, script, IScriptingData.ChangeLocation.MEMORY);
}

public static class InitData {

private final IntList availableDisks;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.cyclops.integratedscripting.network.packet;

import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.cyclops.cyclopscore.network.CodecField;
import org.cyclops.cyclopscore.network.PacketCodec;
import org.cyclops.integratedscripting.inventory.container.ContainerTerminalScripting;

import java.nio.file.Path;

/**
* Packet for sending modified scripts between server and client.
* @author rubensworks
*
*/
public class TerminalScriptingModifiedScriptPacket extends PacketCodec {

@CodecField
private int disk;
@CodecField
private String path;
@CodecField
private String script;

public TerminalScriptingModifiedScriptPacket() {

}

public TerminalScriptingModifiedScriptPacket(int disk, Path path, String script) {
this.disk = disk;
this.path = path.toString();
this.script = script;
}

@Override
public boolean isAsync() {
return false;
}

@Override
@OnlyIn(Dist.CLIENT)
public void actionClient(Level world, Player player) {
if(player.containerMenu instanceof ContainerTerminalScripting container) {
container.setLastScript(disk, Path.of(path), script);
}
}

@Override
public void actionServer(Level world, ServerPlayer player) {
if(player.containerMenu instanceof ContainerTerminalScripting container) {
container.setServerScript(disk, Path.of(path), script);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.cyclops.integratedscripting.proxy;

import org.cyclops.cyclopscore.init.ModBase;
import org.cyclops.cyclopscore.network.PacketHandler;
import org.cyclops.cyclopscore.proxy.CommonProxyComponent;
import org.cyclops.integratedscripting.IntegratedScripting;
import org.cyclops.integratedscripting.network.packet.TerminalScriptingModifiedScriptPacket;

/**
* Proxy for server and client side.
Expand All @@ -16,4 +18,10 @@ public ModBase getMod() {
return IntegratedScripting._instance;
}

@Override
public void registerPacketHandlers(PacketHandler packetHandler) {
super.registerPacketHandlers(packetHandler);

packetHandler.register(TerminalScriptingModifiedScriptPacket.class);
}
}

0 comments on commit 5711456

Please sign in to comment.