Custom File StringList

Discussion in 'Plugin Development' started by Marten Mooij, Oct 17, 2014.

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

    Marten Mooij

  2. Offline

    Gater12

    Marten Mooij
    You are trying to create section on an empty childnode. See line 28 in GhostData
     
  3. Offline

    Marten Mooij

    Gater12 Could you please explain a bit more about what I am doing wrong/how to fix it?
     
  4. Offline

    Gater12

    Marten Mooij
    This line:
    Code:java
    1. config.createSection("Ghosts.");


    You're trying to create a section on an empty node. This of it like a key/value storage. The key is the node name and the value is the value you what to store in associate with the key.
     
  5. Offline

    fireblast709

    Marten Mooij YAML paths are separated by fullstops. When you look at your path, it exists of two parts: the first part of the path is "Ghosts", and the second (child) part is "". Now, if you read the error, you notice that it complains about an empty path, right? And if we look at the explanation I wrote a few lines back, your second part is an empty String.

    Since this is actually hardcoded, it is up to you how you fix it, as long as you have something after "Ghosts.".
     
  6. Offline

    Marten Mooij

  7. Offline

    fireblast709

    Marten Mooij ghosts is null (in the main class). Also note that you don't need to invoke toString(), you should be able to save Sets directly.
     
  8. Offline

    Marten Mooij

    fireblast709 I check to see whether or not ghosts is null and only if ghosts contains something will it save the UUIDs to the StringList.
    Code:java
    1. if(!(ghosts.isEmpty())){
    2. GhostData.getConfig().set("Ghosts", ghosts);
    3. GhostData.saveConfig();
    4. }
    5. }


    Having this problem whenever I use custom files, any help is appreciated.

    It works but now it seems to be doing this:
    Code:
    Ghosts:
    - !!java.util.UUID {}
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  9. Offline

    FerusGrim

    When you save a UUID to a source outside of Java, you need to convert it to a String, using UUID#toString.

    When you load the UUID from an outside source, use: UUID#fromString.
     
  10. Offline

    Marten Mooij

    FerusGrim If I do:
    Code:java
    1. public void onDisable(){
    2. Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.DARK_RED + "[Ghosty] Is Now Disabled.");
    3.  
    4. if(!(ghosts.isEmpty())){
    5. GhostData.getConfig().set("Ghosts", ghosts.toString());
    6. GhostData.saveConfig();
    7. }
    8. }

    It makes it this:
    Code:
    Ghosts: '[b0ec0f58-5935-45bd-bfa1-d4b9b14a715d]'
    
    And I want it to be a StringList.
     
  11. Offline

    CraftCreeper6

    Marten Mooij
    Create a List of Strings
    Code:java
    1. List<String> myList = new LinkedList<String>();


    Add all of the current config list using the addAll(); method.

    Then add the ghost to that list using the add(); method.

    Save the config path using:
    Code:java
    1. MyConfig.set("Ghosts", myList);


    Save your config.
     
  12. Offline

    Marten Mooij

  13. Offline

    CraftCreeper6

    Marten Mooij
    Same concept.

    EDIT: Change String to UUID.
     
  14. Offline

    Marten Mooij

    CraftCreeper6 "Add all of the current config list using the addAll(); method.
    Then add the ghost to that list using the add(); method."
    You're going to have to show me an example since I do not get what you mean by that.
     
  15. Offline

    CraftCreeper6

    Marten Mooij
    Change it back to String.
    Instead of using
    add(player.getUniqueId());

    Use
    add(player.getUniqueId().toString());
     
  16. Offline

    Marten Mooij

    Code:java
    1. Main.ghosts.add(player.getUniqueId().toString());

    Gives me this:
    Code:
    The method add(UUID) in the type List<UUID> is not applicable for the arguments (String)
    EDIT: Changing the ArrayList to use <String>
     
  17. Offline

    CraftCreeper6

    Marten Mooij
    Like I said, change
    Code:java
    1. List<UUID> myList = new LinkedList<UUID>();


    To:
    Code:java
    1. List<String> myList = new LinkedList<String>();
     
  18. Offline

    Marten Mooij

  19. Offline

    CraftCreeper6

  20. Offline

    FerusGrim

    Something is throwing a NullPointerException.

    What line is on line 50? My guess is that you cannot refer to your config using a static call to GhostData.
     
  21. Offline

    fireblast709

    Marten Mooij
    • You try to use a UUID as a name, use UUID.fromString(String) and use that UUID object.
    • They might not be online, thus returning null.
     
  22. Offline

    FerusGrim

    Thinking on this, it's probably a combination of the two.

    Using a string in getPlayerExact() would look for a username with the UUID of the name. A player with that name would never be online, thus returning null. =p
     
  23. Offline

    Marten Mooij

    FerusGrim fireblast709 Updated code:
    Code:java
    1. public static void restoreGhosts(){
    2. for(String uuids : GhostData.getConfig().getStringList("Ghosts")){
    3.  
    4. Player players = Bukkit.getServer().getPlayer(UUID.fromString(uuids));
    5.  
    6. if(players.isOnline()){
    7. players.setGameMode(GameMode.ADVENTURE);
    8. }
    9. }
    10. }
    11. }
    12.  

    The error is on line 33 which is: for(String uuids : GhostData.getConfig().getStringList("Ghosts")){
     
  24. Offline

    fireblast709

    Marten Mooij
    • FerusGrim was right about this one, getConfig() returns null.
    • players.isOnline() is a NullPointerException when that is fixed. isOnline() only works for OfflinePlayers, check null when using Players.
     
  25. Offline

    FerusGrim

    Can we see the config.yml? It's possible the NPE is caused by Ghosts not existing in a proper state to be called.

    However, I'm thinking the issue is with statically referring to GhostData.
     
  26. Offline

    Marten Mooij

    fireblast709 FerusGrim Here is the GhostData file:
    Code:
    Ghosts:
    - b0ec0f58-5935-45bd-bfa1-d4b9b14a715d
    
    Updated code how I think it should be:
    Code:java
    1. public static void restoreGhosts(){
    2. for(String uuids : GhostData.getConfig().getStringList("Ghosts")){
    3.  
    4. Player players = Bukkit.getServer().getPlayer(UUID.fromString(uuids));
    5.  
    6. if(players == null){
    7. return;
    8. }
    9.  
    10. if(players.isOnline()){
    11. players.setGameMode(GameMode.ADVENTURE);
    12. }
    13. }
    14. }
    15. }
    16.  

    How exactly is GhostData.getConfig() return null?
    Thanks so much for your help guys :)
     
  27. Offline

    FerusGrim

    Because getConfig() is something that relies on your class Object, rather than the file itself. Meaning that getConfig(), unless instantiated, is null. Meaning you can't refer to it statically.

    There are two methods that are popular for solving this issue. getInstance(), or putting a constructor in your other classes which saves the class instance.

    getInstance():
    Code:java
    1. public class GhostData extends JavaPlugin {
    2. private static GhostData instance;
    3.  
    4. @Override
    5. public void onEnable() {
    6. instance = this;
    7. }
    8.  
    9. public static GhostData getInstance() {
    10. return instance;
    11. }
    12. }
    13. // Then, do something like this:
    14. GhostData.getInstance().getConfig().getStringList("Ghosts");


    Or, using a constructor:
    Code:java
    1. public class SomeClass {
    2. private final GhostData plugin;
    3.  
    4. public SomeClass(GhostData plugin) {
    5. this.plugin = plugin;
    6. }
    7.  
    8. public void someMethod() {
    9. // Then, do this:
    10. plugin.getConfig().getStringList("Ghosts");
    11. }
    12. }
    13.  
    14. // In the class that extends JavaPlugin:
    15. @Override
    16. public void onEnable() {
    17. new SomeClass(this);
    18. }
    19.  
     
  28. Offline

    Marten Mooij

    Code:java
    1. public static void restoreGhosts(){
    2. for(String uuids : GhostData.getConfig().getStringList("Ghosts")){
    3.  
    4. Player players = Bukkit.getServer().getPlayerExact(uuids);
    5.  
    6. if(players == null){
    7. return;
    8. }
    9.  
    10. players.setGameMode(GameMode.ADVENTURE);
    11. }
    12. }
    13. }

    Here is the GhostData class:
    Code:java
    1. package me.lalala;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5.  
    6. import me.lalalalala.Ghosty.Main;
    7.  
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.configuration.file.FileConfiguration;
    11. import org.bukkit.configuration.file.YamlConfiguration;
    12.  
    13. public class GhostData{
    14.  
    15. public static FileConfiguration config;
    16. public static File file = new File(Main.getPlugin().getDataFolder(), "GhostData");
    17.  
    18. public static void createFile(){
    19. if(!(file.exists())){
    20. try{
    21. file.createNewFile();
    22. }catch(Exception ex){
    23. Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.DARK_RED + "Failed to generate the file: GhostData");
    24. }
    25. config = YamlConfiguration.loadConfiguration(file);
    26. config.createSection("Ghosts");
    27. saveConfig();
    28. }
    29. }
    30.  
    31. public static FileConfiguration getConfig(){
    32. return config;
    33. }
    34.  
    35. public static void reloadConfig(){
    36. config = YamlConfiguration.loadConfiguration(file);
    37. }
    38.  
    39. public static void saveConfig(){
    40. try{
    41. config.save(file);
    42. }catch(IOException ex){
    43. Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.DARK_RED + "Could not save the file: GhostData");
    44. }
    45. }
    46. }

    Not sure how this can return null...
     
    Last edited by a moderator: Apr 10, 2016
  29. Offline

    FerusGrim

    Egh. Sorry, I was assuming you were using the inherited JavaPlugin#getConfig.
     
  30. Offline

    Marten Mooij

    FerusGrim So what can be the problem then :?

    BUMP, any help is appreciated.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
Thread Status:
Not open for further replies.

Share This Page