I am making a plugin but some other plugins already have the command but say something that I want to change. How would I override the command to what my command is to show what I want? If it's a little confusing to make it better, I am making a plugin with commands that another plugin uses, I want the effects of them to be different than they are by making my own plugin that does a similar thing but the display text is what I want to be different i.e. CommandBooks /heal displays Healed! and I want it to show You Have Been Healed! Thanks in advance, you guys are GREAT!
travja Here you go! Code: @EventHandler public void onCommandPreprocess(PlayerChatEvent event) { Player sender = event.getPlayer(); if (event.getMessage().equalsIgnoreCase(""/heal")) { event.setCancelled(true); sender.sendMessage(ChatColor.BLUE + "You have been healed!"); } }
What exactly does the event.setCancelled(true); thing do? and what if a command I want to override has that too? And can I keep the same formatting as far as the onCommand thing or does it have to be with the PlayerChatEvent in order for the command to override? And what really is the difference between onCommandPreprocess and why do we add @EventHandler? Sorry, kind of new to Java/Plugin Development. I guess what I am trying to say is will the effects execution of the command be the same, type /heal and it does the same thing but shows up in blue? EDIT by Moderator: merged posts, please use the edit button instead of double posting.
travja Your text overwhelms me.. Try to tag me (@Royal_Soda) when you are directing a message towards me. event.setCancelled(true); makes it so that anything run by another plugin after that command is cancelled, then your code goes after that so it is executed. onCommand cannot catch what is being processed by another plugin, meanwhile onCommandPreprocess does.
Royal_Soda Sorry, So I have to do Code: @EventHandler public boolean onCommandPreprocess(CommandSender sender, Command cmd, String commandLabel, String[] args){ event.setCancelled(true); //Then continue code as before? } or do I need the @EventHandler?
It must start with: Code: @EventHandler public void onCommandPreprocess(PlayerChatEvent event) { } onCommandPreprocess can be renamed to anything you wish, but it's best left like that. The code I stated above on the second comment does what you want. Consider following me or giving this a like if I helped.
Royal_Soda So no need for the (CommandSender sender, Command cmd) stuff then, just PlayerChatEvent event? Royal_Soda So here is what I plugged in: Code: public void onCommandPreprocess(PlayerChatEvent event){ Player player = event.getPlayer(); if(event.getMessage().equalsIgnoreCase("/Heal")){ event.setCancelled(true); if(args.length == 0){ //heal = 0 args /heal Travja = 1 args player.setFireTicks(0); player.setHealth(20); player.setFoodLevel(20); player.sendMessage(ChatColor.AQUA + "You Have Been Healed!"); }else if(args.length == 1){ if(player.getServer().getPlayer(args[0]) != null){ Player targetPlayer = player.getServer().getPlayer(args[0]); targetPlayer.setHealth(20); targetPlayer.setFoodLevel(20); targetPlayer.setFireTicks(0); player.sendMessage(ChatColor.AQUA + "You Have Been Healed!"); }}else{ player.sendMessage(ChatColor.RED + "PLAYER NOT ONLINE!"); } } if(event.getMessage().equalsIgnoreCase("/Death")){ if(args.length == 0){ player.setHealth(0); player.setFoodLevel(0); s.broadcastMessage(ChatColor.LIGHT_PURPLE + player.getDisplayName() + " Just Committed Suicide!"); }else if(args.length == 1){ if(player.getServer().getPlayer(args[0]) != null){ Player targetPlayer = player.getServer().getPlayer(args[0]); targetPlayer.setHealth(0); targetPlayer.setFoodLevel(0); player.sendMessage(ChatColor.LIGHT_PURPLE + "You Just Killed Someone!"); }}else{ player.sendMessage(ChatColor.RED + "PLAYER NOT ONLINE!"); } } } but it still just shows up with Healed! in yellow and /death doesn't even work... is there anything I need to change? So I don't understand, the args anywhere in the code is underlined in red... Not quite sure what to think. Bump EDIT by Moderator: merged posts, please use the edit button instead of double posting.