Plugin to make other plugins' commands do additional things wont work

Discussion in 'Plugin Development' started by Lanuk, Dec 9, 2011.

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

    Lanuk

    So you all probably know of the essentials /spawn command. What I want to do is make a plugin that will make it so that /spawn not only teleports you to spawn with essentials, but it ALSO displays a message I want it to (specified in my code). I CAN'T seem to get this to work. I am a complete nooby at Java, and the Bukkit API.. but now that I have started, I WILL NOT stop until this plugin WORKS! So... I have resorted to for the third time, asking the bukkit community for help. Here is my code so far... what should I change? Thanks in advance for any help.

    CommandWarning Class
    Code:
    package me.Lanuk.CommandWarning;
    
    import java.util.logging.Logger;
    
    import org.bukkit.event.Event.Priority;
    import org.bukkit.event.Event.Type;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class CommandWarning extends JavaPlugin {
    
        final CommandWarningPlayerListener playerListener = new CommandWarningPlayerListener(this);
        Logger log = Logger.getLogger("Minecraft");
        public PluginManager pm;
    
        public void onDisable() {
            log.info("CommandWarning has been disabled!");
        }
    
        public void onEnable() {
    
            pm = this.getServer().getPluginManager();
            pm.registerEvent(Type.PLAYER_CHAT, playerListener, Priority.Monitor, this);
            //pm.registerEvent(Type.PLAYER_COMMAND_PREPROCESS, playerListener, Priority.Normal, this);
            log.info("CommandWarning has been enabled");
        }
    
    }
    
    CommandWarningPlayerListener Class
    Code:
    package me.Lanuk.CommandWarning;
    
    import java.util.Set;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerChatEvent;
    import org.bukkit.event.player.PlayerListener;
    
    public class CommandWarningPlayerListener extends PlayerListener {
    
        public CommandWarning plugin;
        public CommandWarningPlayerListener (CommandWarning instance) {
            plugin = instance;
        }
        //public CommandWarning plugin;
        //public CommandWarningPlayerChatEvent(CommandWarning instance){
            //plugin = instance;
        //}
        public void onPlayerChat(PlayerChatEvent event) {
            Set<Player> sendTo = event.getRecipients();
            if (event.getMessage().startsWith("/")) {
                String message = event.getMessage();
                plugin.getServer().broadcast(message, "CommandWarning.Send");
                if (sendTo.toString().contains(plugin.getServer().getOperators().toString())) {
                    plugin.getServer().broadcastMessage(message);
                }
            }
        }
    
    }
    yml File
    Code:
    name: CommandWarning
    main: me.Lanuk.CommandWarning.CommandWarning
    version: 0.1
    
              
    EDIT: Right now, as is, this plugin basically does nothing in-game. It enables fine.
     
  2. Offline

    wwsean08

  3. Offline

    Lanuk

    Alright, so everyone is telling me that, and I have tried that, but it hasnt exactly worked either.. Yes, I have tried to read the API, but still dont exactly get what to do. I mean, what does the onPlayerCommandPreprocess event even do in the first place?
     
  4. Offline

    wwsean08

    that is called every time a command is executed with the pertinint information like the command and args, the player etc.
     
  5. Offline

    hatstand

    This, though you don't get the command name and args in separate variables, they're all in the message.
    You can get the command name with something similar to this:
    Code:java
    1. public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event)
    2. {
    3. //Split the entire command string into its components, then get the first one, which will be the command, including the /
    4. String cmd = event.getMessage().split(" ")[0];
    5.  
    6. //Do your stuff here, check if the command is what you want, then act on it. Make sure the event isn't cancelled as well, and account for the sender of the command not being a player.
    7. }
     
  6. Offline

    wwsean08

    true, I was thinking more of looking for specific ones you could just looks for:

    Code:
    if(event.getMessage().startsWith("/command"){
    //do stuff
    }
     
  7. Offline

    Lanuk

    Alright.. so so far I have this:

    Code:
    public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event)
            {
    
                String cmd = event.getMessage().split(" ")[0];
                if(event.getMessage().equals("/ping")){
    
                }
    
            }
    Now, assuming this is correct (which it probably isnt for reasons obvious to everone but me), how do I make it so that a message will be broadcasted in the if statement?
     
  8. Offline

    hatstand

    If by broadcast, you mean send to everyone, then you'll need to get an instance of the server from your plugin (plugin.getServer()) and broadcast the message via broadcastMessage().

    If you mean to send a message to the player, get the player from the event, and use sendMessage().

    As for your existing code, that's fine, but I find using equalsIgnoreCase() is more reliable than equals() for command-related stuff.
     
  9. Offline

    Lanuk

    Ok, thanks. My code now looks like this:

    CommandWarning class

    Code:
    package me.Lanuk.CommandWarning;
    
    import java.util.logging.Logger;
    
    import org.bukkit.event.Event.Priority;
    import org.bukkit.event.Event.Type;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    public class CommandWarning extends JavaPlugin {
    
        final CommandWarningPlayerListener playerListener = new CommandWarningPlayerListener(this);
        Logger log = Logger.getLogger("Minecraft");
        public PluginManager pm;
    
        public void onDisable() {
            log.info("CommandWarning has been disabled!");
        }
    
        public void onEnable() {
    
            pm = this.getServer().getPluginManager();
            //pm.registerEvent(Type.PLAYER_CHAT, playerListener, Priority.Normal, this);
            pm.registerEvent(Type.PLAYER_COMMAND_PREPROCESS, playerListener, Priority.Normal, this);
            log.info("CommandWarning has been enabled");
        }
    
    }
    
    CommandWarningPlayerListener Class
    Code:
    package me.Lanuk.CommandWarning;
    
    import java.util.Set;
    
    import org.bukkit.Server;
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerChatEvent;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import org.bukkit.event.player.PlayerListener;
    
    public class CommandWarningPlayerListener extends PlayerListener {
    
        public CommandWarning plugin;
        public CommandWarningPlayerListener (CommandWarning instance) {
            plugin = instance;
        }
    
        public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event){
     
            String cmd = event.getMessage().split(" ")[0];
            if(event.getMessage().equalsIgnoreCase("/ping")){
                plugin.getServer().broadcastMessage("La");
            }
    
        }
    
    }
    
    As is when I use /ping, it responds with "Pong!" as the Essentials plugin tells it to. It does not display my "La". What else am I missing, does anyone know?
     
  10. Offline

    hatstand

    I tried using startsWith in my code, and it didn't pick up on the command, so I used the split method I posted.

    Try logging something to the console that tells you when or if your code gets through that if statement. If it isn't, log the things you're comparing as well, wrapped in quotes for whitespace (In case you're getting a space strapped onto the end of the command input), with lengths (same reason as before), to make sure you're looking for the same thing it's giving you. If it does, then it's a problem with the broadcast itself. I have seen a few sporadic reports of broadcasts being unreliable, but haven't experienced it myself.
     
  11. Offline

    wwsean08

    ah ok, i've tried the starts with before and it seemed to work, either that or i'm just going senile and am forgetting old code
     
  12. Offline

    hatstand

    I tend to run into issues nobody else does...
     
  13. Offline

    wwsean08

    haha yah, i just checked my code and i used the statsWith and it worked fine
     
  14. Offline

    Lanuk

    Alright so I attempted to display a message in the log like so:

    Code:
    package me.Lanuk.CommandWarning;
    
    import java.util.logging.Logger;
    
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import org.bukkit.event.player.PlayerListener;
    
    public class CommandWarningPlayerListener extends PlayerListener {
    
        Logger log = Logger.getLogger("Minecraft");
    
        public CommandWarning plugin;
        public CommandWarningPlayerListener (CommandWarning instance) {
            plugin = instance;
        }
    
        public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event){
     
            String cmd = event.getMessage().split(" ")[0];
            if(event.getMessage().startsWith("/ping")){
                //plugin.getServer().notifyAll();
                log.info("La");
            }
    
        }
    
    }
    But nothing showed up in the console and I still got my reguar "Pong!". Also, I know this is a stupid question, but what does this:

    String cmd = event.getMessage().split(" ")[0];

    do? cmd isn't being used anywhere so it is currently a pointless line of code.
     
  15. Offline

    Slamakans

    if the sent message == "I have a weirdest potato" then the method event.getMessage().split(" ");
    will split the message wherever there is a space, and return with a String[], in my line of code these array values would be created:
    cmd[0] == "I"
    cmd[1] == "have"
    cmd[2] == "a"
    cmd[3] == "weirdest"
    cmd[4] == "potato"

    now, since the code that you are using has [0] at the end, it instantly sets cmd to the string that is contained in the first array, in my case it would've been "I", and then disposes the other 4 values. I'll show you a little shorter in code also.

    string here will be = "I am a pen"

    Code:java
    1. public void exampleStringThingy(Player player, String string){
    2. // Sends the original string.
    3. player.sendMessage(string);
    4.  
    5. // Splits the string into an array and then sets newString to the value contained in [2] == "a"
    6. String newString = string.split(" ")[2];
    7. player.sendMessage(newString); // Sends that string.
    8.  
    9. // Splits the string into an array and then sets newString to that array.
    10. String newString = string.split(" ");
    11.  
    12. // Sends all the strings contained in that array. .length() gets the length of an array.
    13. for(int i = 1; i < newString.length(); i++){
    14.  
    15. // I do i-1 since an array's length is always 1 more than the actual array number.
    16. player.sendMessage(newString[i-1]);
    17.  
    18. }
    19.  
    20. }

    The above would print these messages in this order:

    Code:
    I am a pen
    
    a
    
    I
    am
    a
    pen
    Tell me if I missed anything or wasn't clear enough or was simply wrong somewhere :)
     
  16. Offline

    Lanuk

    Alright, I think I get it, thanks. I assume "I am a pen" is stored in "string", correct?

    Garr, I can't seem to get this to work at all. :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 22, 2016
Thread Status:
Not open for further replies.

Share This Page