How can I make loading from a string into a location work?

Discussion in 'Plugin Development' started by billofbong, Jan 1, 2012.

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

    billofbong

    What I want is to load a location from a string in a hashmap. Here's what i've got.

    Code:java
    1. package org.awesomecraft.plugins.orphanage;
    2.  
    3. import java.util.HashMap;
    4. import java.util.Map;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Location;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandExecutor;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class Orphanage extends JavaPlugin {
    14. /*
    15.   * Thanks to
    16.   *
    17.   *
    18.   */
    19. public Map<String, String> orphanage = new HashMap<String, String>(); //<World, Orphanage Location>
    20. public Map<String, String> mothers = new HashMap<String, String>(); //<Mother, Orphan>
    21. public Map<String, Boolean> isOrphan = new HashMap<String, Boolean>(); //<Orphan, isOrphan>
    22. public void onDisable() {
    23. System.out.println(this + " is now disabled!");
    24. try {
    25. SLAPI.save(mothers, "mothers.bin");
    26. } catch (Exception ex) {
    27. ex.printStackTrace();
    28. }
    29. try {
    30. SLAPI.save(isOrphan, "orphans.bin");
    31. } catch (Exception ex) {
    32. ex.printStackTrace();
    33. }
    34. try {
    35. SLAPI.save(orphanage, "orphanage.bin");
    36. }
    37. catch(Exception ex) {
    38. ex.printStackTrace();
    39. }
    40. }
    41.  
    42. public void onEnable() {
    43. getCommand("adopt").setExecutor(new CommandExecutor() {
    44.  
    45. public boolean onCommand(CommandSender cs, Command cmnd, String alias, String[] args) {
    46. if(args.length == 1) {
    47. if(cs instanceof Player) {
    48. try {
    49. Player target = getServer().getPlayer(args[0]);
    50. Player sender = (Player) cs;
    51. sender.sendMessage(ChatColor.GREEN + "You are now the parent of " + target.getDisplayName() + "!");
    52. target.sendMessage(ChatColor.GREEN + "You have been adopted by " + sender.getDisplayName() + "!");
    53. mothers.put(sender.toString(), target.toString());
    54. isOrphan.put(target.toString(), Boolean.FALSE);
    55. }
    56. catch(NullPointerException npe) {
    57. cs.sendMessage(ChatColor.RED + "Player not found.");
    58. }
    59. }
    60. else{
    61. cs.sendMessage(ChatColor.RED + "You must be ingame to use this command.");
    62. }
    63. }
    64. return true;
    65. }
    66. });
    67. getCommand("abandon").setExecutor(new CommandExecutor() {
    68.  
    69. public boolean onCommand(CommandSender cs, Command cmnd, String alias, String[] args) {
    70. if(cs instanceof Player) {
    71. Player target = getServer().getPlayer(args[0]);
    72. Player sender = (Player) cs;
    73. if(mothers.containsValue(target.toString()) && mothers.containsKey(sender.toString())) {
    74. mothers.put(null, null);
    75. isOrphan.put(target.toString(), true);
    76. target.sendMessage(ChatColor.RED + "You are an orphan again.");
    77. sender.sendMessage(ChatColor.RED + "You abandoned your orphan on the streets.");
    78. } else {
    79. sender.sendMessage(ChatColor.RED + "You aren't the parent of that person!");
    80. }
    81. }
    82. return true;
    83. }});
    84. getCommand("setorphanage").setExecutor(new CommandExecutor() {
    85.  
    86. public boolean onCommand(CommandSender cs, Command cmnd, String alias, String[] args) {
    87. if(cs instanceof Player) {
    88. Player sender = (Player) cs;
    89. orphanage.put(sender.getWorld().toString(), sender.getLocation().toString()); //Error here
    90. sender.sendMessage(ChatColor.GREEN + "Set an orphanage at your current location.");
    91. }
    92. else {
    93. cs.sendMessage(ChatColor.RED + "You must be ingame to use this command.");
    94. }
    95. return true;
    96. }
    97. });
    98. getCommand("orphanage").setExecutor(new CommandExecutor() {
    99.  
    100. public boolean onCommand(CommandSender cs, Command cmnd, String alias, String[] args) {
    101. if(cs instanceof Player) {
    102. Player sender = (Player) cs;
    103. if(orphanage.get(sender.getWorld().toString()) != null) {
    104. Location tele = orphanage.get(sender.getWorld().toString());
    105. sender.teleport(tele);
    106. }
    107. else {
    108. sender.sendMessage(ChatColor.RED + "An orphanage hasn't been set yet.");
    109. }
    110. }
    111. else {
    112. cs.sendMessage(ChatColor.RED + "You must be ingame to use this command.");
    113. }
    114. return true;
    115. }
    116. });
    117. try {
    118. mothers = (Map<String, String>)SLAPI.load("mothers.bin");
    119. } catch (Exception ex) {
    120. ex.printStackTrace();
    121. }
    122. try {
    123. isOrphan = (Map<String, Boolean>)SLAPI.load("orphans.bin");
    124. } catch (Exception ex) {
    125. ex.printStackTrace();
    126. }
    127. try {
    128. orphanage = (Map<String, String>)SLAPI.load("orphanage.bin");
    129. }
    130. catch(Exception ex) {
    131. ex.printStackTrace();
    132. }
    133.  
    134. System.out.println(this + " is now enabled!");
    135. }
    136. }
    137.  
     
  2. Offline

    hatstand

    Firstly, scrap the isOrphan HashMap, use a Set and check if the user is in the set.

    Secondly, you'll want to get the plugin's data directory, something like this:
    Code:
    getDataFolder() + File.separator + "filename"
    Thirdly, just a pet peeve - Why does everyone serialise their HashMaps instead of spending a bit of time and putting them into a config file - It's much easier on the end user, as it allows them to edit the saved data if they need to.
     
  3. Offline

    masteroftime

    You could store the location as x;y;z in your String. Then use String.split() or StringTokenizer to split up the String and Float.valueOf() to convert the coordinates to floats.

    If you want multi-world support you also have to store the world name.
     
Thread Status:
Not open for further replies.

Share This Page