Posted December 19, 20204 yr 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
December 20, 20204 yr 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)
December 20, 20204 yr 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();
December 20, 20204 yr 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.