Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.cyclops.integratedscripting.part.PartTypes;

import java.nio.file.Path;
import java.util.Map;

import static org.cyclops.integratedscripting.gametest.GameTestHelpersIntegratedScripting.createBasicNetwork;

Expand Down Expand Up @@ -61,4 +62,41 @@ public void setValue(int valueId, CompoundTag value) {
helper.succeed();
}

@GameTest(template = TEMPLATE_EMPTY)
public void testTerminalScriptingIgnoresUnavailableDisks(GameTestHelper helper) {
GameTestHelpersIntegratedScripting.NetworkPositions positions = createBasicNetwork(helper, GameTestsScripts.POS);
IScriptingData scriptingData = ScriptingNetworkHelpers.getScriptingData();
Path path = Path.of("script0.js");
Path pathRenamed = Path.of("script1.js");

// A disk that is not part of the terminal's network
int otherDisk = org.cyclops.integratedscripting.RegistryEntries.ITEM_SCRIPTING_DISK.get()
.getOrCreateDiskId(new ItemStack(org.cyclops.integratedscripting.RegistryEntries.ITEM_SCRIPTING_DISK));
scriptingData.setScript(otherDisk, path, "original", IScriptingData.ChangeLocation.MEMORY);

ContainerTerminalScripting container = new ContainerTerminalScripting(0, helper.makeMockPlayer(GameType.CREATIVE).getInventory(),
PartTarget.fromCenter(positions.terminal()), PartHelpers.getPartContainer(positions.terminal().getPos(), positions.terminal().getSide()),
PartTypes.TERMINAL_SCRIPTING, new ContainerTerminalScripting.InitData(IntList.of(positions.diskId()))) {
// Handle values as if they were sent by the client, as the mock player has no connection
@Override
public void setValue(int valueId, CompoundTag value) {
onUpdate(valueId, value);
}
};

// Changes to the other disk are ignored
container.setServerScript(otherDisk, path, "modified");
container.setServerScript(otherDisk, pathRenamed, "new");
container.createNewServerScript(otherDisk);
helper.assertValueEqual(scriptingData.getScripts(otherDisk), Map.of(path, "original"), "scripts of the other disk");
container.renameServerScript(otherDisk, path, pathRenamed);
helper.assertValueEqual(scriptingData.getScripts(otherDisk), Map.of(path, "original"), "scripts of the other disk after rename");

// Changes to the terminal's own disk are applied
container.setServerScript(positions.diskId(), path, "modified");
helper.assertValueEqual(scriptingData.getScripts(positions.diskId()).get(path), "modified", "script of the own disk");

helper.succeed();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,10 @@ public Int2ObjectMap<Map<Path, String>> getLastScripts() {
}

public void setServerScript(int disk, Path path, @Nullable String script) {
// Clients may only modify disks that are available in this terminal.
if (!getAvailableDisks().contains(disk)) {
return;
}
// The client already has this version, so don't send it back in broadcastChanges.
// Otherwise, this echo could overwrite newer client changes that were not synced yet.
setLastScript(disk, path, script);
Expand All @@ -363,6 +367,10 @@ public void setServerScript(int disk, Path path, @Nullable String script) {
* The client already applied the rename to its own state, so it is corrected if the rename fails.
*/
public void renameServerScript(int disk, Path from, Path to) {
// Clients may only modify disks that are available in this terminal.
if (!getAvailableDisks().contains(disk)) {
return;
}
IScriptingData scriptingData = ScriptingNetworkHelpers.getScriptingData();
String script = scriptingData.getScripts(disk).get(from);
if (script != null && !scriptingData.getScripts(disk).containsKey(to)) {
Expand All @@ -388,6 +396,11 @@ public void renameServerScript(int disk, Path from, Path to) {
}

public void createNewServerScript(int disk) {
// Clients may only modify disks that are available in this terminal.
if (!getAvailableDisks().contains(disk)) {
return;
}

// Determine unique script name
Set<Path> existingScripts = ScriptingNetworkHelpers.getScriptingData().getScripts(disk).keySet();
Path path;
Expand Down
Loading