From 3aa646f5637dfe85badac8abb343ccc606d62342 Mon Sep 17 00:00:00 2001 From: gnembon Date: Sun, 22 Nov 2020 23:35:28 -0400 Subject: [PATCH 1/2] Specify logger when server is not setup. Fixes #567 --- src/main/java/carpet/utils/Messenger.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/carpet/utils/Messenger.java b/src/main/java/carpet/utils/Messenger.java index 6c9daf75..68270e10 100644 --- a/src/main/java/carpet/utils/Messenger.java +++ b/src/main/java/carpet/utils/Messenger.java @@ -15,7 +15,6 @@ import net.minecraft.util.math.Vec3d; import net.minecraft.util.Formatting; import net.minecraft.server.MinecraftServer; import net.minecraft.world.World; -import net.minecraft.world.dimension.DimensionType; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -29,7 +28,7 @@ import java.util.regex.Pattern; public class Messenger { - public static final Logger LOG = LogManager.getLogger(); + public static final Logger LOG = LogManager.getLogger("Messaging System"); private static final Pattern colorExtract = Pattern.compile("#([0-9a-fA-F]{6})"); public enum CarpetFormatting From a297fd14412204c04f664d0a2bb5693451270091 Mon Sep 17 00:00:00 2001 From: gnembon Date: Mon, 23 Nov 2020 02:35:01 -0400 Subject: [PATCH 2/2] Scarpet rules can provide multinary boolean permission level, fixes #572 --- .../java/carpet/commands/ScriptCommand.java | 4 ++-- .../java/carpet/script/CarpetScriptHost.java | 9 +++++---- .../carpet/script/CarpetScriptServer.java | 20 ++++++++++--------- .../carpet/script/bundled/BundledModule.java | 6 +++--- .../java/carpet/settings/SettingsManager.java | 16 ++++++++------- 5 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/main/java/carpet/commands/ScriptCommand.java b/src/main/java/carpet/commands/ScriptCommand.java index 5be12f51..3b916bba 100644 --- a/src/main/java/carpet/commands/ScriptCommand.java +++ b/src/main/java/carpet/commands/ScriptCommand.java @@ -244,13 +244,13 @@ public class ScriptCommand suggests( (cc, bb) -> suggestMatching(CarpetServer.scriptServer.listAvailableModules(true),bb)). executes((cc) -> { - boolean success = CarpetServer.scriptServer.addScriptHost(cc.getSource(), StringArgumentType.getString(cc, "app"), true, false, false); + boolean success = CarpetServer.scriptServer.addScriptHost(cc.getSource(), StringArgumentType.getString(cc, "app"), true, false, null); return success?1:0; }). then(literal("global"). executes((cc) -> { - boolean success = CarpetServer.scriptServer.addScriptHost(cc.getSource(), StringArgumentType.getString(cc, "app"), false, false, false); + boolean success = CarpetServer.scriptServer.addScriptHost(cc.getSource(), StringArgumentType.getString(cc, "app"), false, false, null); return success?1:0; } ) diff --git a/src/main/java/carpet/script/CarpetScriptHost.java b/src/main/java/carpet/script/CarpetScriptHost.java index f4f61fc7..bb85e001 100644 --- a/src/main/java/carpet/script/CarpetScriptHost.java +++ b/src/main/java/carpet/script/CarpetScriptHost.java @@ -39,6 +39,7 @@ import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.function.Function; import java.util.stream.Collectors; import static java.lang.Math.max; @@ -152,12 +153,12 @@ public class CarpetScriptHost extends ScriptHost } public LiteralArgumentBuilder getNewCommandTree( - List,FunctionArgument>> entries + List,FunctionArgument>> entries, Function useValidator ) throws CommandSyntaxException { String hostName = main.getName(); LiteralArgumentBuilder command = literal(hostName). - requires((player) -> CarpetServer.scriptServer.modules.containsKey(hostName)); + requires((player) -> CarpetServer.scriptServer.modules.containsKey(hostName) && useValidator.apply(player)); for (Pair,FunctionArgument> commandData : entries) { command = this.addPathToCommand(command, commandData.getKey(), commandData.getValue()); @@ -243,7 +244,7 @@ public class CarpetScriptHost extends ScriptHost } } - public LiteralArgumentBuilder readCommands() throws CommandSyntaxException + public LiteralArgumentBuilder readCommands(Function useValidator) throws CommandSyntaxException { Value commands = appConfig.get(StringValue.of("commands")); @@ -279,7 +280,7 @@ public class CarpetScriptHost extends ScriptHost } } } - return this.getNewCommandTree(commandEntries); + return this.getNewCommandTree(commandEntries, useValidator); } @Override diff --git a/src/main/java/carpet/script/CarpetScriptServer.java b/src/main/java/carpet/script/CarpetScriptServer.java index 6ec6b14e..5b76e085 100644 --- a/src/main/java/carpet/script/CarpetScriptServer.java +++ b/src/main/java/carpet/script/CarpetScriptServer.java @@ -30,6 +30,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; +import java.util.function.Function; import java.util.stream.Collectors; import static net.minecraft.server.command.CommandManager.argument; @@ -53,7 +54,7 @@ public class CarpetScriptServer /** * Registers a Scarpet App to be always available to be loaded * in the /script load list. - * @see BundledModule#fromPath(ClassLoader, String, String, boolean) + * @see BundledModule#fromPath(String, String, boolean) * * @param app The {@link BundledModule} of the app */ @@ -66,7 +67,7 @@ public class CarpetScriptServer * Registers a Scarpet App to be used as a Rule App * (to be controlled with the value of a Carpet rule). * Libraries should be registered with {@link #registerBuiltInScript(BundledModule)} instead - * @see BundledModule#fromPath(ClassLoader, String, String, boolean) + * @see BundledModule#fromPath(String, String, boolean) * * @param app The {@link BundledModule} of the app. */ @@ -116,7 +117,7 @@ public class CarpetScriptServer Messenger.m(server.getCommandSource(), "Auto-loading world scarpet apps"); for (String moduleName: listAvailableModules(false)) { - addScriptHost(server.getCommandSource(), moduleName, true, true, false); + addScriptHost(server.getCommandSource(), moduleName, true, true, null); } } @@ -192,9 +193,10 @@ public class CarpetScriptServer return modules.get(name); } - public boolean addScriptHost(ServerCommandSource source, String name, boolean perPlayer, boolean autoload, boolean isRuleApp) + public boolean addScriptHost(ServerCommandSource source, String name, boolean perPlayer, boolean autoload, Function commandValidator) { //TODO add per player modules to support player actions better on a server + boolean isRuleApp = commandValidator != null; long start = System.nanoTime(); name = name.toLowerCase(Locale.ROOT); boolean reload = false; @@ -242,7 +244,7 @@ public class CarpetScriptServer { try { - LiteralArgumentBuilder command = newHost.readCommands(); + LiteralArgumentBuilder command = newHost.readCommands(commandValidator); if (command != null) { if (!isRuleApp) Messenger.m(source, "gi "+name+" app "+action+" with /"+name+" command"); @@ -261,7 +263,7 @@ public class CarpetScriptServer } } - else if (!addLegacyCommand(source, name, reload, !isRuleApp)) // this needs to be moved to config reader, only supporting legacy command here + else if (!addLegacyCommand(source, name, reload, !isRuleApp, commandValidator)) // this needs to be moved to config reader, only supporting legacy command here { removeScriptHost(source, name, false, false); Messenger.m(source, "r Failed to build command system for "+name+" thus failed to load the app"); @@ -272,7 +274,7 @@ public class CarpetScriptServer return true; } - private boolean addLegacyCommand(ServerCommandSource source, String hostName, boolean isReload, boolean notifySource) + private boolean addLegacyCommand(ServerCommandSource source, String hostName, boolean isReload, boolean notifySource, Function useValidator) { CarpetScriptHost host = modules.get(hostName); String loaded = isReload?"reloaded":"loaded"; @@ -293,7 +295,7 @@ public class CarpetScriptServer } LiteralArgumentBuilder command = literal(hostName). - requires((player) -> modules.containsKey(hostName)). + requires((player) -> modules.containsKey(hostName) && useValidator.apply(player)). executes( (c) -> { Value response = modules.get(hostName).retrieveForExecution(c.getSource()). @@ -439,6 +441,6 @@ public class CarpetScriptServer apps.keySet().forEach(s -> removeScriptHost(server.getCommandSource(), s, false, false)); CarpetEventServer.Event.clearAllBuiltinEvents(); init(); - apps.forEach((s, pp) -> addScriptHost(server.getCommandSource(), s, pp, false, false)); + apps.forEach((s, pp) -> addScriptHost(server.getCommandSource(), s, pp, false, null)); } } diff --git a/src/main/java/carpet/script/bundled/BundledModule.java b/src/main/java/carpet/script/bundled/BundledModule.java index e66b91fa..8a0d1744 100644 --- a/src/main/java/carpet/script/bundled/BundledModule.java +++ b/src/main/java/carpet/script/bundled/BundledModule.java @@ -30,7 +30,7 @@ public class BundledModule extends Module /** * Creates a new {@link BundledModule} with an app located at a specified place. - * @see #fromPathWithCustomName(ClassLoader, String, String, boolean) + * @see #fromPathWithCustomName(String, String, boolean) * * @param path A {@link String} being the path to the directory where the app is located. * @param scriptName A {@link String} being the name of the script. The extension will be autocompleted @@ -43,10 +43,10 @@ public class BundledModule extends Module /** * Creates a new {@link BundledModule} with an app located at the specified fullPath with a custom name. - * @see #fromPath(ClassLoader, String, String, boolean) + * @see #fromPath(String, String, boolean) * * @param fullPath A {@link String} being the full path to the app's code, including file and extension. - * @param scriptName A {@link String} being the custom name for the script. + * @param customName A {@link String} being the custom name for the script. * @param isLibrary A {@link boolean} indicating whether or not the script is a library * @return The created {@link BundledModule} */ diff --git a/src/main/java/carpet/settings/SettingsManager.java b/src/main/java/carpet/settings/SettingsManager.java index 89be830a..bc43f6ea 100644 --- a/src/main/java/carpet/settings/SettingsManager.java +++ b/src/main/java/carpet/settings/SettingsManager.java @@ -180,17 +180,17 @@ public class SettingsManager observers.forEach(observer -> observer.accept(source, rule, userTypedValue)); staticObservers.forEach(observer -> observer.accept(source, rule, userTypedValue)); ServerNetworkHandler.updateRuleWithConnectedClients(rule); - switchScarpetRule(source, rule); + switchScarpetRuleIfNeeded(source, rule); if (CARPET_RULE_CHANGES.isNeeded()) CARPET_RULE_CHANGES.onCarpetRuleChanges(rule, source); } - void switchScarpetRule(ServerCommandSource source, ParsedRule rule) + private void switchScarpetRuleIfNeeded(ServerCommandSource source, ParsedRule rule) { if (!rule.scarpetApp.isEmpty()) { if (rule.getBoolValue() || (rule.type == String.class && !rule.get().equals("false"))) { - CarpetServer.scriptServer.addScriptHost(source, rule.scarpetApp, false, false, true); + CarpetServer.scriptServer.addScriptHost(source, rule.scarpetApp, false, false, s -> canUseCommand(s, rule.get())); } else { CarpetServer.scriptServer.removeScriptHost(source, rule.scarpetApp, false, true); } @@ -205,7 +205,7 @@ public class SettingsManager for (ParsedRule rule : rules.values()) { if (!rule.scarpetApp.isEmpty()) { - switchScarpetRule(server.getCommandSource(), rule); + switchScarpetRuleIfNeeded(server.getCommandSource(), rule); } } } @@ -332,9 +332,11 @@ public class SettingsManager * {@link boolean} value or "ops". * @return Whether or not the {@link ServerCommandSource} meets the required level */ - public static boolean canUseCommand(ServerCommandSource source, String commandLevel) + public static boolean canUseCommand(ServerCommandSource source, Object commandLevel) { - switch (commandLevel) + if (commandLevel instanceof Boolean) return (Boolean) commandLevel; + String commandLevelString = commandLevel.toString(); + switch (commandLevelString) { case "true": return true; case "false": return false; @@ -344,7 +346,7 @@ public class SettingsManager case "2": case "3": case "4": - return source.hasPermissionLevel(Integer.parseInt(commandLevel)); + return source.hasPermissionLevel(Integer.parseInt(commandLevelString)); } return false; }