InventoryClickEvent stopped working?

Discussion in 'Plugin Development' started by Zhadowseb, May 1, 2016.

Thread Status:
Not open for further replies.
  1. Hey!

    So i created a plugin, that uses the InventoryClickEvent, and it was working perfect, but it just... stopped.

    The code looks like this:

    Code:
        @SuppressWarnings("deprecation")
        @EventHandler (priority = EventPriority.HIGH)
        public void onClick(final InventoryClickEvent e){
    
            if(!(e.getInventory() == inventoryGUI)) {return;}
    ......
    ......
    
    I tried adding the line

    Code:
    e.getWhoClicked().sendMessage("test");
    before and after that code to see if that was that line was the problem, but i found that the code wasn't even entering the onClick, please help ;(

    Btw i HAVE registered events and all that.
     
  2. Offline

    Zombie_Striker

    @Zhadowseb
    Please post the whole method and what you have tried.
    This is testing if the inventory in the event is taking up the same memory space as the other inventory. What you are most likely thinking of is .equals, as this is for testing if the values are the same.
     
  3. @Zombie_Striker

    That is not really the important part, it doesn't even get to that code, like it doesn't get past the public void onClick part and i have no idea why
     
  4. Offline

    Zombie_Striker

    @Zhadowseb
    Can you post how you register the class? Are you sure other events work in that class?
     
  5. @Zombie_Striker

    Yes, i also use PlayerInteractEvent, in the class and it is working fine, the inventory opens like it's supposed to ;)
     
  6. Offline

    Zombie_Striker

    Are you sure there are no other plugins that cancel this event? Right now, this event most likely is triggered after every single plugin. If one of them cancel the event, then this method will not be read.
     
  7. Offline

    Firestar311

    @Zhadowseb Try putting debug statements throughout the method and see exactly where it stops. It is most likely the if statement provided however, if you could post the whole class, we can better see how you are doing things.
     
  8. @Jayfeather311 Well i can tell you the problem isn't the if statement. The very first line in the method i put the line of code: e.getwhoclicked().sendmessage("Test");
    And nothing happend, so the code simply isn't starting as weird as it sounds
     
  9. Offline

    Firestar311

    As I said, you should provide all of your code so that we can see what you are doing.
     
  10. @Jayfeather311 Here it is ;)

    Btw it might not be very effecient, this was my first plugin ;)

    Code:
    package me.Zhadowseb.burrowmenu;
    
    import java.io.File;
    import java.util.ArrayList;
    import java.util.Set;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.DyeColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.block.Sign;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.block.SignChangeEvent;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
    
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
    import net.minecraft.server.v1_8_R3.EntityPlayer;
    
    public class BurrowGUI extends JavaPlugin implements Listener
    {
        public static Economy econ = null;
    
        public WorldGuardPlugin getWorldGuard()
        {
            Plugin plugin = getServer().getPluginManager().getPlugin("WorldGuard");
            if ((plugin == null) || (!(plugin instanceof WorldGuardPlugin)))
            {
                return null; //throws a NullPointerException, telling the Admin that WG is not loaded.
            }
            return (WorldGuardPlugin)plugin;
        }
    
        public static boolean isInt(String s) {
            try {
                Integer.parseInt(s);
            } catch (NumberFormatException nfe) {
                return false;
            }
            return true;
        }
    
        public void loadConfiguration(){
            getConfig().addDefault("NumberOfSigns", 0);
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
    
        @Override
        public void onEnable()
        {
            getWorldGuard();
            loadConfiguration();
            Bukkit.getServer().getLogger().info("[BurrowGUI] version " + this.getDescription().getVersion() + " has been Enabled!");
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
    
            if (!setupEconomy() ) {
                getLogger().severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
                getServer().getPluginManager().disablePlugin(this);
                return;
            }
        }
    
        private boolean setupEconomy() {
            if (getServer().getPluginManager().getPlugin("Vault") == null) {
                return false;
            }
            RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
            if (rsp == null) {
                return false;
            }
            econ = rsp.getProvider();
            return econ != null;
        }
    
        @SuppressWarnings({ "rawtypes", "unchecked" })
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
            Player player = (Player) sender;
          
            if(player.hasPermission("burrowgui.create"))
            {
                if(label.equalsIgnoreCase("bg") || label.equalsIgnoreCase("burrowgui"))
                {
                    if(args.length == 0){
                        player.sendMessage(ChatColor.RED + "Please do either put, delete or edit while looking at the sign!");
                        return true;
                    }
    
                    Location targetBlockOne = player.getTargetBlock((Set) null, 2).getLocation().clone();
                    Sign signOne = (Sign) targetBlockOne.getBlock().getState();
                    int signX = signOne.getLocation().getBlockX();
                    int signY = signOne.getLocation().getBlockY();
                    int signZ = signOne.getLocation().getBlockZ();
    
                    if(args[0].equalsIgnoreCase("delete") && args.length == 1){
                        if(targetBlockOne.getBlock().getType() == Material.SIGN || targetBlockOne.getBlock().getType() == Material.SIGN_POST || targetBlockOne.getBlock().getType() == Material.WALL_SIGN){
                            if(signOne.getLine(1).equalsIgnoreCase(ChatColor.DARK_GREEN + "Burrow menu")){
    
                                int oneTwo = 1;
                                while(oneTwo <= getConfig().getInt("NumberOfSigns")){
                                    int guiSignX = this.getConfig().getConfigurationSection("Signs." + oneTwo).getInt("X");
                                    int guiSignY = this.getConfig().getConfigurationSection("Signs." + oneTwo).getInt("Y");
                                    int guiSignZ = this.getConfig().getConfigurationSection("Signs." + oneTwo).getInt("Z");
                                    if(signX == guiSignX && signY == guiSignY && signZ == guiSignZ){
                                        getConfig().set("Signs." + oneTwo, null);
                                        player.sendMessage(ChatColor.GREEN + "Sign removed from the config!");
                                        oneTwo--;
                                        this.getConfig().set("NumberOfSigns", oneTwo);
                                        return true;
                                    }
                                }
                            } else player.sendMessage(ChatColor.RED + "That is not a burrowGUI sign!");
                        } else player.sendMessage(ChatColor.RED + "That is not a sign!");
                    }
                    if(args[0].equalsIgnoreCase("put") && args.length == 1)
                    {
                        Location targetBlock = player.getTargetBlock((Set) null, 2).getLocation().clone();
                        if(targetBlock.getBlock().getType() == Material.SIGN || targetBlock.getBlock().getType() == Material.SIGN_POST || targetBlock.getBlock().getType() == Material.WALL_SIGN)
                        {
                            Sign sign = (Sign) targetBlock.getBlock().getState();
                            if(sign.getLine(1).equalsIgnoreCase(ChatColor.DARK_GREEN + "Burrow menu"))
                            {
                                int numberOfSigns = this.getConfig().getInt("NumberOfSigns");
                                numberOfSigns++;
                                this.getConfig().set("NumberOfSigns", numberOfSigns);
                                this.saveConfig();
                                this.getConfig().set("Signs." + this.getConfig().getInt("NumberOfSigns") + ".X", targetBlock.getBlockX());
                                this.getConfig().set("Signs." + this.getConfig().getInt("NumberOfSigns") + ".Y", targetBlock.getBlockY());
                                this.getConfig().set("Signs." + this.getConfig().getInt("NumberOfSigns") + ".Z", targetBlock.getBlockZ());
                                this.getConfig().set("Signs." + this.getConfig().getInt("NumberOfSigns") + ".Region", "<Name of region that access is granted too>");
                                this.getConfig().set("Signs." + this.getConfig().getInt("NumberOfSigns") + ".Price", "<Cost to burrow region>");
                                this.getConfig().set("Signs." + this.getConfig().getInt("NumberOfSigns") + ".Time", "<Time the region is burrowed>");
                                this.saveConfig();
                                player.sendMessage(ChatColor.GREEN + "Sign saved to the config! Go edit the config, to finish the sign!");
                            } else player.sendMessage(ChatColor.RED + "That is not a Burrow GUI sign!");
                        } else player.sendMessage(ChatColor.RED + "That has to be a sign!");
                        return true;
                    }
                }
            }
            return true;
        }
    
        @EventHandler
        public void onSignChange(SignChangeEvent e){
            Player p = e.getPlayer();
            if(p.hasPermission("burrowgui.create")){
                if(e.getLine(0).equalsIgnoreCase("[burrowgui]")){
                    e.setLine(0, "");
                    e.setLine(1, ChatColor.DARK_GREEN + "Burrow menu");
                    e.setLine(2, ChatColor.GOLD + "Click to use!");
                }
            }
        }
    
        File plugins = this.getDataFolder().getParentFile();
        File folder = new File(plugins.getPath() + File.separator + "WorldGuard" + File.separator + "worlds" + File.separator + "ServerMaP" + File.separator + "regions.yml");
        FileConfiguration regionYml = YamlConfiguration.loadConfiguration(folder);
    
        Inventory inventoryGUI = Bukkit.createInventory(null, 45, ChatColor.BOLD + "" + ChatColor.GOLD + "Pyramid" + ChatColor.DARK_RED + "PvP " + ChatColor.GREEN + "burrow menu");
    
      
        @SuppressWarnings("deprecation")
        @EventHandler (priority = EventPriority.HIGH)
        public void onPlayerInteract(PlayerInteractEvent e)
        {  
            if(!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
    
            Block b = e.getClickedBlock();
            Material btype = e.getClickedBlock().getType();
            if(btype == null){
              
            }
            Player p = e.getPlayer();
            Action a = e.getAction();
            if(a == Action.RIGHT_CLICK_BLOCK){
                if(btype == Material.SIGN || btype == Material.SIGN_POST || btype == Material.WALL_SIGN){
                    Sign sign = (Sign) b.getState();
                    if(sign.getLine(1).equalsIgnoreCase(ChatColor.DARK_GREEN + "Burrow menu") && sign.getLine(2).equalsIgnoreCase(ChatColor.GOLD + "Click to use!")){
                        int signX = b.getLocation().getBlockX();
                        int signY = b.getLocation().getBlockY();
                        int signZ = b.getLocation().getBlockZ();
                        int one = 1;
                        while(one <= getConfig().getInt("NumberOfSigns")){
                            int guiSignX = getConfig().getConfigurationSection("Signs." + one).getInt("X");
                            int guiSignY = getConfig().getConfigurationSection("Signs." + one).getInt("Y");
                            int guiSignZ = getConfig().getConfigurationSection("Signs." + one).getInt("Z");
                            if(signX == guiSignX && signY == guiSignY && signZ == guiSignZ){
    
                                int priceInt = 0;
                                if(isInt(getConfig().getConfigurationSection("Signs." + one).getString("Price"))){
                                    priceInt = getConfig().getConfigurationSection("Signs." + one).getInt("Price");
                                }
                                else if (!(isInt(getConfig().getConfigurationSection("Signs." + one).getString("Price")))){
                                    priceInt = 0;
                                }
                              
                                int timeInt = 0;
                                if(isInt(getConfig().getConfigurationSection("Signs." + one).getString("Time"))){
                                    timeInt = getConfig().getConfigurationSection("Signs." + one).getInt("Time");
                                }
                                else if (!(isInt(getConfig().getConfigurationSection("Signs." + one).getString("Time")))){
                                    timeInt = 0;
                                }
    
                                String regionString = getConfig().getConfigurationSection("Signs." + one).getString("Region");
                                if(regionYml.getConfigurationSection("regions").contains(regionString)){
    
                                    ItemStack region = new ItemStack(Material.EMPTY_MAP, 1);
                                    ItemMeta regionMeta = region.getItemMeta();
                                    regionMeta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Region");
                                    ArrayList<String> regionLore = new ArrayList<String>();
                                    regionLore.add(ChatColor.DARK_PURPLE + "This sign will allow");
                                    regionLore.add(ChatColor.DARK_PURPLE + "Access to the region: ");
                                    regionLore.add(ChatColor.GOLD + regionString + "");
                                    regionMeta.setLore(regionLore);
                                    region.setItemMeta(regionMeta);
    
                                    ItemStack price = new ItemStack(Material.GOLD_NUGGET, 1);
                                    ItemMeta priceMeta = price.getItemMeta();
                                    priceMeta.setDisplayName(ChatColor.GOLD + "" + ChatColor.BOLD + "Price");
                                    ArrayList<String> priceLore = new ArrayList<String>();
                                    priceLore.add(ChatColor.DARK_PURPLE + "The price of getting");
                                    priceLore.add(ChatColor.DARK_PURPLE + "Access to the region is: ");
                                    priceLore.add(ChatColor.GOLD + "" + priceInt);
                                    priceMeta.setLore(priceLore);
                                    price.setItemMeta(priceMeta);
    
                                    ItemStack time = new ItemStack(Material.WATCH, 1);
                                    ItemMeta timeMeta = time.getItemMeta();
                                    timeMeta.setDisplayName(ChatColor.WHITE + "" + ChatColor.BOLD + "Time");
                                    ArrayList<String> timeLore = new ArrayList<String>();
                                    timeLore.add(ChatColor.DARK_PURPLE + "The amount of time(min) you have");
                                    timeLore.add(ChatColor.DARK_PURPLE + "Access to the region is: ");
                                    timeLore.add(ChatColor.GOLD + "" + timeInt);
                                    timeMeta.setLore(timeLore);
                                    time.setItemMeta(timeMeta);
    
                                    ItemStack accept = new ItemStack(Material.WOOL, 1, DyeColor.GREEN.getData());
                                    ItemMeta acceptMeta = accept.getItemMeta();
                                    acceptMeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + "Accept");
                                    ArrayList<String> acceptLore = new ArrayList<String>();
                                    acceptLore.add(ChatColor.DARK_PURPLE + "If you click this, you ACCEPT the trade!");
                                    acceptMeta.setLore(acceptLore);
                                    accept.setItemMeta(acceptMeta);
    
                                    ItemStack decline = new ItemStack(Material.WOOL, 1, DyeColor.RED.getData());
                                    ItemMeta declineMeta = decline.getItemMeta();
                                    declineMeta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Decline");
                                    ArrayList<String> declineLore = new ArrayList<String>();
                                    declineLore.add(ChatColor.DARK_PURPLE + "If you click this, you DECLINE the trade!");
                                    declineMeta.setLore(declineLore);
                                    decline.setItemMeta(declineMeta);
    
                                    inventoryGUI.setItem(10, region);
                                    inventoryGUI.setItem(13, price);
                                    inventoryGUI.setItem(16, time);
                                    inventoryGUI.setItem(30, accept);
                                    inventoryGUI.setItem(32, decline);
                                    p.openInventory(inventoryGUI);
    
                                } else p.sendMessage(ChatColor.BLACK + "[" + ChatColor.GOLD + "Pyramid" + ChatColor.DARK_RED + "Broadast" + ChatColor.BLACK + "]" + ChatColor.RED + " This sign is out of order!");
                            }
                            one++;
                        }
                    }
                }
            }
        }
      
        @SuppressWarnings("deprecation")
        @EventHandler (priority = EventPriority.HIGHEST)
        public void onClick(final InventoryClickEvent e){
    
            if(!(e.getInventory() == inventoryGUI)) {return;}
          
            if(!(e.getSlot() == 30 || e.getSlot() == 32)){
                e.setCancelled(true);
                return;
            }
            if(e.getSlot() == 32){
                e.setCancelled(true);
                e.getWhoClicked().sendMessage(ChatColor.BLACK + "[" + ChatColor.GOLD + "Pyramid" + ChatColor.DARK_RED + "Sign" + ChatColor.BLACK + "]" + ChatColor.RED + " You have declined the sign trade!");
                e.getWhoClicked().closeInventory();
                return;
            }
          
            String costwet = ChatColor.stripColor(e.getInventory().getItem(13).getItemMeta().getLore().get(2));
            final String region = ChatColor.stripColor(e.getInventory().getItem(10).getItemMeta().getLore().get(2));
            String timeString = ChatColor.stripColor(e.getInventory().getItem(16).getItemMeta().getLore().get(2));
            int time = Integer.parseInt(timeString);
            Double cost = (double) Integer.parseInt(costwet);
          
            if(e.getSlot() == 30){
                EconomyResponse r = econ.withdrawPlayer(e.getWhoClicked().getName(), cost);
                if(r.transactionSuccess()){
                    final Location loc = e.getWhoClicked().getLocation();
                    e.setCancelled(true);
                    e.getWhoClicked().sendMessage(ChatColor.BLACK + "[" + ChatColor.GOLD + "Pyramid" + ChatColor.DARK_RED + "Sign" + ChatColor.BLACK + "] " + ChatColor.RED + cost + ChatColor.GOLD + " Was drawn from your balance!");
                    e.getWhoClicked().sendMessage(ChatColor.BLACK + "[" + ChatColor.GOLD + "Pyramid" + ChatColor.DARK_RED + "Sign" + ChatColor.BLACK + "]" + ChatColor.GREEN + " You accepted the sign trade, and may now procced to enter the region!");
                    getWorldGuard().getRegionManager(e.getWhoClicked().getWorld()).getRegion(region).getMembers().addPlayer(e.getWhoClicked().getName());
                    e.getWhoClicked().closeInventory();
                    Bukkit.getServer().getScheduler().scheduleAsyncDelayedTask(this, new Runnable(){
    
                        @Override
                        public void run() {
                            e.getWhoClicked().teleport(loc);
                            getWorldGuard().getRegionManager(e.getWhoClicked().getWorld()).getRegion(region).getMembers().removePlayer(e.getWhoClicked().getName());
                            e.getWhoClicked().sendMessage(ChatColor.BLACK + "[" + ChatColor.GOLD + "Pyramid" + ChatColor.DARK_RED + "Sign" + ChatColor.BLACK + "]" + ChatColor.RED + " You ran out of access time to the region: " + ChatColor.GOLD + region + "!");
                        }
    
                    }, time * 20 * 60);  
                } else e.getWhoClicked().sendMessage(ChatColor.BLACK + "[" + ChatColor.GOLD + "Pyramid" + ChatColor.DARK_RED + "Sign" + ChatColor.BLACK + "]" + ChatColor.RED + " Something went wrong, you might not have the money you need!");
            }
        }
    }
    
     
  11. Offline

    Firestar311

    Change this line
    Code:java
    1. if(!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;

    To this
    Code:java
    1. if(!(e.getAction().equals(Action.RIGHT_CLICK_BLOCK))) return;


    EDIT: Then change everything that has a == to .equals().
     
  12. Offline

    I Al Istannen

    @Jayfeather311
    For enums it is pretty much agreed to use == (afaik). It works the same as equals, but is null safe and easier to read. If you write "plugin.equals(null)" it will just blow up with a NPE if plugin is really null.

    You need to understand what each operator does and use them accordingly. This is the first link I found, but googleing "java == and equals" will show many more results.


    @Zhadowseb
    To follow up what Zombie_Striker said try replacing the @EventHandler with this:
    "@EventHandler(priority=EventPriority.HIGH, ignoreCancelled = false)". This tells it to also listen to cancelled events, so you can see if that is the problem. Also, why does it need to be high?
     
  13. @I Al Istannen

    This didn't work ;(

    I set it to high because i tried everything else, and wanted to see if that would work, but it didn't :(

    If you have any other ideas please let me know, i really have no idea as to why it isn't working
     
  14. This is still a really big problem, i have since, tried a few different things, and i still have no idea why this isn't working please help :)
     
Thread Status:
Not open for further replies.

Share This Page