Custom Pickup for weapons | CrackShot

Discussion in 'Plugin Development' started by MegasusBMS, Dec 2, 2022.

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

    MegasusBMS

    >I'm working with CrackShot. I want you not to be able to have the same weapon in the hotbar and if you already have the same type of weapon in the hot bar, put it in inv on any slot except the ones from the hot bar.
    >So I started a custom pickup just for weapons, and when try to pick guns up sometimes some artefacts appear, like a invisible weapon in hand, which after a while appears as an extra weapon.

    Code:
        @EventHandler(priority = EventPriority.HIGH)
        public void onItemPickUp(PlayerPickupItemEvent e) {
          
            if(!isWeapon(e.getItem()))
                return;
          
            e.setCancelled(true);
          
            //Costum Pickup
          
            Player p = e.getPlayer();
          
            CSUtility cs = new CSUtility();
            String newWeapon = getWeaponTitle(e.getItem());
          
            Inventory inv = p.getInventory();
            List<String>weaponInToolBar = new ArrayList<String>();
          
            for(int i=0; i<9;i++) {
                ItemStack item = inv.getItem(i);
                if(isWeapon(item)) {
                    weaponInToolBar.add(cs.getWeaponTitle(item));
                  
                }
            }
          
            if(!weaponInToolBar.contains(newWeapon)) {
                e.getItem().remove();
                p.getInventory().addItem(e.getItem().getItemStack());
                p.updateInventory();
                return;
            }
        }
    After some documentation i did this. But the items sometime still dublicates.

    Code:
    
        List<UUID> listOfPlayersThatArePickingUpGuns = new ArrayList<>();
      
        @SuppressWarnings("deprecation")
        @EventHandler(priority = EventPriority.HIGH)
        public void onItemPickUp(PlayerPickupItemEvent e) {
          
            if(!isWeapon(e.getItem()))
                return;
          
            e.setCancelled(true);
          
            //Costum Pickup
          
            Player p = e.getPlayer();
          
            CSUtility cs = new CSUtility();
            String newWeapon = getWeaponTitle(e.getItem());
          
            Inventory inv = p.getInventory();
            List<String>weaponsInToolBar = new ArrayList<String>();
          
            for(int i=0; i<9;i++) {
                ItemStack item = inv.getItem(i);
                if(isWeapon(item)) {
                    weaponsInToolBar.add(cs.getWeaponTitle(item));
                  
                }
            }
          
            if(!weaponsInToolBar.contains(newWeapon) && !listOfPlayersThatArePickingUpGuns.contains(p.getUniqueId())) {
              
                e.getItem().remove();
                listOfPlayersThatArePickingUpGuns.add(p.getUniqueId());
              
                Bukkit.getServer().getScheduler().scheduleAsyncDelayedTask(Main.plugin, new Runnable() {
                    public void run() {
                      
                        p.getInventory().addItem(e.getItem().getItemStack());
                        listOfPlayersThatArePickingUpGuns.remove(p.getUniqueId());
                      
                    }
                    }, 10L);
              
                return;
              
            }
        }
      
    Also there is a way to pickup items to a specify slot in inventory ?

     
    Last edited: Dec 3, 2022
  2. Offline

    Jackk99

    Code:
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.player.PlayerPickupItemEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    
    // Other imports
    
    public class WeaponPickupListener {
        @EventHandler(priority = EventPriority.HIGH)
        public void onItemPickUp(PlayerPickupItemEvent e) {
            if(!isWeapon(e.getItem()))
                return;
         
            Player p = e.getPlayer();
            CSUtility cs = new CSUtility();
            String newWeapon = cs.getWeaponTitle(e.getItem());
         
            Inventory inv = p.getInventory();
            List<String> weaponInToolBar = new ArrayList<String>();
         
            for(int i=0; i<9;i++) {
                ItemStack item = inv.getItem(i);
                if(isWeapon(item)) {
                    weaponInToolBar.add(cs.getWeaponTitle(item));
                }
            }
         
            // Check if the weapon is already in the hotbar
            if(weaponInToolBar.contains(newWeapon)) {
                // If it is, move it to the inventory and cancel the pickup
                p.getInventory().addItem(e.getItem().getItemStack());
                e.setCancelled(true);
                return;
            }
         
            // Otherwise, cancel the pickup and remove the item from the world
            e.setCancelled(true);
            e.getItem().remove();
        }
    }
    
    In this code, when a player picks up a weapon, the listener checks if they already have the same weapon in their hotbar. If they do, the item is moved to their inventory and the pickup is cancelled. If they do not have the same weapon in their hotbar, the pickup is cancelled and the item is removed from the world.
     
Thread Status:
Not open for further replies.

Share This Page