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 @@ -96,6 +96,8 @@ public class ContainerScreenTerminalScripting extends ContainerScreenExtended<Co
@Nullable
private ButtonIcon buttonOpenFolder;
private int buttonOpenFolderDisk = Integer.MIN_VALUE;
// The disk a script is being created on, so the new script can be opened once the server sends it
private int createdScriptDisk = -1;
private final List<ButtonIcon> toolbarEditButtons = Lists.newArrayList();
private ButtonIcon buttonUndo;
private ButtonIcon buttonRedo;
Expand Down Expand Up @@ -197,7 +199,7 @@ public int getTotalRows() {
// The scripts folder can only be opened if this client is running the server, such as in singleplayer
boolean canOpenFolder = Minecraft.getInstance().hasSingleplayerServer();
buttonCreateFile = new ButtonText(this.leftPos + PATHS_X, this.topPos + 222, canOpenFolder ? PATHS_WIDTH - 12 : PATHS_WIDTH, 10, Component.translatable("gui.integratedscripting.create_file"), Component.literal("+"),
(button) -> getMenu().createNewFile(), true);
(button) -> this.createNewFile(), true);
buttonOpenFolder = null;
buttonOpenFolderDisk = Integer.MIN_VALUE;
if (!focusMode) {
Expand Down Expand Up @@ -744,7 +746,22 @@ private void onActiveScriptSelected() {
this.textArea.setHistory(path == null ? new TextHistory() : scriptHistories.computeIfAbsent(Pair.of(getMenu().getActiveDisk(), path), k -> new TextHistory()));
}

private void onRemoteScriptChanged(int disk, Path path) {
private void createNewFile() {
this.createdScriptDisk = getMenu().getActiveDisk();
getMenu().createNewFile();
}

private void onRemoteScriptChanged(int disk, Path path, boolean created) {
// Open a newly created script
if (created && disk == this.createdScriptDisk && disk == getMenu().getActiveDisk()) {
this.createdScriptDisk = -1;
this.saveCursorPos();
getMenu().setActiveScriptPath(path);
this.onActiveScriptSelected();
this.setFocused(this.textArea);
return;
}

// Show changes from outside this editor, such as external file edits, instead of overwriting them on the next edit
if (disk == getMenu().getActiveDisk() && path.equals(getMenu().getActiveScriptPath())) {
String script = getMenu().getActiveScript();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public class ContainerTerminalScripting extends InventoryContainer implements ID
private final int activeScriptPathId;
private final int selectionId;
@Nullable
private BiConsumer<Integer, Path> remoteScriptChangeListener;
private IRemoteScriptChangeListener remoteScriptChangeListener;
private final Map<Pair<Integer, Path>, ScriptSyntaxError> syntaxErrors = Maps.newHashMap();
@Nullable
private BiConsumer<Integer, Path> syntaxErrorListener;
Expand Down Expand Up @@ -312,21 +312,26 @@ public void setLastScript(int disk, Path path, @Nullable String script) {
}
}

public void setRemoteScriptChangeListener(@Nullable BiConsumer<Integer, Path> remoteScriptChangeListener) {
public void setRemoteScriptChangeListener(@Nullable IRemoteScriptChangeListener remoteScriptChangeListener) {
this.remoteScriptChangeListener = remoteScriptChangeListener;
}

/**
* Called on the client when a script was changed by the server.
* @param disk The disk id.
* @param path The script path.
* @param created If the script did not exist before.
*/
public void onRemoteScriptChange(int disk, Path path) {
public void onRemoteScriptChange(int disk, Path path, boolean created) {
if (this.remoteScriptChangeListener != null) {
this.remoteScriptChangeListener.accept(disk, path);
this.remoteScriptChangeListener.onChange(disk, path, created);
}
}

public static interface IRemoteScriptChangeListener {
public void onChange(int disk, Path path, boolean created);
}

public Int2ObjectMap<Map<Path, String>> getLastScripts() {
return lastScripts;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
Expand Down Expand Up @@ -58,8 +59,11 @@ public boolean isAsync() {
@OnlyIn(Dist.CLIENT)
public void actionClient(Level world, Player player) {
if(player.containerMenu instanceof ContainerTerminalScripting container) {
container.setLastScript(disk, Path.of(path), getScript());
container.onRemoteScriptChange(disk, Path.of(path));
Path scriptPath = Path.of(path);
// Determine this before storing the script
boolean created = !container.getLastScripts().getOrDefault(disk, Collections.emptyMap()).containsKey(scriptPath);
container.setLastScript(disk, scriptPath, getScript());
container.onRemoteScriptChange(disk, scriptPath, created);
}
}

Expand Down
Loading