Set player inventory?

Discussion in 'Plugin Development' started by vemacs, Feb 9, 2013.

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

    vemacs

    Here's my code:

    Code:
    package com.nullblock.vemacs.InvTest;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.inventory.ItemStack;
     
    public class InvTestListener implements Listener {
     
        private InvTest plugin;
        private ItemStack[] inventory;
     
        public InvTestListener(InvTest InvTest) {
            plugin = InvTest;
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
     
        @EventHandler(priority = EventPriority.LOW)
        public void onPlayerJoin(PlayerJoinEvent event) {
            Player player = event.getPlayer();
            inventory = player.getInventory().getContents();
            player.getInventory().clear();
        }
     
        @EventHandler(priority = EventPriority.LOW)
        public void onPlayerChat(AsyncPlayerChatEvent event) {
            Player player = event.getPlayer();
            player.getInventory().setContents(inventory);
        }
     
        @EventHandler(priority = EventPriority.LOW)
        public void onPlayerLeave(PlayerQuitEvent event) {
            Player player = event.getPlayer();
            player.getInventory().setContents(inventory);
        }
    }
    
    The code on join seems to work as expected, however, when the AsyncPlayerChatEvent is triggered, the player inventory isn't replaced with the captured one.

    This is what I do clientside:
    -login
    -acquire some items
    -logout

    What I expect the code to do after the next login:
    -wipe the player's inventory
    -when the player chats, the player's inventory gets replaced

    What happens instead:
    -the player's inventory is wiped
    -nothing happens

    The code compiles fine, am I using the inventory functions incorrectly, or is the inventory variable not working properly?

    Thanks in advance.
     
  2. Offline

    Double0negative

    player.updateInventory();
     
  3. Offline

    vemacs

    Adding that after the setContents() doesn't do anything either (it also happens to be deprecated).

    Also, I'm currently using your xServerChat ;)
     
  4. Offline

    Double0negative

    that thing still works :confused:

    and this is what i use in sg

    Code:
    
                p.getInventory().setContents(inv_store.get(p)[0]);
                p.getInventory().setArmorContents(inv_store.get(p)[1]);
    
                p.updateInventory();
    
     
  5. Offline

    vemacs

    Well, I did make quite a lot of changes to it (especially regarding threads).

    Regardless, how did you get the contents? What type is inv_store (it doesn't seem to be an ItemStack or PlayerInventory)?

    (EDIT: it seems to be a HashMap)

    How did you get the inventory contents into inv_store?

    (EDIT 2: see saveInv() and restoreInv())

    Here is my current Listener:

    Code:
    package com.nullblock.vemacs.InvTest;
     
    import java.util.HashMap;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.inventory.ItemStack;
     
    public class InvTestTestListener implements Listener {
       
        private InvTest plugin;
        private HashMap<Player, ItemStack[][]> inv_store = new HashMap<Player, ItemStack[][]>();
        private boolean isLogged;
       
        public InvTestTestListener(InvTest InvTest) {
            plugin = InvTest;
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
       
        @EventHandler(priority = EventPriority.LOW)
        public void onPlayerJoin(PlayerJoinEvent event) {
            Player player = event.getPlayer();
            isLogged = false;
            saveInv(player);
            player.getInventory().clear();
        }
       
        @EventHandler(priority = EventPriority.LOW)
        public void onPlayerChat(AsyncPlayerChatEvent event) {
            Player player = event.getPlayer();
            isLogged = true;
            restoreInv(player);
        }
       
        @EventHandler(priority = EventPriority.LOW)
        public void onPlayerLeave(PlayerQuitEvent event) {
            Player player = event.getPlayer();
            if(isLogged == true){
            restoreInv(player);
            }
        }
       
        public void saveInv(Player p){
            ItemStack[] [] store = new ItemStack[2][1];
            store[0] = p.getInventory().getContents();
            store[1] = p.getInventory().getArmorContents();
            this.inv_store.put(p, store);
        }
       
        @SuppressWarnings("deprecation")
        public void restoreInv(Player p){
            p.getInventory().clear();
            p.getInventory().setContents(this.inv_store.get(p)[0]);
            p.getInventory().setArmorContents(this.inv_store.get(p)[1]);
            this.inv_store.remove(p);
            p.updateInventory();
        }
    }
    
    However, I get an NPE whenever anything is typed:

    Code:
    Caused by: java.lang.NullPointerException
            at com.nullblock.vemacs.nullAuth.InvTestTestListener.restoreInv(InvTestTestListener.java:58)
    What do you think is the issue here? It compiles perfectly fine.

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

    FlaminYogurt

    I can see the date has been a while, but i found this page to be very helpful. I think your problem is double array varible store, and in the hashmap. i dont ever see you use the second dimension to the array or why you would want it?
     
  7. Offline

    comdude2


    I also found this helpful but i understand the 2 dimensions. He's used two dimensions because one is to store the inventory, the other is to store armor contents.
     
Thread Status:
Not open for further replies.

Share This Page