Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

I'm trying to unregister a command I'd previously registered with the CommandDispatcher using something like

private static int registerAlias(CommandDispatcher<CommandSourceStack> dispatcher, String alias, String command) {
	dispatcher.register(Commands.literal(alias)
		.executes((commandContext) -> {
			dispatcher.execute(command, commandContext.getSource());
			return Command.SINGLE_SUCCESS;
		})
	);
	return Command.SINGLE_SUCCESS;
}

The dispatcher doesn't seem to have an unregister method. The closest I found was the removeCommand method here, but that seems to be a Bukkit thing. So I tried another solution that does something similar with reflection:

@SuppressWarnings("unchecked")
private static int unregisterAlias(CommandDispatcher<CommandSourceStack> dispatcher, String alias) {
	Object node = dispatcher.getRoot().getChild(alias);
	if (node != null) {
		try {
			if (node instanceof LiteralCommandNode<?>) {
				Field literals = CommandNode.class.getDeclaredField("literals");
				literals.setAccessible(true);
				((Map<String, ?>) literals.get(node)).remove(alias, node);
			} else if (node instanceof ArgumentCommandNode<?, ?>) {
				Field arguments = CommandNode.class.getDeclaredField("arguments");
				arguments.setAccessible(true);
				((Map<String, ?>) arguments.get(node)).remove(alias, node);
			}
			Field children = CommandNode.class.getDeclaredField("children");
			children.setAccessible(true);
			((Map<String, ?>) children.get(node)).remove(alias, node);
		} catch (ReflectiveOperationException e) {
			throw new RuntimeException("Error removing command: " + alias, e);
		}
	}
	return Command.SINGLE_SUCCESS;
}

However, this didn't work. The method was executed without errors (I checked with the Debugger), but the command was still working ingame, meaning it wasn't actually removed from the dispatcher. How would I actually remove a registered command from a CommandDispatcher?

Edited by Frozen Storm
Resolved

What the heck are you trying to do? If you want to remove a command that you added, just remove or comment out the code that registers that command! If you are trying to completely remove a command during runtime, that is probably impossible.

Edited by Hipposgrumm

I'm not good at modding, but at least I can read a crash report (well enough). That's something, right?

  • Author

Yes, I'm trying to remove a command during runtime. The command is added (registered) either at game start or during runtime with the registerAlias method, and I'd like to be able to remove it without having to restart the game.

Edited by Frozen Storm

  • Author

I managed to accomplish this by simply reregistering the command to a command that simply prints an error message to the player:

 

dispatcher.register(Commands.literal(alias)
  .executes(commandContext -> {
    Minecraft.getInstance().player.sendSystemMessage(Component.literal("Alias " + alias " has been removed."));
    return Command.SINGLE_SUCCESS;
  })
);

 

  • Frozen Storm changed the title to [Solved] Unregistering commands from CommandDispatcher

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.