Deleting scorebored team

Discussion in 'Spigot Plugin Development' started by Technerder, Apr 27, 2017.

  1. how would I go about getting the team a player is in, removing them from the team and then deleting the team?
     
  2. Could we see the code?
     
  3. Code (Text):
    package technerder.kurion.net;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;
    import org.bukkit.scoreboard.Team;

    public class Main extends JavaPlugin {

        public void TeamsCommand(CommandSender sender, Command cmd, String label, String[] args) {

            if (label.equalsIgnoreCase("teams")) {

                if (args[0].equalsIgnoreCase("create")) {

                    ScoreboardManager Manager = Bukkit.getScoreboardManager();
                    Scoreboard board = Manager.getNewScoreboard();
                    Team team = board.registerNewTeam(args[1]);
                    team.addEntry(sender.getName());
                    team.setAllowFriendlyFire(false);

                } else {
                   
                    if (args[0].equalsIgnoreCase("disband")) {
                       
                        String team = sender.getName();
                       
                        team.unregister();

                    } else {

                        if (args[0].equalsIgnoreCase("join")) {

                        } else {

                            if (args[0].equalsIgnoreCase("leave")) {

                            }
                        }
                    }
                }
            }
        }
    }
     

  4. Code (Text):
    if (args[0].equalsIgnoreCase("disband")) {
                       
                        String team = sender.getName();
                       
                        team.unregister();
    I would check to make sure you are using the correct class imports for #getName(). You may be grabbing the command sender's name instead of using the org.bukkit.scoreboard #getName() method.
     
  5. You are trying to unregister a string... What..

    Of course that won't work. A String is not a instance of Team.
     
  6. They're right. I think it's more of something like this.
    - Get Player's Scoreboard
    - Get teams of that scoreboard
    - Clear(Delete) those teams

    First what we can do is set a variable equal to the object scoreboard
    Code (Text):
    Scoreboard sb = ((Player) sender).getScoreboard();
    (Of course, the player part changes depending on where you're putting this. If it's onCommand than using this would be ideal.)

    Then you just grab the teams using the #getTeams method from that variable and finally clear them.
     
    • Agree Agree x 1
  7. To delete a team is to get the Scoreboard like
    ScoreboardManager manager = Bukkit.getScoreboardManager();
    Scoreboard board = manager.getMainScoreboard();

    You can use it like this.
    board.getTeam("teamname").unregister();
     
  8. ok, so
    Code (Text):
    Scoreboard scoreboard = ((Team) sender).getScoreboard();
                        scoreboard.getTeams();
    and from what https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/scoreboard/Team.html tells me im supposed to do this:
    Code (Text):
    scoreboard.unregister;
    yet it is deprecated. whats is the updated alternative for
    Code (Text):
    scoreboard.unregister;
     
  9. There is not really a updated alternative That is really the best way
    but it is really scoreboard.unregister();

    you put
    scoreboard.unregister;

    but it works when it is deprecated
     
  10. would this work?
    Code (Text):
    ScoreboardManager manager = Bukkit.getScoreboardManager();
                        Scoreboard scoreboard = manager.getMainScoreboard();
                        ((Team) scoreboard).unregister();
     
  11. Try something like this.
    Code (Text):
    Set<Team> team = ((Player) sender).getScoreboard().getTeams();
    for(Team team2 : team) {
        team2.unregister();
    }
    This gets the teams the player is in from (Set<Team>) and assigns each team to a team variable called team2. Then it takes team2 and unregisters each team until the player is no longer in any teams. Update the code if you only want to remove specific teams.
     
  12. sender is a CommandSender you cannot just cast that to a Team. Also you want to unregister the Team not the scoreboard.

    Code (Text):
    Player player = (Player) sender;
    Scoreboard scoreboard = player.getScoreboard();

    for (Team team : scoreboard.getTeams()) {
        team.unregister();
    }  
    Obviously check before casting to player. If you have a specific team you want to unregister it from use scoreboard.getTeam(name) otherwise this will unregister all for that player.
     
  13. @Technerder
    Not Really, you are getting the getMainScoreboard you need to get the Team to unregister
    example:
    Scoreboard board = manager.getMainScoreboard();
    board.getTeam(teamname).unregister();

    to get the player team

    Player player = (Player) sender (if using sender)

    Scoreboard board = manager.getMainScoreboard();
    board.getPlayerTeam(player).unregister();
     
  14. public void for a command?