Assistance with PlayerCommandPreprocessEvent

Discussion in 'Spigot Plugin Development' started by Realm, May 3, 2017.

  1. I am trying to cancel Buycrafts /buy command where it pops up with a gui and replace it with a message.
    Ive never used the preprocess event before but I figured it wouldnt be difficult to figure out...

    My attempt:

    Code (Text):
    public class ChangeBuyCommand extends JavaPlugin implements Listener {

        public void onEnable() {
            Bukkit.getPluginManager().registerEvents(this, this);
            saveDefaultConfig();
        }

        @EventHandler (priority = EventPriority.HIGHEST)
        public void onCommandPreprocess(PlayerCommandPreprocessEvent e) {
            Player p = e.getPlayer();
            if (!e.getMessage().startsWith("/buy")) return;
            e.setCancelled(true);
            String message = ChatColor.translateAlternateColorCodes('&', getConfig().getString("buy-replace"));
            String url = ChatColor.translateAlternateColorCodes('&', getConfig().getString("url"));
            p.sendMessage(message);
            p.sendMessage(url);
        }

    }
    It sends me a perfect message in game as expected... but it doesnt cancel the Buycraft GUI from opening... I attempted to decompile and look at how https://www.spigotmc.org/resources/disable-commands.386/ was able to do it. But for that person it worked (I presume)...
    Code (Text):
      @EventHandler(priority=EventPriority.HIGHEST)
      public void onPreprocess(PlayerCommandPreprocessEvent event)
      {
        String command = event.getMessage().substring(1);
        if ((!event.getPlayer().isOp()) && (!event.getPlayer().hasPermission("disablecommands.bypass")))
        {
          List forbiddenCommands = this.plugin.getDisableCommandsConfig().getStringList("forbidden-commands");
          for (String forbiddenCommand : forbiddenCommands)
            if (command.contains(forbiddenCommand)) {
              event.setCancelled(true);
              String msg = this.plugin.getConfig().getString("Messages.Command Deny Message");
              event.getPlayer().sendMessage(ChatColor.RED + msg);
              break;
            }
        }
      }
    Any idea how to disable Buycraft's GUI with that command?
     
  2. foncused

    Moderator Patron

    Why not just disable it in the Buycraft config?
    Code (Text):
    disable-buy-command=true
     
    • Agree Agree x 1
  3. Maybe just close the inventory? I'm assuming you can't type commands while having an inventory opened so it should work?
     
  4. Pretty sure Buycraft enables you to change the command in the config.yml?
     
  5. Typical... I resort to code before the config lol, thanks for the help.


    Sent from my iPhone using Tapatalk
     
    • Funny Funny x 1
  6. I would cancel the inventory open event, try onCommandPreproccess and make a check for the /buy and check for the buy crafts inventory and cancel it. Or have a onInventoryOpen event and cancel the buycrafts inventory
     
  7. Yes sorry


    Sent from my iPhone using Tapatalk
     
  8. Thread tools > Edit title > Prefix > SOLVED

    (Sorry about the bump guys)