PlayerCommandPreprocessEvent does not work.

Discussion in 'Plugin Development' started by Camaflicks, Jun 25, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Camaflicks

    This is really messing me up. I have a section of my plugin where it changes the /pl or /plugins command using a PlayerCommandPreprocessEvent, but it always says its an incorrect command.

    I have no aliases defined. Here is my code:

    Code:java
    1. @EventHandler
    2. public void cmdMsg(PlayerCommandPreprocessEvent event) {
    3.  
    4. Player player = (Player) event;
    5.  
    6. if (event.getMessage().equalsIgnoreCase("pl")) {
    7. event.setCancelled(true);
    8. player.sendMessage(ChatColor.RED + "Hub developed by "
    9. + ChatColor.GOLD + "PistonGaming." + ChatColor.RED + " "
    10. + ChatColor.ITALIC + "Other plugins are " + ChatColor.GOLD
    11. + "" + ChatColor.BOLD + ChatColor.ITALIC + "private.");
    12. } else if (event.getMessage().equalsIgnoreCase("plugins")) {
    13. event.setCancelled(true);
    14. player.sendMessage(ChatColor.RED + "Hub developed by "
    15. + ChatColor.GOLD + "PistonGaming." + ChatColor.RED + " "
    16. + ChatColor.ITALIC + "Other plugins are " + ChatColor.GOLD
    17. + "" + ChatColor.BOLD + ChatColor.ITALIC + "private.");
    18. } else if(event.getMessage().equalsIgnoreCase("pl") && player.isOp()) {
    19. event.setCancelled(false);
    20. } else if(event.getMessage().equalsIgnoreCase("plugins") && player.isOp()) {
    21. event.setCancelled(false);
    22. }
    23.  
    24. }


    Also, how would I go about making it so ops can see the plugin list, but non-ops see what I have above?
     
  2. Offline

    Avery246813579

    Did you register the commands in the plugin.yml?!
     
  3. Offline

    Camaflicks

    Avery246813579 No... xD I thought since those commands already existed that I wouldn't need to :/ I'll try right now.
     
  4. Offline

    Avery246813579

    Camaflicks If you have a command in your plugin. You have to register it in the plugin.yml. Since the plugin.yml basicly says: Hey we have /pl here. Look here to see if you can find it!
     
  5. Offline

    Camaflicks

    Avery246813579 OK :) Well I registered it... and it didn't work. I did /pl and its unknown. I did /plugins and it brings a plugin list. Could you show me an example, or do you not know how? (Of the actually eventhandler)
     
  6. Offline

    Europia79

    Camaflicks

    I'm not sure exactly what's going on... but to me, the first step is to almost always add debugging lines... Debugging lines just display a message to the console or a player... So that you know if the event is being called or not.

    Code:java
    1. @EventHandler
    2. public void cmdMsg(PlayerCommandPreprocessEvent event) {
    3.  
    4. Player player = (Player) event.getPlayer();
    5. player.sendMessage("PlayerCommandPreprocessEvent() called.");
    6. System.out.println("PlayerCommandPreprocessEvent() called.");
    7.  
     
  7. Offline

    Traks

    I don't quite understand how you're trying to do this... Do you have an executor set for the /pl and /plugins command? Or are you only listening to the PlayerCommandPreprocessEvent? If it's the second case, you don't need to add the commands to your plugin's plugin.yml.
     
  8. Offline

    fireblast709

    He is not looking for his own command, but for Bukkit's /plugins
    Camaflicks you are casting an PlayerCommandPreprocessEvent to a Player, which with always break.
    That aside, remember that commands can consist of more than just the command, for example a / at the beginning or possible extra arguments (even if the command doesn't use arguments, it's an easy way to bypass your block)
    Thirdly,
    Code:java
    1. if(something true)
    2. {
    3. event.setCancelled(true);
    4. }
    5. else if(permission check)
    6. {
    7. event.setCancelled(false);
    8. }
    won't really work due to the fact that the permission check won't be ran. You will need to check whether
    Code:java
    1. if(something true AND no permission)
    2. {
    3. event.setCancelled(true);
    4. }
     
  9. Offline

    ZodiacTheories

    fireblast709

    Can't Camaflicks just create a new command using the onCommand method and override the /pl and /plugins one in the plugin.yml?
     
  10. Offline

    HeadGam3z

    ZodiacTheories likes this.
  11. Offline

    Traks

    ZodiacTheories I think that won't work as the registration of commands can't be overridden. For example, say you want to override the /message command of Essentials, you would have to make sure your plugin loads before Essentials, so you can claim the registration of the /message command.
     
  12. Offline

    Jaaakee224

    Camaflicks Since you are using this event, you do not need to add anything to the plugin.yml, since it is an event, not a command that needs to be registered. I can give you some code that I wrote for this soon, it does exactly what you want.
     
  13. Offline

    ZodiacTheories

    Traks

    I just remember reading something that said you could.
     
  14. Offline

    Go Hard

    Camaflicks
    Code:java
    1. @EventHandler
    2. public void onPreCommand(PlayerCommandPreprocessEvent e) {
    3. Player player = e.getPlayer();
    4. String message = e.getMessage();
    5.  
    6. List<String> commands = new ArrayList<String>();
    7.  
    8. commands.add("/plugins");
    9. commands.add("/pl");
    10. if(commands.contains(message)) {
    11. if(!player.hasPermission("example.permission") || !player.isOp()) { //IF they dont have permission it will cancel the ecevt hand send them the message
    12. e.setCancelled(true);
    13. player.sendMessage(ChatColor.RED + "Hub developed by "
    14. + ChatColor.GOLD + "PistonGaming." + ChatColor.RED + " "
    15. + ChatColor.ITALIC + "Other plugins are " + ChatColor.GOLD
    16. + "" + ChatColor.BOLD + ChatColor.ITALIC + "private.");
    17. }
    18. } else { //If they do have permission it won't cancel the event
    19. e.setCancelled(false);
    20. }
    21.  
    22. }
     
  15. Offline

    fireblast709

    No spoonfeeding please, he pretty much has all the answers in the thread already.

    Go Hard ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     
    ZodiacTheories likes this.
  16. Offline

    Jaaakee224

    fireblast709 "some". I'm not going to make him an entire plugin, he already has the plugin done,.I can show him my code so he can see if there is anything wrong. The fault in his code doesn't seem to major either.
     
  17. Offline

    fireblast709

    Jaaakee224 Rather give him pointers so he can fix it himself. That way he will learn from his mistakes.
     
    ZodiacTheories likes this.
  18. Offline

    Camaflicks

    Jaaakee224 Well that would be ultimately helpful!
     
Thread Status:
Not open for further replies.

Share This Page