Solved Scoreboard not working right

Discussion in 'Plugin Help/Development/Requests' started by sgavster, Mar 22, 2015.

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

    sgavster

    Hello! I am trying to get deaths from the config, but it creates a new score each time.. Here is my code:


    PHP:
    package me.sgavster.qmc.func;

    import me.sgavster.qmc.Main;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    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;

    public class 
    scoreBoard implements Listener {

        public static 
    Main m;

        public 
    scoreBoard(Main main) {
            
    main;
        }

        
    ScoreboardManager man Bukkit.getScoreboardManager();

        public 
    void update(Player p) {
            
    Scoreboard b p.getScoreboard();
            if (
    != null) {
                
    Objective o b.getObjective(DisplaySlot.SIDEBAR);

                if (
    != null) {
                    
    o.setDisplayName("§6Quadular§4MC");

                    
    b.clearSlot(DisplaySlot.SIDEBAR);

                    
    o.setDisplaySlot(DisplaySlot.SIDEBAR);
                   
                    
    Score end o.getScore("§6=§4-§6-§4-§6-§4-§6-§4-§6-§4-§6=");
                    
    end.setScore(1);
                   
                    
    Score v1 o.getScore(m.getConfig().getInt(p.getName() + ".votes") == "§aNone. /vote!" "§a" Integer.toString(m.getConfig().getInt(p.getName() + ".votes")));
                    
    v1.setScore(2);
                   
                    
    Score v o.getScore(ChatColor.BLUE "Votes: §a");
                    
    v.setScore(3);
                   
                    
    Score d1 o.getScore(m.getConfig().getInt(p.getName() + ".deaths") == "§aNone!" "§a" Integer.toString(m.getConfig().getInt(p.getName() + ".deaths")));
                    
    d1.setScore(4);
                   
                    
    Score d o.getScore(ChatColor.BLUE "Deaths: §a");
                    
    d.setScore(5);
                   
                    
    Score ol1 o.getScore("§a" Integer.toString(Bukkit.getOnlinePlayers().size()));
                    
    ol1.setScore(6);
                   
                    
    Score ol o.getScore(ChatColor.BLUE "Online: §a");
                    
    ol.setScore(7);

                    
    p.setScoreboard(b);
                } else {
                    
    setScoreboard(p);
                }
            } else {
                
    Scoreboard g man.getNewScoreboard();
                
    Objective o g.registerNewObjective("qmc""dummy");
                
    o.setDisplaySlot(DisplaySlot.SIDEBAR);
                
    o.setDisplayName("§6Quadular§4MC");
                
    g.clearSlot(DisplaySlot.SIDEBAR);
                
    Score end o.getScore("§6=§4-§6-§4-§6-§4-§6-§4-§6-§4-§6=");
                
    end.setScore(1);
               
                
    Score v1 o.getScore(m.getConfig().getInt(p.getName() + ".votes") == "§aNone. /vote!" "§a" Integer.toString(m.getConfig().getInt(p.getName() + ".votes")));
                
    v1.setScore(2);
               
                
    Score v o.getScore(ChatColor.BLUE "Votes: §a");
                
    v.setScore(3);
               
                
    Score d1 o.getScore(m.getConfig().getInt(p.getName() + ".deaths") == "§aNone!" "§a" Integer.toString(m.getConfig().getInt(p.getName() + ".deaths")));
                
    d1.setScore(4);
               
                
    Score d o.getScore(ChatColor.BLUE "Deaths: §a");
                
    d.setScore(5);
               
                
    Score ol1 o.getScore("§a" Integer.toString(Bukkit.getOnlinePlayers().size()));
                
    ol1.setScore(6);
               
                
    Score ol o.getScore(ChatColor.BLUE "Online: §a");
                
    ol.setScore(7);
                
    p.setScoreboard(g);
            }
        }
        public 
    void setScoreboard(Player p) {
            
    Scoreboard b man.getNewScoreboard();
            
    Objective o b.registerNewObjective("qmc""dummy");
            
    o.setDisplaySlot(DisplaySlot.SIDEBAR);
            
    Score end o.getScore("§6=§4-§6-§4-§6-§4-§6-§4-§6-§4-§6=");
            
    end.setScore(1);
           
            
    Score v1 o.getScore(m.getConfig().getInt(p.getName() + ".votes") == "§aNone. /vote!" "§a" Integer.toString(m.getConfig().getInt(p.getName() + ".votes")));
            
    v1.setScore(2);
           
            
    Score v o.getScore(ChatColor.BLUE "Votes: §a");
            
    v.setScore(3);
           
            
    Score d1 o.getScore(m.getConfig().getInt(p.getName() + ".deaths") == "§aNone!" "§a" Integer.toString(m.getConfig().getInt(p.getName() + ".deaths")));
            
    d1.setScore(4);
           
            
    Score d o.getScore(ChatColor.BLUE "Deaths: §a");
            
    d.setScore(5);
           
            
    Score ol1 o.getScore("§a" Integer.toString(Bukkit.getOnlinePlayers().size()));
            
    ol1.setScore(6);
           
            
    Score ol o.getScore(ChatColor.BLUE "Online: §a");
            
    ol.setScore(7);
            
    p.setScoreboard(b);
        }
       
        @
    EventHandler
        
    public void onJoin(PlayerJoinEvent e) {
            
    setScoreboard(e.getPlayer());
        }

        @
    EventHandler
        
    public void onMove(PlayerMoveEvent e) {
            
    Player p e.getPlayer();

            if (
    m.getConfig().getString(p.getName() + ".settings.scoreboard")
                    .
    equalsIgnoreCase("yes")) {
                
    update(p);
            } else {
                
    p.getScoreboard().clearSlot(DisplaySlot.SIDEBAR);
            }
        }
    }
    And what happens:

    [​IMG]

    Every time I die it adds a new score. What is the easiest way to fix this without making it so that the number on the very right is the amount of deaths? Thanks. I really can't figure this out!
     
    Last edited: Mar 22, 2015
  2. Offline

    CheesyFreezy

    @sgavster, this is really simple to do. before you set the name of the fake players you'll have to reset the scores of all the fake players in the scoreboard
     
  3. Offline

    sgavster

    @CheesyFreezy To reset a score you need the OfflinePlayer, how would I get it if it has changed?

    Also: If I restart the server, it says None! then when I die it says <Number of deaths> and on the next line None! Or.. If I relog it just goes back to "None!".. This is so confusing ;-;
     
    Last edited: Mar 22, 2015
  4. @sgavster I get scores from per player config files. I seriously think you should too.
     
  5. Offline

    sgavster

    @CodePlaysMinecraft Well, what's the best way for that? I wouldn't mind doing that just really don't know an easy way..

    If I do that, that still wouldn't fix the problem that the scoreboard adds a score, and doesn't set it each time it is updated. Thanks for any help though.
     
  6. Store everything on an class and when a play join/quit load/save
     
  7. Offline

    sgavster

    @MaTaMoR_ Yeah, I sorta thought of not using PlayerMoveEvent (I HAD player x, y, and z, so I needed it, but now I dont) what is the best way to store player classes and get from it?
     
  8. Offline

    nverdier

    @sgavster Just store the data you need in a file.
     
  9. Offline

    sgavster

    @nverdier I know; I want to know the easiest way to make a per-player file, I can't find a decent solution. I might just scratch the scoreboard because of the problems.
     
  10. Offline

    nverdier

    @sgavster Well I have a library (EnviousAPI), which has a Config class. You can easily create files with that class. Link in signature.
     
  11. Offline

    sgavster

    @nverdier Looks cool. I tried it:

    Code:
        public void setStuff(Config c) {
            if (c.getString("settings.scoreboard") == null) {
                c.set("settings.scoreboard", "yes");
                c.save();
            } else {
                c.set("settings.scoreboard",
                        c.getString("settings.scoreboard"));
                c.save();
            }
            if (c.getString("settings.blood") == null) {
                c.set("settings.blood", "yes");
                c.save();
            } else {
                c.set("settings.blood", c.getString("settings.blood"));
            }
            if(c.get("votes") == null) {
                c.set("votes", 0);
                c.save();
            } else {
                c.set("votes", c.getInt("votes"));
            }
            if(c.get("deaths") == null) {
                c.set("deaths", 0);
                c.save();
            } else {
                c.set("deaths", c.getInt("deaths"));
            }
            if(c.get("crates") == null) {
                c.set("crates", 0);
                c.save();
            } else {
                c.set("crates", c.getInt("crates"));
            }
        }
    
        @EventHandler
        public void onLogin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
            if (p.hasPlayedBefore()) {
                send("§6Quadular§4MC", "§6Welcome Back, §4" + p.getName(), 1, 20,
                        10, p);
            } else {
                send("§6Quadular§4MC", "§6Welcome, §4" + p.getName(), 1, 20, 10, p);
                Config playerConfig = new Config(m, p.getName() + ".yml");
                setStuff(playerConfig);
            }
            p.setMaxHealth(20*2);
        }

    Would this work? (I haven't had time to test yet, sorry.) and if it does, how can I access this file from another method/place

    EDIT: Nope! Here is the error. [​IMG]

    Full class (for lines)
    PHP:
    package me.sgavster.qmc.func;

    import me.sgavster.qmc.Main;
    import me.sgavster.qmc.util.Config;
    import net.minecraft.server.v1_8_R1.ChatSerializer;
    import net.minecraft.server.v1_8_R1.EnumTitleAction;
    import net.minecraft.server.v1_8_R1.IChatBaseComponent;
    import net.minecraft.server.v1_8_R1.PacketPlayOutTitle;
    import net.minecraft.server.v1_8_R1.PlayerConnection;

    import org.bukkit.craftbukkit.v1_8_R1.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;

    public class 
    loginHandler implements Listener {
        public static 
    Main m;

        public 
    loginHandler(Main main) {
            
    main;
        }

        public static 
    void send(String titleString subtitleint inint out,
                
    int stayPlayer p) {

            
    PlayerConnection connection = ((CraftPlayerp).getHandle().playerConnection;
            
    IChatBaseComponent titleJSON ChatSerializer.a("{'text': '" title
                    
    "'}");
            
    IChatBaseComponent subtitleJSON ChatSerializer.a("{'text': '"
                    
    subtitle "'}");
            
    PacketPlayOutTitle titlePacket = new PacketPlayOutTitle(
                    
    EnumTitleAction.TITLEtitleJSONinstayout);
            
    PacketPlayOutTitle subtitlePacket = new PacketPlayOutTitle(
                    
    EnumTitleAction.SUBTITLEsubtitleJSON);
            
    connection.sendPacket(titlePacket);
            
    connection.sendPacket(subtitlePacket);

        }

        public 
    void setStuff(Config c) {
            if (
    c.getString("settings.scoreboard") == null) {
                
    c.set("settings.scoreboard""yes");
                
    c.save();
            } else {
                
    c.set("settings.scoreboard",
                        
    c.getString("settings.scoreboard"));
                
    c.save();
            }
            if (
    c.getString("settings.blood") == null) {
                
    c.set("settings.blood""yes");
                
    c.save();
            } else {
                
    c.set("settings.blood"c.getString("settings.blood"));
            }
            if(
    c.get("votes") == null) {
                
    c.set("votes"0);
                
    c.save();
            } else {
                
    c.set("votes"c.getInt("votes"));
            }
            if(
    c.get("deaths") == null) {
                
    c.set("deaths"0);
                
    c.save();
            } else {
                
    c.set("deaths"c.getInt("deaths"));
            }
            if(
    c.get("crates") == null) {
                
    c.set("crates"0);
                
    c.save();
            } else {
                
    c.set("crates"c.getInt("crates"));
            }
        }

        @
    EventHandler
        
    public void onLogin(PlayerJoinEvent e) {
            
    Player p e.getPlayer();
            if (
    p.hasPlayedBefore()) {
                
    send("§6Quadular§4MC""§6Welcome Back, §4" p.getName(), 120,
                        
    10p);
            } else {
                
    send("§6Quadular§4MC""§6Welcome, §4" p.getName(), 12010p);
                
    Config playerConfig = new Config(mp.getName() + ".yml");
                
    setStuff(playerConfig);
            }
            
    p.setMaxHealth(20*2);
        }
    }
     
    Last edited: Mar 24, 2015
  12. Offline

    nverdier

  13. Offline

    mrCookieSlime

    Moved to Alternatives Section.
     
  14. Offline

    CheesyFreezy

    @sgavster, what?! don't do this... this is not what you have to do!

    create a for loop with all the offline players in a scoreboard
    Code:
    for(OfflinePlayer players : b.getPlayers()) {
       b.resetScore(players);
    }
     
  15. Offline

    sgavster

    @CheesyFreezy I did, it didn't really help, well.. it did but:

    When I log in it still says 'None!' when that is not true. I'll look into that again, I stopped the code for now.

    @nverdier, here: http://pastebin.com/4uK3QTsH

    It is at the very very end.. I was playing around..
     
  16. Offline

    nverdier

  17. Offline

    sgavster

  18. Offline

    nverdier

    @sgavster Oh haha, did you edit that in? Well it was a stupid problem I made while editing the class, fixed now :D
     
  19. Offline

    sgavster

    @nverdier Yeah, I edited it in. I'll update it. Also, is there a way to access it from another method?
     
  20. Offline

    nverdier

    @sgavster From another method? Create a field and use that.

    If you meant from another class, either
    a) Create a constructor in the other class, which has a Config parameter. When you instantiate the class, pass it the Config.
    b) Have a getter in the class you create the Config in, and then pass the other class the instance of the first class. Then use the getter to retrieve the Config.
     
  21. Offline

    sgavster

    @nverdier It worked great, btw.

    I'm a bit confused.. So I'm assuming there is no config.get(), lol.

    Could I get an example of how to do it from another class? (I dont expect to be spoon fed, I just would like to know where/how to start, I'm a bit confused)

    Thanks so much..
     
  22. Offline

    nverdier

    @sgavster Well the class I have isn't a config manager. You just pass the instances around if you want. For your reasons, I see why you'd want a config manager. You can easily create one on your own, just have a Map, which stores the string of the config name, and the Config instance itself. Then, when you want to get a Config, just use Map#get(Object key).
     
  23. Offline

    sgavster

    @nverdier On restart, wouldn't that remove the Map, so I would have to re-access it?

    EDIT: I'm assuming if I called a new Config(main, name) each time I needed it it would be bad..
     
  24. Offline

    nverdier

    @sgavster Well when you restart, everything disappears... You'd go through the same process of creating all of the Config classes, and adding them.
     
  25. Offline

    sgavster

    @nverdier Oh really? I need it to be like a constant-config, because it stores the votes, settings, deaths etc.. My server would restart quite often so I don't know what to do
     
  26. Offline

    nverdier

    @sgavster Well the Config class I gave you creates a file, and stores stuff. When the server restarts, you have to load all of the data, which you do. When you restart the server, you create a new Config, which loads the file and values in it. When you do that, you can just add it to the Map.
     
  27. Offline

    sgavster

    @nverdier Ohh okay. So the stuff does stay.

    If I have it in that class, how would I access it from another class? Because I only have the config on one method, but need it in like 10.
     
  28. Offline

    nverdier

    @sgavster Just create a Map with all of the configs as the values, and then create a getter for that Map. Pass an instance of the class it's in to the class you want to access the Configs in, and then use the getter method to retrieve the Map. Once you have the Map, you can get the Configs out of it.
     
  29. Offline

    sgavster

    @nverdier Oh man.. I've never even had to use maps like this.. Is there anywhere I can look for help on this? It's not that it's confusing just I'm not sure how to do it. :p
     
  30. Offline

    nverdier

    @sgavster
    1) Create a map in the class you're creating the configs in.
    2) When you create a Config, add it to said Map.
    3) Create a getter method that returns the Map.
    4) In another class (which you've passed an instance of the first class to), call the getter method, then get Configs out of the Map that is returned.
     
Thread Status:
Not open for further replies.

Share This Page