My CoinsAPI not working?

Discussion in 'Spigot Plugin Development' started by avighnash, May 23, 2016.

  1. Hello. So I have class called CoinsAPI:

    Code (Text):
    package us.universalpvp.te.utils;

    import java.util.HashMap;
    import java.util.UUID;

    import org.bukkit.entity.Player;

    public class CoinsAPI {

        private HashMap<UUID, Integer> coins = new HashMap<>();

        public void addCoins(Player p, int amount) {
            coins.put(p.getUniqueId(), amount);
        }

        public void removeCoins(Player p, int amount) {
            coins.put(p.getUniqueId(), coins.get(p.getUniqueId()) - amount);
        }

        public int getCoins(Player p) {
            if (!coins.containsKey(p.getUniqueId())) {
                return 0;
            } else {
                int i = coins.get(p.getUniqueId());
                return i;
            }

        }
    }
     
    I also have a team in my scoreboard class:

    Code (Text):
    coinsTeam.setPrefix(ChatColor.WHITE + "Coins: ");
                    coinsTeam.setSuffix(ChatColor.GREEN + "" + i);
                    coinsTeam.addEntry(ChatColor.BLACK.toString());

                    obj.getScore(ChatColor.BLACK.toString()).setScore(8);
    with int
    Code (Text):
        private int i;
     
    and in my PLAYER JOIN EVENT:

    Code (Text):
    CoinsAPI ca = new CoinsAPI();
            i = ca.getCoins(p);

            ca.addCoins(p, 100);
    but on the scoreboard, the coins team still remains on 0 when I go on my network. Why is that?
     
  2. Why not change that to return coins.get(p.getUniqueId()); ?
     
  3. That's literally the same thing tho. xD
     
  4. You're basicly doing this sometimes:
    String example = "Test";
    return example;

    While you could do:
    return "Test";

    Same goes for ints:
    int example = map.getInt(player);
    return example;

    While you could do:
    return map.getInt(player);
     
  5. Still remains as 0.
     
  6. @avighnash Mind showing all the code at:
    Code (Java):
    CoinsAPI ca = new CoinsAPI();
            i = ca.getCoins(p);

            ca.addCoins(p, 100);
    ?
     
  7. Code (Text):
        @EventHandler
        public void onPlayerJoinScoreboard(PlayerJoinEvent e) {

            Player p = e.getPlayer();

            String primaryGroup = Vault.getChat().getPrimaryGroup(p);
            if (primaryGroup.equalsIgnoreCase("default")) {

            } else if (primaryGroup.equalsIgnoreCase("default")) {
                rankName = (ChatColor.translateAlternateColorCodes('&', "&7Default "));

            } else if (primaryGroup.equalsIgnoreCase("Owner")) {
                rankName = (ChatColor.translateAlternateColorCodes('&', "&3[Owner]&3 "));

            } else if (primaryGroup.equalsIgnoreCase("Admin")) {
                rankName = (ChatColor.translateAlternateColorCodes('&', "&c[Admin]&c "));

            } else if (primaryGroup.equalsIgnoreCase("Mod")) {
                rankName = (ChatColor.translateAlternateColorCodes('&', "&2[Mod]&2 "));

            } else if (primaryGroup.equalsIgnoreCase("Helper")) {
                rankName = (ChatColor.translateAlternateColorCodes('&', "&1[Helper]&1 "));

            } else if (primaryGroup.equalsIgnoreCase("Build")) {
                rankName = (ChatColor.translateAlternateColorCodes('&', "&b[Builder]&b "));

            } else if (primaryGroup.equalsIgnoreCase("CoOwner")) {
                rankName = (ChatColor.translateAlternateColorCodes('&', "&e[Co-Owner]&e "));
            }

            world = p.getWorld().getName().replace("Hub", "Lobby");

            CoinsAPI ca = new CoinsAPI();
            i = ca.getCoins(p);

            ca.addCoins(p, 100);

            p.setScoreboard(setHubBoard());

        }
     
  8. @avighnash my guess is that you assign a different scoreboard to everyone online. Mind showing the setHubBoard() method?
     
  9. That's classified :/ cannot show that. But I can show the coins team... and yes, everyone online gets a different scoreboard, regarding stats and coins.
    Code (Text):
    final Team coinsTeam = sco.registerNewTeam("Coins");

    coinsTeam.setPrefix(ChatColor.WHITE + "Coins: ");
                    coinsTeam.setSuffix(ChatColor.GREEN + "" + i);
                    coinsTeam.addEntry(ChatColor.BLACK.toString());

                    obj.getScore(ChatColor.BLACK.toString()).setScore(8);
     
  10. You're creating a new instance of CoinsAPI each time.
     
    • Agree Agree x 1
  11. Ohh.. so what should I do so I can fix that? Should I make the methods static?
     
  12. @avighnash you could, although that is not the proper way. You should keep a single CoinsAPI field which you reference every time
     
  13. So basically like this:

    Code (Text):
    CoinsAPI ca = new CoinsAPI();
            i = ca.getCoins(p);

            CoinsAPI addCoins = new CoinsAPI();
            addCoins.addCoins(p, 100);
     
  14. Okay. So it still does not work:
    Here is my player join event:

    Code (Text):
    @EventHandler
        public void onPlayerJoinScoreboard(PlayerJoinEvent e) {

            Player p = e.getPlayer();

            String primaryGroup = Vault.getChat().getPrimaryGroup(p);
            if (primaryGroup.equalsIgnoreCase("default")) {

            } else if (primaryGroup.equalsIgnoreCase("default")) {
                rankName = (ChatColor.translateAlternateColorCodes('&', "&7Default "));

            } else if (primaryGroup.equalsIgnoreCase("Owner")) {
                rankName = (ChatColor.translateAlternateColorCodes('&', "&3[Owner]&3 "));

            } else if (primaryGroup.equalsIgnoreCase("Admin")) {
                rankName = (ChatColor.translateAlternateColorCodes('&', "&c[Admin]&c "));

            } else if (primaryGroup.equalsIgnoreCase("Mod")) {
                rankName = (ChatColor.translateAlternateColorCodes('&', "&2[Mod]&2 "));

            } else if (primaryGroup.equalsIgnoreCase("Helper")) {
                rankName = (ChatColor.translateAlternateColorCodes('&', "&1[Helper]&1 "));

            } else if (primaryGroup.equalsIgnoreCase("Build")) {
                rankName = (ChatColor.translateAlternateColorCodes('&', "&b[Builder]&b "));

            } else if (primaryGroup.equalsIgnoreCase("CoOwner")) {
                rankName = (ChatColor.translateAlternateColorCodes('&', "&e[Co-Owner]&e "));
            }

            world = p.getWorld().getName().replace("Hub", "Lobby");

            i = CoinsAPI.getCoins(p);

            CoinsAPI.addCoins(p, 100);

            p.setScoreboard(setHubBoard());

        }
    the scoreboard team remains the same..


    here is the coinsapi class:

    Code (Text):
    package us.universalpvp.te.utils;

    import java.util.HashMap;
    import java.util.UUID;

    import org.bukkit.entity.Player;

    public class CoinsAPI {

        private static HashMap<UUID, Integer> coins = new HashMap<>();

        public static void addCoins(Player p, int amount) {
            coins.put(p.getUniqueId(), amount);
        }

        public static void removeCoins(Player p, int amount) {
            coins.put(p.getUniqueId(), coins.get(p.getUniqueId()) - amount);
        }

        public static int getCoins(Player p) {
            if (!coins.containsKey(p.getUniqueId())) {
                return 0;
            } else {
                return coins.get(p.getUniqueId());
            }

        }

    }
     
  15. And what I mean by "does not work", is that the coins remain at 0.
     
  16. @avighnash You assign a unique scoreboard to each online players.

    Each scoreboard has its own set of scores. So, if you set only the score of the player, they won't see other people's scores. You need to either keep a global scoreboard or update the score for each player
     
  17. I will just show my scoreboard class:

    Code (Text):
    package us.universalpvp.te.scoreboard;

    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.entity.PlayerDeathEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.Team;

    import us.universalpvp.te.MainClass;
    import us.universalpvp.te.utils.CoinsAPI;
    import us.universalpvp.te.utils.ColorScroller;
    import us.universalpvp.te.utils.Vault;

    @SuppressWarnings("deprecation")
    public class HubScoreboard implements Listener {

        public HubScoreboard() {
        }

        private String rankName = " ";
        private int i;
        private String world = " ";

        @EventHandler
        public void onPlayerKill(PlayerDeathEvent e) {
            int kills = MainClass.getInstance().getConfig().getInt(e.getEntity().getPlayer().getName());
            kills++;
            MainClass.getInstance().getConfig().set(e.getEntity().getKiller().getName(), kills);

            MainClass.getInstance().saveConfig();

        }

        private Scoreboard setHubBoard() {

            Scoreboard sco = MainClass.getInstance().sb;

            final Objective obj = sco.registerNewObjective("the hubboard", "dummy");

            new BukkitRunnable() {

                final Team playersTeam = sco.registerNewTeam("Player");
                final Team coinsTeam = sco.registerNewTeam("Coins");
                final ColorScroller displayName = new ColorScroller(ChatColor.AQUA, " UniversalPvP", "", "ยงf", "", true,
                        false, ColorScroller.ScrollType.FORWARD);

                @Override
                public void run() {

                    obj.setDisplayName(displayName.next());
                    obj.setDisplaySlot(DisplaySlot.SIDEBAR);

                    String s = ChatColor.translateAlternateColorCodes('&', "&9&luniversalpvp.us");

                    Score space = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.BLUE.toString()));
                    space.setScore(12);

                    Score rank = obj.getScore("Rank:");
                    rank.setScore(11);

                    Score rankNameDisplay = obj.getScore(rankName);
                    rankNameDisplay.setScore(10);

                    Score space4 = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.LIGHT_PURPLE.toString()));
                    space4.setScore(9);

                    coinsTeam.setPrefix(ChatColor.WHITE + "Coins: ");
                    coinsTeam.setSuffix(ChatColor.GREEN + " " + i);
                    coinsTeam.addEntry(ChatColor.BLACK.toString());

                    obj.getScore(ChatColor.BLACK.toString()).setScore(8);

                    obj.getScore(ChatColor.DARK_PURPLE.toString()).setScore(7);

                    playersTeam.setPrefix(ChatColor.WHITE + "Players: ");
                    playersTeam.setSuffix(ChatColor.GREEN + " " + Bukkit.getOnlinePlayers().size());
                    playersTeam.addEntry(ChatColor.RED.toString());

                    obj.getScore(ChatColor.RED.toString()).setScore(6);

                    Score space3 = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN.toString()));
                    space3.setScore(5);

                    Score server = obj.getScore(ChatColor.WHITE + "Server: " + ChatColor.GREEN + world);
                    server.setScore(4);

                    Score space2 = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.YELLOW.toString()));
                    space2.setScore(3);

                    Score line1 = obj.getScore("---------------");
                    line1.setScore(2);

                    Score ip = obj.getScore(s);
                    ip.setScore(1);

                }
            }.runTaskTimer(MainClass.getInstance(), 0, 5);

            return sco;

        }

        @EventHandler
        public void onPlayerJoinScoreboard(PlayerJoinEvent e) {

            Player p = e.getPlayer();

            String primaryGroup = Vault.getChat().getPrimaryGroup(p);
            if (primaryGroup.equalsIgnoreCase("default")) {

            } else if (primaryGroup.equalsIgnoreCase("default")) {
                rankName = (ChatColor.translateAlternateColorCodes('&', "&7Default "));

            } else if (primaryGroup.equalsIgnoreCase("Owner")) {
                rankName = (ChatColor.translateAlternateColorCodes('&', "&3[Owner]&3 "));

            } else if (primaryGroup.equalsIgnoreCase("Admin")) {
                rankName = (ChatColor.translateAlternateColorCodes('&', "&c[Admin]&c "));

            } else if (primaryGroup.equalsIgnoreCase("Mod")) {
                rankName = (ChatColor.translateAlternateColorCodes('&', "&2[Mod]&2 "));

            } else if (primaryGroup.equalsIgnoreCase("Helper")) {
                rankName = (ChatColor.translateAlternateColorCodes('&', "&1[Helper]&1 "));

            } else if (primaryGroup.equalsIgnoreCase("Build")) {
                rankName = (ChatColor.translateAlternateColorCodes('&', "&b[Builder]&b "));

            } else if (primaryGroup.equalsIgnoreCase("CoOwner")) {
                rankName = (ChatColor.translateAlternateColorCodes('&', "&e[Co-Owner]&e "));
            }

            world = p.getWorld().getName().replace("Hub", "Lobby");

            i = CoinsAPI.getCoins(p);

            CoinsAPI.addCoins(p, 100);

            p.setScoreboard(setHubBoard());

        }

    }
     
  18. @avighnash how does that not throw any errors? You keep registering a new objective to the same scoreboard. I believe that should throw an exception if the objective already exists
     
  19. The objective does, I still need to move it out of the method*
     
  20. I I have no words, this thread has so much bad advice it's unreal