Make item change based on other item click

Discussion in 'Plugin Development' started by voltywolty, Dec 8, 2021.

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

    voltywolty

    Hey all, I'm currently creating a class system. I have it where they select one item in the GUI and it sets them as that class, changes the item amount to 1 due to the point system. However, I also wanna make it work with when they click another class, the other class amount will be set back to 64 and allow them to become the other class. I thought I could get it to work just depending on whether their in a certain Set or not, but it doesn't work. Here is the code, sorry that it's a bit long, its inefficient and sloppy right now, but I'm going to fix it once I'm done.

    Code (open)

    Code:
    @EventHandler
        public void onDwarfLoadoutClick(InventoryClickEvent event) {
            Player player = (Player) event.getWhoClicked();
            ItemStack clicked = event.getCurrentItem();
    
            if (event.getView().getTitle().equals("Select a Kit - BETA") && event.getSlotType() != InventoryType.SlotType.OUTSIDE) {
                if (clicked == null || !clicked.hasItemMeta())
                    return;
    
                if (clicked.getType() == Material.BROWN_DYE) {
                    if (clicked.getItemMeta().getLore().contains("§d(Click to Un-equip Class)") || this.plugin.rangerKit.contains(player) || this.plugin.paladinKit.contains(player)) {
                        GameListener.this.plugin.warriorKit.remove(player);
    
                        event.setCurrentItem(DwarfItems.warriorKit);
                        event.getInventory().setItem(45, DwarfLoadoutItems.pointsRemaining);
    
                        player.playSound(player.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 0.5F, 0.3F);
                    }
                    else {
                        GameListener.this.plugin.warriorKit.add(player);
                        GameListener.this.plugin.rangerKit.remove(player);
                        GameListener.this.plugin.paladinKit.remove(player);
    
                        player.playSound(player.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 0.5F, 1.0F);
    
                        ItemStack item = clicked;
                        warriorUnequip(item);
    
                        ItemStack points = event.getInventory().getItem(45);
                        ItemMeta pointsMeta = points.getItemMeta();
                        points.setAmount(1);
                        points.setItemMeta(pointsMeta);
    
                        event.getInventory().setItem(45, points);
    
                        event.setCurrentItem(item);
                    }
                }
                else if (clicked.getType() == Material.STONE_SWORD) {
                    if (clicked.getItemMeta().getLore().contains("§d(Click to Un-equip Class)") || this.plugin.rangerKit.contains(player) || this.plugin.warriorKit.contains(player)) {
                        GameListener.this.plugin.paladinKit.remove(player);
    
                        event.setCurrentItem(DwarfItems.paladinKit);
                        event.getInventory().setItem(45, DwarfLoadoutItems.pointsRemaining);
    
                        player.playSound(player.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 0.5F, 0.3F);
                    }
                    else {
                        GameListener.this.plugin.warriorKit.remove(player);
                        GameListener.this.plugin.rangerKit.remove(player);
                        GameListener.this.plugin.paladinKit.add(player);
    
                        player.playSound(player.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 0.5F, 1.0F);
    
                        ItemStack item = clicked;
                        paladinUnequip(item);
    
                        ItemStack points = event.getInventory().getItem(45);
                        ItemMeta pointsMeta = points.getItemMeta();
                        points.setAmount(1);
                        points.setItemMeta(pointsMeta);
    
                        event.getInventory().setItem(45, points);
    
                        event.setCurrentItem(item);
                    }
                }
                else if (clicked.getType() == Material.BOW) {
                    GameListener.this.plugin.rangerKit.add(player);
                    GameListener.this.plugin.warriorKit.remove(player);
                    GameListener.this.plugin.paladinKit.remove(player);
                }
                else if (clicked.getType() == Material.MELON_SEEDS) {
                    player.openInventory(KitGUI.kitGUI);
                }
                event.setCancelled(true);
            }
    
            if (event.getView().getTitle().equals("Dwarf Loadout") && event.getSlotType() != InventoryType.SlotType.OUTSIDE) {
                if (clicked.getType() == Material.PUMPKIN_SEEDS) {
                    player.openInventory(KitGUI.kitSelectorGUI);
                }
                else if (clicked.getType() == Material.MELON_SEEDS) {
                    player.openInventory(KitGUI.kitMeleeGUI);
                }
                event.setCancelled(true);
            }
        }
     
    Last edited: Dec 8, 2021
  2. Offline

    Tim_M

    Just get the itemstack the player clicked on and do .setamount on it and also do it on the ones that should be set back to 64 by doing inventory.getItem(index) (something like that). You may have to call player.updateInventory
     
Thread Status:
Not open for further replies.

Share This Page