Find which team the player is & add a score to the team.

Discussion in 'Plugin Development' started by S431, Jun 28, 2017.

Thread Status:
Not open for further replies.
  1. This is my Team method
    Code:
    public class Teams {
    public static ArrayList<String> redTeam = new ArrayList<String>();
    public static ArrayList<String> orangeTeam = new ArrayList<String>();
    public static ScoreboardManager manager = Bukkit.getScoreboardManager();
    public static Scoreboard board = manager.getNewScoreboard();
    public static Team red = board.registerNewTeam("red");
    public static Team orange = board.registerNewTeam("orange");
    public static Objective objective = board.registerNewObjective("Players", "dummy");
    public static void addPlayerRed(Player p){
        if(redTeam.size() == 3){
            p.sendMessage(ChatColor.RED + "The Team is Full!");
        }
        if(redTeam.size() < 3){
            p.sendMessage(ChatColor.BLACK + "Your added to" + ChatColor.RED + "red" + ChatColor.BLACK + "team!");
            redTeam.add(p.getName());
            red.addPlayer(p);
            red.setPrefix(ChatColor.RED + "[redteam]");
            red.setAllowFriendlyFire(false);
            p.setScoreboard(board);
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
        }
    }
    public static void addPlayerOrange(Player p){
        if(orangeTeam.size() == 3){
            p.sendMessage(ChatColor.RED + "The Team is Full!");
        }
        if(orangeTeam.size() < 3){
            p.sendMessage(ChatColor.BLACK + "You're added to" + ChatColor.GOLD + "Orange" + ChatColor.BLACK + "Team!");
            orangeTeam.add(p.getName());
            orange.addPlayer(p);
            orange.setPrefix(ChatColor.GOLD + "[orangeteam]");
            orange.setAllowFriendlyFire(false);
            p.setScoreboard(board);
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
        }
    }
    }
    And this is my main/command class.
    Code:
    public class Main extends JavaPlugin{
        public void onEnable(){
            Logger logger = getLogger();
            PluginDescriptionFile pdfFile = this.getDescription();
            logger.info(pdfFile.getName() +"is enabled");
            getServer().getPluginManager().registerEvents(new PlayerInteract(),this);
        }
    public void onDisable(){
        Logger logger = getLogger();
        PluginDescriptionFile pdfFile = this.getDescription();
        logger.info(pdfFile.getName() + "is disabled");
    }
    public boolean onCommand(CommandSender sender,Command cmd,String label,String[] args){
        Player p = (Player) sender;
        if(label.equalsIgnoreCase("join")){
            if(args.length == 0){
                p.sendMessage(ChatColor.RED + "Please choose a team to join!");
            }
            if(args.length > 0){
                if (args[0].equalsIgnoreCase("join")){
                    if(args[1].equalsIgnoreCase("redteam")){
                        Teams.addPlayerRed(p);
                    }
                    if(args[1].equalsIgnoreCase("orangeteam")){
                        Teams.addPlayerOrange(p);
                    }
                }
            }
        }
        if(label.equalsIgnoreCase("leave")){
            if(Teams.redTeam.contains(p.getName())){
        Teams.redTeam.remove(p.getName());
        Teams.red.removePlayer(p);
        p.sendMessage(ChatColor.BLACK + "You Left Red Team!");
            }
            if(Teams.orangeTeam.contains(p.getName())){
                Teams.orangeTeam.remove(p.getName());
                Teams.orange.removePlayer(p);
                p.sendMessage(ChatColor.BLACK + "You Left Orange Team!");
                    }
            p.sendMessage("You aren't in a team!");
        }
    return true;
    }
    }
    I'd like to make a playerInteract Event and make it so that if a player right-clicks an item,it adds a score to the Player's Team.
     
  2. Online

    timtower Administrator Administrator Moderator

    @S431 Couple things.
    1. Don't log your own plugins. Bukkit does that for you.
    2. Your join command doesn't check if args[1] exists.
    3. Your join command should check cmd.getName() instead of the label.
    4. args[0] would be "redteam" or "orangeteam" in your case, not the command itself.
    5. Don't use public static, you don't need it, you just made a class that Bukkit can't clean.
    6. Start by making the scores.
     
  3. [edit]I just decided to use another method.
     
    Last edited: Jun 30, 2017
Thread Status:
Not open for further replies.

Share This Page