OnCommand usage

Discussion in 'Plugin Development' started by dxwarlock, Feb 19, 2012.

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

    dxwarlock

    I know Im probably doing this wrong..as only commands I register in my plugin.yml is noticed by the plugin.

    Im guessing it would need
    "public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event)" but not sure the way to use it correctly.

    what Im attempting to do is change my guest plugin..that promote people on the command /village..to include kicking people on using /give /fly ect..

    here is a snippet of what I have as an example:

    Code:
        public boolean onCommand(CommandSender cs, Command cmd, String alias, String[] args) {
            Player player = (Player)cs;
            String user1 = player.getName();
            if (cmd.getName().equalsIgnoreCase("village")) {
                if (args.length != 0) {
                    return false;
                }
                else {
                    SetRank(cs);
                }
            }
            if (cmd.getName().equalsIgnoreCase("give")) {
                if (player.hasPermission("cmd.exempt")) {
                    return false;
                }
                else {
                    player.kickPlayer("Tried to use /GIVE");
                }
            }
    the /village works well of course because Im calling out village in my plugin.yml.

    what would be the correct way to catch commands not defined in my plugin, and to preform actions when it sees it?

    for example /give /gamemode are all valid commands admin/ops can use. but wish to catch others trying it, and kick them to give them the idea that spamming that command 400 times wont help :)
     
  2. Not sure but i think something like this:
    Code:java
    1. @EventHandler
    2. public void cmd(PlayerCommandPreprocessEvent event) {
    3. if(event.getMessage() == "give") {
    4. event.setCancelled(true);
    5. event.getPlayer().kickPlayer("Tried to use /GIVE");
    6. }
    7. }

    would work... by the way u need:
    getServer().getPluginManager().registerEvents(this, this);
    in your onEnable if my code will be in your main class, but if it will be in another class add
    getServer().getPluginManager().registerEvents(new nameofotherclass(), this);
    to your onEnable and
    implements Listener in the public class part
    like this: http://wiki.bukkit.org/Introduction...em#Registering_Events_with_Plugin_as_Listener
     
  3. Offline

    dxwarlock

    would that work also the command was a full give command?
    like "/give 1 1 0"
    or only if they did the "/give"

    what I had also tried, trying to get it to work, kind of worked. but it looked for give in any command, even if someone did:
    "/msg bob hey bob can you give me a pickaxe?"

    it would see 'give' in the string of the command and kick them. which was bad, so dont seem I was doing it right that way either :p

    so if what you put would work for a command with arg long as it 'started' with /give that might be what I need!

    Think its getting closer but its not noticing the command name in the IF, no matter what I put
    Code:java
    1. @EventHandler
    2. public void cmd(PlayerCommandPreprocessEvent event) {
    3. Player player = event.getPlayer();
    4. String cmd = event.getMessage();
    5. player.sendMessage("CHECK " + cmd);
    6. if(event.getMessage() == "give") {
    7. player.sendMessage("PASSED");
    8. event.setCancelled(true);
    9. event.getPlayer().kickPlayer("Tried to use /GIVE");
    10. }
    11. }

    the above code gives the 'CHECK /give" response, so I know its checking it..but can try /give, /give dxwarlock 1 1, etc and it never makes it to the 'PASSED" remark.

    Figured out what I needed, need to do a split to get the first command sent..if anyone else needs it
    Code:java
    1. @EventHandler
    2. public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e)
    3. {
    4. Player p = e.getPlayer();
    5. String user1 = p.getName();
    6. String[] cmd = e.getMessage().split(" ");
    7. if(!p.hasPermission("dx.cmd")) {
    8. if(cmd[0].equalsIgnoreCase("/give")){
    9. e.setCancelled(true);
    10. p.kickPlayer("Tried to use /GIVE");
    11. Bukkit.getServer().broadcastMessage(user1 + ChatColor.RED + " used command " + e.getMessage());
    12. }
    13. }
    14. }


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

Share This Page