Solved Add team to MYSQL Value

Discussion in 'Spigot Plugin Development' started by DerEyve, Feb 14, 2020 at 6:53 PM.

  1. Hey so im trying to make a NickSystem. It works perfectly but when i try to set the team of my Scoreboard (for the Tablist) it doesnt work
    Here's my Code
    So thats the join listener
    Code (Java):
    @SuppressWarnings("deprecation")
        public void tablist() {
           
            Bukkit.getScheduler().scheduleSyncDelayedTask(Main.getInstance(), new Runnable() {
               
                @Override
                public void run() {
                    for(Player all : Bukkit.getOnlinePlayers()) {
                       
                        Scoreboard sb = Bukkit.getScoreboardManager().getNewScoreboard();
                       
                        sb.registerNewTeam("0000Owner");
                        sb.registerNewTeam("0001Admin");
                        sb.registerNewTeam("0002Developer");
                        sb.registerNewTeam("0003Moderator");
                        sb.registerNewTeam("0004Supporter");
                        sb.registerNewTeam("0005Builder");
                        sb.registerNewTeam("0006Supreme");
                        sb.registerNewTeam("0007Premium");
                        sb.registerNewTeam("0008Player");
                       
                        sb.getTeam("0000Owner").setPrefix("§4Inhaber §8┃ §4");
                        sb.getTeam("0001Admin").setPrefix("§4Admin §8┃ §4");
                        sb.getTeam("0002Developer").setPrefix("§bDev §8┃ §b");
                        sb.getTeam("0003Moderator").setPrefix("§cMod §8┃ §c");
                        sb.getTeam("0004Supporter").setPrefix("§9Sup §8┃ §9");
                        sb.getTeam("0005Builder").setPrefix("§2Builder §8┃ §2");
                        sb.getTeam("0006Supreme").setPrefix("§dSupreme §8┃ §d");
                        sb.getTeam("0007Premium").setPrefix("§6Premium §8┃ §6");
                        sb.getTeam("0008Player").setPrefix("§a");
                       
                        String team = "";
                       
                        if(!MYSQLNick.getNickname(all.getUniqueId()).equals("none")) {
                            all.setPlayerListName(MYSQLNick.getNickname(all.getUniqueId()));
                            return;
                        } else {
                            if(all.hasPermission("jslno.owner")) {
                                team = "0000Owner";
                                all.setPlayerListName(all.getName());
                            } else if(all.hasPermission("jslno.admin")) {
                                team = "0001Admin";
                                all.setPlayerListName(all.getName());
                            } else if(all.hasPermission("jslno.developer")) {
                                team = "0002Developer";
                                all.setPlayerListName(all.getName());
                            } else if(all.hasPermission("jslno.moderator")) {
                                team = "0003Moderator";
                                all.setPlayerListName(all.getName());
                            } else if(all.hasPermission("jslno.supporter")) {
                                team = "0004Supporter";
                                all.setPlayerListName(all.getName());
                            } else if(all.hasPermission("jslno.builder")) {
                                team = "0005Builder";
                                all.setPlayerListName(all.getName());
                            } else if(all.hasPermission("jslno.supreme")) {
                                team = "0006Supreme";
                                all.setPlayerListName(all.getName());
                            } else if(all.hasPermission("jslno.premium")) {
                                team = "0007Premium";
                                all.setPlayerListName(all.getName());
                            } else {
                                team = "0008Player";
                                all.setPlayerListName(all.getName());
                            }
                        }
                        sb.getTeam(team).addPlayer(all);
                        all.setScoreboard(sb);
                    }
                }
            }, 5);
    Im also setting tablist() in Join event:
    Code (Java):
    @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            Player p = (Player) e.getPlayer();
            tablist();
           
            if(MYSQLNick.getNickname(p.getUniqueId()).equals("none")) {
                if(p.hasPermission("jslno.owner")) {
                    e.setJoinMessage("§cJSLNO §8» §a+ §4Inhaber §8┃ §4" + e.getPlayer().getName());
                } else if(p.hasPermission("jslno.admin")) {
                    e.setJoinMessage("§cJSLNO §8» §a+ §4Admin §8┃ §4" + e.getPlayer().getName());
                } else if(p.hasPermission("jslno.developer")) {
                    e.setJoinMessage("§cJSLNO §8» §a+ §bDeveloper §8┃ §b" + e.getPlayer().getName());
                } else if(p.hasPermission("jslno.moderator")) {
                    e.setJoinMessage("§cJSLNO §8» §a+ §cModerator §8┃ §c" + e.getPlayer().getName());
                } else if(p.hasPermission("jslno.supporter")) {
                    e.setJoinMessage("§cJSLNO §8» §a+ §9Supporter §8┃ §9" + e.getPlayer().getName());
                } else if(p.hasPermission("jslno.builder")) {
                    e.setJoinMessage("§cJSLNO §8» §a+ §2Builder §8┃ §2" + e.getPlayer().getName());
                } else if(p.hasPermission("jslno.supreme")) {
                    e.setJoinMessage("§cJSLNO §8» §a+ §dSupreme §8┃ §d" + e.getPlayer().getName());
                } else if(p.hasPermission("jslno.premium")) {
                    e.setJoinMessage("§cJSLNO §8» §a+ §6Premium §8┃ §6" + e.getPlayer().getName());
                } else {
                    e.setJoinMessage("§cJSLNO §8» §a+ " + e.getPlayer().getName());
                }
            } else {
                e.setJoinMessage("§cJSLNO §8» §a+ §6Premium §8┃ §6" + MYSQLNick.getNickname(p.getUniqueId()));
            }
        }
    And here's the MYSQLNick
    Code (Java):
    public class MYSQLNick {
       
        public static void unnick(UUID uuid) {
           
            PreparedStatement ps;
            try {
                ps = (PreparedStatement) NickSQL.con.prepareStatement("UPDATE nickname SET nickname = ? WHERE UUID = ?");
                ps.setString(1, "none");
                ps.setString(2, uuid.toString());
                ps.executeUpdate();
               
               
               
            } catch (SQLException e) {
                e.printStackTrace();
            }
           
        }
       
        public static String getNickname(UUID uuid) {
            try {
                PreparedStatement ps = (PreparedStatement) NickSQL.con.prepareStatement("SELECT nickname FROM nickname WHERE UUID = ?");
                ps.setString(1, uuid.toString());
                ResultSet rs = ps.executeQuery();
                while(rs.next()) {
                    return rs.getString("Nickname");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return "none";
           
        }
       
    Thanks for the help!
     
  2. Strahan

    Benefactor

    What, specifically, do you mean it doesn't work? What happens? I implemented your code with minor changes and it works fine for me. The minor changes aren't what would fix it (removed Main.getInstance() as my test plugin is one class and removed the calls to the MySQL stuff and just made a private String getNickname() { return "none"; } for testing and used getNickname() in lieu of the MySQL call).

    That said, even though it works for me, it's rather inefficient. You should leverage the config to store the data. It's better because 1. you don't need a bazillion setup statements or logic branching and 2. stuff like this should never be hardcoded anyway - what if you change your ranks? Gotta recompile and reinstall. Ick.

    PS - and don't embed the color character. Use ChatColor, that's why it exists.
    PPS - Also I've never really used scoreboards before, so I may just not know what I'm talking about but shouldn't the board and teams be created outside of the method scope? Don't they have to persist for all players, or does that not matter?
     
  3. Hey, thanks for the reply, but i've got it working! I've exported it in JRE-11 instead of JRE-8
     
    • Like Like x 1
  4. Mark as solved
    You can create a scoreboard for each player individually