Jump to content

Recommended Posts

Posted (edited)

How can I register local commands for a logical client side-only mod? I want to access these commands when for example playing on a server. However it seems the commands are first searched on the server and of course it fails when none is found.

i can see a part of the commands logic in CommandSuggestionHelper::updateCommandInfo(). This is called when I input a string in the command and seems to generate the suggestions by making a request to the server. And ChatScreen::keyPressed() calls sendMessage() which contains a comment

Quote

//if (net.minecraftforge.client.ClientCommandHandler.instance.executeCommand(mc.player, msg) != 0) return; //Forge: TODO Client command re-write

It looks like it might not be yet implemented. So is this possible? I am using version 1.16.5 - 36.1.0.

 

What I've tried (taken from MinecraftByExample)

public class TestCommand {
	public static void register(CommandDispatcher<CommandSource> dispatcher) {
		LiteralArgumentBuilder<CommandSource> mbequoteCommand = Commands.literal("mbequote")
				.then(Commands.literal("python")
						.executes(commandContext -> sendMessage(commandContext, QuoteSource.MONTY_PYTHON.getQuote())))
				.then(Commands.literal("blues")
						.executes(commandContext -> sendMessage(commandContext, QuoteSource.BLUES_BROTHERS.getQuote())))
				.then(Commands.literal("yogi")
						.then(Commands.literal("bear").executes(
								commandContext -> sendMessage(commandContext, QuoteSource.YOGI_BEAR.getQuote())))
						.then(Commands.literal("miaow").executes(
								commandContext -> sendMessage(commandContext, QuoteSource.YOGI_BERRA.getQuote())))
						.executes(commandContext -> sendMessage(commandContext, "Which Yogi do you mean?"))
				);
		dispatcher.register(mbequoteCommand);
	}

	static int sendMessage(CommandContext<CommandSource> commandContext, String message) throws CommandSyntaxException {
		TranslationTextComponent finalText = new TranslationTextComponent("chat.type.announcement",
				commandContext.getSource().getDisplayName(), new StringTextComponent(message));

		Entity entity = commandContext.getSource().getEntity();
		if (entity != null) {
			commandContext.getSource().getServer().getPlayerList().broadcastMessage(finalText, ChatType.CHAT,
					entity.getUUID());
			// func_232641_a_ is sendMessage()
		} else {
			commandContext.getSource().getServer().getPlayerList().broadcastMessage(finalText, ChatType.SYSTEM,
					Util.NIL_UUID);
		}
		return 1;
	}

	enum QuoteSource {
		MONTY_PYTHON(new String[] { "Nobody expects the Spanish Inquisition!",
				"What sad times are these when passing ruffians can say 'Ni' at will to old ladies.",
				"That's the machine that goes 'ping'.", "Have you got anything without spam?",
				"We interrupt this program to annoy you and make things generally more irritating.",
				"My brain hurts!" }),
		YOGI_BERRA(new String[] { "When you come to a fork in the road... take it.", "It ain't over till it's over.",
				"The future ain't what it used to be.", "If the world was perfect, it wouldn't be." }),
		YOGI_BEAR(new String[] { "I'm smarter than the average bear." }),
		BLUES_BROTHERS(new String[] { "Four fried chickens and a Coke, please.",
				"It's 106 miles to Chicago, we've got a full tank, half pack of cigarettes, it's dark out, and we're wearing sunglasses. Hit it.",
				"Are you the police?  No ma'am, we're musicians." });

		public String getQuote() {
			return quotes[new Random().nextInt(quotes.length)];
		}

		QuoteSource(String[] quotes) {
			this.quotes = quotes;
		}

		private String[] quotes;
	}
}

This command works on local multiplayer environment but not when connected to a dedicated/external server (meaning the server gets the request and replies with command not found)

Edited by Antonii
Detailed the issue

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...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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