Jump to content

[1.16.1] Command Event


Recommended Posts

I want to creat an anti cheat mod for my server.

So i use the Command Event to canceled some commands but it dont work but why


	public static void Command(CommandEvent event) {
		String command = event.getParseResults().getReader().toString();
		if (command.equals("attribute")) {
		} else if (command.equals("effect")) {
		} else if (command.equals("enchant")) {


When i enter one of these commands the event will not canceled on on the i get this:


[m[36m[20:41:34] [Server thread/DEBUG] [ne.ma.ca.Cave/]: com.mojang.brigadier.StringReader@1821b9f7

Link to comment
Share on other sites

11 hours ago, diesieben07 said:

You just called a random object's toString method. Not sure why you expected that to magically somehow give you the command name.You need to look at CommandDispatcher#execute to see how it handles the ParseResults. Another thing that might be helpful is to use the debugger to see where in the parse results the command name is.

okay i do something similar and it works


Another question is it possible to determine the command source. So whether the command is executed in a command block or by a player or on the console (on a server)

Link to comment
Share on other sites

33 minutes ago, diesieben07 said:

ParseResults#getContext().getSource() gives you a CommandSource, which has all the information about the source of the command. However CommandSource#source, which is the true source of the command, is private and has no getter, so you need reflection to access it.

getEntity and friends will give you the context of the command, this is relevant for e.g. /execute. CommandSource#source would be the source running the /execute command where as getEntity will be the target entity of the execute command.

So if i want to check if the command is execute on a server console i had to check if the command source is not an Entity and not an Block (Command Block) so with this i get the entity source  but how to get a block source


		Entity entity = event.getParseResults().getContext().getSource().getEntity();


Link to comment
Share on other sites

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.

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.


  • Create New...

Important Information

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