InventoryClickEvent bug

Discussion in 'Plugin Development' started by Keokix, Aug 19, 2019.

Thread Status:
Not open for further replies.
  1. I got the most weird bug ive ever seen...me and 2 other devs couldnt fix it so i hope some of u can do it.
    I made a plugin "coinflip" where u crate a challenge with /cf c number and then u choose a woolcolor
    After that u can see all challenges in /cf o where they r listed with wool....the bug now is, if u click on the wool, another inv opens to confirm u wanna accept the challenge BUT the inventory clickevent is not working anymore in there...BUT if u click not on a challenge and on an empty slot beside (i made it to test it) THEN the clickevent IS working even if the confirm inv is opened with the exactly SAME line in code and the SAME inv is opened...but once the clickevent works and once not...

    this is for the inventories

    Code:
    package Lite.Core.Listener;
    
    import Lite.Core.Commands.MavacsStupidCoinflip;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    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 java.util.ArrayList;
    
    import static Lite.Core.Commands.MavacsStupidCoinflip.p2;
    
    //Author: Keokix_
    
    
    //            settings.getCfChallenges().set("Coinflip.lenght",lenght);
    //            String length2 = Integer.toString(lenght);
    //            settings.getCfChallenges().set("Coinflip." + length2 + ".player" ,MavacsStupidCoinflip.p2.getName());
    //            settings.getCfChallenges().set("Coinflip." + length2 + ".money" , MavacsStupidCoinflip.money);
    //            settings.getCfChallenges().set("Coinflip." + length2 + ".point" , colorid.get(lenght));
    //            settings.getCfChallenges().set("Coinflip." + length2 + ".color" , e.getSlot());
    //            lenght++;
    //            settings.saveCfChallenges();
    
    public class CfMenue implements Listener {
        // 1 = white, 2 = orange, 3 = magenta, 4 = light blue, 5 = yellow, 6 = lime, 7 = pink, 8  = gray, 9 = light gray, 10 = cyan, 11 = purple, 12 = blue, 13 = brown, 14 = green, 15 = red, 16 = black
        public static int color = 1;
        public static int y = 0;
        public static int number = 0;
        public static int lenght = 0;
        public static ArrayList<Short> colorid = new ArrayList<>();
        public static ArrayList<String> Playername = new ArrayList<>();
        public static ArrayList<String> Money = new ArrayList<>();
        public static int a = 2;
        public static int b = 3;
        //   static SettingsManager settings = SettingsManager.getInstance();
    
        //   public List<String> getChallenges(String s) {
        //      return settings.getCfChallenges().getStringList(s.toLowerCase()+".cffile");
        //   }
    
        @EventHandler
        public static void onClick(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked();
            if (e.getInventory().getTitle().equals("!Choose your color!")) {
                if(e.isRightClick() || e.isLeftClick()){
                    e.setCancelled(true);
                }
                colorid.add((short) e.getSlot());
                p.closeInventory();
                p.sendMessage(ChatColor.GREEN + "You created a challenge about " +  MavacsStupidCoinflip.money + "$!");
                sort();
            }
    
            if(e.getInventory().getTitle().equals("!Coinflip Challenges!")) {
                if(e.isRightClick() || e.isLeftClick()){
                    e.setCancelled(true);
                }
                if(e.getSlot() <= 27) {
                    p2.openInventory(confirm());
                }
    
            }
            if(e.getInventory().getTitle().equals("!Confirm your choice!")) {
                System.out.println(p2.getInventory().getName() + " test");
                if(e.isRightClick() || e.isLeftClick()){
                    e.setCancelled(true);
                }
                if(e.getSlot() == 11){
                    if(e.isRightClick() || e.isLeftClick()){
                        e.setCancelled(true);
                        p.sendMessage("test done");
                    }
                }
                else if(e.getSlot() == 15){
                    if(e.isRightClick() || e.isLeftClick()){
                        e.setCancelled(true);
                        p.closeInventory();
                        p.sendMessage(ChatColor.RED + "Accepting got cancelled");
                    }
                }
            }
        }
    
        public static Inventory confirm(){
            Inventory confirm = Bukkit.createInventory(null, 27, "!Confirm your choice!");
            p2.sendMessage(ChatColor.AQUA + "Confirm your choice please!");
            ItemStack stack2 = new ItemStack(Material.getMaterial(35), 1);
            ItemMeta meta = stack2.getItemMeta();
            ArrayList<String> lore = new ArrayList<String>();
            lore.add(ChatColor.GREEN + "Accept");
            meta.setLore(lore);
            stack2.setItemMeta(meta);
            stack2.setDurability((short) 5);
            confirm.setItem(11, stack2);
            lore.remove(ChatColor.GREEN + "Accept");
            lore.add(ChatColor.RED + "Deny");
            meta.setLore(lore);
            stack2.setItemMeta(meta);
            stack2.setDurability((short) 14);
            confirm.setItem(15,stack2);
            return confirm;
        }
    
        public static void sort() {
            if (colorid.size() == 1) {
                Playername.add(p2.getName());
                Money.add(String.valueOf(MavacsStupidCoinflip.money));
            }
            else if (colorid.size() > 1) {
                String temp = Playername.get(Playername.size()-1);
                String temp2 = Money.get(Money.size()-1);
                Money.add(temp2);
                Playername.add(temp);
                for (int i = 0; i < colorid.size()-2; i++) {
                    Money.add(Money.size()-a, Money.get(Money.size()-b));
                    Playername.add(Playername.size()-a, Playername.get(Playername.size()-b));
                    a++;
                    b++;
                }
                Playername.add(0,p2.getName());
                Money.add(0,String.valueOf(MavacsStupidCoinflip.money));
            }
        }
        public static Inventory accepted(){
            Inventory accepted = Bukkit.createInventory(null,18, "!Coinflip Challenge Result!");
    
            return accepted;
        }
        public static Inventory createwool(){
            Inventory woolinv = Bukkit.createInventory(null,18, "!Choose your color!");
            p2.sendMessage(ChatColor.AQUA + "Please choose a color for your challenge!");
            woolinv.setItem(0, new ItemStack(35,1));
            for(int i = 0; i < 15; i++) {
                ItemStack itemstack = new ItemStack(Material.getMaterial(35), 1);
                itemstack.setDurability((short) color);
                woolinv.setItem(color, itemstack);
                color++;
            }
            return woolinv;
        }
    
    
        public static Inventory inv(){
            int z = colorid.size();
            Inventory inv = Bukkit.createInventory(null, 27, "!Coinflip Challenges!");
            y = 0;
            for(int x = z; x > 0 ; x--) {
                ItemStack itemstack = new ItemStack(Material.getMaterial(35), 1);
                itemstack.setDurability( colorid.get(x-1));
                ItemMeta meta = itemstack.getItemMeta();
                ArrayList<String> lore = new ArrayList<String>();
                int number2 = number+1;
                lore.add("Challenge Number: " + number2);
                lore.add("Creator: " + Playername.get(number));
                lore.add("Money: " + Money.get(number));
                meta.setLore(lore);
                itemstack.setItemMeta(meta);
                itemstack.getItemMeta().setDisplayName("Challenge" + number2);
                inv.setItem(y, itemstack);
                number++;
                y++;
            }
    
            return inv;
        }
    
    
    and this is the class for commands etc


    Code:
    //Author : Keokix_
    
    package Lite.Core.Commands;
    
    import Lite.Core.API.EconomyAPI;
    import Lite.Core.Listener.CfMenue;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class MavacsStupidCoinflip implements CommandExecutor {
    
        public static int money;
        public static Player p2;
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;
            p2 = p;
            if (label.equalsIgnoreCase("coinflip") || label.equalsIgnoreCase("cf")) {
    
                if(args.length == 0){
                    sender.sendMessage(ChatColor.GOLD + "-------------------------" + ChatColor.GREEN + "Coinflip" + ChatColor.GOLD + "---------------------");
                    sender.sendMessage(ChatColor.GREEN + "Coinflip is a game between 2 players.");
                    sender.sendMessage(ChatColor.GREEN + "Do /coinflip create (amount) or /cf c (amount) to start a challenge.");
                    sender.sendMessage(ChatColor.GREEN + "And /coinflip open or /cf o , to see all open challenges ");
                    sender.sendMessage(ChatColor.GREEN + "After that, you will can choose a color of your choice.");
                    sender.sendMessage(ChatColor.GREEN + "If another player accepts your challenge, it will show up for you");
                    sender.sendMessage(ChatColor.GREEN + "and the coinflip starts wiht 50/50 chance for both of you!");
                    sender.sendMessage(ChatColor.GREEN + "Both players used the same bet for the game.");
                    sender.sendMessage(ChatColor.GREEN + "After a few seconds, one of you both win all of the money,");
                    sender.sendMessage(ChatColor.GREEN + "while the other one lose everything!");
                    sender.sendMessage(ChatColor.GREEN + "Be careful, high risk, high reward!");
                    sender.sendMessage(ChatColor.GOLD + "-----------------------------------------------------------");
                    return true;
                }
                if(args.length == 1){
    
                    if(args[0].equalsIgnoreCase("create") || args[0].equalsIgnoreCase("c")){
                        sender.sendMessage(ChatColor.RED + "You need to add an amount you want to bet!");
                    }
                    else if(args[0].equalsIgnoreCase("open") || args[0].equalsIgnoreCase("o")){
                        CfMenue.number = 0;
                        p.openInventory(CfMenue.inv());
                    }
                    else{
                        sender.sendMessage(ChatColor.RED + "This is not a valid command!");
                    }
                    return true;
                }
                else if(args.length == 2) {
                    if (args[0].equalsIgnoreCase("create") || args[0].equalsIgnoreCase("c")) {
                        if (CfMenue.colorid.size() < 9) {
                            try{
                                int amount = Integer.parseInt(args[1]);  //money from challenge
                                money = amount;
                                if (EconomyAPI.balance(p) >= amount) {
                                    p.openInventory(CfMenue.createwool());
                                    CfMenue.color = 1;
                                    CfMenue.number = 0;
                                }
                                else {
                                    MavacsStupidCoinflip.p2.sendMessage(ChatColor.RED + "You dont have enough money for that!");
                                }
                            }
                            catch(NumberFormatException ex){
    
                            }
                            return true;
                        }
                        else{
                            sender.sendMessage(ChatColor.RED + "There cant be more then 9 challenges at once!");
                            sender.sendMessage(ChatColor.RED + "Please join one, or wait for someone else to join!");
                        }
                    }
                    else{
                        sender.sendMessage(ChatColor.RED + "This is not a valid command");
                    }
                }
                return true;
            }
            return true;
        }
    
    }
    
     
  2. Offline

    KarimAKL

    @Keokix Holy, that's alot of static. Try removing 'static' from onClick(InventoryClickEvent e). (Your IDE will probably tell you to make some other stuff non-static as well)
     
  3. thats not the problem, already tried that :/
     
  4. Offline

    KarimAKL

    @Keokix In that case, have you tried opening the confirmation inventory 1-3 ticks later?

    Btw, while we're on the subject, may i ask why everything is static?
     
  5. because i made what ide told me xD i have no idea what needs to be static or not (im a beginner and as long as it works idc xD) and no i dont tried that but then why it works when i open the confirm inv by clicking on an empty slot but not if i open it by clicking on wool? it both opens the inv, but once that part is getting skipped ( see following code) when i open the inv by clicking on wool and if i click on empty slot its not getting ignored EVEN if its exactly the SAME inv that is getting opened

    Code:
            if(e.getInventory().getTitle().equals("!Confirm your choice!")) {
                System.out.println(p2.getInventory().getName() + " test");
                if(e.isRightClick() || e.isLeftClick()){
                    e.setCancelled(true);
                }
                if(e.getSlot() == 11){
                    if(e.isRightClick() || e.isLeftClick()){
                        e.setCancelled(true);
                        p.sendMessage("test done");
                    }
                }
                else if(e.getSlot() == 15){
                    if(e.isRightClick() || e.isLeftClick()){
                        e.setCancelled(true);
                        p.closeInventory();
                        p.sendMessage(ChatColor.RED + "Accepting got cancelled");
                    }
                }
            }
     
  6. Offline

    KarimAKL

    @Keokix Can you send an image of the inventory? I don't really see anything wrong with the code.
     
  7. @KarimAKL
    yeah...thats the problem...there is nothing wrong with the code -.- thats why this bug is so unbeliveable weird...
    First screen is the inv with challenges and last both r first screen after clicking on wool (and if u can see, i could take red wool out and move it) and then on emtpy slot(where i cant move anything and the inventory clickevent works) ...so u see invs r exactly the same BUT by clicking on wool nothing works by clicking on emtpy slot the inv works and i can accept or deny the challenge by clicking on red or green...
    http://prntscr.com/ov1927
    http://prntscr.com/ov19md
    http://prntscr.com/ov1aa8
     
    Last edited: Aug 20, 2019
  8. Offline

    KarimAKL

    @Keokix I don't see the "System.out.println(p2.getInventory().getName() + " test");" message in the chat, are you sure the code is getting through that if statement?
     
  9. yeah, thats just not in the chat because i already removed it. .. but since the bug is the same it doesnt matter. its rly exactly just that the last if statement with if(e. getplayer. getinv. gettitle. equals(! confirm your choice! ) is getting once ignored and once not. that is already obviously becaude once its getting used and knce not but around it, i could print debug out. its rly just thst the if statekent is for some reasons getting ignored if i open the inv by clicking on wool instead of and empty slot
     
Thread Status:
Not open for further replies.

Share This Page