Disconnect event help

Discussion in 'Plugin Development' started by Wingas, Jan 14, 2017.

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

    Wingas

    I want to make plugin, that will store all player items into chest, and places chest in players location ON DISCONNECT, so my plugin spawns chest on the disconnected players location, but doesnt clear players inventory, and doesnt store items into spawned chest, any ideas why?

    My code:
    Code:
        @EventHandler
        public void onDc(PlayerQuitEvent e){
            Player p = e.getPlayer();
            ItemStack[] inv = p.getInventory().getContents();
            p.getLocation().getBlock().setTypeId(54);
            Block b = p.getLocation().getBlock();
            if(b.getTypeId()==54){
            Chest chest = ((Chest) b.getState());
            chest.getBlockInventory().addItem(inv);
            }
            p.getInventory().clear();
            p.updateInventory();
           
        }

    CONSOLE ERROR:
     
  2. Offline

    Jay3105

    Try to use chest.getBlockInventory.setContent(inv) instead of addItem(inv)
     
  3. Offline

    Wingas

    same..
     
  4. Online

    timtower Administrator Administrator Moderator

    @Wingas Check for null values, skip them.
     
  5. @Wingas
    If @Jay3105's method does not work you're doing something wrong, as that does not check for null values. Please post your code.
     
  6. Offline

    Wingas

    Code:
            if(!inv.equals(null)&&chest.equals(null)){
               
            }
    hm?

    Code:
        @EventHandler
        public void onDc(PlayerQuitEvent e){
            Player p = e.getPlayer();
            ItemStack[] inv = p.getInventory().getContents();
            p.getLocation().getBlock().setTypeId(54);
            Block b = p.getLocation().getBlock();
            Chest chest = ((Chest) b.getState());
            chest.getBlockInventory().setContents(inv);
            p.getInventory().clear();
            p.updateInventory();
           
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  7. @Wingas
    And can you post the error you are getting now?
     
  8. Offline

    Wingas

     
  9. Online

    timtower Administrator Administrator Moderator

    @Wingas Player inventory is bigger then a chest.
     
  10. @Wingas Building on what @timtower said, use a double chest instead of a single. Or listen for a right click on the chest and open a virtual inv


    Sent from my iPhone using Tapatalk
     
  11. Offline

    Zombie_Striker

    This will throw an NPE. When checking for null values, you need to use ==.
     
Thread Status:
Not open for further replies.

Share This Page