better globals and script unload

This commit is contained in:
gnembon committed 2019-05-17 09:36:27 -03:00
1 parent ed06d4e0cd
commit f9b0041271
2 files changed
+33 -13

No files matched your search

@@ -181,7 +181,6 @@ public class ScriptCommand
executes((cc) -> executes((cc) ->
{ {
boolean success = CarpetServer.scriptServer.addScriptHost(cc.getSource(), StringArgumentType.getString(cc, "package")); boolean success = CarpetServer.scriptServer.addScriptHost(cc.getSource(), StringArgumentType.getString(cc, "package"));
Messenger.m(cc.getSource(), success?"w Successfully added a module":"r Failed to add a module");
return success?1:0; return success?1:0;
}) })
); );
@@ -190,8 +189,7 @@ public class ScriptCommand
suggests( (cc, bb) -> suggestMatching(CarpetServer.scriptServer.modules.keySet(),bb)). suggests( (cc, bb) -> suggestMatching(CarpetServer.scriptServer.modules.keySet(),bb)).
executes((cc) -> executes((cc) ->
{ {
boolean success =CarpetServer.scriptServer.removeScriptHost(StringArgumentType.getString(cc, "package")); boolean success =CarpetServer.scriptServer.removeScriptHost(cc.getSource(), StringArgumentType.getString(cc, "package"));
Messenger.m(cc.getSource(), success?"w Successfully removed a module":"r Failed to remove a module");
return success?1:0; return success?1:0;
})); }));
@@ -229,25 +227,27 @@ public class ScriptCommand
ScriptHost host = getHost(context); ScriptHost host = getHost(context);
ServerCommandSource source = context.getSource(); ServerCommandSource source = context.getSource();
Messenger.m(source, "w Global functions"+((host == CarpetServer.scriptServer.globalHost)?":":" in "+host.getName()+":")); Messenger.m(source, "lb Stored functions"+((host == CarpetServer.scriptServer.globalHost)?":":" in "+host.getName()+":"));
for (String fname : host.getAvailableFunctions()) for (String fname : host.getAvailableFunctions())
{ {
Expression expr = host.getExpressionForFunction(fname); Expression expr = host.getExpressionForFunction(fname);
Tokenizer.Token tok = host.getTokenForFunction(fname); Tokenizer.Token tok = host.getTokenForFunction(fname);
List<String> snippet = ExpressionInspector.Expression_getExpressionSnippet(tok, expr); List<String> snippet = ExpressionInspector.Expression_getExpressionSnippet(tok, expr);
Messenger.m(source, "w Function "+fname+" defined at: line "+(tok.lineno+1)+" pos "+(tok.linepos+1)); Messenger.m(source, "wb "+fname,"t defined at: line "+(tok.lineno+1)+" pos "+(tok.linepos+1));
for (String snippetLine: snippet) for (String snippetLine: snippet)
{ {
Messenger.m(source, "li "+snippetLine); Messenger.m(source, "w "+snippetLine);
} }
Messenger.m(source, "gi ----------------"); Messenger.m(source, "gi ----------------");
} }
//Messenger.m(source, "w "+code); //Messenger.m(source, "w "+code);
Messenger.m(source, "w Global Variables:"); Messenger.m(source, "w ");
Messenger.m(source, "lb Global variables"+((host == CarpetServer.scriptServer.globalHost)?":":" in "+host.getName()+":"));
for (String vname : host.globalVariables.keySet()) for (String vname : host.globalVariables.keySet())
{ {
Messenger.m(source, "w Variable "+vname+": ", "wb "+ host.globalVariables.get(vname).evalValue(null).getPrettyString()); if (!vname.startsWith("global")) continue;
Messenger.m(source, "wb "+vname+": ", "w "+ host.globalVariables.get(vname).evalValue(null).getPrettyString());
} }
return 1; return 1;
} }
@@ -12,6 +12,7 @@ import carpet.utils.Messenger;
import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.tree.CommandNode;
import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
@@ -21,6 +22,8 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import static net.minecraft.server.command.CommandManager.argument; import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal; import static net.minecraft.server.command.CommandManager.literal;
@@ -32,6 +35,7 @@ public class CarpetScriptServer
public Map<String, ScriptHost> modules; public Map<String, ScriptHost> modules;
long tickStart; long tickStart;
public boolean stopAll; public boolean stopAll;
Set<String> holyMoly;
public static List<ModuleInterface> bundledModuleData = new ArrayList<ModuleInterface>(){{ public static List<ModuleInterface> bundledModuleData = new ArrayList<ModuleInterface>(){{
add(new CameraPathModule()); add(new CameraPathModule());
@@ -44,6 +48,8 @@ public class CarpetScriptServer
tickStart = 0L; tickStart = 0L;
stopAll = false; stopAll = false;
resetErrorSnooper(); resetErrorSnooper();
holyMoly = CarpetServer.minecraft_server.getCommandManager().getDispatcher().getRoot().getChildren().stream().map(CommandNode::getName).collect(Collectors.toSet());
} }
ModuleInterface getModule(String name) ModuleInterface getModule(String name)
@@ -113,8 +119,9 @@ public class CarpetScriptServer
ModuleInterface module = getModule(name); ModuleInterface module = getModule(name);
if (module == null) if (module == null)
{ {
Messenger.m(source, "r Unable to load the package - not found"); Messenger.m(source, "r Unable to locate the package, but created empty host "+name+" instead");
return false; modules.put(name, newHost);
return true;
} }
String code = module.getCode(); String code = module.getCode();
if (code == null) if (code == null)
@@ -139,13 +146,13 @@ public class CarpetScriptServer
} }
modules.put(name, newHost); modules.put(name, newHost);
addCommand(name); addCommand(source, name);
return true; return true;
} }
public void addCommand(String hostName) public void addCommand(ServerCommandSource source, String hostName)
{ {
ScriptHost host = modules.get(hostName); ScriptHost host = modules.get(hostName);
if (host == null) if (host == null)
@@ -154,8 +161,16 @@ public class CarpetScriptServer
} }
if (!host.globalFunctions.containsKey("__command")) if (!host.globalFunctions.containsKey("__command"))
{ {
Messenger.m(source, "gi Package "+hostName+" loaded.");
return; return;
} }
if (holyMoly.contains(hostName))
{
Messenger.m(source, "gi Package "+hostName+" loaded with no command.");
Messenger.m(source, "gi Tried to mask vanilla command.");
return;
}
LiteralArgumentBuilder<ServerCommandSource> command = literal(hostName). LiteralArgumentBuilder<ServerCommandSource> command = literal(hostName).
requires((player) -> modules.containsKey(hostName)). requires((player) -> modules.containsKey(hostName)).
executes( (c) -> executes( (c) ->
@@ -195,6 +210,7 @@ public class CarpetScriptServer
return 1; return 1;
}))); })));
} }
Messenger.m(source, "gi Package "+hostName+" loaded with /"+hostName+" command");
CarpetServer.minecraft_server.getCommandManager().getDispatcher().register(command); CarpetServer.minecraft_server.getCommandManager().getDispatcher().register(command);
CarpetSettings.notifyPlayersCommandsChanged(); CarpetSettings.notifyPlayersCommandsChanged();
} }
@@ -248,14 +264,18 @@ public class CarpetScriptServer
ExpressionException.errorSnooper=null; ExpressionException.errorSnooper=null;
} }
public boolean removeScriptHost(String name) public boolean removeScriptHost(ServerCommandSource source, String name)
{ {
name = name.toLowerCase(Locale.ROOT); name = name.toLowerCase(Locale.ROOT);
if (!modules.containsKey(name)) if (!modules.containsKey(name))
{
Messenger.m(source, "r No such host found: ", "wb " + name);
return false; return false;
}
// stop all events associated with name // stop all events associated with name
modules.remove(name); modules.remove(name);
CarpetSettings.notifyPlayersCommandsChanged(); CarpetSettings.notifyPlayersCommandsChanged();
Messenger.m(source, "w Removed host "+name);
return true; return true;
} }
} }