From 9fc408e598fc278166434181212057de5b83642d Mon Sep 17 00:00:00 2001 From: c0508383 Date: Tue, 12 Jul 2022 11:58:54 -0500 Subject: [PATCH] Added a configurable expansion to scripts' length, adding 65535 more characters with every additional increase. --- src/main/java/noppes/npcs/CustomNpcs.java | 8 +++++ .../client/gui/script/GuiScriptInterface.java | 7 ++-- .../npcs/client/gui/util/GuiNpcTextArea.java | 34 ++++++++++++++----- .../npcs/controllers/ScriptContainer.java | 31 ++++++++++++++++- 4 files changed, 68 insertions(+), 12 deletions(-) diff --git a/src/main/java/noppes/npcs/CustomNpcs.java b/src/main/java/noppes/npcs/CustomNpcs.java index 2ce2ca785..6dd04d0cc 100644 --- a/src/main/java/noppes/npcs/CustomNpcs.java +++ b/src/main/java/noppes/npcs/CustomNpcs.java @@ -54,6 +54,11 @@ public class CustomNpcs { @ConfigProp(info = "Enables global NPC scripts to be used in the scripter. You can still see and write code in the scripter, but these scripts won't run. False by default, use with caution!") public static boolean GlobalNPCScripts = false; + @ConfigProp(info = "If scripts are too long (>65535 characters), they normally won't be saved in NBT data.\n" + + "This config adds additional compound tags to scripts that need it, so you can store much larger scripts!\n" + + "Every additional compound tag adds 65535 more characters to your script length limit. Use incrementally, with caution.") + public static int ExpandedScriptLimit = 2; + @ConfigProp(info = "Navigation search range for NPCs. Not recommended to increase if you have a slow pc or on a server. Minimum of 16, maximum of 96.") public static int NpcNavRange = 32; @@ -216,6 +221,9 @@ public class CustomNpcs { if (TrackingInfoAlignment > 8) TrackingInfoAlignment = 8; + if (ExpandedScriptLimit < 0) + ExpandedScriptLimit = 0; + EnchantInterface.load(); CustomItems.load(); diff --git a/src/main/java/noppes/npcs/client/gui/script/GuiScriptInterface.java b/src/main/java/noppes/npcs/client/gui/script/GuiScriptInterface.java index 9612996e7..258cf8018 100644 --- a/src/main/java/noppes/npcs/client/gui/script/GuiScriptInterface.java +++ b/src/main/java/noppes/npcs/client/gui/script/GuiScriptInterface.java @@ -320,11 +320,11 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb public void confirmClicked(boolean flag, int i) { if(flag) { if(i == 0) { - this.openLink("http://www.kodevelopment.nl/minecraft/customnpcs/scripting"); + this.openLink("https://www.curseforge.com/minecraft/mc-mods/customnpc-plus"); } if(i == 1) { - this.openLink("http://www.kodevelopment.nl/customnpcs/api/"); + this.openLink("https://kamkeel.github.io/CustomNPC-Plus/"); } if(i == 2) { @@ -380,6 +380,7 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb if(guibutton.id == 101) { (this.getTextField(2)).setText(NoppesStringUtils.getClipboardContents()); + this.setScript(); } ScriptContainer container; @@ -427,7 +428,7 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb container = (ScriptContainer)this.handler.getScripts().get(this.activeTab - 1); if(container != null) { this.setScript(); - this.AWTWindow = new GuiJTextArea(container.script).setListener(this); + AWTWindow = new GuiJTextArea(container.script).setListener(this); } } diff --git a/src/main/java/noppes/npcs/client/gui/util/GuiNpcTextArea.java b/src/main/java/noppes/npcs/client/gui/util/GuiNpcTextArea.java index e7560b25c..d684c0696 100644 --- a/src/main/java/noppes/npcs/client/gui/util/GuiNpcTextArea.java +++ b/src/main/java/noppes/npcs/client/gui/util/GuiNpcTextArea.java @@ -27,7 +27,8 @@ public class GuiNpcTextArea extends GuiNpcTextField implements IGui{ private int startClick = -1; private boolean clickVerticalBar = false; private boolean wrapLine = true; - + private List lines = new ArrayList<>(); + public GuiNpcTextArea(int id,GuiScreen guiscreen, int i, int j, int k, int l, String s) { super(id,guiscreen, i, j, k, l, s); posX = i; @@ -39,6 +40,24 @@ public class GuiNpcTextArea extends GuiNpcTextField implements IGui{ this.setText(s); } + @Override + public void setText(String text) { + super.setText(text); + + List list = new ArrayList(); + StringBuilder line = new StringBuilder(); + for(char c : getText().toCharArray()){ + if(c == '\r' || c == '\n'){ + list.add(line.toString()); + line = new StringBuilder(); + } + else + line.append(c); + } + list.add(line.toString()); + lines = list; + } + @Override public void updateCursorCounter(){ cursorCounter++; @@ -52,10 +71,12 @@ public class GuiNpcTextArea extends GuiNpcTextField implements IGui{ if(c == '\r' || c == '\n'){ this.setText(originalText.substring(0, cursorPosition) + c + originalText.substring(cursorPosition)); } - this.setCursorPositionZero(); + boolean bo = super.textboxKeyTyped(c, i); + String newText = getText(); + this.setText(newText); + + this.setCursorPositionZero(); this.moveCursorBy(cursorPosition); - boolean bo = super.textboxKeyTyped(c, i); - String newText = getText(); if(i != Keyboard.KEY_DELETE) cursorPosition += newText.length() - originalText.length(); if(i == Keyboard.KEY_LEFT && cursorPosition > 0) @@ -63,7 +84,6 @@ public class GuiNpcTextArea extends GuiNpcTextField implements IGui{ if(i == Keyboard.KEY_RIGHT && cursorPosition < newText.length()) cursorPosition++; return bo; - } return false; } @@ -84,7 +104,6 @@ public class GuiNpcTextArea extends GuiNpcTextField implements IGui{ int x = i - posX; int y = (j - posY - 4) / font.height() + getStartLineY(); cursorPosition = 0; - List lines = getLines(); int charCount = 0; int lineCount = 0; int maxSize = width - (isScrolling()?14:4); @@ -153,8 +172,7 @@ public class GuiNpcTextArea extends GuiNpcTextField implements IGui{ int startLine = getStartLineY(); int maxLine = height / font.height() + startLine; - - List lines = getLines(); + int charCount = 0; int lineCount = 0; int maxSize = width - (isScrolling()?14:4); diff --git a/src/main/java/noppes/npcs/controllers/ScriptContainer.java b/src/main/java/noppes/npcs/controllers/ScriptContainer.java index c5c4658f4..9a496bc83 100644 --- a/src/main/java/noppes/npcs/controllers/ScriptContainer.java +++ b/src/main/java/noppes/npcs/controllers/ScriptContainer.java @@ -47,6 +47,13 @@ public class ScriptContainer { public void readFromNBT(NBTTagCompound compound) { this.script = compound.getString("Script"); + for (int i = 0; i < CustomNpcs.ExpandedScriptLimit; i++) { + if (compound.hasKey("ExpandedScript"+i)) { + this.script += compound.getString("ExpandedScript"+i); + } else { + break; + } + } //this.type = compound.getString("Type"); this.console = NBTTags.GetLongStringMap(compound.getTagList("Console", 10)); this.scripts = NBTTags.getStringList(compound.getTagList("ScriptList", 10)); @@ -54,7 +61,29 @@ public class ScriptContainer { } public NBTTagCompound writeToNBT(NBTTagCompound compound) { - compound.setString("Script", this.script); + if (this.script.length() < 65535) { + compound.setString("Script", this.script); + } else { + if (CustomNpcs.ExpandedScriptLimit > 0) { + int i = 0; + int length = this.script.length(); + while (length > 0 && i <= CustomNpcs.ExpandedScriptLimit) { + String str = ""; + if (i == 0) { + compound.setString("Script", this.script.substring(0, 65535)); + str = this.script.substring(0, 65535); + } else { + int end = (length - 65535) >= 0 ? 65535 * (i + 1) : 65535 * i + length; + str = this.script.substring(65535 * i, end); + compound.setString("ExpandedScript" + (i - 1), str); + } + i++; + length -= str.length(); + } + } else { + compound.setString("Script", this.script.substring(0, 65535)); + } + } //compound.setString("Type", this.type); compound.setTag("Console", NBTTags.NBTLongStringMap(this.console)); compound.setTag("ScriptList", NBTTags.nbtStringList(this.scripts));