Fully remove scarpet commands when apps are unloaded (#1452)

Using new Loader 0.14+ features
This commit is contained in:
altrisi authored and GitHub committed 2022-10-02 13:32:27 +02:00
1 parent e9f1509a1d
commit f63c6acb05
7 files changed
+69 -3

No files matched your search

@@ -0,0 +1,5 @@
package carpet.fakes;
public interface CommandDispatcherInterface {
void carpet$unregister(String node);
}
@@ -0,0 +1,5 @@
package carpet.fakes;
public interface CommandNodeInterface {
void carpet$removeChild(String name);
}
@@ -0,0 +1,22 @@
package carpet.mixins;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.tree.RootCommandNode;
import carpet.fakes.CommandDispatcherInterface;
import carpet.fakes.CommandNodeInterface;
@Mixin(CommandDispatcher.class)
public class CommandDispatcher_scarpetCommandsMixin<S> implements CommandDispatcherInterface {
@Shadow @Final
private RootCommandNode<S> root;
@Override
public void carpet$unregister(String node) {
((CommandNodeInterface)this.root).carpet$removeChild(node);
}
}
@@ -0,0 +1,30 @@
package carpet.mixins;
import java.util.Map;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import com.mojang.brigadier.tree.ArgumentCommandNode;
import com.mojang.brigadier.tree.CommandNode;
import com.mojang.brigadier.tree.LiteralCommandNode;
import carpet.fakes.CommandNodeInterface;
@Mixin(CommandNode.class)
public class CommandNode_scarpetCommandsMixin<S> implements CommandNodeInterface {
@Shadow @Final
private Map<String, CommandNode<S>> children;
@Shadow @Final
private Map<String, LiteralCommandNode<S>> literals;
@Shadow @Final
private Map<String, ArgumentCommandNode<S, ?>> arguments;
@Override
public void carpet$removeChild(String name) {
this.children.remove(name);
this.literals.remove(name);
this.arguments.remove(name);
}
}
@@ -192,7 +192,7 @@ public class CarpetScriptHost extends ScriptHost
String hostName = main.name();
Predicate<CommandSourceStack> configValidator = getCommandConfigPermissions();
LiteralArgumentBuilder<CommandSourceStack> command = literal(hostName).
requires((player) -> CarpetServer.scriptServer.modules.containsKey(hostName) && useValidator.test(player) && configValidator.test(player));
requires((player) -> useValidator.test(player) && configValidator.test(player));
for (Pair<List<CommandToken>,FunctionArgument> commandData : entries)
{
command = this.addPathToCommand(command, commandData.getKey(), commandData.getValue());
@@ -472,7 +472,7 @@ public class CarpetScriptHost extends ScriptHost
}
String hostName = getName();
LiteralArgumentBuilder<CommandSourceStack> command = literal(hostName).
requires((player) -> scriptServer().modules.containsKey(hostName) && commandValidator.test(player) && configValidator.test(player)).
requires((player) -> commandValidator.test(player) && configValidator.test(player)).
executes( (c) ->
{
CarpetScriptHost targetHost = scriptServer().modules.get(hostName).retrieveOwnForExecution(c.getSource());
@@ -505,7 +505,7 @@ public class CarpetScriptHost extends ScriptHost
{
command = command.
then(literal(function).
requires((player) -> scriptServer().modules.containsKey(hostName) && scriptServer().modules.get(hostName).getFunction(function) != null).
requires((player) -> scriptServer().modules.get(hostName).getFunction(function) != null).
executes((c) -> {
CarpetScriptHost targetHost = scriptServer().modules.get(hostName).retrieveOwnForExecution(c.getSource());
Value response = targetHost.handleCommandLegacy(c.getSource(),function, null, "");
@@ -1,6 +1,7 @@
package carpet.script;
import carpet.CarpetSettings;
import carpet.fakes.CommandDispatcherInterface;
import carpet.script.annotation.AnnotationParser;
import carpet.script.api.Auxiliary;
import carpet.script.api.BlockIterators;
@@ -343,6 +344,7 @@ public class CarpetScriptServer extends ScriptServer
events.removeAllHostEvents(modules.get(name));
modules.get(name).onClose();
modules.remove(name);
((CommandDispatcherInterface)server.getCommands().getDispatcher()).carpet$unregister(name);
if (!isRuleApp) unloadableModules.remove(name);
CommandHelper.notifyPlayersCommandsChanged(server);
if (notifySource) Messenger.m(source, "gi Removed "+name+" app");