Creating a new string deletes entire config.

Discussion in 'Plugin Development' started by OTF Catastrophe, Jul 8, 2016.

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

    OTF Catastrophe

    So inside my parkour plugin I have this bit of code to add an arena name to a string list called "Arenas" and the players location when you to the command /pk create [mapName]. My issue is, when I try doing it, it adds the arena name to Arenas and creates the arena in an arraylist called arenaData, BUT it deletes the whole config. Can anyone check out this big of code and let me know whats wrong with it?


    Code:
                     if (tpcmd.equalsIgnoreCase("create"))
                     {
                        
                         if ((player.hasPermission("pk.create")) || (player.hasPermission("pk.*")))
                         {
                            
                             if (args.length == 2)
                             {
                                
                                 if (!arenaList.contains(args[1]))
                                 {
                                     
                                      arenaList.add(args[1]);
                                      getConfig().set("Arenas", arenaList);
                                     
                                      saveConfig();
                                     
                                     Location tmp = player.getLocation();
                                    
                                      getConfig().set("arenaData." + args[1] + ".x", Double.valueOf(tmp.getX()));
                                      getConfig().set("arenaData." + args[1] + ".y", Double.valueOf(tmp.getY()));
                                      getConfig().set("arenaData." + args[1] + ".z", Double.valueOf(tmp.getZ()));
                                      getConfig().set("arenaData." + args[1] + ".yaw", Float.valueOf(tmp.getYaw()));
                                      getConfig().set("arenaData." + args[1] + ".pitch", Float.valueOf(tmp.getPitch()));
                                      getConfig().set("arenaData." + args[1] + ".world", tmp.getWorld().getName());
                                   
                                      saveConfig();
                                
                                     player.sendMessage(prefix.replace("&", "§") + "§aArena " + args[1] + " has been created!");
                                    
                                 }
                                
                                 else
                                 {
                                    
                                     player.sendMessage(prefix.replace("&", "§") + "§cArena " + args[1] + " has already been created!");
                                    
                                 }
                                
                             }
                            
                             else
                             {
                                
                                 player.sendMessage(prefix.replace("&", "§") + "§cMissing arguments! Correct usage /pk create {mapName}");
                                
                             }
                            
                         }
                        
                         else
                         {
                            
                             player.sendMessage(prefix.replace("&", "§") + config.getString("Messages.noPermissionMessage").replace("&", "§"));
                            
                         }
                        
                         return true;
                        
                     }
     
  2. Offline

    mine-care

    use
    Code:
    ChatColor#translateAlternateColorCodes(char colorChar, String param);
    instead.
    The only fishy thing i see in the code is the method saveConfig(), is that the default method inherited by JavaPlugin or is it a custom method/overwrite?
    Is the config 'deleted' or overwritten?
    Is there any variable in the config that is not manipulated through the code above?
    Have you previously loaded this list from config?
     
  3. Offline

    OTF Catastrophe

    Regarding the translateAlternateColorCodes, if I put that in once in the code is it going to be used on all strings or would I have to keep putting it in every time I want to translate color codes?

    I use the this#saveDefaultConfig(); in the onEnable() to copy over the config from in the plugin itself. Would this cause any issues if I used saveConfig()?

    And also yes the "Arenas" string is put in the config by default with no string list with it. I define it as a string list with "List<String> arenaList = (List<String>) config#getStringList("Arenas");"
     
  4. Offline

    ipodtouch0218

    You already translate "&" into "§" for every string.
    Code:
    player.sendMessage(prefix.replace("&", "§") + "§aArena " + args[1] + " has been created!");
    It would just be
    Code:
    player.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix) + "§aArena " + args[1] + " has been created!");
     
  5. Offline

    mine-care

    Hm. Can you show me how your initial config looks like, and how it looks like after it is overwritten?


    Not really, i thought saveConfig() was a custom method of yourse thats why i asked.


    Not quite, if i understood that correctly, that is technically impossible in Java through the default API. This method accepts as parameter the String with the untranslated color codes, and the character that identifies the color code, (& for example) and retuns a string with translated color codes.
     
  6. Offline

    ipodtouch0218

    You could assign a prefix String and just use prefix after you translated color codes, or you could just use a simple method.
    Code:
    public String getColoredString(String s) {
        String message = getConfig().getString(s); //Get string from config and make new String
        message = ChatColor.translateAlternateColorCodes('&', message); //Translate & into §
        return message; // Returns the message with ChatColor (Used for assigning the variable)
    }
    You can just put this at the begging of your command when you know it's your command
    (after cmd.getName() check)

    EDIT: (sorry for spoonfeed) Anyway, this may throw a NullPointerException if "s" is not in the config. You can either make it throw a NullPointerException or use it in a try-catch statement.
     
  7. Offline

    OTF Catastrophe

    Thank you for clarifying but I still find the method #replace("&", "§") to just be simpler to use instead of adding in another method to be able to do the same thing.

    I have a custom method I use in a FileManager for saving config by calling save(); but it never really ever worked out the way I wanted to as I probably butched the code. The way the config looks at the start of it pasting in from the jar file is as so:

    Code:
    Sheepcraft:
      Prefix: '&8(&eSheepcraft&8) '
      Vault:
        enable: true
        prize: 1000
      disablePotions:
        enable: false
        deniedMessage: '&cPotions are disabled in parkour!'
    Messages:
      noPermissionMessage: '&cYou don''t have permission to do that!'
      reloadMessage: '&aSheepcraft has been reloaded!'
      winMessage:
        Title:
          '1': '&aCongratulations %player%!'
          '2': You beat the parkour!
      failMessage:
        Title:
          '1': '&cYou''ve failed!'
          '2': '&8Back to your last checkpoint!'
      joinMessage:
        Title:
          '1': '&aYou joined parkour!'
          '2': Good luck %player%
      leaveMessage:
        Title:
          '1': '&cYou left parkour!'
          '2': '&8Better luck next time.'
      checkpointMessage:
        Title:
          '1': '&eCheckpoint!'
          '2': ''
    Modifications:
      finishItem:
        itemID: '159:5'
      resetItem:
        itemID: '159:14'
      checkpointItem:
        itemID: '159:4'
      speedItem:
        itemID: '159:3'
        effectMultiplier: 2
        effectTime: 200
      jumpboostItem:
        itemID: '159:10'
        effectMultiplier: 2
        effectTime: 200
      slowItem:
        itemID: '159:8'
        effectMultiplier: 2
        effectTime: 200
      blindnessItem:
        itemID: '159:15'
        effectMultiplier: 10
        effectTime: 200
    Arenas:
    arenaData:
    And when it's updated in the main directory from me creating the arena, it looks like this:

    Code:
    Sheepcraft: {}
    Arenas:
    //Arena Names
    arenaData:
    //Arena Data
     
  8. Offline

    AlvinB

    You need to get the previous config, and edit that. Not make a completely new config, since that will just lose all the data you had.
     
  9. Offline

    mine-care

    It may sound simpler, but it is not future proof, so if for any reason the color character changes, your code will not work whereas the api will be updated thus the ChatColor method will be updated.

    Can i see that method?
     
  10. Offline

    OTF Catastrophe

    So basically I need a config for reading only, and another config for the Arenas? That wouls solve my problem right?


    My FileManager class:

    Code:
    package me.SirApathy.sheepcraftParkour.configuration;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    public class FileManager
    {
       
        private static FileConfiguration Config = null;
        private static File configFile = null;
       
        static FileManager instance = new FileManager();
       
        public static void load()
        {
           
            Config = getConfig();
           
        }
       
       
        public void setup(Plugin p)
        {
           
            File dir = new File("/plugins/SheepcraftParkour");
           
            if (!dir.exists())
            {
               
                dir.mkdir();
               
            }
    
        }
       
        public static void reload()
        {
           
            if (Config == null)
            {
               
                configFile = new File("plugins/SheepcraftParkour/config.yml");
               
                Config = YamlConfiguration.loadConfiguration(configFile);
               
            }
           
        }
       
        public static FileConfiguration getConfig()
        {
           
            if (Config == null)
            {
               
                reload();
               
            }
           
            return Config;
           
        }
       
        public static void save()
        {
           
            if ((Config == null) || (configFile == null))
            {
               
                return;
               
            }
           
            try
            {
               
                Config.save(configFile);
               
            }
           
            catch (IOException ex)
            {
               
                Logger.getLogger(JavaPlugin.class.getName()).log(Level.SEVERE, "Could not save configFile to " + configFile, ex);
               
            }
           
        }
     
        public static FileManager getInstance()
        {
           
            return instance;
           
        }
       
    }
    
     
  11. Offline

    AlvinB

    Why are you creating a completely new system for using the config.yml file? Why not use the standard methods inside the JavaPlugin class?
     
Thread Status:
Not open for further replies.

Share This Page