Personal scoreboard?

Discussion in 'Plugin Development' started by sebasju1234, May 26, 2013.

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

    sebasju1234

    Hi members of the Bukkit forums,

    I wanted to ask a question about scoreboards. Is it possible to give each player a personal scoreboard? I am making a RPG plugin and I need this for their skill levels.

    This is my code:
    Code:
    package com.hotmail.rpgplugin.rpg.data;
     
    import java.io.File;
    import java.io.IOException;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Score;
     
    import com.hotmail.rpgplugin.rpg.core.Plugin;
    import com.hotmail.rpgplugin.rpg.listeners.PlayerListener;
    import com.hotmail.rpgplugin.rpg.skills.ShowSkills;
     
    public class PlayerDataHandler implements Listener {
     
        private static Plugin plugin;
       
        public static FileConfiguration playerConfig = null;
       
        public PlayerDataHandler(Plugin instance) {
            plugin = instance;
        }
       
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onPlayerJoin(PlayerJoinEvent event) {
            Player player = event.getPlayer();
           
            File file = new File("plugins" + File.separator + "RPGPlugin" + File.separator + "users" + File.separator + event.getPlayer().getName() + ".yml");
           
            if(!file.exists())
            {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                   
                playerConfig = YamlConfiguration.loadConfiguration(file);
               
                playerConfig.set("Skills.combat", 1);
                playerConfig.set("Skills.fishing", 1);
                playerConfig.set("Skills.woodcutting", 1);
                playerConfig.set("Skills.cooking", 1);
                playerConfig.set("Skills.mining", 1);
                playerConfig.set("Skills.smithing", 1);
                playerConfig.set("Skills.magic", 1);
                playerConfig.set("Skills.archery", 1);
                playerConfig.set("Skills.farming", 1);
     
                playerConfig.set("Skills.combat.xp", 0);
                playerConfig.set("Skills.fishing.xp", 0);
                playerConfig.set("Skills.woodcutting.xp", 0);
                playerConfig.set("Skills.cooking.xp", 0);
                playerConfig.set("Skills.mining.xp", 0);
                playerConfig.set("Skills.smithing.xp", 0);
                playerConfig.set("Skills.magic.xp", 0);
                playerConfig.set("Skills.archery.xp", 0);
                playerConfig.set("Skills.farming.xp", 0);
               
                playerConfig.set("Skills.combat.level", 1);
                playerConfig.set("Skills.fishing.level", 1);
                playerConfig.set("Skills.woodcutting.level", 1);
                playerConfig.set("Skills.cooking.level", 1);
                playerConfig.set("Skills.mining.level", 1);
                playerConfig.set("Skills.smithing.level", 1);
                playerConfig.set("Skills.magic.level", 1);
                playerConfig.set("Skills.archery.level", 1);
                playerConfig.set("Skills.farming.level", 1);
     
                try {
                    playerConfig.save(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            else
            {
                playerConfig = YamlConfiguration.loadConfiguration(file);
               
                ShowSkills.skillObjective.setDisplaySlot(DisplaySlot.SIDEBAR);
                ShowSkills.skillObjective.setDisplayName("Skills");
     
                ShowSkills.skillCombat.setScore(playerConfig.getInt("Skills.combat.level"));
                ShowSkills.skillFishing.setScore(playerConfig.getInt("Skills.fishing.level"));
                ShowSkills.skillWoodcutting.setScore(playerConfig.getInt("Skills.woodcutting.level"));
                ShowSkills.skillCooking.setScore(playerConfig.getInt("Skills.cooking.level"));
                ShowSkills.skillMining.setScore(playerConfig.getInt("Skills.mining.level"));
                ShowSkills.skillSmithing.setScore(playerConfig.getInt("Skills.smithing.level"));
                ShowSkills.skillMagic.setScore(playerConfig.getInt("Skills.magic.level"));
                ShowSkills.skillArchery.setScore(playerConfig.getInt("Skills.archery.level"));
                ShowSkills.skillFarming.setScore(playerConfig.getInt("Skills.farming.level"));
               
                player.setScoreboard(ShowSkills.skillsBoard);
            }
        }
       
        public static int getSkillData(Player player, String skillType) {
            File file = new File("plugins" + File.separator + "RPG" + File.separator + "users" + File.separator + player.getName() + ".yml");
     
            playerConfig = YamlConfiguration.loadConfiguration(file);
           
            return playerConfig.getInt("Skills." + skillType + ".level");
        }
       
        public static int getSkillXPData(Player player, String skillType) {
            File file = new File("plugins" + File.separator + "RPGPlugin" + File.separator + "users" + File.separator + player.getName() + ".yml");
     
            playerConfig = YamlConfiguration.loadConfiguration(file);
           
            return playerConfig.getInt("Skills." + skillType + ".xp");
        }
       
        public static void updateSkillXPData(Player player, String skillType, int XP) {
            File file = new File("plugins" + File.separator + "RPGPlugin" + File.separator + "users" + File.separator + player.getName() + ".yml");
           
            playerConfig = YamlConfiguration.loadConfiguration(file);
           
            playerConfig.set("Skills." + skillType + ".xp",  XP);   
           
            try
            {
                playerConfig.save(file);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
       
        public static void updateSkillData(final Player player, final String skillType, int level) {
            File file = new File("plugins" + File.separator + "RPGPlugin" + File.separator + "users" + File.separator + player.getName() + ".yml");
     
            playerConfig = YamlConfiguration.loadConfiguration(file);
                   
            playerConfig.set("Skills." + skillType + ".level",  level);
     
            Bukkit.broadcastMessage(skillType);
           
            try
            {
                playerConfig.save(file);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
     
                ShowSkills.skillObjective.setDisplaySlot(DisplaySlot.SIDEBAR);
                ShowSkills.skillObjective.setDisplayName("Skills");
               
            if (skillType == "combat")
            {
                ShowSkills.skillCombat.setScore(playerConfig.getInt("Skills.combat.level"));
            } else if (skillType == "fishing") {
                ShowSkills.skillFishing.setScore(playerConfig.getInt("Skills.fishing.level"));
            } else if (skillType == "woodcutting") {
                ShowSkills.skillWoodcutting.setScore(playerConfig.getInt("Skills.woodcutting.level"));
            } else if (skillType == "cooking") {
                ShowSkills.skillCooking.setScore(playerConfig.getInt("Skills.cooking.level"));
            } else if (skillType == "mining") {
                ShowSkills.skillMining.setScore(playerConfig.getInt("Skills.minig.level"));
            } else if (skillType == "smithing") {
                ShowSkills.skillSmithing.setScore(playerConfig.getInt("Skills.smithing.level"));
            } else if (skillType == "magic") {
                ShowSkills.skillMagic.setScore(playerConfig.getInt("Skills.magic.level"));
            } else if (skillType == "archery") {
                ShowSkills.skillArchery.setScore(playerConfig.getInt("Skills.archery.level"));
            } else if (skillType == "farming") {
                ShowSkills.skillFarming.setScore(playerConfig.getInt("Skills.farming.level"));
            }
               
            player.setScoreboard(ShowSkills.skillsBoard);
        }
    }
    
    But it shows the level of the last updated players.

    Who can help me? Thanks in advance!
     
  2. Offline

    caseif

    Create an instance of a Scoreboard, then call .setScoreboard(scoreboard) on the player you wish to apply it to.
     
  3. Offline

    chasechocolate

    I would use a HashMap<String, Scoreboard> for saving instances of player scoreboards.
     
    Wizardo367 likes this.
  4. Offline

    isleepzzz

    That sounds like the proper thing to do, however can you please spoon-feed me some example code so I may see it in action?
    I am attempting to accomplish the same thing, however I am having some issues:/
     
  5. Offline

    Plo124

    isleepzzz
    Scoreboard board = //make new scoreboard
    hashMap.put(player.getName(),board);
    // Do logic (make objectives, e.t.c)
     
  6. Offline

    isleepzzz

    Ehhh I attempted with that :/
    But I'ma need more spoon-feeding unfortunately :(
    I don't know where to add it so it's their own personal scoreboards.
    I'm thinking the onPlayerJoin event, but I'm still not sure.
     
  7. Offline

    isleepzzz

  8. Offline

    isleepzzz

    Still need this super bad please :/ Some example code would be phenomenal.

    Damn no one knows? :'(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  9. Offline

    russjr08

    What part are you stuck on exactly? It seems like the others have given you all the code that you need :)

    When you call player.setScoreboard(), it sends the scoreboard to the player. You store that scoreboard in a hashmap (along with the player name) for easily accessing / updating it. Each time you update it, you need to call player.setScoreboard() again to make sure the player receives the update.
     
  10. Offline

    Paljaspers

    This is what I used, a simple wave/points scoreboard, I only made it update points for now because I'm not sure if this is the correct way to do it. It works fine for points (I have it add points when a mob is killed in another class) but it just seems so devious.

    Code:java
    1. public class ScoreManager {
    2.  
    3. static ScoreboardManager manager = Bukkit.getScoreboardManager();
    4. static Map<Player, Scoreboard> scoreboards = new HashMap<Player, Scoreboard>();
    5.  
    6. public static void createScoreboard(Player player) {
    7.  
    8. Scoreboard board = manager.getNewScoreboard();
    9.  
    10. Objective objective = board.registerNewObjective("dtvScoreboard", "dummy");
    11. Score wave = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN + "Wave:"));
    12. Score points = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN + "Points:"));
    13.  
    14. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    15. objective.setDisplayName("DTV");
    16.  
    17. wave.setScore(0);
    18. points.setScore(0);
    19.  
    20. player.setScoreboard(board);
    21. scoreboards.put(player, board);
    22.  
    23. }
    24.  
    25. public static void updatePoints(Player player, int addPoints) {
    26. Scoreboard board = scoreboards.get(player);
    27.  
    28. Objective objective = board.getObjective(DisplaySlot.SIDEBAR);
    29.  
    30. Score points = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN + "Points:"));
    31.  
    32. points.setScore(points.getScore() + addPoints);
    33.  
    34. player.setScoreboard(board);
    35. scoreboards.put(player, board);
    36. }
    37.  
    38. }
     
Thread Status:
Not open for further replies.

Share This Page