I'm wanting to make a plugin when a player does /join they will be added to a random team (Red/Blue), i know i will have to make 2 array lists but i don't know how to make it do it only adds the players who have done /join to one of the array list, also i want the teams to be even. Thanks!
This is relatively simple to do, create a command and when they type that command see how many players are on a team and add the player to the list with the fewer team members. if you dont know how to do a command look at this
coobro123 I understand that but from the context of his post it seems he hasn't done programming of the Bukkit api before. The link is to the tutorial page where all the code he will need is explained on that page. there's no point duplicating work
coobro123 NoLiver92 Thanks for your help NoLiver92, coobro123 no i'm not, to sum it all up is how do i get the amount of players and split it if they did the commands /join. I know how to make a command i just don't understand is how do i get the players how did /join (Add them to a arrayList?) then make a random to split them so all i need help with is the random if what i said for /join is correct.
PHILLIPS_71 Oh ok, sorry, your orriginal post was unclear, when the player does the command the event fires, you just have to get the player with event.getplayer(); you dont really need random if you want the teams even the best way would be check which list has the least people and add them to it, then if they are both the same just add the person to the first list. this would change every time depending on when the players did the command. for the lists you will have to define them: (have one for each team) List<String> TeamA = new List<String>(); then adding them should be as simple as: TeamA.add(player.getName()); by splitting them as they do the command into 2 list saves alot of code then having to get all players then split them (uses less ram etc). if you get that working, if its needed you can always go back and add pieces of code later
NoLiver92 Thanks for your help really appreciated! This is what i have so far, Code: List<String> red = new ArrayList<String>(); List<String> blue = new ArrayList<String>(); public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { if(commandLabel.equalsIgnoreCase("join") && args.length == 0) { Player player = (Player)sender; red.add(player.getName()); blue.add(player.getName()); Bukkit.broadcastMessage(ChatColor.YELLOW + "[" + ChatColor.AQUA + "Game" + ChatColor.YELLOW + "] " + ChatColor.DARK_GREEN + player + ChatColor.GREEN + "Joined the game!"); player.sendMessage(ChatColor.GREEN + "You have been added to a team."); } return false; } } Also how would i make it so a player cant just keep doing the command?
Check if a player is inside either the red or blue array list, use red.contains(player) || blue.contains(player). If they are then ignore the rest of the code (use an if statement).
savagesun I hope this is right also here is the plugin.yml then i do /join it does nothing it does not give me any errors at all it does nothing it also does not say unknown command. Code: public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { if(commandLabel.equalsIgnoreCase("join")) { Player player = (Player)sender; if(red.contains(player) || blue.contains(player)) return red.add(player.getName()); blue.add(player.getName()); Bukkit.broadcastMessage(ChatColor.YELLOW + "[" + ChatColor.AQUA + "Game" + ChatColor.YELLOW + "] " + ChatColor.DARK_GREEN + player + ChatColor.GREEN + "Joined the game!"); player.sendMessage(ChatColor.GREEN + "You have been added to a team."); } return false; } Code: name: SCGames main: me.PHILLIPS_71.core version: 1.0 description: SurvivalCraft Mini-Games Server. author: PHILLIPS_71 commands: join: description: Adds you to a game.
It looks right except you are missing the ';' at the end of the return statement. This is most likely your issue, assuming it isn't just a copy / paste error.
savagesun When i add the ; to return it come sup red but is the plugin.yml right? because the /join is not working for some reason
The reason it is red is because you haven't added the curly brackets after the if statement: if(//statement here){//code here} also change the commands to this: Make sure the command is registered in the onEnable() function in your main file
NoLiver92 This is what i have still not working :/ i did what you said to do in the plugin.yml Code: public class Teams extends JavaPlugin implements Listener { @Override public void onEnable() { getServer().getPluginManager().registerEvents(new Teams(), this); } List<String> red = new ArrayList<String>(); List<String> blue = new ArrayList<String>(); public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { if(commandLabel.equalsIgnoreCase("join")) { Player player = (Player)sender; if(red.contains(player) || blue.contains(player)){ return false; } red.add(player.getName()); blue.add(player.getName()); Bukkit.broadcastMessage(ChatColor.YELLOW + "[" + ChatColor.AQUA + "Game" + ChatColor.YELLOW + "] " + ChatColor.DARK_GREEN + player + ChatColor.GREEN + "Joined the game!"); player.sendMessage(ChatColor.GREEN + "You have been added to a team."); } return false; } } Plugin.yml Code: name: SCGames main: me.PHILLIPS_71.core version: 1.0 description: SurvivalCraft Mini-Games Server. author: PHILLIPS_71 commands: join: description: Adds you to a game.
PHILLIPS_71 Firstly you need something like this in onenable(): Code: this.getCommand("guild").setExecutor(new Command(this)); (this is from my own code so change Command to this also the yml thing was messed up by the forum. commands: <2 spaces>join: <4 spaces>description: Adds you to a game. put the number of spaces in as specified above also you need to implement commandExecutor (i think its that) you implemented listeners but not the one for commands
Thanks got it to work, but how can i make it where is says you have joined a team to be lick you have join the red team? and i don't this this is working i added the return; but it was coming up in red and wants me to change it to return false; and is the red.add and blue.add right? Thanks so much for your help much appreciated! NoLiver92 Code: if(red.contains(player) || blue.contains(player)){ return false; } red.add(player.getName()); blue.add(player.getName());
List<Player> join = new ArrayList<Player>(); onCommand method if cmd equals, join Player p = ((Player)sender); join.add(p); then if u want to do more with join start a event or whatever and then do methods with all joined players in it. I am guessing its a pvp sort of thing or mini games.
what you need to do is determine which team to add the player to (can be done with if statements) at the moment your adding the player to both teams, with this it will become easy to send the player a message when they join the team telling them what team they joined. also keep it return true or it will return the usage of the command. coobro123 Do you actually read anything before posting........ If you took some time and care you would know that that bit of basic information was at the top of the posts and we have move past that. Please take the time to see whats been posted before you start posting stuff thats already been said
NoLiver92 Ok thanks, i just don't understand how i i get them to join a team and if the other team has more players it will just add a player to the other team to make it even.
PHILLIPS_71 for the teams get the length of both teams and do something like this: Code: if(blue.length == red.length) { blue.add(playername); } else { /red.add(playerneme); }
It's probably an idea to use Sets rather than Lists, as each player can only be in a team once, and it doesn't need to be ordered. Therefore, instead of using: Code: List<String> red = new ArrayList<String>(); List<String> blue = new ArrayList<String>(); Use: Code: Set<String> red = new HashSet<String>(); Set<String> blue = new HashSet<String>(); Other than that, the rest of the code stays the same as what NoLiver92 posted. Just use .size() to get the size of a collection, length is used in arrays, not ArrayLists or sets. So: Code: if (red.size() > blue.size()) { blue.add(playerName); } else { red.add(playerName); } It should be noted that this puts players in red if both teams are equal, if you want it the other way, simply switch the occurences of red and blue.
Found a flaw with this. When the red team is smaller, they arent equal and the player goes to red and when red is bigger, they arent equal and the player goes to red. It always goes to red unless it is equal so I used this instead: Code: if(blue.size() == red.size()){ blue.add(playername); player.sendMessage("You Joined the BLUE team!"); } else if(blue.size() < red.size()) { blue.add(playername); player.sendMessage("You Joined the BLUE team!"); } else if(red.size() < blue.size()) { red.add(playername); player.sendMessage("You joined the RED team!"); }
ziimzy I actually agree with you, i forgot if a player logs off they wont be in the list anymore so this would cause the flaw. thanks for spotting it