Deaths in scoreboard

Discussion in 'Spigot Plugin Development' started by Skud31, May 2, 2015.

  1. Hi guys,
    I'm here because I try many times and I don't know how to set the number of deaths in a scoreboard.
    Example: If I die 1 time, the scoreboard show 1 death.
    Thanks you guys.

    EDIT: Sorry for my english, I'm french.
    -Skud31​
     
  2. Make a scoreboard and a config with the playername and kills. Then make a deathevent and add 1 to the config to the player. You show the scoreboard to each player with his own deathcount. And you can update it in the eventdeath or pro minute or something
     
  3. This part goes into the Main-Class

    Code (Text):
       
    @Override
    public void onEnable() {
        registerEvents();
        for(Player onlinePlayers : Bukkit.getOnlinePlayers()) {
            onlinePlayers.setScoreboard(<yourEventHandlerClass>.yourScoreBoard);
        }
    }
       
    @Override
    public void onDisable() {
        for(Player onlinePlayers : Bukkit.getOnlinePlayers()) {
              onlinePlayers.setScoreboard(<yourEventHandlerClass>.yourScoreboardManager.getNewScoreboard());
        }
    }
       
    public void registerEvents() {
        PluginManager pluginManager = getServer().getPluginManager();
           
        pluginManager.registerEvents(new <your EventHandlerClass>(this), this);
    }

    The following part must be in your EventHandler-Class


    Code (Text):
       
    public static ScoreboardManager yourScoreboardManager = Bukkit.getScoreboardManager();
    public static Scoreboard yourScoreBoard = yourScoreboardManager.getNewScoreboard();
       
    public static Objective yourObjective = yourScoreBoard.registerNewObjective("KillBoard", "dummy");
    public static Score yourKills = yourObjective.getScore(ChatColor.GREEN + "Kills:");

    public static Map<String, Integer> kills = new HashMap();
    public int killCount = 0;
       
    public <yourFile>(<yourMainClass> mainClass) {  
        yourObjective.setDisplaySlot(DisplaySlot.SIDEBAR);
        yourKills.setScore(yourKills.getScore());
    }
    // Note: I set the Objective and the Score in the constructor of my class. This is needed when you want to update after every Kill.
    Code (Text):

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent playerJoinEvent) {
           
        Player eventPlayer = playerJoinEvent.getPlayer();
           
        eventPlayer.setScoreboard(yourScoreBoard);
    }
    Code (Text):
       
    @EventHandler
    public void onEntityDeath(EntityDeathEvent entityDeathEvent) {
           
        Player killer = entityDeathEvent.getEntity().getKiller();
        if(kills.get(killer.getName()) != null) {
            killCount = ((Integer)kills.get(killer.getName())).intValue();
        }
        killCount += 1;
        kills.put(killer.getName(), Integer.valueOf(killCount));

        yourKills.setScore(killCount);
    }
    Warning: This scoreboard will reset if your execute the command /reload (/rl) or stop the server.
     
  4. Inkzzz

    Resource Staff

    I advise using player UUID's rather than player usernames.
     
  5. It doesn't work for me

    Main Class:

    Code (Text):
    package fr.Skud31.Degats;

    import java.io.ByteArrayOutputStream;
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.IOException;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.logging.Logger;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.scheduler.BukkitScheduler;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;
    import org.bukkit.scoreboard.Team;

    public class Main
    extends JavaPlugin
    {
        Events ev = new Events(this);
        Language handler = new Language(this);
       
        @SuppressWarnings({ "unchecked", "rawtypes" })
        public static Map<String, Integer> death = new HashMap();
        public int deathCount = 0;
       
        public Location lobbyLocation;
        private Logger log = Logger.getLogger("Minecraft");
        boolean ok = false;
        boolean inGame = false;
        boolean GameFinish = false;
        String motd;
        ScoreboardManager manager;
        Scoreboard board;
        Team blue;
        Team red;
        Objective kills;
        Objective health;
        Objective points;
        Objective playerpoints;
        Score bluePlayers;
        Score redPlayers;
        Score bluePoints;
        Score redPoints;

       
        @SuppressWarnings("deprecation")
        public void onEnable()
        {
           
            this.manager = getServer().getScoreboardManager();
            this.board = this.manager.getNewScoreboard();
            this.blue = this.board.registerNewTeam("blue");
            this.red = this.board.registerNewTeam("red");
            this.kills = this.board.registerNewObjective("Kills", "playerKillCount");
            this.health = this.board.registerNewObjective("Mort", "deathCount");
            this.points = this.board.registerNewObjective("Points", "dummy");
            this.playerpoints = this.board.registerNewObjective("PlayerPoints", "playerKillCount");
            this.blue.setPrefix(ChatColor.AQUA + "");
            this.red.setPrefix(ChatColor.GOLD + "");
            this.points.getScore(ChatColor.GRAY + "--------").setScore(-1);
            this.points.getScore(ChatColor.RED + "Morts:" + ChatColor.WHITE + " " + deathCount).setScore(-2);
            this.points.setDisplaySlot(DisplaySlot.SIDEBAR);
            this.points.setDisplayName(ChatColor.GREEN + this.handler.getCaption("PointsScoreboard"));
            this.kills.setDisplaySlot(DisplaySlot.PLAYER_LIST);
            this.bluePoints = this.points.getScore(Bukkit.getOfflinePlayer(ChatColor.AQUA +  "Cyan :"));
            this.redPoints = this.points.getScore(Bukkit.getOfflinePlayer(ChatColor.GOLD + "Orange :"));
            this.bluePoints.setScore(0);
            this.redPoints.setScore(0);
            for (Player online : Bukkit.getOnlinePlayers())
            {
                Score score = this.points.getScore(online);
                score.setScore(0);
            }
           
        }
    }
    Event Class:

    Code (Text):
    package fr.Skud31.Degats;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.DyeColor;
    import org.bukkit.Effect;
    import org.bukkit.GameMode;
    import org.bukkit.Instrument;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Note;
    import org.bukkit.Note.Tone;
    import org.bukkit.Sound;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.entity.CreatureSpawnEvent;
    import org.bukkit.event.entity.EntityDamageByBlockEvent;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    import org.bukkit.event.entity.EntityExplodeEvent;
    import org.bukkit.event.entity.FoodLevelChangeEvent;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerDropItemEvent;
    import org.bukkit.event.player.PlayerInteractAtEntityEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.event.player.PlayerPickupItemEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.event.player.PlayerRespawnEvent;
    import org.bukkit.event.player.PlayerToggleFlightEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.scoreboard.Score;

    public class Events
    implements Listener
    {

        private final Main plugin;

        public Events(Main plugin)
        {
            this.plugin = plugin;
        }

        @SuppressWarnings("deprecation")
        @EventHandler
        public void onConnect(PlayerJoinEvent event)
        {
            Player player = event.getPlayer();
            player.setScoreboard(this.plugin.board);
                player.setMaxHealth(20.0F);
                player.setGameMode(GameMode.SURVIVAL);
                player.getInventory().clear();
                player.updateInventory();
    }

    public void onDeath(final PlayerDeathEvent event)
       {

         final Player player = event.getEntity();
         Player killer = player.getKiller().getPlayer();
         
    if(this.plugin.death.get(player.getName()) !=null){
               this.plugin.deathCount = ((Integer)this.plugin.death.get(player.getName()).intValue());
             }
             
             this.plugin.deathCount =+1;
             this.plugin.death.put(player.getName(), Integer.valueOf(this.plugin.deathCount));
             }
               
    }
     
  6. @Skud31: You forgot to define the Scoreboard and the Object

    This foes into your EventHandler-Class

    Code (Text):
     
    public static ScoreboardManager yourScoreboardManager = Bukkit.getScoreboardManager();
    public static Scoreboard yourScoreBoard = yourScoreboardManager.getNewScoreboard();
     
    public static Objective yourObjective = yourScoreBoard.registerNewObjective("KillBoard", "dummy");
    public static Score yourKills = yourObjective.getScore(ChatColor.GREEN + "Kills:");
    public static Map<String, Integer> kills = new HashMap();
     
    public int killCount = 0;
     
    public <yourEventHandlerClass>(<yourMainClass> mainClass) {
        this.mainClass = mainClass;
         
        yourObjective.setDisplaySlot(DisplaySlot.SIDEBAR);
        yourKills.setScore(yourKills.getScore());
    }
    Edit: I didn't see that you defined 'em in your onEnable. So let me go over it for a minute.

    I found your problem:

    This goes into your EventHandler-Class (Event: PlayerDeathEvent)

    Code (Text):
    yourKills.setScore(killCount);
     
    #6 HardTimeStudio, May 3, 2015
    Last edited: May 3, 2015
  7. Yeah.
     
    • Optimistic Optimistic x 1
  8. @HardTimeStudio learn Java - or any object oriented programming language - before spoonfeeding them static infested code. Also, spoon feeding is frowned upon as he will learn nothing from it.

    @Skud31
    Related documentation has been linked in the (part) behind the steps.
     
    • Agree Agree x 1
  9. I want the Scoreboard show: [​IMG]
    But the number of deaths (Morts in french) doesn't work.
    I hope you can help me. :confused: