Checking if command has been executed

Discussion in 'Plugin Development' started by beatcomet, Jul 13, 2011.

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

    beatcomet

    Hi guys, I need hlpe with somehing.
    I need to check if a command has been executed and I have no idea how, I tried using bot CommandPreProcessEvent and PlayerChatEvent, but none of the work...
     
  2. Offline

    DrAgonmoray

    @beatcomet
    Use onCommand. it goes in your main class (the one with onEnable and onDisable)
    PHP:
    public boolean onCommand(CommandSender senderCommand commandString labelString[] args) {
    In one of my plugins I use it like this:
    PHP:
        public boolean onCommand(CommandSender senderCommand commandString labelString[] args) {
            
    Player player = (Player)sender;
            if (
    command.getName().equalsIgnoreCase("teletab")) {
                if (
    canSet(player)) {
                    if (
    player.getItemInHand().getType().equals(Material.GREEN_RECORD)) {
                        
    green player.getLocation();
                        
    player.sendMessage(ChatColor.GREEN+"Green Teletab set!");
                    } else if (
    player.getItemInHand().getType().equals(Material.GOLD_RECORD)) {
                        
    gold player.getLocation();
                        
    player.sendMessage(ChatColor.GOLD+"Gold Teletab set!");
                    }
                    return 
    true;
                } else {
                    return 
    true;
                }
            }
            return 
    false;
        }
     
  3. Offline

    ItsHarry

    What do you mean with has been executed?
    You mean a command from a different plugin? Or a plugin that toggles something?
     
  4. Offline

    beatcomet

    It will workd with other plugins?
    I need to check if a command like /god has been executed and then write it on a config file (it's not a problem)
     
  5. Offline

    ItsHarry

    @beatcomet What he posted is not what you asked.

    You basically want to know if the user has typed /god ?
    For that, make a HashMap and store a boolean when the user types /god
     
  6. Offline

    nisovin

    The CommandPreProcessEvent will work. If it didn't, it means you did it wrong.
     
  7. Use PlayerCommandPreprocessEvent. It's basically executed everytime a player uses a command BEFORE it is handed to onCommand.
    So check if the label is god and do whatever you want with it.
     
  8. Offline

    beatcomet

    Can you give me an example?
    I don't really know how to check the label of other plugin's commands....
     
  9. Code:java
    1. @Override
    2. public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event){
    3. String cmd = event.getMessage();
    4. Player ply = event.getPlayer();
    5. ply.sendMessage("You performed the following command: "+cmd);
    6. }

    cmd would now contain the command the player typed.
     
  10. Offline

    beatcomet

    tried it, but it does nothing...
    I will check it, mybe I did something wrong ...

    Edit:

    Here is what I did with your help

    Code:java
    1.  
    2. public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event){
    3. String cmd = event.getMessage();
    4. Player ply = event.getPlayer();
    5. String name = ply.getName();
    6. ply.sendMessage("You performed the following command: "+cmd);
    7. if((Boolean)plugin.config.getProperty(name) == true){
    8. plugin.config.setProperty(name, false);
    9. plugin.config.save();
    10. }if((Boolean) plugin.config.getProperty(name) == false){
    11. plugin.config.setProperty(name, true);
    12. plugin.config.save();
    13. }
    14. }
    15.  


    I hope it will work ..
     
  11. The sendMessage thingy is of course just for debugging. If it doesn't shows up, you either didn't register your listener or failed to put it in the correct class because of the missing @Override.

    And for god's sake, stop using getProperty(...) on the configuration and casting it to the expected value. Just take getBoolean(String root, boolean default), and it will always give you the expected result.
    Same for every other data type that you store.


    And you should still check if the given cmd is the one you want, because right now it would execute for every single command that the player types ...
     
  12. Offline

    beatcomet

    it's ok, it works.
    Here is the main class :

    Code:java
    1.  
    2. package me.beatcomet.GM;
    3.  
    4. import java.io.File;
    5. import java.util.logging.Logger;
    6.  
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.Event;
    12. import org.bukkit.plugin.PluginManager;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14. import org.bukkit.util.config.Configuration;
    15.  
    16. public class GM extends JavaPlugin{
    17. Logger log = Logger.getLogger("Minecraft");
    18. public PListener playerListener = new PListener (this);
    19. File configFile = new File("plugins/" + "GM" + "/log.txt");
    20. Configuration config = new Configuration(configFile);
    21. public void onEnable(){
    22. log.info("GM version 1.0 has been ENABLED!");
    23. PluginManager pm = this.getServer().getPluginManager();
    24. pm.registerEvent(Event.Type.PLAYER_JOIN, playerListener, Event.Priority.Normal, this);
    25. pm.registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS, playerListener, Event.Priority.Normal, this);
    26. new File("plugins/" + "GM").mkdir();
    27. if (!configFile.exists()) {
    28. try {
    29. configFile.createNewFile();
    30.  
    31. } catch (Exception e) {
    32. // sending console message in case the data file could not be
    33. // created
    34. log.info("[GM] Error when creating data file.");
    35. }
    36. }
    37. // Loading data file
    38. config.load();
    39. }
    40.  
    41. public void onDisable(){
    42. log.info("GM version 1.0 has been DISABLED!");
    43. }
    44. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    45. if(sender instanceof Player){
    46. Player player = (Player) sender;
    47. if(player.isOp()){
    48. if(commandLabel.equalsIgnoreCase("stt")){
    49. if(args.length == 0){
    50. String name = player.getName();
    51. boolean pl = (Boolean) config.getProperty(name);
    52. String construct = null;
    53. for (Player p : getServer().getOnlinePlayers()) {
    54. if (pl == true) {
    55. this.config.setProperty(p.getName()+" status", ": God");
    56. }
    57. if (pl == false) {
    58. this.config.setProperty(p.getName()+" status", ": Not God");
    59. }
    60. construct = ChatColor.GOLD + p.getName() + ChatColor.AQUA
    61. + this.config.getProperty(p.getName()+ " status")
    62. + ChatColor.BLACK + " | ";
    63. }
    64. sender.sendMessage(construct);
    65.  
    66. }
    67. }
    68. }return true;
    69. }
    70. return false;
    71. }
    72. }
    73.  


    Here is the player Listener :

    Code:java
    1.  
    2. package me.beatcomet.GM;
    3.  
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    6. import org.bukkit.event.player.PlayerJoinEvent;
    7. import org.bukkit.event.player.PlayerListener;
    8.  
    9. public class PListener extends PlayerListener{
    10. GM plugin;
    11. public PListener(GM instance){
    12. plugin = instance;
    13. }
    14. public void onPlayerJoin(PlayerJoinEvent event){
    15. Player player = event.getPlayer();
    16. String name = player.getName();
    17. if(plugin.config.getProperty(name) == null)
    18. {
    19. plugin.config.setProperty(name, false);
    20. plugin.config.save();
    21. }
    22. }
    23. public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event){
    24. String cmd = event.getMessage();
    25. Player ply = event.getPlayer();
    26. String name = ply.getName();
    27. boolean player = (Boolean)plugin.config.getProperty(name);
    28. if(cmd.equalsIgnoreCase("/god")){
    29. if(player == true){
    30. plugin.config.setProperty(name, false);
    31. plugin.config.save();
    32. }if(player == false){
    33. plugin.config.setProperty(name, true);
    34. plugin.config.save();
    35. }
    36. }
    37. }
    38. }
    39.  


    People my want to use it as an example :)
     
Thread Status:
Not open for further replies.

Share This Page