InventoryClickEvent

Discussion in 'Spigot Plugin Development' started by Carlos2025, Jun 25, 2018.

  1. Hello, I have a problem when clicking on the inventory I think you are not reading the name of the inventory that is in the config.yml, when clicking on some items you must put on an armor but apparently it is not executing the action.
    It's probably a rookie mistake.

    Code (Text):
        @SuppressWarnings("deprecation") // no sacar los item del inv
        @EventHandler
        public void onInteract(InventoryClickEvent event){
            FileConfiguration Messages = main.getMessages();
            String titulo = ChatColor.translateAlternateColorCodes('&', Messages.getString("Messages.InvPvP"));
            String tituloo = ChatColor.stripColor(titulo);
            if(!ChatColor.stripColor(event.getInventory().getName()).equals(tituloo)) {  
           
            if(event.getCurrentItem() == null) return;
            ItemStack click = event.getCurrentItem();
            Player jugador = (Player) event.getWhoClicked();
     
  2. Register your events?

    Code (Java):
    @Override
        public void onEnable(){
    Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
       
     
  3. Yes, I'm implementing in my plugin that you can change the name of the inventory by means of the config.yml but I do not know what the error is, there are no errors in the console.
     
  4. Implementing Listener in your plugin's main class is not enough,
    you must register your event in the onEnable method.

    If you place a debug print statement (a server broadcast, for example),
    and then you click your inventory (Something that should definitely produce your debug message),
    if you get no output in your chat, then your events are not registered.

    So use my example above.

    Failing to register your events doesn't result in any errors, it just results
    in your stuff not working.
     
  5. Code (Text):
        @Override
        public void onEnable(){
            Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN+" ---------------------------------------------------");
            Bukkit.getConsoleSender().sendMessage(ChatColor.WHITE+" "+nombre+""+" Ha sido activado version: "+version+"");
            Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN+" Plugin creador por MystiCub NETWORK - Carlos2025");
            Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN+" ---------------------------------------------------");
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            Potis = new Potis(null);
            registrarComandos();
            registerConfig();
            registerKills();
            registerMessages();
            checkearMessages();
            registerEvents();
            checkeoUpdate();
            instance = this;
    }

    Code (Text):
        public void registerEvents(){
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(new Yunque(this), this);
            pm.registerEvents(new Potis(this), this);
            pm.registerEvents(new AntiCraft(this), this);
            pm.registerEvents(new DropIngots(this), this);
            pm.registerEvents(new PvP(this), this);
            pm.registerEvents(new kills(this), this);
            pm.registerEvents(new Entrar(this), this);
            pm.registerEvents(new Bloqueo(this), this);
        }
     
  6. Not sure if I understood correctly... or even anything at all. However:
    Code (Java):

    @EventHandler
        public void onInteract(InventoryClickEvent event){

            FileConfiguration Messages = main.getMessages();

            // Getting the string from your custom yml with colored codes.
            String titulo = ChatColor.translateAlternateColorCodes('&', Messages.getString("Messages.InvPvP"));

            Removing the color codes.
            String tituloo = ChatColor.stripColor(titulo);
    //       if the inventory name without any color codes is not equal to the removed color codes string from the file then continue.
            if(!ChatColor.stripColor(event.getInventory().getName()).equals(tituloo)) {  // This will most likely get every inventory apart from the one that is equal to the titulo string, you might want to fix this.
           
            if(event.getCurrentItem() == null) return; // null check.
            ItemStack click = event.getCurrentItem();
            Player jugador = (Player) event.getWhoClicked(); // getting the player that clicked the event.
     
    Your issue is on your if statement if I understood correctly. You should remove the ! since not doing so, the code would be fire for every inventory but not the one that is the same as the tituloo inventory name
     
  7. I did what he recommended but it did not work
     
  8. You don't have to register the events within the onEnable method, you can do it anywhere in the project and it will still work as long as the method that the registration is within is called/used.

    For example:

    Code (Java):
    public void onEnable() {
        Registration.registerEvents(this);
    }

    @Carlos2025
    Put your full class into a code snippet, without that we cannot see your full code and give you any more assistance than @darklazerog has already done.
     
  9. https://ghostbin.com/paste/36y3o
     
  10. https://gist.github.com/Drkmaster83...visions#diff-0b27918290ff5323bea1e3b78a9cf04e

    Your code isn't the best, and the ways that you create files are dirty and mixed. With that said, I changed some things but I don't expect my changes to fix the code.

    You really should consider a more organized design of the program, it seems somewhat directionless. We also need more (all) code if you have any hope of us helping.

    In broken Spanish (en Español roto):
    Tu codigo no es la mejor, y tus maneras de creando archivos son sucio y mezclado. Con eso, cambié algunas cosas, pero no espero mis cambios para reparar el codigo.

    En realidad, tu debes considerar un diseño mas organizado para el program. Aparece sin direccion. Y necesitamos mas (todo) codigo si tu tienes esperanza de reparando tu codigo.
     
  11. Hmm, interesting..
    I thought it had to be on the onEnable since everyone does it that way.
     
  12. The #1 rule in programming is that there is always multiple solutions to the same problem... except when there isn't, haha. More often than not there are multiple solutions.
     
    • Funny Funny x 1
    • Informative Informative x 1