Teleporting players in hashmaps to individual locations.

Discussion in 'Plugin Development' started by Woobie, Oct 9, 2012.

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

    Woobie

    Here is part of my code.. this works, but doesnt work how i want it to :)
    I'm trying to add different spawn location for players in 2 different hasmaps..
    Code:
          if (args[0].equalsIgnoreCase("bluespawn")) {
                    if(p.hasPermission("mccod.setspawn")){
                            Location loc = p.getLocation();
                            if(!(warpCounter > 100)){
                                warpLocations[warpCounter] = loc;
                                warpName[warpCounter] = args[0];
                                warpCounter++;
                                p.sendMessage(ChatColor.GREEN+ "Blue team spawn set!");
                            } else {
                                p.sendMessage(ChatColor.RED+ "Limit exceeded!");
                            }
                    }
                }
           
            if (args[0].equalsIgnoreCase("start")) {
                for(int i = 0; i < warpName.length; i++){
                    for(Player player : Bukkit.getOnlinePlayers()){
                        Location warpLocation = warpLocations[i];
                        if(blue.contains(player.getName())){
                            player.teleport(warpLocation);
                    }
                       
                        if(red.contains(player.getName())){
                            player.teleport(warpLocation);
                       
                }
            }
        }
    }
    Obviously that teleports both of the "teams" to the same place.
     
  2. Offline

    andf54

    String name = ...;
    HashMap<String, ArrayList<Location>> spawn = ...;
    Random rand = ...;

    // Adding a spawn location for a given team:
    ArratList<Location> teamSpawns = spawn.get(teamName);
    if(teamSpawn = null){
    teamSpawn = new ArrayList<Location>();
    spawn.put(teamName);
    }
    teamSpawn.add(newSpawnLocation);

    // Getting a random spawn location for a given team:
    teamSpawns = spawn.get(teamName);
    if(teamSpawns = null) teamSpawns = new ArrayList<Location>();
    int index = rand.nextInt(teamSpawns.size());
    Location spawnLocation = teamSpawns.get(index);
     
  3. Offline

    Woobie

    I dont want random spawn locations, if you have a look at my code, you see i'm defining the spawn location with a command.
    So wherever i'm standing, it will set a warp there, which i am going to use for the team spawns.
     
  4. Offline

    andf54

    Your code is very messy, it is hard to understand what you want.

    Use "HashMap<teamName, spawnLocation> spawnLocations" then.

    Location loc;
    if(blue.contains(player.getName())) loc = spawnLocations.get("blue");
     
  5. Offline

    Woobie

    My code is always messy :)
    Also, thats still not what i'm asking.

    Lets say i have 2 teams, red and blue.
    I want to set the red team, and blue team spawn location with a command.



    And when i use this start command, it would teleport the red and blue team to their spawns.

    I have this "start" command set up, but as i said, it teleports both of the teams to same location.

    Code:
     if (args[0].equalsIgnoreCase("start")) {
     
                for(int i = 0; i < warpName.length; i++){
     
                    for(Player player : Bukkit.getOnlinePlayers()){
     
                        Location warpLocation = warpLocations[i];
     
                        if(blue.contains(player.getName())){
     
                            player.teleport(warpLocation); //over here, i would change the "warpLocation" to whatever i defined the blue team spawn.
     
                    }
     
                        if(red.contains(player.getName())){
     
                            player.teleport(warpLocation); //same thing over here.
    
    I could probably just use a config, and set the x, y, z locations, but its easier to get the player location with command and save it as warp.
     
  6. Offline

    Dave_

    Well off the top of my head you could reference your warping plugin you are using and save the location to there as a warp.

    You could also make it yourself, it is actually really easy. You would probably make a HashMap that looks kind of like this:
    Code:
    Map<Integer, Player> teamPlayer = new HashMap<Integer, Player>();
    Then you could do something like this to assign players to teams:
    Code:
                public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
                    int blueTeam = 0;
                    if(label.equalsIgnoreCase("blue")){
                        teamPlayer.put(blueTeam, (Player) sender);
                        sender.sendMessage("You joined the blue team!");
                        return true;
                    }else{
                        return false;
                    }
                }
    Doing that you can tp all players in the blue team to a location from a For statement like this:

    Code:
    for(Player player : teamPlayer.get(blueTeam)){
        player.teleport(warpLocation);
    }

    Hope that helped at least a little bit ;)
     
  7. Offline

    Woobie

    That's what i have atm...

    This
    Code:
    for(Player player : teamPlayer.get(blueTeam)){
        player.teleport(warpLocation);
    }
    is the actual problem.. i am trying to teleport the blue team, and red team to different spawnpoints, but i dont know how to set the locations of the spawnpoints for each team.
    I want them to be different

    Okay people, i made a picture for you, i hope you understand what i want!
    View attachment 11342
    How hard can it be to understand such a simple thing?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  8. Offline

    Dave_

    Let me see more of your source, That way I can review it better. I understand what you are trying to do..
     
  9. Offline

    Gravious

    Code:
    Location redLocation = null; // or have set locations: new Location(world, X, Y, Z);
    Location blueLocation = null;  // or have set locations: new Location(world, 100, 60, 100);
     
    // Setting the location would be like this, obviously:
    public void onCommand(parameters and shit)
    {
        if (args[0].equalsIgnoreCase("setblue"))
            blueLocation = ((Player)sender).getLocation();
        if (args[0].equalsIgnoreCase("setred"))
            redLocation = ((Player)sender).getLocation();
     
        if (args[0].equalsIgnoreCase("start"))
        {
            for (Player p: Bukkit.getOnlinePlayers())
            {
                 if (blueTeam.contains(p))
                     p.teleport(blueLocation);
                 else if (redTeam.contains(p))
                     p.teleport(redLocation);
            }
        }
    }
    
    You mean like that?
    Because to me it seems the answer has been given multiple times already.
     
    Woobie likes this.
  10. Offline

    Woobie

    That looks promising, ill try that tomorrow.
    Thanks!
     
  11. Offline

    Gravious

    Do you want to have multiple spawn locations or just one the members of one team will go to?

    Because I don't really get what you mean with:

    Code:
                            if(!(warpCounter > 100)){
                                warpLocations[warpCounter] = loc;
                                warpName[warpCounter] = args[0];
                                warpCounter++;
                                p.sendMessage(ChatColor.GREEN+ "Blue team spawn set!");
                            } else {
                                p.sendMessage(ChatColor.RED+ "Limit exceeded!");
                            }
    
     
  12. Offline

    Dave_

    That looks very identical to most posts. OP needs to slow down.

    In your code blueTeam and redTeam are HashMaps right?
     
  13. Offline

    Woobie

    Neither do i :D
    It just allows you to set warp where you are spawning, so for example: /setwarp something
    And then you could do /warp something

    Arraylists actually, but i dont understand the difference between them. Both works just the same.
    Also, show me the "identical posts"

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  14. I don't believe he used HashMaps in his code, that's why he used some weird array stuff. That you can also not understand because you can't see the whole source and he didn't tell you what it is:p
     
  15. Offline

    Dave_

    And how are you going about saving the locations so you dont have to keep setting them each reload/restart.

    Ide suggest using YamlConfiguration http://wiki.bukkit.org/Introduction_to_the_New_Configuration

    Oh, yes I saw the square brackets. It confused me because the title of the post is HashMap.

    Well they have some differences.


    With the HashMap you could save a Player object to the team's ID with relative ease. In my opinion anyways.

    Well most the code I've seen does the same thing..

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
    blackwolf12333 likes this.
  16. Offline

    Woobie

    I was using hashmaps earlier, but got some better code from a friend, and he used Arraylists, so i decided to keep it
     
  17. Offline

    Dave_

    Ok Ill adjust to your situation, I thought you wanted to use HashMaps due to the title. If you could better help us to help you some updated code would be nice.
     
  18. Offline

    Woobie

    Cant get on my computer anymore, but i'll post the code here tomorrow.
    WARNING - The code is very messy!
     
  19. Offline

    Dave_

    Alright I'll follow this post, and its ok. I'm sure we can help you clean it up a bit.
     
  20. Offline

    Woobie

    Okay well, here is the main class.
    The purple text is the part where i am trying to set the spawns.
    Code:JAVA
    1.  
    2. package me.woobie.mccod;
    3.  
    4. import java.util.ArrayList;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.Location;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.player.AsyncPlayerChatEvent;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. public class Main extends JavaPlugin implements Listener {
    18.  
    19. public final Location[] warpLocations = new Location[100];
    20. public final String[] warpName = new String[100];
    21. public int warpCounter = 0;
    22.  
    23. public void onEnable() {
    24. getServer().getPluginManager().registerEvents(this, this);
    25. }
    26.  
    27. public ArrayList <String> red = new ArrayList <String> ();
    28. public ArrayList <String> blue = new ArrayList <String> ();
    29.  
    30. @EventHandler
    31. public void onPlayerChat (AsyncPlayerChatEvent e) {
    32. Player p = e.getPlayer();
    33. if (red.contains(p.getName())) {
    34. p.setDisplayName(ChatColor.RED + "[Red] "+ ChatColor.GRAY + p.getName());
    35. p.setPlayerListName(ChatColor.RED+ p.getName());
    36. }
    37. if (blue.contains(p.getName())) {
    38. p.setDisplayName(ChatColor.BLUE + "[Blue] "+ ChatColor.GRAY + p.getName());
    39. p.setPlayerListName(ChatColor.BLUE+ p.getName());
    40. }
    41. }
    42.  
    43. public boolean onCommand (CommandSender sender, Command cmd, String commandLabel, String[] args) {
    44.  
    45. if (!(sender instanceof Player)) {
    46. sender.sendMessage("The console can't use MCCoD!");
    47. return true;
    48. }
    49.  
    50. Player p = (Player) sender;
    51.  
    52. if (cmd.getName().equalsIgnoreCase("mccod")) {
    53. if(args.length == 0){
    54. if(p.hasPermission("mccod.main")){
    55. p.sendMessage("The main command");
    56. }
    57.  
    58. else {
    59. p.sendMessage(ChatColor.GREEN+ "You do not have the correct permissions to use this command");
    60. }
    61. }
    62. }
    63.  
    64.  
    65. // Location blueLocation = null;
    66. // Location redLocation = null;
    67. //
    68. // if (args[0].equalsIgnoreCase("setblue")){
    69. // blueLocation = ((Player)sender).getLocation();
    70. // p.sendMessage(ChatColor.GREEN+ "Blue team spawn set!");
    71. // }
    72. //
    73. // if (args[0].equalsIgnoreCase("setred")){
    74. // redLocation = ((Player)sender).getLocation();
    75. //p.sendMessage(ChatColor.GREEN+ "Red team spawn set!");
    76. // }
    77. //
    78. // if (args[0].equalsIgnoreCase("start")){
    79. // for (Player plr: Bukkit.getOnlinePlayers())
    80. // if (blue.contains(p)){
    81. // plr.teleport(blueLocation);
    82. // }
    83. //
    84. // else if (red.contains(p)){
    85. // plr.teleport(redLocation);
    86. // }
    87. // Bukkit.getServer().broadcastMessage(ChatColor.GREEN+ "The MCCoD game has started!");
    88. // Bukkit.getServer().broadcastMessage(ChatColor.GREEN+ "15 minutes until the next game starts.");
    89.  
    90.  
    91. }
    92. if (args[0].equalsIgnoreCase("info")) {
    93. p.sendMessage(ChatColor.GREEN+ "==========[MCCoD]==========");
    94. p.sendMessage(ChatColor.GREEN+ "Name: " +ChatColor.BLUE+ "MCCoD");
    95. p.sendMessage(ChatColor.GREEN+ "Version: " +ChatColor.BLUE+ "v0.1");
    96. p.sendMessage(ChatColor.GREEN+ "Description: " +ChatColor.BLUE+ "Call of Duty in Minecraft!");
    97. p.sendMessage(ChatColor.GREEN+ "/mccod info2" +ChatColor.BLUE+ "For the next page");
    98. p.sendMessage(ChatColor.GREEN+ "==========[Page 1/2]==========");
    99.  
    100. }
    101. if (args[0].equalsIgnoreCase("info2")) {
    102. p.sendMessage(ChatColor.GREEN+ "==========[MCCoD]==========");
    103. p.sendMessage(ChatColor.GREEN+ "Author: " +ChatColor.BLUE+ "Woobie");
    104. p.sendMessage(ChatColor.GREEN+ "Dev page: " +ChatColor.BLUE+ "Coming soon");
    105. p.sendMessage(ChatColor.GREEN+ "Source code: " +ChatColor.BLUE+ "Coming soon");
    106. p.sendMessage(ChatColor.GREEN+ "==========[Page 2/2]==========");
    107.  
    108. }
    109.  
    110. if (args[0].equalsIgnoreCase("red")) {
    111. if (!(blue.contains(p.getName()))) {
    112. red.add(p.getName());
    113. p.sendMessage(ChatColor.GREEN+ "You have joined the " + ChatColor.RED + "red" + ChatColor.GREEN + " team.");
    114. return true;
    115. }
    116. else p.sendMessage(ChatColor.GREEN+ "You are already on the" + ChatColor.BLUE + " blue " + ChatColor.GREEN+ "team!");
    117. return true;
    118. }
    119.  
    120. if (args[0].equalsIgnoreCase("blue")) {
    121. if (!(red.contains(p.getName()))) {
    122. blue.add(p.getName());
    123. p.sendMessage(ChatColor.GREEN+ "You have joined the " + ChatColor.BLUE + "blue" + ChatColor.GREEN+ " team.");
    124. return true;
    125. }
    126. else p.sendMessage(ChatColor.GREEN+ "You are already on the" + ChatColor.RED + " red " + ChatColor.GREEN + "team!");
    127. }
    128. if (args[0].equalsIgnoreCase("leave")) {
    129. if (red.contains(p.getName())) red.remove(p.getName());
    130. if (blue.contains(p.getName())) blue.remove(p.getName());
    131. else {
    132. p.sendMessage(ChatColor.GREEN+ "You never joined a team!");
    133. return true;
    134. }
    135. p.sendMessage(ChatColor.GREEN + "You have left.");
    136. p.setDisplayName(ChatColor.RESET + p.getName());
    137. p.setPlayerListName(ChatColor.RESET+ p.getName());
    138. }
    139. return false;
    140. }
    141. {
    142. }
    143. {
    144. {
    145. }
    146. }
    147. }
    148.  
     
Thread Status:
Not open for further replies.

Share This Page