Solved PlayerItemHeldEvent, but with more previous slots.

Discussion in 'Plugin Development' started by khave, Nov 4, 2015.

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

    khave

    Hello, I'd like some help regarding having more options in the PlayerItemHeldEvent. In the event you can get the previous slot the player had and the new slot, which the player is currently holding. The event gets called when you change slots.
    How would I got about getting, say 4 previous slots? Should I create a map of the integers and add them or should I choose a different route and make something like a scheduler that keeps checking if the previous slot is not the same and then adding them to a list?

    What I have so far is the PlayerItemHeldEvent where it adds the new slot the player switches too, to a Multimap of the player's UUID and the integers.
     
  2. Offline

    Scimiguy

    Keep track of the event, and store the slots in a HashMap<UUID, SortedSet<int>>

    No need for scheduling for this
     
  3. Offline

    khave

    I tried a bit of what you said, but could not get it to work. Here is my code. I try it out in-game and nothing seems to happen, where it should say "Hello World" when I go through slot 5-8..

    Code:
    private HashMap<UUID, List<Integer>> slots;
    
    public ItemChangeTimer(){
    slots = new HashMap<>();
    }
    
    @EventHandler
        public void onItemChange(PlayerItemHeldEvent event){
            Player player = event.getPlayer();
    
            if(!(player.getInventory().getHeldItemSlot() >= 5 && player.getInventory().getHeldItemSlot() <= 8)){
                slots.clear();
                return;
            }
    
            if(!slots.containsKey(player.getUniqueId())){
                slots.put(player.getUniqueId(), new ArrayList<Integer>());
            }
    
            slots.get(player.getUniqueId()).add(event.getNewSlot());
    
            if(slots.get(player.getUniqueId()).size() >= 4){
                if(slots.get(player.getUniqueId()).get(0) == 5 && slots.get(player.getUniqueId()).get(1) == 6 && slots.get(player.getUniqueId()).get(2) == 7 && slots.get(player.getUniqueId()).get(3) == 8) {
                    event.getPlayer().sendMessage("Hello World!");
                }
                slots.clear();
            }
        }
     
  4. Offline

    Zombie_Striker

    Very specific. Doing ANYTHING but changing that ONE item to those specific spots in that specific order would cause it not to work. All you have to do is accidentally click slot 1 and it will never work.
     
  5. Offline

    khave

    I clear the slots list frequently though?
     
  6. Offline

    Scimiguy

    Last edited: Nov 5, 2015
  7. Offline

    khave

    Thank you! Did some work and got it to work with the LinkedList, but I'll try out the CircularFifoQueqe too.
     
Thread Status:
Not open for further replies.

Share This Page