Solved Why won't this plugin work? (Alert online mods when a player types a specific command)

Discussion in 'Plugin Development' started by redcanoe, Aug 15, 2014.

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

    redcanoe

    I'm trying to make a plugin that alerts people with the permission "arcane.mod" whenever a player types a specific command - For example, "opme" or "hack" is what I have it set to.

    Code:java
    1. @EventHandler
    2. public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e)
    3.  
    4. {
    5.  
    6. Player player = e.getPlayer();
    7. if (e.getMessage() == ("opme") || e.getMessage() == ("hack")){
    8.  
    9. Bukkit.broadcast(ChatColor.GOLD + "[Alert] " + ChatColor.WHITE + player.getName() + ChatColor.GOLD +
    10. " issued command \"" + ChatColor.WHITE + e.getMessage() + ChatColor.GOLD + "\".", "bukkit.broadcast.admin");
    11.  
    12. player.sendMessage(ChatColor.GRAY + "you typed something...");
    13.  
    14. }
    15. }


    It's just not doing anything! It only worked when I changed the == over to != for some reason. But it didn't alert for every command but opme/hack... It alerted for every single command. :/

    Thanks!
     
  2. Offline

    JustinsCool15

    redcanoe
    You need to put "/opme" and "/hack" I think.
     
  3. Offline

    xAstraah

    @redcano, You are trying to get the message that a Player has sent on a PlayerCommandPreProcessEvent i would recommend using a AsyncPlayerChatEvent and hopefully that will solve your problem.
     
  4. Offline

    LordVakar

    .equals to compare strings
    EDIT: Why didn't the people above me even see that, don't post without actually analyzing the code.
     
  5. Offline

    xAstraah

    redcanoe, Also i would recommend using .contains (Couldn't edit my original post)

    LordVakar, I believe it is mostly conventions to use .equals on strings since '==' also works.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  6. Offline

    LordVakar

  7. Offline

    xAstraah

    @LordVaka, Okay your point has now cleared up a mind**** :)
     
  8. Offline

    redcanoe

    I ended up doing this and it worked, thanks:

    Code:java
    1. @EventHandler
    2. public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e)
    3.  
    4. {
    5.  
    6. Player player = e.getPlayer();
    7. if (e.getMessage().startsWith("/opme") || e.getMessage().startsWith("/hack")|| e.getMessage().startsWith("/gamemode")){
    8.  
    9. Bukkit.broadcast(ChatColor.GOLD + "[Alert] " + ChatColor.WHITE + player.getName() + ChatColor.GOLD +
    10. " issued command \"" + ChatColor.WHITE + e.getMessage() + ChatColor.GOLD + "\".", "bukkit.broadcast.admin");
    11.  
    12. }
    13. }
     
Thread Status:
Not open for further replies.

Share This Page