HashMap help needed: Every key is the same!

Discussion in 'Plugin Development' started by cakenggt, Mar 23, 2012.

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

    cakenggt

    Like the title says, when I run: generaterelays world 1 I get a list of the keys and values in the resulting hashmap, and all of the keys are the same. Below is my plugin and a log file.

    Code:JAVA
    1. package me.cakenggt.MassRelay;
    2.  
    3.  
    4. import java.io.FileInputStream;
    5. import java.io.FileOutputStream;
    6. import java.i:confused:bjectInputStream;
    7. import java.i:confused:bjectOutputStream;
    8. import java.util.HashMap;
    9. import java.util.Map;
    10.  
    11. import org.bukkit.Location;
    12. import org.bukkit.Material;
    13. import org.bukkit.World;
    14. import org.bukkit.block.Block;
    15. import org.bukkit.command.Command;
    16. import org.bukkit.command.CommandSender;
    17. import org.bukkit.entity.Player;
    18. import org.bukkit.event.EventHandler;
    19. import org.bukkit.event.EventPriority;
    20. import org.bukkit.plugin.java.JavaPlugin;
    21.  
    22. public class MassRelay extends JavaPlugin {
    23. private HashMap<Location, Double> interGalacticRelays = new HashMap<Location, Double>();
    24. private HashMap<Location, Double> intraGalacticRelays = new HashMap<Location, Double>();
    25.  
    26. public void onDisable() {
    27. System.out.println(this + " is now disabled!");
    28. }
    29.  
    30. public void onEnable() {
    31. //interGalacticRelays = load("interGalacticRelays");
    32. //intraGalacticRelays = load("intraGalacticRelays");
    33. System.out.println(this + " is now enabled!");
    34. }
    35.  
    36. @EventHandler(priority = EventPriority.HIGHEST)
    37. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    38. if(cmd.getName().equalsIgnoreCase("generaterelays")){
    39. Player player = null;
    40. if (sender instanceof Player) {
    41. player = (Player) sender;
    42. }
    43. if (player != null) {
    44. sender.sendMessage("this command cannot be run by a player");
    45. return false;
    46. }
    47. if (args.length < 1){
    48. sender.sendMessage("you must specify a world after the command");
    49. }
    50. World world = this.getServer().getWorld(args[0]);
    51. int galaxies = 500;
    52. if (args.length == 2)
    53. galaxies = Integer.parseInt(args[1]);
    54. for (double i = 0; i < galaxies; i ++){
    55. int gRanX = (int)((Math.random() * 59950000) - 29975000);
    56. int gRanZ = (int)((Math.random() * 59950000) - 29975000);
    57. int gY = world.getHighestBlockYAt(gRanX, gRanZ);
    58. Location gRelay = new Location(world, gRanX, gY, gRanZ);
    59. Block block = gRelay.getBlock();
    60. block.setType(Material.BEDROCK);
    61. block.getRelative(1, 0, 0).setType(Material.BEDROCK);
    62. block.getRelative(1, 1, 0).setType(Material.BEDROCK);
    63. block.getRelative(1, 2, 0).setType(Material.BEDROCK);
    64. block.getRelative(1, 3, 0).setType(Material.BEDROCK);
    65. block.getRelative(1, 4, 0).setType(Material.BEDROCK);
    66. block.getRelative(-1, 0, 0).setType(Material.BEDROCK);
    67. block.getRelative(-1, 1, 0).setType(Material.BEDROCK);
    68. block.getRelative(-1, 2, 0).setType(Material.BEDROCK);
    69. block.getRelative(-1, 3, 0).setType(Material.BEDROCK);
    70. interGalacticRelays.put(gRelay, i);
    71. intraGalacticRelays.put(gRelay, i);
    72. System.out.println("Generated intergalactic relay " + i + " at X:" + gRelay.getX() + " Z:" + gRelay.getZ());
    73. for (double j = 1; j < 100; j ++){
    74. int rRanX = (int)((Math.random() * 100000) - 50000);
    75. int rRanZ = (int)((Math.random() * 100000) - 50000);
    76. Location rRelay = gRelay.add(rRanX, world.getHighestBlockYAt(rRanX, rRanZ), rRanZ);
    77. Block rblock = rRelay.getBlock();
    78. rblock.setType(Material.BEDROCK);
    79. rblock.getRelative(1, 0, 0).setType(Material.BEDROCK);
    80. rblock.getRelative(1, 1, 0).setType(Material.BEDROCK);
    81. rblock.getRelative(1, 2, 0).setType(Material.BEDROCK);
    82. rblock.getRelative(1, 3, 0).setType(Material.BEDROCK);
    83. rblock.getRelative(1, 4, 0).setType(Material.BEDROCK);
    84. rblock.getRelative(-1, 0, 0).setType(Material.BEDROCK);
    85. rblock.getRelative(-1, 1, 0).setType(Material.BEDROCK);
    86. rblock.getRelative(-1, 2, 0).setType(Material.BEDROCK);
    87. rblock.getRelative(-1, 3, 0).setType(Material.BEDROCK);
    88. intraGalacticRelays.put(rRelay, i + (j/100));
    89. double address = i + (j/100);
    90. System.out.println("Generated intragalactic relay " + address + " at X:" + rRelay.getX() + " Z:" + rRelay.getZ());
    91. }
    92. }
    93. for (Map.Entry<Location, Double> entry : intraGalacticRelays.entrySet()) {
    94. System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
    95. }
    96. //save(interGalacticRelays, "interGalacticRelays");
    97. //save(intraGalacticRelays, "intraGalacticRelays");
    98. return true;
    99. }
    100. if(cmd.getName().equalsIgnoreCase("relay")){
    101. Double address = Double.parseDouble(args[0]);
    102. Player player;
    103. if (sender instanceof Player) {
    104. player = (Player) sender;
    105. }
    106. else
    107. return false;
    108. Location playerLoc = player.getLocation();
    109. int interRange = 3800000;
    110. int intraRange = 150000;
    111. if (intraGalacticRelays.containsKey(player.getLocation().add(0, -1, 0).getBlock().getLocation())) {
    112. if (interGalacticRelays.containsKey(player.getLocation().add(0, -1, 0).getBlock().getLocation()) && address%1 == 0){
    113. for (Location relay : interGalacticRelays.keySet()) {
    114. if (relay.distance(playerLoc) <= interRange && interGalacticRelays.get(relay) == address){
    115. player.teleport(relay.add(0, 1, 0));
    116. return true;
    117. }
    118. }
    119. }
    120. for (Location relay : intraGalacticRelays.keySet()) {
    121. if (relay.distance(playerLoc) <= intraRange && intraGalacticRelays.get(relay) == address){
    122. player.teleport(relay.add(0, 1, 0));
    123. return true;
    124. }
    125. }
    126. }
    127. if (player.isOp()){
    128. for (Location relay : intraGalacticRelays.keySet()) {
    129. System.out.println(relay);
    130. System.out.println(intraGalacticRelays.get(relay));
    131. System.out.println(address);
    132. if (intraGalacticRelays.get(relay) == address){
    133. player.teleport(relay.add(0, 1, 0));
    134. return true;
    135. }
    136. }
    137. }
    138. player.sendMessage("Sorry, but that relay is out of range.");
    139. return true;
    140. }
    141.  
    142. if(cmd.getName().equalsIgnoreCase("chart")){
    143. //Double address = Double.parseDouble(args[0]);
    144. sender.sendMessage("chart command not coded yet");
    145. return true;
    146. }
    147.  
    148. if(cmd.getName().equalsIgnoreCase("nearestrelay")){
    149. Player player;
    150. if (sender instanceof Player) {
    151. player = (Player) sender;
    152. }
    153. else
    154. return false;
    155. Location playerLoc = player.getLocation();
    156. double radius = Double.parseDouble(args[0]);
    157. for (Location relay : intraGalacticRelays.keySet()) {
    158. if (relay.distance(playerLoc) <= radius){
    159. player.sendMessage("Relay " + intraGalacticRelays.get(relay) + " at X:" + relay.getX() + " Z:" + relay.getZ());
    160. }
    161. }
    162. return true;
    163. }
    164. return false;
    165. }
    166.  
    167. public void save(HashMap<Location,Double> relay, String path)
    168. {
    169. try{
    170. oos.writeObject(relay);
    171. oos.flush();
    172. oos.close();
    173. //Handle I/O exceptions
    174. }catch(Exception e){
    175. e.printStackTrace();
    176. }
    177. }
    178.  
    179. @SuppressWarnings("unchecked")
    180. public HashMap<Location,Double> load(String path) {
    181. try{
    182. Object result = ois.readObject();
    183. //you can feel free to cast result to HashMap<Player,Boolean> if you know there's that HashMap in the file
    184. return (HashMap<Location,Double>)result;
    185. }catch(Exception e){
    186. System.out.println("There is no file named " + path);
    187. e.printStackTrace();
    188. return new HashMap<Location, Double>();
    189. }
    190. }
    191. }


    And here is the log file I get from starting the server and running generaterelays world 1

    Log file too long, uploaded to pastebin
     
  2. Offline

    Sir Savary

    Please change syntax to syntax=java
     
  3. Offline

    cakenggt

    How do I do that? On the code screen I only see PHP and HTML options.
     
  4. Offline

    Sir Savary

    Edit the post, look for the BB tags. Should see CODE tags. Change those to SYNTAX=JAVA and /SYNTAX
     
  5. Offline

    cakenggt

    Allright, should be fixed now. Thanks, I never knew that you could do that before.
     
  6. Offline

    Sir Savary

    I wish they would add a button for it, it makes it so much easier to read. I will look through the code now, see what I can do.

    Alright, going to hate me for this, but, could you please, split this up into some more classes? Make it bit easier to read? And on top of that, I will be able to help you better if I know what you are trying to accomplish.

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

    cakenggt

    I have included the only relevant section of code below. The HashMap creation isn't in this section of code, however I included it at the top so that you would know the structure of the hashmaps that are used in the command. The command, when given by the console, creates x clusters of 100 teleport pads across the map, where x is specified in the command given. These locations for the teleport pads are given names. The location of the pad and it's name (a double value) are stored in two hashmaps. The problem that I am running into is that the hashmaps are for some reason filled with the last teleport location calculated. Could this be because of my using a variable name for the location in this line of code: interGalacticRelays.put(gRelay, i); ? If so, how can I put the location in a hashmap without using a variable? Should I change the line of code to: interGalacticRelays.put(new Location(world, gRanX, gY, gRanZ), i); ?

    Code:JAVA
    1.  
    2. private HashMap<Location, Double> interGalacticRelays = new HashMap<Location, Double>();
    3. private HashMap<Location, Double> intraGalacticRelays = new HashMap<Location, Double>();
    4.  
    5. if(cmd.getName().equalsIgnoreCase("generaterelays")){
    6. Player player = null;
    7. if (sender instanceof Player) {
    8. player = (Player) sender;
    9. }
    10. if (player != null) {
    11. sender.sendMessage("this command cannot be run by a player");
    12. return false;
    13. }
    14. if (args.length < 1){
    15. sender.sendMessage("you must specify a world after the command");
    16. }
    17. World world = this.getServer().getWorld(args[0]);
    18. int galaxies = 500;
    19. if (args.length == 2)
    20. galaxies = Integer.parseInt(args[1]);
    21. for (double i = 0; i < galaxies; i ++){
    22. int gRanX = (int)((Math.random() * 59950000) - 29975000);
    23. int gRanZ = (int)((Math.random() * 59950000) - 29975000);
    24. int gY = world.getHighestBlockYAt(gRanX, gRanZ);
    25. Location gRelay = new Location(world, gRanX, gY, gRanZ);
    26. Block block = gRelay.getBlock();
    27. block.setType(Material.BEDROCK);
    28. block.getRelative(1, 0, 0).setType(Material.BEDROCK);
    29. block.getRelative(1, 1, 0).setType(Material.BEDROCK);
    30. block.getRelative(1, 2, 0).setType(Material.BEDROCK);
    31. block.getRelative(1, 3, 0).setType(Material.BEDROCK);
    32. block.getRelative(1, 4, 0).setType(Material.BEDROCK);
    33. block.getRelative(-1, 0, 0).setType(Material.BEDROCK);
    34. block.getRelative(-1, 1, 0).setType(Material.BEDROCK);
    35. block.getRelative(-1, 2, 0).setType(Material.BEDROCK);
    36. block.getRelative(-1, 3, 0).setType(Material.BEDROCK);
    37. interGalacticRelays.put(gRelay, i);
    38. intraGalacticRelays.put(gRelay, i);
    39. System.out.println("Generated intergalactic relay " + i + " at X:" + gRelay.getX() + " Z:" + gRelay.getZ());
    40. for (double j = 1; j < 100; j ++){
    41. int rRanX = (int)((Math.random() * 100000) - 50000);
    42. int rRanZ = (int)((Math.random() * 100000) - 50000);
    43. Location rRelay = gRelay.add(rRanX, world.getHighestBlockYAt(rRanX, rRanZ), rRanZ);
    44. Block rblock = rRelay.getBlock();
    45. rblock.setType(Material.BEDROCK);
    46. rblock.getRelative(1, 0, 0).setType(Material.BEDROCK);
    47. rblock.getRelative(1, 1, 0).setType(Material.BEDROCK);
    48. rblock.getRelative(1, 2, 0).setType(Material.BEDROCK);
    49. rblock.getRelative(1, 3, 0).setType(Material.BEDROCK);
    50. rblock.getRelative(1, 4, 0).setType(Material.BEDROCK);
    51. rblock.getRelative(-1, 0, 0).setType(Material.BEDROCK);
    52. rblock.getRelative(-1, 1, 0).setType(Material.BEDROCK);
    53. rblock.getRelative(-1, 2, 0).setType(Material.BEDROCK);
    54. rblock.getRelative(-1, 3, 0).setType(Material.BEDROCK);
    55. intraGalacticRelays.put(rRelay, i + (j/100));
    56. double address = i + (j/100);
    57. System.out.println("Generated intragalactic relay " + address + " at X:" + rRelay.getX() + " Z:" + rRelay.getZ());
    58. }
    59. }
    60. for (Map.Entry<Location, Double> entry : intraGalacticRelays.entrySet()) {
    61. System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
    62. }
    63. //save(interGalacticRelays, "interGalacticRelays");
    64. //save(intraGalacticRelays, "intraGalacticRelays");
    65. return true;
    66. }
     
  8. Offline

    JayEffKay

    Yep, that should fix it. Right now you're just putting a reference to the gRelay object as key in the hashmap (that's just how Java works), you've gotta make sure every key is a new object. I think gRelay.clone() should work as well.
    But I'm with Savary and suggest you rethink you data structures and try a bit more object oriented approach. At least think about making a separate class for your GalacticRelays.
     
Thread Status:
Not open for further replies.

Share This Page