Secondary Config Returning Null

Discussion in 'Plugin Development' started by pookeythekid, Mar 21, 2014.

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

    pookeythekid

    Hello there. I've encountered a problem with my plugin. I've downloaded a config manager to create multiple files and it seems to work great (I'm not 100% sure it's up to date, but I don't think the code for files in general has change in a while). However, there's one very big problem: my strings are null when read by the plugin. Anyone know why?

    Here are the links to my config manager classes that I've downloaded, and below is my RespawnListener class.
    http://hastebin.com/rurixomate.coffee
    http://hastebin.com/kubelexemo.avrasm

    My RespawnListener:
    Code:java
    1. package me.pookeythekid.WorldSpawn.Listeners;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import me.pookeythekid.WorldSpawn.Main;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.Location;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.EventPriority;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.player.PlayerRespawnEvent;
    14.  
    15. public class RespawnListener implements Listener {
    16.  
    17. public Main M;
    18.  
    19. public RespawnListener(Main instance) {
    20. M = instance;
    21. }
    22.  
    23.  
    24. ArrayList<String> deathWorlds = new ArrayList<String>();
    25. ArrayList<String> worldPairList = new ArrayList<String>();
    26.  
    27.  
    28. @EventHandler(priority=EventPriority.HIGHEST, ignoreCancelled = true)
    29. public void onPlayerRespawn(final PlayerRespawnEvent e) {
    30.  
    31. if (M.getConfig().getBoolean("usePlugin")) {
    32.  
    33. respawnPlayer(e.getPlayer(), e);
    34.  
    35. e.getPlayer().sendMessage("You have respawned.");
    36.  
    37. }
    38. }
    39.  
    40.  
    41.  
    42. public void respawnPlayer(Player p, PlayerRespawnEvent e) {
    43.  
    44. deathWorlds.clear();
    45. worldPairList.clear();
    46.  
    47.  
    48. String[] worldPairs = M.getConfig().getString("worlds").split(",");
    49.  
    50. for (String s : worldPairs) {
    51.  
    52. String[] indiWorlds = s.split("-");
    53. deathWorlds.add(indiWorlds[0]);
    54. worldPairList.add(s);
    55.  
    56. }
    57.  
    58. if (deathWorlds.contains(p.getWorld().getName())) {
    59.  
    60. for (String s : worldPairs) {
    61.  
    62. String[] indiWorlds = s.split("-");
    63.  
    64. p.sendMessage(indiWorlds[0]);
    65. p.sendMessage(indiWorlds[1]);
    66.  
    67. if (worldPairList.contains(indiWorlds[0] + "-" + indiWorlds[1])) {
    68.  
    69. if (!(M.worldsConf.getConfigurationSection(indiWorlds[1]).equals(null))) {
    70.  
    71. Location loc = e.getRespawnLocation();
    72.  
    73. loc.setWorld(Bukkit.getServer().getWorld(indiWorlds[1]));
    74. loc.setX(M.worldsConf.getDouble(indiWorlds[1] + ".x"));
    75. loc.setY(M.worldsConf.getDouble(indiWorlds[1] + ".y"));
    76. loc.setZ(M.worldsConf.getDouble(indiWorlds[1] + ".z"));
    77. loc.setPitch(M.worldsConf.getLong(indiWorlds[1] + ".Pitch"));
    78. loc.setYaw(M.worldsConf.getLong(indiWorlds[1] + ".Yaw"));
    79.  
    80. e.setRespawnLocation(loc);
    81.  
    82. break;
    83.  
    84. } else if (M.worldsConf.getConfigurationSection(indiWorlds[1]).equals(null)) {
    85.  
    86. if (M.getConfig().getBoolean("respawnBed")
    87. && e.getPlayer().getBedSpawnLocation() != null)
    88.  
    89. e.setRespawnLocation(e.getPlayer().getBedSpawnLocation());
    90.  
    91. else if (!(M.getConfig().getBoolean("respawnBed")))
    92.  
    93. e.setRespawnLocation(Bukkit.getServer().getWorld(indiWorlds[1]).getSpawnLocation());
    94.  
    95. }
    96.  
    97. }
    98.  
    99. }
    100.  
    101. }
    102.  
    103. }
    104.  
    105. }
    106.  


    I know that it's the "indiWorlds[1] + ".x"" parts that are returning null because when I respawn, I'm teleported to not the world's spawnpoint, but to x: 0, y: 0, z: 0, Pitch: 0, Yaw: 0. I'm assuming that they're returning null because they're number types, so if a number is null then it equals 0.

    If anyone can help with this, that'd be great. Thanks. :)
     
  2. Offline

    AoH_Ruthless

    pookeythekid
    Well can you tell us which strings specifically are null? What are you doing when you get an NPE in your log (post the stacktrace also)
     
  3. Offline

    MRPS

  4. Offline

    pookeythekid

    AoH_Ruthless Sorry, did I use the term "returning null" incorrectly? I'm not getting a stack trace because the plugin's doing as it's being told, and it just recognizes the strings as 0 and/or nothing.
    Here's my config that it's reading from if it helps.
    Code:
    # +----------------------------------------------------+
    # <        These are spawnpoints of worlds that        >
    # <      players will respawn into when they die.      >
    # <      Note that any of these worlds will only      >
    # <      take function if they are listed in the      >
    # <        'worlds' section in the main config.        >
    # +----------------------------------------------------+
     
    world:
      x: 308.4252674931878
      y: 60.0
      z: -112.96386428373135
      Pitch: 23.450804
      Yaw: -35.32099
    MRPS If you're getting a number that is nothing, I'm pretty sure you're going to receive a zero.

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

    MRPS


    null is nothing, 0 is an integer/short/byte which is of 32, 16, or 8 bit allocation
     
  6. Offline

    AoH_Ruthless

    pookeythekid
    Null and 0 have a similar connotative sense in everyday language, but not always in Java. In a coding sense, null means it literally doesn't exist. 0, while associated with the word 'nothing' (or its synonyms) is something 'tangible' for lack of a better term.

    But yes, a double, float, integer, etc will return 0 in the scenario you have.


    Code:java
    1. String[] worldPairs = M.getConfig().getString("worlds").split(",");

    I don't see that anywhere in your config, hence it is null.
     
  7. Offline

    xTigerRebornx

    pookeythekid I believe this is true, since Bukkit's configuration system returns a double when using getDouble() with a config, instead of a Double (see this)
     
  8. Offline

    pookeythekid

    AoH_Ruthless Oh yes, I gave you my second config, respawnworlds.yml. I'll give you my main config.

    Main Config:
    Code:
    #######################
    ## ::-------------:: ##
    ## :: WorldSpawns :: ##
    ## ::-------------:: ##
    #######################
     
     
    ####################
    ## :: Settings :: ##
    ####################
     
    # Should this plugin even take effect?
    # This is for using or not using this plugin without deleting and
    # re-installing it all the time. Set to "true" to say yes. Set to
    # "false" to say no. For those of you plugin developers who understand
    # listeners, this plugin's PlayerRespawnEvent listener is set to "HIGHEST"
    # priority. This is to override any other plugins' respawning handlers.
    # For everyone -- not just fancy plugin developers -- set this to "false"
    # if you wish for your other plugins to handle respawning, or for regular
    # Minecraft to respawn players at the default world's spawnpoint. Note that
    # this plugin will not be disabled entirely; settings below may still take
    # effect when a player respawns. To completely disable this plugin, you'd
    # have to delete it.
    usePlugin: true
     
    # If a player has a bed spawn, there is no spawnpoint for the world in which
    # they are respawning, and/or the usePlugin setting above is disabled, should
    # that player respawn at their home bed?
    respawnBed: false
     
     
     
    ####################
    ## ::  Worlds  :: ##
    ####################
     
    # This is the list of worlds in which players will die and respawn in.
    # Notice that WorldSpawns will not handle player respawning in worlds
    # that are not listed here; either other plugins or default Minecraft
    # will handle respawning instead. Items in this list are sparated by
    # commas. Do not use spaces!!! Within each item, there are two worlds.
    # As you can see, they are arranged like this: <world>-<world>
    # The world BEFORE the "-" is the world that the player will DIE in.
    # The world AFTER the "-" is the world that the player will RESPAWN in.
    # If a world does not have a spawnpoint and a player respawns into it,
    # the world's default spawnpoint will be used. Although impractical,
    # please do not list the same pair of worlds twice, and do not list
    # three worlds in one world group.
    worlds: world-world,world_nether-world,world_the_end-world
    BTW Thanks for the description of the difference between null and 0. :)
     
  9. Offline

    AoH_Ruthless

    pookeythekid
    "However, there's one very big problem: my strings are null when read by the plugin. Anyone know why?" - I still don't understand what exactly is null. Please elaborate.
     
  10. Offline

    pookeythekid

    AoH_Ruthless In my description (inaccurate compared to yours), null either means 0, or it means that the program is somehow not reaching the config, and therefore the strings are not 'tangible'. Otherwise, the program is reaching the strings, but is having strange difficulties interpreting them, so instead of dealing with the problems it spits out a zero. This is my guess, I really don't know what's going on.
     
  11. Offline

    AoH_Ruthless

  12. Offline

    pookeythekid

    AoH_Ruthless
    If you mean looking over it a thousand times and trying several different methods for hours on end, yes I did.

    Edit: I also know for sure that the listener is firing off because I sent myself a message when I respawned, and that worked just fine.
     
  13. Offline

    AoH_Ruthless

    pookeythekid
    That isn't what debugging is.

    Code:java
    1. @EventHandler
    2. public void onEvent(PlayerTakesCrapEvent e) {
    3. System.out.println("1");
    4. if (x) {
    5. System.out.println("2");
    6. if (y) {
    7. System.out.println("3");
    8. }
    9. }
    10. }

    Then in your console, see how far your code gets and adjust accordingly.
     
  14. Offline

    pookeythekid

    AoH_Ruthless
    Um... Isn't that just checking if the plugin works?
     
  15. Offline

    AoH_Ruthless

    pookeythekid
    That's what debugging is ... seeing how far your code gets so you know exactly which line(s) of your code are wrong.
     
  16. Offline

    pookeythekid

    AoH_Ruthless
    I... don't understand... Are you saying that it's checking for a glitch in the class itself, not in the code that I write?
     
  17. Offline

    AoH_Ruthless

    pookeythekid
    This isn't a very complicated concept.

    Debugging your source code simply means you are looking for errors in your code. You don't know why your code isn't working or even where the issue is, so you can use debugging to pinpoint the location of the error.
    It is a tool used to assist the coder in fixing mistakes they make in the code they write.
     
  18. Offline

    pookeythekid

    AoH_Ruthless
    Oh, well then yes, I have debugged it. I have spent much time pinpointing the error, and that's why I created this thread; the error(s) are in the lines that get the second config file that I have.

    AoH_Ruthless
    Have you read the config managers that I gave in the main post of this thread? I've read them over and over... The MyConfig class makes sense to me, nothing seems wrong. The MyConfigManager class, well... As much as I can understand about it makes sense, but I don't know the whole thing well enough to declare if it's wrong or not.

    Edit: Um... nevermind, the links are gone... It's my first time using Hastebin, so I'm not surprised that I don't know why. :p
    Here's the code for the classes, since they're evidently not in the links I gave.
    MyConfig class:
    Code:java
    1. package me.pookeythekid.WorldSpawn.ConfigManager;
    2.  
    3. import java.io.File;
    4. import java.io.InputStream;
    5. import java.util.List;
    6. import java.util.Set;
    7.  
    8. import org.bukkit.configuration.ConfigurationSection;
    9. import org.bukkit.configuration.file.FileConfiguration;
    10. import org.bukkit.configuration.file.YamlConfiguration;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class MyConfig
    14. {
    15. private int comments;
    16. private MyConfigManager manager;
    17.  
    18. private File file;
    19. private FileConfiguration config;
    20.  
    21. public MyConfig(InputStream configStream, File configFile, int comments, JavaPlugin plugin)
    22. {
    23. this.comments = comments;
    24. this.manager = new MyConfigManager(plugin);
    25. this.file = configFile;
    26. this.config = YamlConfiguration.loadConfiguration(configStream);
    27. }
    28.  
    29. public Object get(String path) {return this.config.get(path);}
    30.  
    31. public Object get(String path, Object def) {return this.config.get(path, def);}
    32.  
    33. public String getString(String path) {return this.config.getString(path);}
    34.  
    35. public String getString(String path, String def) {return this.config.getString(path, def);}
    36.  
    37. public int getInt(String path) {return this.config.getInt(path);}
    38.  
    39. public int getInt(String path, int def) {return this.config.getInt(path, def);}
    40.  
    41. public boolean getBoolean(String path) {return this.config.getBoolean(path);}
    42.  
    43. public boolean getBoolean(String path, boolean def) {return this.config.getBoolean(path, def);}
    44.  
    45. public void createSection(String path) {this.config.createSection(path);}
    46.  
    47. public ConfigurationSection getConfigurationSection(String path) {return this.config.getConfigurationSection(path);}
    48.  
    49. public double getDouble(String path) {return this.config.getDouble(path);}
    50.  
    51. public double getDouble(String path, double def) {return this.config.getDouble(path, def);}
    52.  
    53. public float getLong(String path) {return this.config.getLong(path);}
    54.  
    55. public float getLong(String path, long def) {return this.config.getLong(path, def);}
    56.  
    57. public List<?> getList(String path) {return this.config.getList(path);}
    58.  
    59. public List<?> getList(String path, List<?> def) {return this.config.getList(path, def);}
    60.  
    61. public boolean contains(String path) {return this.config.contains(path);}
    62.  
    63. public void removeKey(String path) {this.config.set(path, null);}
    64.  
    65. public void set(String path, Object value) {this.config.set(path, value);}
    66.  
    67. public void set(String path, Object value, String comment)
    68. {
    69. if(!this.config.contains(path))
    70. {
    71. this.config.set(manager.getPluginName() + "_COMMENT_" + comments, " " + comment);
    72. comments++;
    73. }
    74. this.config.set(path, value);
    75. }
    76.  
    77. public void set(String path, Object value, String[] comment)
    78. {
    79. for(String comm : comment)
    80. {
    81. if(!this.config.contains(path))
    82. {
    83. this.config.set(manager.getPluginName() + "_COMMENT_" + comments, " " + comm);
    84. comments++;
    85. }
    86. }
    87. this.config.set(path, value);
    88. }
    89.  
    90. public void setHeader(String[] header)
    91. {
    92. manager.setHeader(this.file, header);
    93. this.comments = header.length + 2;
    94. this.reloadConfig();
    95. }
    96.  
    97. public void reloadConfig() {this.config = YamlConfiguration.loadConfiguration(manager.getConfigContent(file));}
    98.  
    99. public void saveConfig()
    100. {
    101. String config = this.config.saveToString();
    102. manager.saveConfig(config, this.file);
    103. }
    104.  
    105. public Set<String> getKeys() {return this.config.getKeys(false);}
    106. }

    MyConfigManager class:
    Code:java
    1. package me.pookeythekid.WorldSpawn.ConfigManager;
    2. import java.io.BufferedReader;
    3. import java.io.BufferedWriter;
    4. import java.io.ByteArrayInputStream;
    5. import java.io.File;
    6. import java.io.FileOutputStream;
    7. import java.io.FileReader;
    8. import java.io.FileWriter;
    9. import java.io.IOException;
    10. import java.io.InputStream;
    11. import java.io.OutputStream;
    12. import java.nio.charset.Charset;
    13.  
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class MyConfigManager
    17. {
    18. private JavaPlugin plugin;
    19.  
    20. public MyConfigManager(JavaPlugin plugin)
    21. {
    22. this.plugin = plugin;
    23. }
    24.  
    25. public MyConfig getNewConfig(String fileName, String[] header)
    26. {
    27. File file = this.getConfigFile(fileName);
    28.  
    29. if(!file.exists())
    30. {
    31. this.prepareFile(fileName);
    32. if(header != null && header.length != 0)
    33. this.setHeader(file, header);
    34. }
    35.  
    36. MyConfig config = new MyConfig(this.getConfigContent(fileName), file, this.getCommentsNum(file), plugin);
    37. return config;
    38. }
    39.  
    40. public MyConfig getNewConfig(String fileName)
    41. {
    42. return this.getNewConfig(fileName, null);
    43. }
    44.  
    45. private File getConfigFile(String file)
    46. {
    47. if(file.isEmpty() || file == null)
    48. return null;
    49.  
    50. File configFile;
    51.  
    52. if(file.contains("/"))
    53. {
    54. if(file.startsWith("/"))
    55. configFile = new File(plugin.getDataFolder() + file.replace("/", File.separator));
    56. else configFile = new File(plugin.getDataFolder() + File.separator + file.replace("/", File.separator));
    57. }
    58. else configFile = new File(plugin.getDataFolder(), file);
    59.  
    60. return configFile;
    61. }
    62.  
    63. public void prepareFile(String filePath, String resource)
    64. {
    65. File file = this.getConfigFile(filePath);
    66.  
    67. if(file.exists())
    68. return;
    69.  
    70. try
    71. {
    72. file.getParentFile().mkdirs();
    73. file.createNewFile();
    74.  
    75. if(resource != null)
    76. if(!resource.isEmpty())
    77. this.copyResource(plugin.getResource(resource), file);
    78.  
    79. }
    80. catch (IOException e){e.printStackTrace();}
    81. }
    82.  
    83. public void prepareFile(String filePath)
    84. {
    85. this.prepareFile(filePath, null);
    86. }
    87.  
    88. public void setHeader(File file, String[] header)
    89. {
    90. if(!file.exists())
    91. return;
    92.  
    93. try
    94. {
    95. String currentLine;
    96. StringBuilder config = new StringBuilder("");
    97. BufferedReader reader = new BufferedReader(new FileReader(file));
    98.  
    99. while((currentLine = reader.readLine()) != null)
    100. config.append(currentLine + "\n");
    101.  
    102. reader.close();
    103. config.append("# +----------------------------------------------------+ #\n");
    104.  
    105. for(String line : header)
    106. {
    107. if(line.length() > 50)
    108. continue;
    109.  
    110. int lenght = (50 - line.length()) / 2;
    111. StringBuilder finalLine = new StringBuilder(line);
    112.  
    113. for(int i = 0; i < lenght; i++)
    114. {
    115. finalLine.append(" ");
    116. finalLine.reverse();
    117. finalLine.append(" ");
    118. finalLine.reverse();
    119. }
    120.  
    121. if(line.length() % 2 != 0)
    122. finalLine.append(" ");
    123.  
    124. config.append("# < " + finalLine.toString() + " > #\n");
    125. }
    126. config.append("# +----------------------------------------------------+ #");
    127.  
    128. BufferedWriter writer = new BufferedWriter(new FileWriter(file));
    129. writer.write(this.prepareConfigString(config.toString()));
    130. writer.flush();
    131. writer.close();
    132. }
    133. catch (IOException e){e.printStackTrace();}
    134. }
    135.  
    136. public InputStream getConfigContent(File file)
    137. {
    138. if(!file.exists())
    139. return null;
    140. try
    141. {
    142. int commentNum = 0;
    143.  
    144. String addLine;
    145. String currentLine;
    146. String pluginName = this.getPluginName();
    147.  
    148. StringBuilder whole = new StringBuilder("");
    149. BufferedReader reader = new BufferedReader(new FileReader(file));
    150.  
    151. while((currentLine = reader.readLine()) != null)
    152. {
    153. if(currentLine.startsWith("#"))
    154. {
    155. addLine = currentLine.replaceFirst("#", pluginName + "_COMMENT_" + commentNum + ":");
    156. whole.append(addLine + "\n");
    157. commentNum++;
    158. }
    159. else whole.append(currentLine + "\n");
    160. }
    161.  
    162. String config = whole.toString();
    163. InputStream configStream = new ByteArrayInputStream(config.getBytes(Charset.forName("UTF-8")));
    164.  
    165. reader.close();
    166. return configStream;
    167. }
    168. catch (IOException e){e.printStackTrace();return null;}
    169. }
    170.  
    171. private int getCommentsNum(File file)
    172. {
    173. if(!file.exists())
    174. return 0;
    175. try
    176. {
    177. int comments = 0;
    178. String currentLine;
    179.  
    180. BufferedReader reader = new BufferedReader(new FileReader(file));
    181.  
    182. while((currentLine = reader.readLine()) != null)
    183. if(currentLine.startsWith("#"))
    184. comments++;
    185.  
    186. reader.close();
    187. return comments;
    188. }
    189. catch (IOException e){e.printStackTrace();return 0;}
    190. }
    191.  
    192. public InputStream getConfigContent(String filePath)
    193. {
    194. return this.getConfigContent(this.getConfigFile(filePath));
    195. }
    196.  
    197. private String prepareConfigString(String configString)
    198. {
    199. int lastLine = 0;
    200. int headerLine = 0;
    201.  
    202. String[] lines = configString.split("\n");
    203. StringBuilder config = new StringBuilder("");
    204.  
    205. for(String line : lines)
    206. {
    207. if(line.startsWith(this.getPluginName() + "_COMMENT"))
    208. {
    209. String comment = "#" + line.trim().substring(line.indexOf(":") + 1);
    210.  
    211. if(comment.startsWith("# +-"))
    212. {
    213. if(headerLine == 0)
    214. {
    215. config.append(comment + "\n");
    216. lastLine = 0;
    217. headerLine = 1;
    218. }
    219. else if(headerLine == 1)
    220. {
    221. config.append(comment + "\n\n");
    222. lastLine = 0;
    223. headerLine = 0;
    224. }
    225. }
    226. else
    227. {
    228. String normalComment;
    229. if(comment.startsWith("# ' "))
    230. normalComment = comment.substring(0, comment.length() - 1).replaceFirst("# ' ", "# ");
    231. else normalComment = comment;
    232.  
    233. if(lastLine == 0)
    234. config.append(normalComment + "\n");
    235. else if(lastLine == 1)
    236. config.append("\n" + normalComment + "\n");
    237.  
    238. lastLine = 0;
    239. }
    240. }
    241. else
    242. {
    243. config.append(line + "\n");
    244. lastLine = 1;
    245. }
    246. }
    247. return config.toString();
    248. }
    249.  
    250. public void saveConfig(String configString, File file)
    251. {
    252. String configuration = this.prepareConfigString(configString);
    253.  
    254. try
    255. {
    256. BufferedWriter writer = new BufferedWriter(new FileWriter(file));
    257. writer.write(configuration);
    258. writer.flush();
    259. writer.close();
    260.  
    261. }
    262. catch (IOException e){e.printStackTrace();}
    263. }
    264.  
    265. public String getPluginName()
    266. {
    267. return plugin.getDescription().getName();
    268. }
    269.  
    270. private void copyResource(InputStream resource, File file)
    271. {
    272. try
    273. {
    274. OutputStream out = new FileOutputStream(file);
    275.  
    276. int length;
    277. byte[] buf = new byte[1024];
    278.  
    279. while((length = resource.read(buf)) > 0)
    280. out.write(buf, 0, length);
    281.  
    282. out.close();
    283. resource.close();
    284. }
    285. catch (Exception e) {e.printStackTrace();}
    286. }
    287. }


    AoH_Ruthless
    Okay, I've tried everything I can possibly think of. I'm positive that it's the config managers that I'm using that are not working somehow. I've looked them over many times, I don't see anything that is wrong as far as my understanding of it, and how much sense the code makes to me. But I have a new solution, possibly one involving much less code. I'm going to learn how to create a text file in the plugin's folder inside of the server, and I'm going to learn to efficiently use BufferedWriters and BufferedReaders to use as a worlds file.

    Thanks for the help anyway. :)

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

    AoH_Ruthless

    pookeythekid
    This is what you have:
    Code:java
    1. if (!(M.worldsConf.getConfigurationSection(indiWorlds[1]).equals(null))) {
    2.  
    3. Location loc = e.getRespawnLocation();
    4.  
    5. loc.setWorld(Bukkit.getServer().getWorld(indiWorlds[1]));
    6. loc.setX(M.worldsConf.getDouble(indiWorlds[1] + ".x"));
    7. loc.setY(M.worldsConf.getDouble(indiWorlds[1] + ".y"));
    8. loc.setZ(M.worldsConf.getDouble(indiWorlds[1] + ".z"));
    9. loc.setPitch(M.worldsConf.getLong(indiWorlds[1] + ".Pitch"));
    10. loc.setYaw(M.worldsConf.getLong(indiWorlds[1] + ".Yaw"));
    11.  
    12. e.setRespawnLocation(loc);


    Try something like:

    Code:java
    1. if (M.worldsConf.getConfigurationSection(indiWorlds[1]) != null) {
    2.  
    3. World world = Bukkit.getServer().getWorld(indiWorlds[1]));
    4. double x = M.worldsConf.getDouble(indiWorlds[1] + ".x");
    5. double y = M.worldsConf.getDouble(indiWorlds[1] + ".y");
    6. double z = M.worldsConf.getDouble(indiWorlds[1] + ".z");
    7. double pitch = M.worldsConf.getDouble(indiWorlds[1] + ".Pitch"); // Doubles are better than longs, but you should make a method to get floats in your config class.
    8. double yaw = M.worldsConf.getDouble(indiWorlds[1] + ".Yaw");
    9.  
    10. Location loc = new Location(world,x,y,z,yaw,pitch);
    11. e.setRespawnLocation(loc);
     
  20. Offline

    pookeythekid

    AoH_Ruthless
    Lol I did make the method to get longs in the config class. I figured I'd do that because I always use the getLong() method from Bukkit, and that usually works. Thanks for the code, I'll try it out. I'm almost sure I've literally tried every other possible way of doing it.

    AoH_Ruthless
    By the way, here's my most recent way of doing it:
    Code:java
    1. // I have worldIsNull in another class now
    2. // WC means WorldChecks, my new class.
    3. if (!(WC.worldIsNull(indiWorlds[1])) {
    4.  
    5. loc.setWorld(Bukkit.getServer().getWorld(indiWorlds[1]);
    6. loc.setX(M.worldsConf.getDouble(indiWolrds[1] + ".x");
    7. // and so on until getting the Z
    8. loc.setPitch(M.worldsConf.getLong(indiWorlds[1] + ".Pitch");
    9. // same with Yaw
    10.  
    11. e.setRespawnLocation(loc);


    Edit: Also, I should learn BufferedReaders and Writers anyway. May come in handy sometime when I need a text file.

    AoH_Ruthless
    .... It makes so much sense now.... It IS the config manager that's broken! I sent myself a message when I respawned: "p.sendMessage(M.worldsConf.getString("world.y"));"
    It sent me: "MemorySection[path='world.y', root='YamlConfiguration']"
    That's why it's been so darn confusing!!! If the code gets the coordinates from the config, it does get something; it's not null, so it passes the null check. But that weird message I received can't be interpreted as a double, so the program assume that it's 0, and that's why it keeps teleporting me to x: 0, y: 0, z: 0, Pitch: 0, Yaw: 0, but I haven't worked out why the worlds part doesn't work yet... The worlds are retrieved from the main config, the one that doesn't use the config manager classes.

    But anyway! That's why this whole thing has been so dang confusing, because it's not getting nothing, but it's getting the wrong thing.

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

    AoH_Ruthless

    pookeythekid
    Actually since world.y is a number you can't be using getString().
     
  22. Offline

    pookeythekid

    AoH_Ruthless
    ... Oh... Well, I figured using getString() would work because it would convert the number to a string, instead of me having to use toString(). I'll try the latter, and then tell you what I got.

    Edit: Oh, that's why I used getString(). There is no option to do getDouble("world.y").toString() with my config manager, not much surprise that it makes an error when I send a player a double. But I'm going to learn to use text documents anyway. I'll probably need text documents in the future, and this whole second config thing isn't working well at all for me.
     
  23. Offline

    AoH_Ruthless

  24. Offline

    pookeythekid

    AoH_Ruthless
    ... *facepalm* Thanks.... I'll try that...

    AoH_Ruthless
    Well, still kind of proves my point. I got a message saying: "0.0". That must mean zero. : P
    Oh yes, and I also sent myself a loc.toString(). It said: "Location{world=CraftWorld{name=world},x=0.0,y=0.0,z=0.0,pitch=0.0,yaw=0.0}". That would explain why only the world name seems to work.

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

    AoH_Ruthless

    pookeythekid
    A location cannot be compared to string like that.
    Post your current code.
     
  26. Offline

    pookeythekid

    AoH_Ruthless
    Here's my whole class if it helps more.
    Code:java
    1. package me.pookeythekid.WorldSpawn.Listeners;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import me.pookeythekid.WorldSpawn.Main;
    6. import me.pookeythekid.WorldSpawn.PersonalAPI.WorldChecks;
    7.  
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.Location;
    10. import org.bukkit.World;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.EventPriority;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.event.player.PlayerRespawnEvent;
    16.  
    17. public class RespawnListener implements Listener {
    18.  
    19. public Main M;
    20.  
    21. public RespawnListener(Main instance) {
    22. M = instance;
    23. }
    24.  
    25. public WorldChecks WC;
    26.  
    27. public RespawnListener(WorldChecks instance) {
    28.  
    29. WC = instance;
    30.  
    31. }
    32.  
    33.  
    34. ArrayList<String> deathWorlds = new ArrayList<String>();
    35. ArrayList<String> worldPairList = new ArrayList<String>();
    36.  
    37.  
    38. @EventHandler(priority=EventPriority.HIGHEST, ignoreCancelled = true)
    39. public void onPlayerRespawn(final PlayerRespawnEvent e) {
    40.  
    41. if (M.getConfig().getBoolean("usePlugin")) {
    42.  
    43. respawnPlayer(e);
    44.  
    45. e.getPlayer().sendMessage("You have respawned.");
    46.  
    47. }
    48. }
    49.  
    50.  
    51.  
    52. public void respawnPlayer(PlayerRespawnEvent e) {
    53.  
    54. Player p = e.getPlayer();
    55.  
    56. deathWorlds.clear();
    57. worldPairList.clear();
    58.  
    59.  
    60. String[] worldPairs = M.getConfig().getString("worlds").split(",");
    61.  
    62. for (String s : worldPairs) {
    63.  
    64. String[] indiWorlds = s.split("-");
    65. deathWorlds.add(indiWorlds[0]);
    66. worldPairList.add(s);
    67.  
    68. }
    69.  
    70. if (deathWorlds.contains(p.getWorld().getName())) {
    71.  
    72. for (String s : worldPairs) {
    73.  
    74. String[] indiWorlds = s.split("-");
    75.  
    76. p.sendMessage(indiWorlds[0]);
    77. p.sendMessage(indiWorlds[1]);
    78.  
    79. if (worldPairList.contains(indiWorlds[0] + "-" + indiWorlds[1])) {
    80.  
    81. if (M.worldsConf.getConfigurationSection(indiWorlds[1]) != null) {
    82.  
    83. World world = Bukkit.getServer().getWorld(indiWorlds[1]);
    84. double x = M.worldsConf.getDouble(indiWorlds[1] + ".x");
    85. double y = M.worldsConf.getDouble(indiWorlds[1] + ".y");
    86. double z = M.worldsConf.getDouble(indiWorlds[1] + ".z");
    87.  
    88. Location loc = new Location(world, x, y, z);
    89. e.setRespawnLocation(loc);
    90. p.sendMessage(loc.toString());
    91. p.sendMessage(String.valueOf(M.worldsConf.getDouble("world.y")));
    92.  
    93. break;
    94.  
    95. } else if (WC.worldIsNull(indiWorlds[1])) {
    96.  
    97. if (M.getConfig().getBoolean("respawnBed")
    98. && e.getPlayer().getBedSpawnLocation() != null) {
    99.  
    100. e.setRespawnLocation(e.getPlayer().getBedSpawnLocation());
    101. break;
    102.  
    103. } else if (!(M.getConfig().getBoolean("respawnBed"))) {
    104.  
    105. e.setRespawnLocation(Bukkit.getServer().getWorld(indiWorlds[1]).getSpawnLocation());
    106. break;
    107.  
    108. }
    109.  
    110. }
    111.  
    112. }
    113.  
    114. }
    115.  
    116. }
    117.  
    118. }
    119.  
    120. }
    121.  

    The p.sendMessage(indiWorlds[1]) and p.sendMessage(indiWorlds[2]) was just a check for me to be certain that the world was working. I haven't taken it out yet, but it doesn't work. I've tried setting it to a different respawn world in my config, and I'm positive that the code is set up so that it should work, but I respawned in the same, default world. I figure if it doesn't know how to handle a world that's being read wrong, it chooses the default world.
     
  27. Offline

    AoH_Ruthless

    pookeythekid
    What does your "worlds" string in your regular config look like?
     
  28. Offline

    pookeythekid

    AoH_Ruthless
    Code:
    worlds: world-world,world_nether-world,world_the_end-world
     
Thread Status:
Not open for further replies.

Share This Page