Solved How do I disable PLUGIN:COMMAND commands

Discussion in 'Plugin Development' started by mehboss, Sep 13, 2016.

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

    mehboss

    I have done this and when I run the plugin on my server it doesn't cancel it, can someone please correct this for me? thanks.

    MY CODE (open)
    package me.mehboss.anticolon;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class AntiColon extends JavaPlugin implements Listener, CommandExecutor {

    public void onEnable() {
    Bukkit.getServer().getLogger().info("EasyStream has been Enabled!");

    }

    public void onDisable() {
    Bukkit.getServer().getLogger().info("EasyStream has been Disabled!");
    }

    @EventHandler
    public void onCommand(CommandSender sender, Command cmd, String commandLabels, PlayerCommandPreprocessEvent event) {

    if (event.getMessage().split(" ")[0].contains(":")) {
    event.setCancelled(true);
    sender.sendMessage("Please refrain from using colon commands!");

    }

    }
    }
     
  2. Offline

    Zombie_Striker

    @mehboss
    1. No one will fix your plugin for you. We will help you, but this is your plugin. You have to fix it.
    2. You forgot to register your listener. In order for events to work you have to add the following line to the onEnable
      Code:
      Bukkit.getPluginManager().registerEvents(this,this);
    3. Since this class already extends JavaPlugin, remove "CommandExecutor".
    4. Don't log your plugins. Bukkit does this for you. That will just spam the console.
    5. You can't put events inside of the onCommand. If you want to listen to PlayerCommendPreprocessEvent, create a new event.
    6. Because you only want to cancel the event, you do not actually need the onCommand.
    7. onCommand is not an event. Remove the @EventHandler annotation.
     
  3. Offline

    mehboss

    @Zombie_Striker

    Here is the new code, it doesn't want to work still, also I will remove the logger.

    Code:
    
    package me.mehboss.anticolon;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class AntiColon extends JavaPlugin implements Listener {
    
        public void onEnable() {
            Bukkit.getServer().getLogger().info("EasyStream has been Enabled!");
            Bukkit.getPluginManager().registerEvents(this,this);
         
        }
     
        public void onDisable() {
            Bukkit.getServer().getLogger().info("EasyStream has been Disabled!");
        }
           public void onPlayerCommandPreprocessEvent(CommandSender sender, Command cmd, String commandLabels, PlayerCommandPreprocessEvent event) {
               if (event.getMessage().split(" ")[0].contains(":")) {
                   event.setCancelled(true);
                   sender.sendMessage("Please refrain from using colon commands!");
                   }
                 }
               }
         
    
     
    Last edited: Sep 14, 2016
  4. Offline

    Zombie_Striker

    @mehboss
    That is still not an event. Events look like
    Code:
    @EventHandler
    public void onSomething([The Event] event){
     //Your code
    }
     
    cococow123 likes this.
  5. Offline

    mehboss

    @Zombie_Striker
    Could you give me a better example of it please, thanks.

    what would the ([The Event] event) { thing be?
     
  6. Offline

    Jakeeeee

  7. Offline

    mehboss

    @Jakeeeee @Zombie_Striker

    So public void onColonCommand (PlayerCommandPreprocessEvent event) {

    would this one be right?


    EDIT: When you say I need to create a new event do I have to create a new class? Also question above ^
     
    Last edited: Sep 14, 2016
  8. Offline

    Zombie_Striker

  9. Offline

    mehboss

  10. Offline

    Jakeeeee

    You don't have to create a new class.
     
  11. Offline

    mehboss

    @Jakeeeee @Zombie_Striker
    Isn't this still an event, it has the event string on the end,
    Code:
           public void onPlayerCommandPreprocessEvent(CommandSender sender, Command cmd, String commandLabels, PlayerCommandPreprocessEvent event)
     
  12. Offline

    Zombie_Striker

    @mehboss
    Nope. You can't just make up parameters. You can only have the event inside the parameters.
     
  13. Offline

    mehboss

    Doesn't work this..
    Code:
    package me.mehboss.anticolon;
    
    import org.bukkit.Bukkit;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import org.bukkit.plugin.java.JavaPlugin;
       
    public class AntiColon extends JavaPlugin implements Listener {
    
        public void onEnable() {
            Bukkit.getServer().getLogger().info("EasyStream has been Enabled!");
            Bukkit.getPluginManager().registerEvents(this,this);
           
        }
       
        public void onDisable() {
            Bukkit.getServer().getLogger().info("EasyStream has been Disabled!");
        }
            public void onColonCommand(PlayerCommandPreprocessEvent event) {
                  if (event.getMessage().split(" ")[0].contains(":")) {
                      event.setCancelled(true);
                    }
                  }
                }
            
     
  14. Online

    timtower Moderator Moderator

  15. Offline

    mehboss

    Still does not work
    Code:
    package me.mehboss.anticolon;
    
    import org.bukkit.Bukkit;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import org.bukkit.plugin.java.JavaPlugin;
      
    public class AntiColon extends JavaPlugin implements Listener {
    
        public void onEnable() {
            Bukkit.getServer().getLogger().info("EasyStream has been Enabled!");
            Bukkit.getPluginManager().registerEvents(this,this);
          
        }
      
        public void onDisable() {
            Bukkit.getServer().getLogger().info("EasyStream has been Disabled!");
        }
            public void onColonCommand(PlayerCommandPreprocessEvent event) {
                  if (event.getMessage().split(" ")[0].contains(":")) {
                      event.setCancelled(true);
                    }
                  }
                }
            
     
  16. Online

    timtower Moderator Moderator

    @mehboss You still don't have the @EventHandler on the event.
     
  17. Offline

    mehboss

    @timtower
    As simple as that, dang.. I feel stupid, thanks <3

    EDIT:
    Also timtower, sorry xD you responded so fast I didn't see the first time you said to put an eventhandler on it :O

    EDIT #2:
    @timtower
    My name is Tim so this is awkward.. anyways, like this so far? XD
    Code:
    package me.mehboss.anticolon;
    
    import org.bukkit.Bukkit;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class AntiColon extends JavaPlugin implements Listener {
     
        public void onEnable() {
            Bukkit.getServer().getLogger().info("EasyStream has been Enabled!");
            Bukkit.getPluginManager().registerEvents(this,this);
            registerConfig();
       
        }
     
        private void registerConfig() {
            getConfig().options().copyDefaults(true);
            saveConfig();
            getLogger();
        }
    
        public void onDisable() {
            Bukkit.getServer().getLogger().info("EasyStream has been Disabled!");
        }
     
        @EventHandler
            public void onColonCommand(PlayerCommandPreprocessEvent event) {
                  if (event.getMessage().split(" ")[0].contains(":")) {
                      event.setCancelled(true);
                      String noColonMessage = ChatColor.translateAlternateColorCodes('&',
                            this.getConfig().getString("noColonMessage"));
                     event.getPlayer().sendMessage(noColonMessage);
                    if (event.getPlayer().hasPermission("anticolon.bypass")) {
                    event.setCancelled(false);
                  }
    
            }             
        }
    }
     
    Last edited: Sep 15, 2016
  18. Online

    timtower Moderator Moderator

    @mehboss It gets better.
    Yet I would just remove the permission myself.
     
  19. Offline

    mehboss

  20. Offline

    ArsenArsen

    Wrong ChatColor!!
    Also I believe what @timtower wanted to point out is that he would remove the perm check, you can also remove those three lines and add the check in the first statement
    That does nothing, remove it.
    No need to do this, bukkit does it for you
    Just use saveDefaultConfig() instead.

    Also format your code ;)
     
  21. Online

    timtower Moderator Moderator

    @ArsenArsen Nope, wrong.
    @mehboss I would personally just make sure that the player won't have the permission to run the command at all and just ignore the label stuff.
     
  22. Offline

    mehboss

    @timtower
    I want ops to still be able to do colon commands though ;)
     
    Last edited: Sep 16, 2016
  23. Offline

    mehboss

    @timtower + @Zombie_Striker + @Jakeeeee

    Hey guys, so I have another question.

    EXAMPLES: lets say someone does /warps and opens the GUI, how would I make it so that they can't edit their inventory while they have the /warps GUI open?

    CODE:
    Code:
    package me.mehboss.warpgui;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.Plugin;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class Menu implements Listener {
       
        private Inventory inv;
        private ItemStack crates, info, mobarena, shop, redstained1;
       
        public Menu(Plugin p, String cratename, String infoname, String mobname, String shopname, String rstain1name, String name) {
            String GUISize = Bukkit.getPluginManager().getPlugin("WarpGUI").getConfig().getString("GUI-Size");
            inv = Bukkit.getServer().createInventory(null, Integer.parseInt(GUISize), "Warps");
           
            crates = crateItem(ChatColor.GREEN + "Crates");
            info = infoItem(ChatColor.YELLOW + "Info");
            mobarena = mobarenaItem(ChatColor.RED + "Mob Arena");
            shop = shopItem(ChatColor.AQUA + "Shop");
            redstained1 = rstainedItem(ChatColor.translateAlternateColorCodes('&', "&8&l>> &cRocket&6MC &8&l<<"));
           
            inv.setItem(10, crates);
    //1st warp ^
           
            inv.setItem(12, info);
    //2nd warp ^
           
            inv.setItem(14, mobarena);
    //3rd warp ^
           
            inv.setItem(16, shop);
    //4th warp ^
           
            inv.setItem(0, redstained1);
            inv.setItem(1, redstained1);
            inv.setItem(2, redstained1);
            inv.setItem(3, redstained1);
            inv.setItem(4, redstained1);
            inv.setItem(5, redstained1);
            inv.setItem(6, redstained1);
            inv.setItem(7, redstained1);
            inv.setItem(8, redstained1);
            inv.setItem(18, redstained1);
            inv.setItem(19, redstained1);
            inv.setItem(20, redstained1);
            inv.setItem(21, redstained1);
            inv.setItem(22, redstained1);
            inv.setItem(23, redstained1);
            inv.setItem(24, redstained1);
            inv.setItem(25, redstained1);
            inv.setItem(26, redstained1);
    //All of the stained glass ^
           
            Bukkit.getServer().getPluginManager().registerEvents(this, p);
            return;
        }
       
        private ItemStack crateItem(String name) {
            ItemStack crates = new ItemStack(Material.CHEST);
            ItemMeta cm = crates.getItemMeta();
            cm.setDisplayName(ChatColor.GREEN + "Crates");
               String crateLore = ChatColor.translateAlternateColorCodes('&', "&7Find the crates here!");
                List<String> loreList = new ArrayList<String>();
                loreList.add(crateLore);
                cm.setLore(loreList);
            crates.setItemMeta(cm);
            return crates;
        }
       
        private ItemStack infoItem(String name) {
            ItemStack info = new ItemStack(Material.PAPER);
            ItemMeta im = info.getItemMeta();
            im.setDisplayName(ChatColor.YELLOW + "Info");
               String infoLore = ChatColor.translateAlternateColorCodes('&', "&7Find some useful info here!");
                List<String> loreList = new ArrayList<String>();
                loreList.add(infoLore);
                im.setLore(loreList);
            info.setItemMeta(im);
            return info;
        }
       
        private ItemStack mobarenaItem(String name) {
            ItemStack mobarena = new ItemStack(Material.MOB_SPAWNER);
            ItemMeta mm = mobarena.getItemMeta();
            mm.setDisplayName(ChatColor.RED + "Mob Arena");
            String mobLore = ChatColor.translateAlternateColorCodes('&', "&7Get some free food here!");
            List<String> loreList = new ArrayList<String>();
            loreList.add(mobLore);
            mm.setLore(loreList);
            mobarena.setItemMeta(mm);
            return mobarena;
        }
       
        private ItemStack shopItem(String name) {
            ItemStack shop = new ItemStack(Material.DIAMOND);
            ItemMeta sm = shop.getItemMeta();
            sm.setDisplayName(ChatColor.AQUA + "Shop");
               String shopLore = ChatColor.translateAlternateColorCodes('&', "&7Find the shop here!");
                List<String> loreList = new ArrayList<String>();
                loreList.add(shopLore);
                sm.setLore(loreList);
            shop.setItemMeta(sm);
            return shop;
        }
       
        private ItemStack rstainedItem(String name) {
            ItemStack redstained1 = new ItemStack(Material.STAINED_GLASS_PANE);
            ItemMeta rs1 = redstained1.getItemMeta();
            rs1.setDisplayName(ChatColor.translateAlternateColorCodes('&', "&8&l>> &cRocket&6MC &8&l<<"));
            redstained1.setItemMeta(rs1);
            return redstained1;
        }
       
        public void show(Player p) {
            p.openInventory(inv);
        }
       
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
            if (!event.getInventory().getName().equalsIgnoreCase(inv.getName())) return;
            if (event.getCurrentItem().getItemMeta().getDisplayName().contains("Crates")) {
                event.setCancelled(true);
                event.getWhoClicked().closeInventory();
                  Player player = (Player)event.getWhoClicked();
                    player.performCommand("warp Crates");
            }
            if (event.getCurrentItem().getItemMeta().getDisplayName().contains("Info")) {
                event.setCancelled(true);
                event.getWhoClicked().closeInventory();
                  Player player = (Player)event.getWhoClicked();
                    player.performCommand("warp Info");
            }
            if (event.getCurrentItem().getItemMeta().getDisplayName().contains("Mob Arena")) {
                event.setCancelled(true);
                event.getWhoClicked().closeInventory();
                  Player player = (Player)event.getWhoClicked();
                    player.performCommand("warp MobArena");
            }
            if (event.getCurrentItem().getItemMeta().getDisplayName().contains("Shop")) {
                event.setCancelled(true);
                event.getWhoClicked().closeInventory();
                  Player player = (Player)event.getWhoClicked();
                    player.performCommand("warp Shop");
            }
            if (event.getCurrentItem().getItemMeta().getDisplayName().contains(ChatColor.translateAlternateColorCodes('&', "&8&l>> &cRocket&6MC &8&l<<"))) {
                event.setCancelled(true);
            }
        }
    }
    
     
  24. Offline

    Zombie_Striker

    @mehboss
    Don't necro old thread. Make a new thread if this is for a new issue.

    For your issue:
    1. Listen to InventoryClickEvent.
    2. If the player has the GUI open, and if they are clicking inside their inventory
    3. cancel the event.
     
  25. Offline

    mehboss

    @Zombie_Striker
    Examples please? Also reason why I commented on this is because timtower flagged me for spam by making a new thread xD idk if I made another thread I would get another warning
     
  26. Offline

    bwfcwalshy Retired Staff

    Why are you getting String to parse to int? Why not just getInt?
     
Thread Status:
Not open for further replies.

Share This Page