From 7e8635ac3af602db993e3e9ee8ad17a0a746c850 Mon Sep 17 00:00:00 2001 From: McHorse Date: Thu, 18 Nov 2021 10:22:02 +0000 Subject: [PATCH] Add copy text context menu to REPL messages, s variable and clear() function to REPL --- .../mappet/api/scripts/ScriptManager.java | 5 ++++- .../mappet/client/gui/scripts/GuiRepl.java | 16 +++++++++++----- .../mappet/client/gui/utils/text/GuiText.java | 5 +++++ src/main/resources/assets/mappet/lang/en_US.lang | 2 +- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/main/java/mchorse/mappet/api/scripts/ScriptManager.java b/src/main/java/mchorse/mappet/api/scripts/ScriptManager.java index 46815397..c38c7547 100644 --- a/src/main/java/mchorse/mappet/api/scripts/ScriptManager.java +++ b/src/main/java/mchorse/mappet/api/scripts/ScriptManager.java @@ -62,7 +62,10 @@ else if (key instanceof MinecraftServer) if (context != null) { - engine.put("c", new ScriptEvent(context, "", "")); + ScriptEvent event = new ScriptEvent(context, "", ""); + + engine.put("c", event); + engine.put("s", event.getSubject()); } engine.eval("var __p__ = print; print = function(message) { ____manager____.replPrint(message); __p__(message); };"); diff --git a/src/main/java/mchorse/mappet/client/gui/scripts/GuiRepl.java b/src/main/java/mchorse/mappet/client/gui/scripts/GuiRepl.java index 27ed8f54..45ec9bb9 100644 --- a/src/main/java/mchorse/mappet/client/gui/scripts/GuiRepl.java +++ b/src/main/java/mchorse/mappet/client/gui/scripts/GuiRepl.java @@ -38,11 +38,6 @@ public GuiRepl(Minecraft mc) this.log = new GuiScrollElement(mc); this.log.flex().relative(this).w(1F).h(1F, -100).column(0).vertical().stretch().scroll(); - this.log.context(() -> - { - return new GuiSimpleContextMenu(this.mc) - .action(Icons.CLOSE, IKey.lang("mappet.gui.scripts.repl.context.clear"), () -> this.log.removeAll()); - }); this.add(this.repl, this.log); @@ -57,6 +52,14 @@ public boolean keyTyped(GuiContext context) { String text = this.repl.getText(); + if (text.trim().startsWith("clear()")) + { + this.repl.clear(); + this.log.removeAll(); + + return true; + } + if (!text.isEmpty()) { Dispatcher.sendToServer(new PacketRepl(text)); @@ -131,6 +134,9 @@ public GuiReplText(Minecraft mc, boolean odd, int vertical) this.odd = odd; this.padding(10, vertical); + + this.context(() -> new GuiSimpleContextMenu(mc) + .action(Icons.COPY, IKey.str("Copy text"), () -> GuiScreen.setClipboardString(this.getText().get()))); } @Override diff --git a/src/main/java/mchorse/mappet/client/gui/utils/text/GuiText.java b/src/main/java/mchorse/mappet/client/gui/utils/text/GuiText.java index 31b0ed20..b868dbfd 100644 --- a/src/main/java/mchorse/mappet/client/gui/utils/text/GuiText.java +++ b/src/main/java/mchorse/mappet/client/gui/utils/text/GuiText.java @@ -36,6 +36,11 @@ private int height() return height + this.paddingV * 2; } + public IKey getText() + { + return this.temp; + } + public GuiText text(String text) { return this.text(IKey.str(text)); diff --git a/src/main/resources/assets/mappet/lang/en_US.lang b/src/main/resources/assets/mappet/lang/en_US.lang index dd09ca03..8e58ffc9 100644 --- a/src/main/resources/assets/mappet/lang/en_US.lang +++ b/src/main/resources/assets/mappet/lang/en_US.lang @@ -607,7 +607,7 @@ mappet.gui.scripts.libraries.context.remove=Remove this library mappet.gui.scripts.documentation.title=Scripting documentation mappet.gui.scripts.repl.title=REPL (interactive console) mappet.gui.scripts.repl.hello_world=Hello, world! -mappet.gui.scripts.repl.welcome=This is Mappet's Read-Eval-Print Loop (REPL). You can type in §6JS§r code, and execute it by pressing Enter key. You can cycle through eval history by holding §6Ctrl§r key and arrow §6Up§r or §6Down§r.\n\nFollowing variables are available:\n\n- §6c§r is a IScriptEvent that has player as a subject\n- §6mappet§r is a IScriptFactory +mappet.gui.scripts.repl.welcome=This is Mappet's Read-Eval-Print Loop (REPL). You can type in §6JS§r code, and execute it by pressing Enter key. You can cycle through eval history by holding §6Ctrl§r key and arrow §6Up§r or §6Down§r.\n\nFollowing variables are available:\n\n- §6c§r is a IScriptEvent that has player as a subject\n- §6s§r is a IScriptPlayer\n- §6mappet§r is a IScriptFactory\n\nYou can execute §7clear()§r function to clear all messages on the screen. mappet.gui.scripts.repl.context.clear=Clear mappet.gui.scripts.keys.word_wrap=Toggle word wrapping mappet.gui.scripts.context.paste_morph=Paste morph NBT...