Clear Inventory and then reset it on how it was before

Discussion in 'Plugin Development' started by Nonliker1000, Nov 21, 2014.

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

    Nonliker1000

    Hey guys, i am trying to code a plugin where it clears you´re inventory > then teleports you to spawn > and then puts your inventory back to with the items like it was before your inventory got cleared.
    Anyways heres my code:



    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    2. {
    3. if (cmd.getName().equalsIgnoreCase("spawn"))
    4. {
    5. final Player p = (Player)sender;
    6. @SuppressWarnings("unused")
    7. ItemStack armadura = p.getInventory().getHelmet();
    8. p.setHealth(1.0D);
    9. p.getInventory().clear();
    10. p.getInventory().setHelmet(new ItemStack(Material.AIR));
    11. p.getInventory().setChestplate(new ItemStack(Material.AIR));
    12. p.getInventory().setLeggings(new ItemStack(Material.AIR));
    13. p.getInventory().setBoots(new ItemStack(Material.AIR));
    14. p.sendMessage(ChatColor.DARK_RED + "§7Teleporting in §23 Seconds§7. ");
    15.  
    16. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this.plugin, new Runnable()
    17. {
    18. public void run()
    19. {
    20.  
    21. p.sendMessage("§7Teleported to §aSPAWN§7...");
    22. p.teleport(new Location(Bukkit.getWorld("world"), 10000, 70, 1000));
    23. p.setWalkSpeed(1);
    24. ItemStack a = new ItemStack(Material.CHEST);
    25. ItemMeta b = a.getItemMeta();
    26. b.setDisplayName("§aKit Selector");
    27. a.setItemMeta(b);
    28. p.getInventory().addItem(new ItemStack[] { a });
    29.  
    30.  
    31. ItemStack w = new ItemStack(Material.COMPASS);
    32. ItemMeta mw = a.getItemMeta();
    33. mw.setDisplayName("§aWarps");
    34. w.setItemMeta(mw);
    35. p.getInventory().addItem(new ItemStack[] { w });
    36. }
    37. }, 100L);
    38. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this.plugin, new Runnable()
    39. {
    40. public void run()
    41. {
    42. p.setHealth(20.0D);
    43. }
    44. }, 100L);
    45. }
    46. return true;
    47. }
    48. }
    49.  


    Btw ignore the the items stack "compass" and "chest"
    I´ll remove that anyway.

    Thanks in advance, Best Regards - NonLikahhh :)
     
  2. Offline

    xJeremyCx

    You can save the items in a List or something then put those back.
     
  3. Offline

    Nonliker1000

    thanks man can you give me a example? c;
     
  4. Offline

    Skionz


    Code:
    ArrayList<ItemStack> list = new ArrayList<ItemStack>();
    list.add(new ItemStack(Material.BEDROCK);
     
  5. Offline

    Th3Br1x

    Nonliker1000
    Well, you could use an ArrayList for this, but i recommend using a HashMap, so you can still 'track' which Inventory belongs to which player.
     
    rbrick and Skionz like this.
  6. Offline

    Nonliker1000

    Thanks dude could i get an example?
     
  7. Offline

    Skionz

  8. Offline

    Nonliker1000

  9. Offline

    AronTheGamer

  10. Offline

    ROTN

    Instead of saving the individual items, how about saving the PlayerInventory (Player#getInventory()) to a HashMap. It'll save everything in the inventory, and will only take a few lines.
     
Thread Status:
Not open for further replies.

Share This Page