Skip to content

Commit 902460f

Browse files
committed
ScriptCommand - Unregister command when a script is unloaded
1 parent ea46952 commit 902460f

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

src/main/java/com/github/skriptdev/skript/api/skript/command/ScriptCommandBuilder.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.hypixel.hytale.component.Store;
1010
import com.hypixel.hytale.server.core.command.system.AbstractCommand;
1111
import com.hypixel.hytale.server.core.command.system.CommandContext;
12+
import com.hypixel.hytale.server.core.command.system.CommandRegistration;
1213
import com.hypixel.hytale.server.core.command.system.CommandSender;
1314
import com.hypixel.hytale.server.core.command.system.arguments.system.Argument;
1415
import com.hypixel.hytale.server.core.command.system.arguments.system.OptionalArg;
@@ -58,6 +59,7 @@ public static ScriptCommandBuilder create(int commandType, SkriptLogger logger)
5859
private final Map<String, CommandArg> args = new LinkedHashMap<>();
5960
private final Map<String, Argument<?, ?>> argsFromCommand = new LinkedHashMap<>();
6061
private AbstractCommand hyCommand;
62+
private CommandRegistration commandRegistration;
6163

6264
private final SectionConfiguration sec = new SectionConfiguration.Builder()
6365
.addOptionalLiteral("can-generate-permission", Boolean.class)
@@ -279,6 +281,12 @@ private void createLocalVariables(CommandContext ctx, TriggerContext triggerCont
279281
});
280282
}
281283

284+
public void unregister() {
285+
if (this.commandRegistration != null) {
286+
this.commandRegistration.unregister();
287+
}
288+
}
289+
282290
public String getCommandName() {
283291
return this.commandName;
284292
}
@@ -291,7 +299,7 @@ public void build(ScriptCommandBuilder parent) {
291299
if (this.hyCommand == null) return;
292300

293301
if (parent == null) {
294-
HySk.getInstance().getCommandRegistry().registerCommand(this.hyCommand);
302+
this.commandRegistration = HySk.getInstance().getCommandRegistry().registerCommand(this.hyCommand);
295303
} else {
296304
parent.hyCommand.addSubCommand(this.hyCommand);
297305
}

src/main/java/com/github/skriptdev/skript/plugin/elements/command/ScriptCommand.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ public boolean check(@NotNull TriggerContext ctx) {
183183
return ctx instanceof ScriptCommandContext sctx && sctx.getCommand().equals(this.commandBuilder.getCommandName());
184184
}
185185

186+
@Override
187+
public void unload() {
188+
this.commandBuilder.unregister();
189+
}
190+
186191
@Override
187192
public String toString(@NotNull TriggerContext ctx, boolean debug) {
188193
String type = switch (this.commandType) {

0 commit comments

Comments
 (0)