Solved Have picked up items NOT go to hotbar

Discussion in 'Plugin Development' started by Yukari, Oct 25, 2013.

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

    Yukari

    I'm trying to write a plugin where items that are picked up do not go to the hotbar, but instead go to the inventory by default. So that even if your hotbar has space, what you pickup will not go onto the hotbar.

    I can't seem to find/think of any good way of doing this. PlayerPickupItemEvent doesn't seem to have anything of use. The only workable solution I've been able to come up with is to get the ItemStack, iterate through the player inventory slots for the first empty one, spawn same item into said spot. Then cancel the pickup event, and look for items within a reasonable range from the player, and if said item matches the picked up item delete it (from the ground).

    Is there perhaps any easier way to do this?
     
  2. Offline

    maxben34

    Yukari

    I have an idea. How about you fill up a player's hotbar spaces with any item different from the type you want to add to their inventory.

    Add the item to their inventory

    Remove all items from hotbar.

    Since it will happen all at once (in 1 tick) it will become unnoticed by the player.
     
  3. Offline

    nisovin

    You don't have to look for nearby items, you can use event.getItem().remove(). But otherwise yeah, that seems about right.
     
  4. Offline

    Yukari

    nisovin Ah, I wasn't aware you could just do that. That does make this a bit simpler.

    maxben34 Hadn't thought of doing it that way. Sounds like the best solution. But, if you remove it in the same tick, then won't it just cancel out? Wouldn't you have to remove it in the next tick?

    If anybody's interested, here's what I've got that works reasonably well, although you can barely see the stone blocks for the 1/20th of a second they appear.

    Code:java
    1. private List<Integer> tempStones = new ArrayList<Integer>();
    2.  
    3. private ItemStack stone = new ItemStack(Material.STONE);
    4.  
    5. @EventHandler(priority = EventPriority.NORMAL,ignoreCancelled=true)
    6. public void onPlayerPickupItemEvent(PlayerPickupItemEvent event) {
    7.  
    8. if ( event.getItem().getItemStack().getEnchantments().isEmpty() ) return; // Ensures that this only acts upon enchanted items
    9.  
    10. final PlayerInventory inventory = event.getPlayer().getInventory();
    11.  
    12. for ( int i = 0 ; i < 9 ; i++ ) { // Hotbar slots are 0 through 8
    13. if ( inventory.getItem(i) == null ) {
    14. inventory.setItem(i, stone);
    15. tempStones.add(i);
    16. }
    17. }
    18.  
    19. instance.getServer().getScheduler().scheduleSyncDelayedTask(instance, new Runnable() {
    20. public void run() {
    21. for ( int i : tempStones ) {
    22. inventory.clear(i);
    23. }
    24. tempStones.clear();
    25. }
    26. }, 1);
    27.  
    28. }
    29.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  5. Offline

    maxben34

    That should be the best way of doing it. Nice job.
     
Thread Status:
Not open for further replies.

Share This Page