Solved Keep Inventory on death

Discussion in 'Plugin Development' started by lolgrapig112, Dec 24, 2012.

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

    lolgrapig112

    Dear,

    Currently I am working on a plugin for a server of my friend evrything works except that:
    When a player dies he does not gets his items back.

    I treid many ways on google so my question is could somewhan make a respawn event where
    the the player gets his items back ?(keep inventory on death)

    if so many thanks :) also please add some information about what you have done so i can learn about it.

    Greetzs,
    lolgrapig112

    My code so far i know it is messy but it works

    Code:
    package com.gmail.mitchoost;
     
    import java.io.File;
    import java.util.HashMap;
    import java.util.List;
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import com.massivecraft.factions.FPlayer;
    import com.massivecraft.factions.FPlayers;
    import org.bukkit.event.entity.*;
    import org.bukkit.event.*;
    import org.bukkit.inventory.ItemStack;
     
    public class FactionWars extends JavaPlugin implements Listener{
        public final Logger logger = Logger.getLogger("Minecraft");
        public final Location[] warpLoactions = new Location[2];
        public final String[] warpName = new String[2];
        public static FactionWars Plugin;
        public int warpCounter = 0;
        public int number = 30;
        public int fac1 = 0;
        public int fac2 = 0;
        public int COOLDOWN_TIME = 4;
        public int rank = 0;
        public int rank2 = 0;
        private final HashMap<Player, List<ItemStack>> lastDrops = new HashMap<Player, List<ItemStack>>();
            public void onEnable(){
                getLogger().info("Enabled.");
                this.getServer().getPluginManager().registerEvents(this, this);
                getConfig().options().copyDefaults(true); 
                File file = new File(getDataFolder()+ File.separator + "config.yml");
             
                if(!file.exists()){
                    this.getLogger().info("Generating config file!!!");
                 
                    this.getConfig().addDefault("faction", "Rank");
                    this.getConfig().options().copyDefaults(true);
                    this.saveConfig();
                }
            }
            public void onDisable(){
                getLogger().info("Disabled.");
            }
            String start = "false";
            String faction1 = "faction1";
            String faction2 = "faction2";
            String faction1id = "faction1";
            String faction2id = "faction2";
            String yours = "";
            String exit = "false";
            String message = "";
            @EventHandler
            // Checks if a player dies then it does the rank thingies
            public void onDeath(PlayerDeathEvent e) {
                if (start == ("true")){
                    Player p = (Player) e.getEntity();
                    FPlayer me = FPlayers.i.get(p);
                    Player name = p.getServer().getPlayer(p.getName());
                if (me.getFaction().getTag().equals(faction1)){
                    if (fac1 <= 1){
                        getServer().broadcastMessage(ChatColor.GOLD + "<FactionBattle> " + ChatColor.GREEN + faction2 + ChatColor.GOLD + " Won this glorius battle.");
                        getServer().broadcastMessage(ChatColor.RED + "type /fexit to exit");
                        e.getDrops().clear();
                        if (this.getConfig().get(faction2+ "name") != null){
                            rank = Integer.parseInt(this.getConfig().getString(faction2 + "rank"));
                            rank = rank + 1;
                            this.getConfig().set(faction2 + "rank", rank);
                            this.saveConfig();
                        }else{
                            rank = 1;
                            this.getConfig().addDefault(faction2 + "name", faction2);
                            this.getConfig().addDefault(faction2 + "rank", rank);
                            this.saveConfig();
                        }
                        exit = "true";
                        fac1 = 0;
                        number = 0;
                        }else if (fac1 != 0){
                            fac1--;
                            getServer().broadcastMessage(ChatColor.GOLD + "<FactionBattle> " + ChatColor.RED + name.getName() + ChatColor.GOLD +" of faction:  " + me.getFaction().getTag()  + " Died in his glorius fight!!");
                        }
                }else if(me.getFaction().getTag().equals(faction2)){
                    if (fac2 <= 1){
                        getServer().broadcastMessage(ChatColor.GOLD + "<FactionBattle> " + ChatColor.GREEN + faction1 + ChatColor.GOLD + " Won this glorius battle.");
                        getServer().broadcastMessage(ChatColor.RED + "type /fexit to exit");
                        e.getDrops().clear();
                        if (this.getConfig().get(faction2+ "name") != null){
                            rank = Integer.parseInt(this.getConfig().getString(faction1 + "rank"));
                            rank = rank + 1;
                            this.getConfig().set(faction1 + "rank", rank);
                            this.saveConfig();
                        }else{
                            this.getConfig().addDefault(faction1 + "name", faction1);
                            this.getConfig().addDefault(faction1 + "rank", rank);
                            this.saveConfig();
                        }
                        fac2 = 0;
                        exit = "true";
                        number = 0;
                        }else if (fac2 != 0){
                            fac2--;
                            e.getDrops().clear();
                            getServer().broadcastMessage(ChatColor.GOLD + "<FactionBattle> " + ChatColor.RED + name.getName() + ChatColor.GOLD +" of faction:  " + me.getFaction().getTag()  + " Died in his glorius fight!!");
                        }
                }else{
                 
                }
                }
            }
     
            // all the commands go here
            public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] args){
                Player p = (Player) sender;
                // the join command
                if (commandlabel.equalsIgnoreCase("fjoin")){
                    if (p.hasPermission("fb.join")){
                    if (start == ("true")){
                    FPlayer me = FPlayers.i.get(p);
                    yours = me.getFaction().getTag().toLowerCase();
                    if (me.getFaction().getTag().equals(faction1)){
                        p.sendMessage(ChatColor.GOLD + "<FactionBattle> " + ChatColor.RED + "You joined the battle.");
                        fac1++;
                        getServer().dispatchCommand(getServer().getConsoleSender(), "warp "+ "fac1 " + p.getName());
                    }else if (me.getFaction().getTag().equals(faction2)){
                        p.sendMessage(ChatColor.GOLD + "<FactionBattle> " + ChatColor.RED + "You joined the battle.");
                        fac2++;
                        getServer().dispatchCommand(getServer().getConsoleSender(), "warp "+ "fac2 " + p.getName());
                    }else{
                        p.sendMessage(ChatColor.GOLD + "<FactionBattle> " + ChatColor.RED + "Your faction is not chosen!");
                    }
                    }
                    }
                    // the start command
                }else if (commandlabel.equalsIgnoreCase("fstart")){
                    if (p.hasPermission("fb.start")){
                        if (args[0] != ""  ){
                    faction1 = args[0];
                    faction2 = args[1];     
                    start = "true";
                    getServer().broadcastMessage(ChatColor.GOLD + "<FactionBattle> " + ChatColor.RED + "Battle started between: " + ChatColor.GOLD + faction1 + ChatColor.RED + " and " + ChatColor.GOLD + faction2);
                    getServer().broadcastMessage(ChatColor.GOLD + "<FactionBattle> " + ChatColor.RED + "type /fjoin to join." + ChatColor.GOLD + " You have 30 second to join."); 
                 
                    this.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable(){
                    public void run(){
                        if (number != -1){
                            if (number != 0){
                                number--;
                              if (number <= 10){
                                getServer().broadcastMessage(ChatColor.RED + "" + number + " Seconds left to start!!");
                                number--;
                              }
                            }else{
                                number--;
                                getServer().broadcastMessage(ChatColor.GOLD + "<FactionBattle> " + ChatColor.RED + " You can not join anymore, You fool!!");
                                start = "false";
                                }
                            }
                        }
                    }, 0L, 20L);
                }else{
                    p.sendMessage(ChatColor.RED + "Command usage : /fstart <faction1> <faction2>");
                }
                    }
                    // the exit command
                }else if (commandlabel.equalsIgnoreCase("fexit")){
                    if(p.hasPermission("fb.exit")){
                        if (exit == ("true")){
                            p.sendMessage(ChatColor.GOLD + "<FactionBattle> " + ChatColor.RED + "You where a good fighter!!");
                            getServer().dispatchCommand(getServer().getConsoleSender(), "spawn " + p.getName());
                        }
                    }
                    // the stop command
                }else if (commandlabel.equalsIgnoreCase("fstop")){
                    if (p.hasPermission("fb.stop")){
                    fac1 = 0;
                    fac2 = 0;
                    faction1= "";
                    faction2= "";
                    start= "false";
                    exit = "true";
                    number = 0;
                    getServer().broadcastMessage(ChatColor.GOLD + "<FactionBattle>" + ChatColor.RED + "Server admin closed this battle. type /fexit to exit");
                    }
                    // the leaderboard command
                }else if (commandlabel.equalsIgnoreCase("fboard")){
                    if (p.hasPermission("fb.board")){
                    if (args[0] != ""  ){
                    if (this.getConfig().get(args[0]+ "name") != null){
                    message = this.getConfig().getString(args[0] + "rank");
                    p.sendMessage(ChatColor.GOLD + "<FactionBattle> " + ChatColor.GOLD + args[0].toString() + " has won: " + ChatColor.RED + message + ChatColor.GOLD + " Battles!!!");
                 
                }else{
                    p.sendMessage(ChatColor.GOLD + "<FactionBattle> " + ChatColor.RED + "This faction does not exist or is not ranked.");
                }
             
                }else{
                    p.sendMessage(ChatColor.GOLD + "<FactionBattle> " + ChatColor.RED + "use /fboard <faction>");
                }
                }
                }
                return false;
            }
     
    }
    
    All i need is a way that when a player dyes he gets his inventory back.

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

    MP5K

    Hello lolgrapig112
    I haven't tested the code but i should work fine.
    Code:
        public HashMap<Player , ItemStack[]> items = new HashMap<Player , ItemStack[]>();
       
        @EventHandler()
        public void onRespawn(PlayerRespawnEvent event){
            if(items.containsKey(event.getPlayer())){
                event.getPlayer().getInventory().clear();
                for(ItemStack stack : items.get(event.getPlayer())){
                    event.getPlayer().getInventory().addItem(stack);
                }
               
                items.remove(event.getPlayer());
            }
        }
       
        @EventHandler()
        public void onDeath(PlayerDeathEvent event){
            ItemStack[] content = event.getEntity().getInventory().getContents();
            items.put(event.getEntity(), content);
            event.getEntity().getInventory().clear();
        }
     
  3. Offline

    lolgrapig112

    Trying right now but getplayer does not work so i replaced it with getEntity
    I keep you updated

    Greetz,
    lolgrapig112

    The getplayer did worke my fault :)

    Works great Thanks alot :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
Thread Status:
Not open for further replies.

Share This Page