GUI Doesn't Work with multiple people!

Discussion in 'Plugin Development' started by Ivye, Feb 13, 2021.

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

    Ivye

    I am trying to create a gui that will allow people on my server to enchant their pickaxes, and the GUI seems to work when only one person uses it but as soon as a second person opens the gui the first person can just move all the items around as if it were a normal inventory!

    This is the code for initializing the GUI

    Code:
        private UUID uuid;
        private HashMap<UUID, Inventory> actions;
       
        public void Gui(Player pOpen ) {
           
            gridprice = 2500;
            crossprice = 5000;
           
            UUID puuid = pOpen.getUniqueId();
            HashMap<UUID, Inventory> actions = new HashMap<UUID, Inventory>();
            actions.put(puuid,Bukkit.createInventory(null, 54, "Upgrade Pickaxe"));
    
            initializeItems(pOpen);
           
        }
    And this is the code to check what people are clicking.

    Code:
    @EventHandler
        public void onInventoryClick(final InventoryClickEvent e) {
            if (!actions.containsKey(e.getWhoClicked().getUniqueId())) return;
    
            e.setCancelled(true);
    
            final ItemStack clickedItem = e.getCurrentItem();
    
            if (clickedItem == null || clickedItem.getType() == Material.AIR) return;
    
            final Player p = (Player) e.getWhoClicked();
    
            int sl = e.getRawSlot();
           
                switch(sl) {
                case 12:
                        int gridlvl = p.getInventory().getItemInMainHand().getItemMeta().getEnchantLevel(CustomEnchants.GRID);
                        String pm = String.valueOf(gridlvl);
                        p.sendMessage(pm);
                        double havr = newshard.getShards(p);
                        int havrn = (int)havr;
                        gridprice = 2500;
                        for (int i = 0; i < gridlvl;i++) {
                            double gridpriced = (gridprice * 1.20);
                            gridprice = (int)gridpriced;
                        }
                        if (gridlvl > 9) {
                            gridprice = 0;
                        }
                        String gridpricestring = String.valueOf(gridprice);
                        p.sendMessage(gridpricestring);
                        if (havrn >= gridprice) {
                            if (gridlvl < 10) {
                                p.closeInventory();
                                newshard.setShards(p, havr - (double)gridprice);
                                p.getInventory().getItemInMainHand().addUnsafeEnchantment(CustomEnchants.GRID, (gridlvl + 1));
                            }else{
                                p.closeInventory();
                                p.sendMessage("§cThis enchantment's level is currently at max.");
                            }
                        }else {
                            p.closeInventory();
                            p.sendMessage("§cYou do not have enough §bRime.");
                        }
                    break;
                case 13:
                    break;
                case 14:
                    break;
                case 21:
                    break;
                case 22:
                    break;
                case 23:
                    break;
                case 30:
                    break;
                case 31:
                    break;
                case 32:
                    break;
                case 39:
                    break;
                case 40:
                    break;
                case 41:
                    break;
                default:
                    e.setCancelled(true);
                    break;
            }
        }
     
  2. Offline

    Strahan

    You're wiping out the contents of the Map every time a player opens the GUI.

    Also per SOLID design it should be Map<UUID, Inventory> actions not HashMap (a class should be directly substitutable with its base-class). Also item meta is tagged nullable, so you really shouldn't hang methods off it until you check that first. You really should not be embedding the color character either, use ChatColor. That's why it is there.

    Lastly, checking slots is not wise either. If you one day decide to restructure your GUI, you're hosed and gotta recompile the plugin. Better to check what they are clicking to determine the functionality. I tag ItemStacks being added to the inv and read control codes using the PersistentDataContainer when clicked so I know what they want.

    PS - speaking of SOLID, you really shouldn't have that calculation and enchantment stuff in your click handler anyway. Read up on SRP and abstraction.
     
  3. Offline

    Ivye

    so I changed to using a map and stopped setting it to a new one at the gui creation but now it results in an internal server error when i try to open the gui.
     
  4. Offline

    Strahan

    We're not psychic. Post your updated code and post the error's stacktrace.
     
Thread Status:
Not open for further replies.

Share This Page