Skip to content

Commit

Permalink
Add copy text context menu to REPL messages, s variable and clear() f…
Browse files Browse the repository at this point in the history
…unction to REPL
  • Loading branch information
mchorse committed Nov 18, 2021
1 parent 605454f commit 7e8635a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/main/java/mchorse/mappet/api/scripts/ScriptManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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); };");
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/mchorse/mappet/client/gui/scripts/GuiRepl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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));
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/assets/mappet/lang/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand Down

0 comments on commit 7e8635a

Please sign in to comment.