Skip to content

Commit

Permalink
Allow new scripts to be created
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Dec 4, 2023
1 parent 21c7dbb commit 0bfb59e
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ private void renderHighlight(Rect2i[] p_98139_) {
tesselator.end();
RenderSystem.disableColorLogicOp();
RenderSystem.enableTexture();

RenderSystem.setShaderColor(1, 1, 1, 1);
}

private Pos2i convertScreenToLocal(Pos2i posScreen) {
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 org.cyclops.cyclopscore.client.gui.component.WidgetScrollBar;
import org.cyclops.cyclopscore.client.gui.component.button.ButtonText;
import org.cyclops.cyclopscore.client.gui.component.input.WidgetArrowedListField;
import org.cyclops.cyclopscore.client.gui.container.ContainerScreenExtended;
import org.cyclops.cyclopscore.helper.Helpers;
Expand All @@ -22,6 +23,7 @@
import org.lwjgl.glfw.GLFW;

import javax.annotation.Nullable;
import java.awt.*;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -51,6 +53,7 @@ public class ContainerScreenTerminalScripting extends ContainerScreenExtended<Co
private WidgetScrollBar scrollBar;
private int firstRow;
private WidgetTextArea textArea;
private ButtonText buttonCreateFile;

public ContainerScreenTerminalScripting(ContainerTerminalScripting container, Inventory inventory, Component title) {
super(container, inventory, title);
Expand Down Expand Up @@ -93,6 +96,10 @@ public int getTotalRows() {
textArea = new WidgetTextArea(Minecraft.getInstance().font, this.leftPos + SCRIPT_X + 1, this.topPos + SCRIPT_Y + 1, SCRIPT_WIDTH, SCRIPT_HEIGHT, Component.translatable("gui.integratedscripting.script"));
textArea.setListener(this::onActiveScriptModified);
addRenderableWidget(textArea);

buttonCreateFile = new ButtonText(this.leftPos + 19, this.topPos + 238, 56, 10, Component.translatable("gui.integratedscripting.create_file"), Component.literal("+"),
(button) -> getMenu().createNewFile(), true);
addRenderableWidget(buttonCreateFile);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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.TerminalScriptingCreateNewScriptPacket;
import org.cyclops.integratedscripting.network.packet.TerminalScriptingModifiedScriptPacket;
import org.cyclops.integratedscripting.part.PartTypeTerminalScripting;

Expand Down Expand Up @@ -199,6 +200,19 @@ public void setServerScript(int disk, Path path, @Nullable String script) {
ScriptingNetworkHelpers.getScriptingData().setScript(disk, path, script, IScriptingData.ChangeLocation.MEMORY);
}

public void createNewServerScript(int disk) {
// Determine unique script name
Set<Path> existingScripts = ScriptingNetworkHelpers.getScriptingData().getScripts(disk).keySet();
Path path;
int i = 0;
do {
path = Path.of("script" + i++ + ".js");
} while (existingScripts.contains(path));

// Create empty script
ScriptingNetworkHelpers.getScriptingData().setScript(disk, path, "", IScriptingData.ChangeLocation.MEMORY);
}

@Nullable
public Path getActiveScriptPath() {
return activeScriptPath;
Expand Down Expand Up @@ -234,6 +248,13 @@ public void setActiveScript(String scriptNew) {
}
}

public void createNewFile() {
int disk = getActiveDisk();
if (disk >= 0) {
IntegratedScripting._instance.getPacketHandler().sendToServer(new TerminalScriptingCreateNewScriptPacket(disk));
}
}

public static class InitData {

private final IntList availableDisks;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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;


/**
* Packet for letting a client trigger the creation of a new script by sending to the server.
* @author rubensworks
*
*/
public class TerminalScriptingCreateNewScriptPacket extends PacketCodec {

@CodecField
private int disk;

public TerminalScriptingCreateNewScriptPacket() {

}

public TerminalScriptingCreateNewScriptPacket(int disk) {
this.disk = disk;
}

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

@Override
@OnlyIn(Dist.CLIENT)
public void actionClient(Level world, Player player) {

}

@Override
public void actionServer(Level world, ServerPlayer player) {
if(player.containerMenu instanceof ContainerTerminalScripting container) {
container.createNewServerScript(disk);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.cyclops.cyclopscore.network.PacketHandler;
import org.cyclops.cyclopscore.proxy.CommonProxyComponent;
import org.cyclops.integratedscripting.IntegratedScripting;
import org.cyclops.integratedscripting.network.packet.TerminalScriptingCreateNewScriptPacket;
import org.cyclops.integratedscripting.network.packet.TerminalScriptingModifiedScriptPacket;

/**
Expand All @@ -23,5 +24,6 @@ public void registerPacketHandlers(PacketHandler packetHandler) {
super.registerPacketHandlers(packetHandler);

packetHandler.register(TerminalScriptingModifiedScriptPacket.class);
packetHandler.register(TerminalScriptingCreateNewScriptPacket.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"_comment": "Gui",
"gui.integratedscripting.disk": "Disk",
"gui.integratedscripting.script": "Script",
"gui.integratedscripting.create_file": "Create File",

"valuetype.integratedscripting.error.translation.unknown_to_graal": "Could not find a translator from %s values to Graal.",
"valuetype.integratedscripting.error.translation.unknown_to_graal_nbt": "Could not find a translator from %s values to Graal during NBT translation.",
Expand Down

0 comments on commit 0bfb59e

Please sign in to comment.