From 6413c17657183342529b87a0458fe41f1906094b Mon Sep 17 00:00:00 2001 From: gnembon Date: Fri, 22 Nov 2019 00:23:50 -0400 Subject: [PATCH] cleanup undef --- .../java/carpet/script/CarpetEventServer.java | 6 +++ .../java/carpet/script/CarpetScriptHost.java | 14 +++++++ src/main/java/carpet/script/Context.java | 16 +------ src/main/java/carpet/script/Expression.java | 42 ++++++++++++++----- src/main/java/carpet/script/ScriptHost.java | 10 +++++ 5 files changed, 63 insertions(+), 25 deletions(-) diff --git a/src/main/java/carpet/script/CarpetEventServer.java b/src/main/java/carpet/script/CarpetEventServer.java index 9b9c66ff..fee7cff4 100644 --- a/src/main/java/carpet/script/CarpetEventServer.java +++ b/src/main/java/carpet/script/CarpetEventServer.java @@ -479,6 +479,7 @@ public class CarpetEventServer return Event.byName.get(event).handler.addEventCallDirect(host, function); } + public boolean removeEvent(String event, String funName) { @@ -489,6 +490,11 @@ public class CarpetEventServer return true; } + public void removeEventDirectly(String event, ScriptHost host, String funName) + { + Event.byName.get(event).handler.removeEventCall(host.getName(), funName); + } + public void removeAllHostEvents(String hostName) { Event.byName.values().forEach((e) -> e.handler.removeAllCalls(hostName)); diff --git a/src/main/java/carpet/script/CarpetScriptHost.java b/src/main/java/carpet/script/CarpetScriptHost.java index 86ff2aa9..15f37fa2 100644 --- a/src/main/java/carpet/script/CarpetScriptHost.java +++ b/src/main/java/carpet/script/CarpetScriptHost.java @@ -61,6 +61,20 @@ public class CarpetScriptHost extends ScriptHost } } + @Override + public void delFunction(String funName) + { + super.delFunction(funName); + // mcarpet + if (funName.startsWith("__on_")) + { + // this is nasty, we have the host and function, yet we add it via names, but hey - works for now + String event = funName.replaceFirst("__on_",""); + if (CarpetEventServer.Event.byName.containsKey(event)) + scriptServer.events.removeEventDirectly(event, this, funName); + } + } + public CarpetScriptHost retrieveForExecution(ServerCommandSource source) { if (!perUser) diff --git a/src/main/java/carpet/script/Context.java b/src/main/java/carpet/script/Context.java index 80f85056..2fd665f8 100644 --- a/src/main/java/carpet/script/Context.java +++ b/src/main/java/carpet/script/Context.java @@ -18,7 +18,7 @@ public class Context static final int LOCALIZATION = 8; static final int LVALUE = 9; - private Map variables = new HashMap<>(); + public Map variables = new HashMap<>(); protected ScriptHost host; @@ -56,20 +56,6 @@ public class Context void delVariable(String variable) { - if (variable.startsWith("global_")) - { - host.globalVariables.remove(variable); - return; - } - variables.remove(variable); - } - void clearAll(String variable) - { - if (variable.startsWith("global_")) - { - host.globalVariables.remove(variable); - return; - } variables.remove(variable); } diff --git a/src/main/java/carpet/script/Expression.java b/src/main/java/carpet/script/Expression.java index 7a2b1305..bed69b33 100644 --- a/src/main/java/carpet/script/Expression.java +++ b/src/main/java/carpet/script/Expression.java @@ -2864,26 +2864,48 @@ public class Expression implements Cloneable addLazyFunction("undef", 1, (c, t, lv) -> { - String varname = lv.get(0).evalValue(c).getString(); - if (varname.startsWith("_")) - throw new InternalExpressionException("Cannot replace local built-in variables, i.e. those that start with '_'"); - if (varname.endsWith("*")) + Value remove = lv.get(0).evalValue(c); + if (remove instanceof FunctionValue) { + c.host.delFunction(remove.getString()); + return (cc, tt) -> Value.NULL; + } + String varname = remove.getString(); + boolean isPrefix = varname.endsWith("*"); + if (isPrefix) varname = varname.replaceAll("\\*+$", ""); + if (isPrefix) + { for (String key: c.host.globalFunctions.keySet()) { - if (key.startsWith(varname)) c.host.globalFunctions.remove(key); + if (key.startsWith(varname)) c.host.delFunction(key); } - for (String key: c.host.globalVariables.keySet()) + if (varname.startsWith("global_")) { - if (key.startsWith(varname)) c.host.globalVariables.remove(key); + for (String key : c.host.globalVariables.keySet()) + { + if (key.startsWith(varname)) c.host.delGlobalVariable(key); + } + } + if (!varname.startsWith("_")) + { + for (String key : c.variables.keySet()) + { + if (key.startsWith(varname)) c.delVariable(key); + } } - c.clearAll(varname); } else { - c.host.globalFunctions.remove(varname); - c.delVariable(varname); + c.host.delFunction(varname); + if (varname.startsWith("global_")) + { + c.host.delGlobalVariable(varname); + } + if (!varname.startsWith("_")) + { + c.delVariable(varname); + } } return (cc, tt) -> Value.NULL; }); diff --git a/src/main/java/carpet/script/ScriptHost.java b/src/main/java/carpet/script/ScriptHost.java index 9f4d1ed6..2a106f1a 100644 --- a/src/main/java/carpet/script/ScriptHost.java +++ b/src/main/java/carpet/script/ScriptHost.java @@ -46,6 +46,16 @@ public class ScriptHost globalFunctions.put(funName, function); } + public void delFunction(String funName) + { + globalFunctions.remove(funName); + } + + public void delGlobalVariable(String varName) + { + globalFunctions.remove(varName); + } + public ScriptHost retrieveForExecution(String /*Nullable*/ user) { if (!perUser)