Solved Hub wont check array correctly HELP

Discussion in 'Plugin Development' started by DividedByZero, Jul 31, 2014.

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

    DividedByZero

    when the checkForPlayer(player) is called it is suppost to see if the player is already inside of the arraylist and return false; but for some reason it returns false everytime even tho there is something in the array.
    Code:java
    1. package org.dbz.swordfighttour.Hub;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5. import java.util.List;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.Location;
    9. import org.bukkit.configuration.file.FileConfiguration;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.inventory.ItemStack;
    12. import org.bukkit.plugin.Plugin;
    13. import org.dbz.swordfighttour.DataConfigFile.DataConfigFile;
    14.  
    15. public class Hub {
    16. private Plugin plugin;
    17. public Hub(Plugin plugin) {
    18. this.plugin = plugin;
    19. }
    20.  
    21. public List<Player> players = new ArrayList<Player>();
    22. public HashMap<Player, ItemStack[]> hubinv = new HashMap<Player, ItemStack[]>();
    23. public HashMap<Player, ItemStack[]> hubarmor = new HashMap<Player, ItemStack[]>();
    24. public HashMap<Player, Location> hubloc = new HashMap<Player, Location>();
    25.  
    26. public void setHubSpawn(Player player) {
    27. Location loc = player.getLocation();
    28.  
    29. DataConfigFile dataconfigfile = new DataConfigFile(plugin);
    30. FileConfiguration s = dataconfigfile.getDataConfig();
    31.  
    32. s.set("hub.data.x", loc.getX());
    33. s.set("hub.data.y", loc.getY());
    34. s.set("hub.data.z", loc.getZ());
    35. s.set("hub.data.pitch", loc.getPitch());
    36. s.set("hub.data.yaw", loc.getYaw());
    37. s.set("hub.data.world", loc.getWorld().getName());
    38. dataconfigfile.saveDataConfig();
    39. }
    40.  
    41. public Location getHubSpawn() {
    42. DataConfigFile dataconfigfile = new DataConfigFile(plugin);
    43. FileConfiguration s = dataconfigfile.getDataConfig();
    44. Location l = new Location(
    45. Bukkit.getServer().getWorld(s.getString("hub.data.world")),
    46. s.getDouble("hub.data.x"),
    47. s.getDouble("hub.data.y"),
    48. s.getDouble("hub.data.z"),
    49. (float) s.getDouble("hub.data.pitch"),
    50. (float) s.getDouble("hub.data.yaw")
    51. );
    52. return l;
    53. }
    54.  
    55. public void addPlayer(Player player) {
    56. Location loc = player.getLocation();
    57. ItemStack[] inv = player.getInventory().getContents();
    58. ItemStack[] armor = player.getInventory().getArmorContents();
    59.  
    60. hubinv.put(player, inv);
    61. hubarmor.put(player, armor);
    62. hubloc.put(player, loc);
    63.  
    64. players.add(player);
    65.  
    66. player.teleport(getHubSpawn());
    67. }
    68.  
    69. public void removePlayer(Player player) {
    70. players.remove(player);
    71. restorePlayer(player);
    72. }
    73.  
    74.  
    75. public void restorePlayer(Player player) {
    76. Location loc = hubloc.get(player);
    77. ItemStack[] inv = hubinv.get(player);
    78. ItemStack[] armor = hubarmor.get(player);
    79.  
    80. hubloc.remove(player);
    81. hubinv.remove(player);
    82. hubarmor.remove(player);
    83.  
    84. player.teleport(loc);
    85. player.getInventory().setContents(inv);
    86. player.getInventory().setArmorContents(armor);
    87. }
    88.  
    89. public boolean checkForPlayer(Player player) {
    90. if(players.contains(player)) {
    91. return true;
    92. } else {
    93. return false;
    94. }
    95. }
    96.  
    97. public void toggleAction(Player player) {
    98. if(!(checkForPlayer(player))) {
    99. player.sendMessage("None Found");
    100. players.add(player);
    101. } else {
    102. player.sendMessage("Found");
    103. players.remove(player);
    104. }
    105. }
    106. }
     
  2. Don't store Player objects, it can cause memory leaks if not handled safely. Store their UUID or username.

    Then in checkPlayer():
    return players.contains(player.getName());
     
  3. Offline

    mythbusterma

    DividedByZero

    You might also want to consider using Sets (specifically HashSet) of the player's names as a measure against duplicate entries.
     
  4. Offline

    DividedByZero

    i have tried this with uuid and the contains method isnt working.
    im doing if(players.contains(player.getUniqueId()) {
    return true;
    } else {
    return false;
    }

    and it always return false;
     
  5. Offline

    FabeGabeMC

    DividedByZero
    check if it contains the player's UUID as a string:
    Code:java
    1. if(players.contains(player.getUniqueId().toString())) {
    2. return true;
    3. } else {
    4. return false;
    5. }
     
  6. Offline

    DividedByZero

    FabeGabeMC that wont work. Just tried it still returns true every single time
     
  7. Offline

    FabeGabeMC

    DividedByZero
    Then try checking for the username, since UUID's haven't come into play yet. (name change)
     
  8. Offline

    Necrodoom

    UUIDs work fine, and it won't break how hashmaps work either.
    DividedByZero now it always returns true instead of false?
     
  9. Offline

    DividedByZero

    @FabeGaveMC the way that im checking for a player in the players ArrayList always returns true even when i print it in chat with toString() it ways its empty but when i run sendMessage( it says true
     
  10. Offline

    travja

    DividedByZero I've had that problem before.. I solved it by making the array static. doesn't quite make sense.. but that's what I did to make it work
     
  11. Offline

    DividedByZero

    travja ur awesome! Totally fixed it. Now works with ease. This is a very strange bug. Anyonr who could give me a reason why this occurs would be a great help!
     
Thread Status:
Not open for further replies.

Share This Page