Solved Teams aren't working

Discussion in 'Plugin Development' started by cheeseypotato, Oct 27, 2019.

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

    cheeseypotato

    Hello people,
    I am trying to make a command that when run adds the player who executed the command to the team they want - eg: /jointeam blue. But my teams aren't working at all and it's like they don't even exist. For example, when two players join team blue and friendly fire is disabled, then can still damage as if they weren't added to the team at all. Can anyone help me?

    Command Class

    Code:
    package me.billycombo.commands;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;
    import org.bukkit.scoreboard.Team;
    
    public class joinTeam implements CommandExecutor, Listener {
    
        public String cmd2 = "jointeam";
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equalsIgnoreCase(cmd2)) {
                if (args.length != 1) {
                    sender.sendMessage(ChatColor.RED + "Invalid usage, please try again using this format - /jointeam <team>");
                    return true;
                }else {
                    if (sender instanceof Player) {
                       
                        if (args[0].equalsIgnoreCase("red")) {
                            ScoreboardManager sbManager = Bukkit.getScoreboardManager();
                            Scoreboard sBoard = sbManager.getNewScoreboard();
                       
                            org.bukkit.scoreboard.Team Red = sBoard.registerNewTeam("Red");
                            //final Team Blue = sBoard.registerNewTeam("Blue");
                           
                            Player player = (Player) sender;
                           
                            Red.addEntry(player.getName());
                            //Blue.removeEntry(player.getName());
                           
                            sender.sendMessage("You have joined " + ChatColor.RED + "RED " + ChatColor.WHITE + "team!");
                            return true;
                        } else {
                            if (args[0].equalsIgnoreCase("blue")) {
                                ScoreboardManager sbManager = Bukkit.getScoreboardManager();
                                Scoreboard sBoard = sbManager.getNewScoreboard();
                           
                                Team Blue = sBoard.registerNewTeam("Blue");
                                //Team Red = sBoard.registerNewTeam("Red");
                               
                                Player player = (Player) sender;
                               
                                Blue.addEntry(player.getName());
                                Blue.setAllowFriendlyFire(false);
                                //Red.removeEntry(player.getName());
                               
                                sender.sendMessage("You have joined " + ChatColor.BLUE + "BLUE " + ChatColor.WHITE + "team!");
                               
                            } else {
                                sender.sendMessage(ChatColor.RED + args[0] + " is an invalid team, please choose from either red or blue.");
                                return true;
                            }
                        }
                    } else {
                        sender.sendMessage(ChatColor.RED + "Only players can use this command!");
                        return true;
                    }
                       
                       
                       
                        return true;
                }
            }
            return true;
        }
    }
    Main

    Code:
    package me.billycombo.Main;
    
    import org.bukkit.Bukkit;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;
    import org.bukkit.scoreboard.Team;
    
    import me.billycombo.Main.events.Snowballs;
    import me.billycombo.commands.BlueSpawnSet;
    import me.billycombo.commands.joinTeam;
    import net.md_5.bungee.api.ChatColor;
    
    public class Main extends JavaPlugin implements Listener {
       
        private Snowballs event = new Snowballs(this);
        private joinTeam event2 = new joinTeam();
       
    @Override
    public void onEnable(){
           
            this.getCommand("setbluespawn").setExecutor(new BlueSpawnSet(this));
            this.getCommand("jointeam").setExecutor(new joinTeam());
            getServer().getPluginManager().registerEvents(event, this);
            getServer().getPluginManager().registerEvents(event2, this);
            this.saveDefaultConfig();
            saveConfig();
           
            ScoreboardManager sbManager = Bukkit.getScoreboardManager();
            Scoreboard sBoard = sbManager.getNewScoreboard();
           
            Team Blue = sBoard.registerNewTeam("Blue");
            Team Red = sBoard.registerNewTeam("Red");
           
            Red.setPrefix(ChatColor.RED + "[RED TEAM] " + ChatColor.WHITE);
            Blue.setPrefix(ChatColor.BLUE + "[BLUE TEAM] " + ChatColor.WHITE);
           
            //Red.setAllowFriendlyFire(false);
            //Red.setNameTagVisibility(NameTagVisibility.HIDE_FOR_OTHER_TEAMS);
              Blue.setAllowFriendlyFire(false);
            //Blue.setNameTagVisibility(NameTagVisibility.HIDE_FOR_OTHER_TEAMS);
           
        }
    
    public void onDisable() {
        this.saveConfig();
    }
    
    }
     
  2. You create two teams in the onEnable but you add them to a new Scoreboard which is never used again. Same thing in the command class: you create a new scoreboard for each player. Of course that wont work because they all need to be in the same scoreboard
     
  3. Offline

    cheeseypotato

    Sorry, can you be more clear on what I have done that is wrong and what I need to do? :)
     
  4. You create multiple scoreboards with multiple teams (getNewScoreboard()). But you want all the teams to be in the same Scoreboard so you can use the Main Scoreboard (getMainScoreboard()) or you have to somehow save the instance of your new scoreboard and add all the teams to that one. That means you also have to register the teams once, not every time someone executes the command
    You also have to add each player to his scoreboard using player.setScoreboard(Scoreboard)
     
  5. Offline

    cheeseypotato

    Got it to work thank you so much!
     
Thread Status:
Not open for further replies.

Share This Page