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

 

	@SubscribeEvent
	public static void Command(CommandEvent event) {
		
		String command = event.getParseResults().getReader().toString();
		Cave.LOGGER.debug(command);
		
		if (command.equals("attribute")) {
			
			event.setCanceled(true);
			
		} else if (command.equals("effect")) {
			
			event.setCanceled(true);
			
		} else if (command.equals("enchant")) {
			
			event.setCanceled(true);
			
		}
		
	}

 

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

  • Author
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)

  • Author
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();

 

  • Author

i'm not the best at java so what do you mean by "you need reflection to access it"

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.