[TUTORIAL] Saving Player Inventories!

Discussion in 'Resources' started by FatAussieFatBoy, Sep 16, 2013.

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

    FatAussieFatBoy

    So after about a week looking for a Good Tutorial on how to Save a Players Inventory on Death, I found literally nothing that was Helpful at all, so I am here now to put all others who have had the Problem to ease, This is a Tutorial for any Level of Coders, but I recommend that you learn how to Create the Basis of a Plugin here : http://forums.bukkit.org/threads/tutorial-how-to-make-a-basic-plugin.170780/ First (if you don't know already). But without further to due Here is the Tutorial you have all been waiting for -

    Firstly we want to add the extends JavaPlugin and implements Listener to the Plugin Main Class like so -

    Code:
    public class Main extends JavaPlugin implements Listener


    Now we want to create the HashMaps that will Store and Access the Players Inventory -

    Code:
    public HashMap<Player, ItemStack[]> invsave = new HashMap<Player, ItemStack[]>();
     
    public HashMap<Player, ItemStack[]> armorsave = new HashMap<Player, ItemStack[]>();
    Now that's done we can start with the Main Body Code to Save, Store and Give the Players Inventory when Respawned -

    Code:
    //Player Spawn Event
    @EventHandler
    public void onPlayerSpawn(PlayerRespawnEvent event) {
        Player p = event.getPlayer();
        p.getInventory().setContents(invsave.get(p));
        p.getInventory().setArmorContents(armorsave.get(p));
        }
     
    //Player Death Event
    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent event) {
        if(event.getEntity() instanceof Player) {
            Player p = (Player) event.getEntity();
            if(event.getDrops() != null) {
                invsave.put(p, p.getInventory().getContents());
                armorsave.put(p, p.getInventory().getArmorContents());
                event.getDrops().clear();
                Bukkit.broadcastMessage("[" + GOLD + "FUNKits" + WHITE + "] " + RED + p.getName() + GREY + " Has been Killed!");
            }else{
                Bukkit.broadcastMessage("[" + GOLD + "FUNKits" + WHITE + "] " + RED + p.getName() + GREY + " Has been Killed!");
            }
        }
    }
    That's It, I Hope this has helped If not then MSG me and I will try to explain this Better for you.

    NOTE : That I will be updating this Forum when I learn how to create a HashMap that will store all the Inventories even if the Player Leaves and doesn't respawn

    (Don't forget to fix all errors by hovering over the Code with the Red Underline)

    FatAussieFatBoy <3
     
    MercilessPvP likes this.
  2. Offline

    timtower Administrator Administrator Moderator

    FatAussieFatBoy Just something to keep in mind: Storing the Player class as index isn't really smart, try storing the names of the players instead
     
    Scizzr and Chinwe like this.
  3. Offline

    TomTheDeveloper

    There is a simple way that is already implemented in the game that saves your inventory on death! Its just one command to enable it:

    /gamerule keep inventory true
     
  4. Offline

    Garris0n

    Unless you only want it for specific people...
     
    Ultimate_n00b and Scizzr like this.
Thread Status:
Not open for further replies.

Share This Page