I've been trying to create a teams plugin where a player creates a team, it adds their name to a list in a config and then that player can invite other players to his team that adds those players to another list. I have that working fine and it works great the only problem I have now is getting the players team names when 2 players hit each other. I'm trying to make it so that when the 2 players hit each other, it checks if they are in the same team and if they are cancel the hit. I'm not sure how to read the list from the config. This is how it shows up in the config LeaderUsername: TeamName: Leaders: - - LeaderUsername Members: - - MemberUsername - - MemberUsername - - MemberUsername TeamName: Test If there is an easier way to setup a config for teams, please please please let me know as this is the first time I have tried making a teams plugin. Here is the code I have currently which works fine. Key Points Getting the teams on hit using the current config layout or a new layout presented by one of you Reading through the list General bug fixes (Listed from most important to least important) Code: public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { FileConfiguration teams = null; File teamsFile = new File("plugins" + File.separator + "PrimitiveTeams" + File.separator + "teams.yml"); teams = YamlConfiguration.loadConfiguration(teamsFile); /* * * if (cmd.getName().equalsIgnoreCase("teams")) { * * Player player = (Player) sender; * * if (args.length == 0) { return false; } * * if (args.length == 1) { if (args[1].equalsIgnoreCase("create")) { if * (args.length == 1) { teams.set(args[1].toString() + "." + player.getName() + * ".Leader", true); * * try { teams.save(teamsFile); } catch (IOException e) { } * * } } } } */ if (cmd.getName().equalsIgnoreCase("teams")) { if (args.length == 0) { sender.sendMessage(ChatColor.RED + "Usage: " + ChatColor.GREEN + "/teams " + ChatColor.RED + "(create, invite) (name)"); return false; } if (args[0].equalsIgnoreCase("create")) { if (args.length == 1) { sender.sendMessage(ChatColor.RED + "Usage: " + ChatColor.GREEN + "/teams " + args[0] + ChatColor.RED + " (name)"); return false; } if (args.length == 2) { Player player = (Player) sender; String teamName = args[1].toString(); if (teams.contains(teamName)) { player.sendMessage(ChatColor.GOLD + "A team by the name, " + ChatColor.YELLOW + teamName + ChatColor.GOLD + " already exists!"); return false; } else { List<String> leaders = new ArrayList<String>(); leaders.add(player.getName()); teams.set(player.getName() + "." + teamName + ".Leaders", Arrays.asList(leaders)); teams.set(player.getName() + ".TeamName", teamName); try { teams.save(teamsFile); } catch (IOException e) { } player.sendMessage(ChatColor.GOLD + "You have successfully created a team named " + ChatColor.YELLOW + teamName + ChatColor.GOLD + "!"); } } } else if (args[0].equalsIgnoreCase("invite")) { Player player = (Player) sender; if (args.length == 1) { player.sendMessage(ChatColor.GOLD + "Please specify the player you want to invite!"); return false; } if (args.length == 2) { Player target = Bukkit.getPlayer(args[1]); String teamName = teams.getString(player.getName() + ".TeamName"); if (teamName == "" || teamName == null) { player.sendMessage(ChatColor.GOLD + "You must first create a team!"); return false; } else { player.sendMessage(ChatColor.GOLD + "You have successfully sent an invite to " + ChatColor.YELLOW + target.getName() + ChatColor.GOLD + "! They have " + ChatColor.YELLOW + "60 seconds" + ChatColor.GOLD + " to accept!"); invitePlayer(target, 60, teamName); } } } else if (args[0].equalsIgnoreCase("accept")) { Player player = (Player) sender; if (args.length == 1) { player.sendMessage(ChatColor.GOLD + "Please specify a team leader's name!"); return false; } else if (args.length == 2) { Player teamLeader = Bukkit.getPlayer(args[1]); String teamName = teams.getString(teamLeader.getName() + ".TeamName"); if (invitedPlayers.containsKey(player.getName())) { // If the player was invited at some point, // // check if the invitation has expired long inviteEnds = invitedPlayers.get(player.getName()); if (inviteEnds >= System.currentTimeMillis()) { // If the invitation is still valid, let him // // join // the game List<String> members = new ArrayList<String>(); members.add(player.getName()); teams.set(teamLeader.getName() + "." + teamName + ".Members", Arrays.asList(members)); try { teams.save(teamsFile); } catch (IOException e) { } player.sendMessage(ChatColor.GOLD + "You have successfully joined the team " + ChatColor.YELLOW + teamName + ChatColor.GOLD + "!"); } else { // If the invitation has expired, tell the player and remove him from the // // invitation list invitedPlayers.remove(player.getName()); player.sendMessage(ChatColor.RED + "You're invitation has expired!"); player.sendMessage(ChatColor.GRAY + "You'll need to get invited again to join the game."); } } else { // If the player hasn't ever received an invite or the last one expired and was // removed, tell him player.sendMessage( ChatColor.RED + "You need to receive an invitation before you can join a game!"); } } } } return false; } Any help is much appreciated, plisov
@plisov You should think about creating class for your teams, or load the config file onEnable. that will be a pain if you load the file everytime players hit each others. Try the event EntityDamageByEntityEvent
Code: package me.plisov.primitiveteams; import java.io.File; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityDamageByEntityEvent; public class PlayerHit implements Listener { @EventHandler public void onPlayerHit(EntityDamageByEntityEvent event) { FileConfiguration teams = null; File teamsFile = new File("plugins" + File.separator + "PrimitiveTeams" + File.separator + "teams.yml"); teams = YamlConfiguration.loadConfiguration(teamsFile); Player damager = (Player) event.getDamager(); Player damagee = (Player) event.getEntity(); if (teams.getStringList(damager.getName()).contains(damagee.getName()) || teams.getStringList(damager.getName()).contains(damagee.getName())) { event.setCancelled(true); } } }
Alright I've done that. Am I doing this correctly because when I go to try it, it still allows me to hit the other player even though we are in the same team Code: package me.plisov.primitiveteams; import java.io.File; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityDamageByEntityEvent; public class PlayerHit implements Listener { @EventHandler public void onPlayerHit(EntityDamageByEntityEvent event) { FileConfiguration teams = null; File teamsFile = new File("plugins" + File.separator + "PrimitiveTeams" + File.separator + "teams.yml"); teams = YamlConfiguration.loadConfiguration(teamsFile); if (event.getDamager() instanceof Player && event.getEntity() instanceof Player) { Player damager = (Player) event.getDamager(); Player damagee = (Player) event.getEntity(); if (teams.getStringList(damager.getName()).contains(damagee.getName()) || teams.getStringList(damager.getName()).contains(damagee.getName())) { event.setCancelled(true); } } } }
But how would I check if their in the same class? I could make a sameTeam method but what would go into it that would check if they're in the same team?
There are multiple teams in the teams.yml. If I do teams.contains it will check if the config contains the players names even if they are in different teams.
Timtower is right. Create a class called team. This is the "Team" object. Initialize it by calling the team name in the Constructor. Once you got that, create a List containing all of the players names in the team object by adding the names into the list. Lastly, create a method to get the team, get the list of players, and check if the players are on the same team. By the way, each time you restart the server, you will need to load the teams up again.
I've gotten to here. How do I get the list from the constructor? Code: package me.plisov.primitiveteams; import java.io.File; import java.util.Arrays; import java.util.List; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; public class Team { public Team(Player player, String teamName) { FileConfiguration teams = null; File teamsFile = new File("plugins" + File.separator + "PrimitiveTeams" + File.separator + "teams.yml"); teams = YamlConfiguration.loadConfiguration(teamsFile); List<String> members = teams.getStringList(player.getName() + "." + teamName + ".Members"); } public void getTeam(Player player, String teamName) { } }
@plisov dont use the player, use UUID instead. You will be in trouble when you will have to load it from config. Put the member list as a field, then create a getter. You wil lbe able to get it with the instance of your class.
Create a GLOBAL variable called "members" in your class that is a List of Strings. Set that equal to the members in your team config. Afterwards, create a getter method that returns the list of players.
@MightyOne I never said to make it public, in fact I should've said to make it private but I forgot. And I should've said to create a list, then add the players individually in case a new player joins so you can also add them too