Saving arena objects to config not working

Discussion in 'Plugin Development' started by bennie3211, Apr 21, 2016.

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

    bennie3211

    Hello,

    I'm trying to save an arena object to a config, but It gives weird values back when saving it to the config. Does anyone knows what I do wrong?

    This is what I use to load and save the arena objects:
    Code:
    package me.gonnakillyou2.arena;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.configuration.file.FileConfiguration;
    
    import me.gonnakillyou2.infected.Infected;
    
    public class ArenaHandler {
    
        private Infected plugin;
    
        public ArenaHandler(Infected plugin) {
            this.plugin = plugin;
        }
    
        @SuppressWarnings("deprecation")
        public void loadArenas() {
            FileConfiguration c = getPlugin().getArenaConfiguration().getArenaConfig();
    
            if (!c.isSet("Arena"))
                return;
           
            for (String arenaName : c.getConfigurationSection("Arena").getKeys(false)) {
                String path = "Arena." + arenaName + ".";
    
                Set<Location> zombieSpawnpoints = new HashSet<Location>();
                Set<Location> misteryboxs = new HashSet<Location>();
                Set<Door> doors = new HashSet<Door>();
    
                Integer maxPlayers = c.getInt(path + "MaximumPlayers", 100);
                Integer startingPoints = c.getInt(path + "StartPoints", 500);
                Integer misteryboxPoints = c.getInt(path + "MisteryboxPoints", 900);
                Integer teleporterPoints = c.getInt(path + "TeleporterPoints", 500);
                Integer zombieHitPoints = c.getInt(path + "ZombieHitPoints", 5);
                Integer zombieKillPoints = c.getInt(path + "ZombieKillPoints", 10);
               
                Location spawnpoint = getPlugin().getLocationUtil().strToLoc(c.getString(path + "SpawnpointLocation"));
               
                if (c.isSet(path + "ZombieSpawnpointLocation")) {
                    for (String i : c.getStringList(path + "ZombieSpawnpointLocation")) {
                        zombieSpawnpoints.add(getPlugin().getLocationUtil().strToLoc(i));
                    }
                }
               
                if (c.isSet(path + "MisteryboxLocation")) {
                    for (String i : c.getStringList(path + "MisteryboxLocation")) {
                        misteryboxs.add(getPlugin().getLocationUtil().strToLoc(i));
                    }
                }
               
                for (String doorName : c.getConfigurationSection(path + "Doors").getKeys(false)) {
                    String doorPath = path + "Doors." + doorName + ".";
                   
                    Map<Location, Material> blocks = new HashMap<Location, Material>();
                    Map<Location, Byte> data = new HashMap<Location, Byte>();
                    Set<Location> paySigns = new HashSet<Location>();
                   
                    int price = c.getInt(doorPath + "Price", 999999);
                   
                    for (String i : c.getStringList(doorPath + "Blocks")) {
                        String[] str = i.split("\\:");
                        Location l;
                       
                        if (str.length != 6)
                            continue;
                       
                        l = getPlugin().getLocationUtil().strToLoc(str[0] + ":" + str[1] + ":" + str[2] + ":" + str[3]);
                       
                        if (l == null)
                            continue;
                       
                        try {
                            blocks.put(l, Material.getMaterial(Integer.parseInt(str[4])));
                            data.put(l, (byte) Byte.parseByte(str[5]));
                        } catch (NumberFormatException e) {
                            System.out.println("[Inf] Error while loading door " + doorName.toUpperCase() + "!");
                            continue;
                        }
                    }
                   
                    for (String i : c.getStringList(doorPath + "PaySigns")) {
                        Location l = getPlugin().getLocationUtil().strToLoc(i);
                       
                        if (l == null)
                            continue;
                       
                        paySigns.add(l);
                    }
                   
                    Door door = new Door(doorName, price, blocks, data, paySigns);
                    doors.add(door);
                }
               
                Arena arena = new Arena(arenaName, spawnpoint, maxPlayers, startingPoints, misteryboxPoints, teleporterPoints, zombieHitPoints, zombieKillPoints, doors, misteryboxs, zombieSpawnpoints);
                getPlugin().getArenaManager().getArena().add(arena);
    
               //Debugging values
                System.out.println("Arena: " + arenaName +
                                    "\nspawnpoint: " + spawnpoint +
                                    "\nmaxPlayers: " + maxPlayers +
                                    "\nstartingPoints: " + startingPoints +
                                    "\nzombieHitPoints: " + zombieHitPoints +
                                    "\nzombieKillPoints: " + zombieKillPoints +
                                    "\nmisteryboxPoints: " + misteryboxPoints +
                                    "\nteleporterPoints: " + teleporterPoints +
                                    "\nzombieSpawnpoints: " + zombieSpawnpoints +
                                    "\nmisteryboxPoints: " + misteryboxs);
            }
        }
    
        @SuppressWarnings("deprecation")
        public void saveArenas() {
            FileConfiguration c = getPlugin().getArenaConfiguration().getArenaConfig();
           
            for (Arena a : getPlugin().getArenaManager().getArena()) {
                String path = "Arena." + a.getName() + ".";
                List <String> locs = new ArrayList<String>();
               
                c.set(path + "MaximumPlayers", a.getMax());
                c.set(path + "StartPoints", a.getStartPoints());
                c.set(path + "MisteryboxPoints", a.getMisteryboxPoints());
                c.set(path + "TeleporterPoints", a.getTeleporterPoints());
                c.set(path + "ZombieHitPoints", a.getZombieHitPoints());
                c.set(path + "ZombieKillPoints", a.getZombieKillPoints());
                c.set(path + "SpawnpointLocation", getPlugin().getLocationUtil().locToStr(a.getSpawnpoint(), true));
               
                locs.clear();
                for (Location l : a.getInfectedSpawnLocations()) {
                    locs.add(getPlugin().getLocationUtil().locToStr(l, true));
                }
                c.set(path + "ZombieSpawnpointLocation", locs);
               
                locs.clear();
                for (Location l : a.getMisteryboxLocations()) {
                    locs.add(getPlugin().getLocationUtil().locToStr(l, false));
                }
                c.set(path + "MisteryboxLocation", locs);
               
                for (Door door : a.getDoors()) {
                    String doorPath = path + "Doors." + door.getName() + ".";
                   
                    locs.clear();
                    for (Location l : door.getBlocks().keySet()) {
                        String locData = getPlugin().getLocationUtil().locToStr(l, false);
                       
                        int ID = door.getBlocks().get(l).getId();
                        byte data = 0;
                       
                        if (door.getBlockData().containsKey(l))
                            data = door.getBlockData().get(l);
                       
                        locs.add(locData + ":" + ID + ":" + data);
                    }
                    c.set(doorPath + "Blocks", locs);
                   
                    locs.clear();
                    for (Location l : door.getPaySigns()) {
                        locs.add(getPlugin().getLocationUtil().locToStr(l, false));
                    }
                    c.set(doorPath + "PaySigns", locs);
                   
                    c.set(doorPath + "Price", door.getPrice());
                }
               
                getPlugin().getArenaConfiguration().save();
            }
        }
    
        /**
         * @return the plugin
         */
        public Infected getPlugin() {
            return plugin;
        }
    }
    
    And if I start with the server the config looks like this:
    Code:
    Arena:
      test:
        MaximumPlayers: 234
        StartPoints: 123
        MisteryboxPoints: 125
        TeleporterPoints: 138
        ZombieHitPoints: 11
        ZombieKillPoints: 49
        SpawnpointLocation: world:-16:62:-70:-135:0
        ZombieSpawnpointLocation:
        - world:20:80:30:20:135
        - world:21:80:30:20:130
        - world:22:80:30:20:130
        MisteryboxLocation:
        - world:20:80:30
        - world:21:81:31
        Doors:
          Dirt:
            Blocks:
            - world:-8:64:-75:3:0
            - world:-8:64:-74:1:0
            - world:-8:63:-75:1:0
            - world:-8:63:-74:3:0
            - world:-8:62:-75:1:0
            - world:-8:62:-74:3:0
            PaySigns:
            - world:-9:63:-73
            - world:-7:63:-73
            Price: 100
    And when I save the arena objects it looks like this:
    Code:
    Arena:
      test:
        MaximumPlayers: 234
        StartPoints: 123
        MisteryboxPoints: 125
        TeleporterPoints: 138
        ZombieHitPoints: 11
        ZombieKillPoints: 49
        SpawnpointLocation: world:-16.0:62.0:-70.0:-135.0:0.0
        ZombieSpawnpointLocation: &id001
        - world:-9.0:63.0:-73.0
        - world:-7.0:63.0:-73.0
        MisteryboxLocation: *id001
        Doors:
          Dirt:
            Blocks: *id001
            PaySigns: *id001
            Price: 100
    
    Can someone tell me why there comes *id001 and &id001 inside the config? I think it has to be something with the saving method or that I use 1 list to save multiple things, but I'm not sure.

    Thnx
     
  2. Offline

    Abstract97

    @bennie3211 Perhaps try using a different character that you split your locations with, instead of using the colon ':' character use a comma ',' then let me know if the problem still persists.
     
  3. Offline

    bennie3211

    Okay, I tried that, to replace the ':' with ',' and this was the result when saving in the config:

    Code:
    Arena:
      test:
        MaximumPlayers: 234
        StartPoints: 123
        MisteryboxPoints: 125
        TeleporterPoints: 138
        ZombieHitPoints: 11
        ZombieKillPoints: 49
        SpawnpointLocation: world,-16.0,62.0,-70.0,-135.0,0.0
        ZombieSpawnpointLocation: &id001
        - world,-9.0,63.0,-73.0
        - world,-7.0,63.0,-73.0
        MisteryboxLocation: *id001
        Doors:
          Dirt:
            Blocks: *id001
            PaySigns: *id001
            Price: 100
    And for the complete information I will give my LocationUtil class:

    Code:
    package me.gonnakillyou2.util;
    
    import org.bukkit.Location;
    
    import me.gonnakillyou2.infected.Infected;
    
    public class LocationUtil {
    
        private Infected plugin;
    
        public LocationUtil(Infected plugin) {
            this.plugin = plugin;
        }
    
        public String locToStr(Location l, boolean yp) {
            if (yp)
                return l.getWorld().getName() + "," + l.getX() + "," + l.getY() + "," + l.getZ() + "," + l.getYaw() + ","
                        + l.getPitch();
            return l.getWorld().getName() + "," + l.getX() + "," + l.getY() + "," + l.getZ();
        }
    
        public Location strToLoc(String str) {
            String[] l = str.split("\\,");
    
            if (l.length == 4)
                return new Location(getPlugin().getServer().getWorld(l[0]), Double.parseDouble(l[1]),
                        Double.parseDouble(l[2]), Double.parseDouble(l[3]));
            if (l.length == 6)
                return new Location(getPlugin().getServer().getWorld(l[0]), Double.parseDouble(l[1]),
                        Double.parseDouble(l[2]), Double.parseDouble(l[3]), Float.parseFloat(l[4]), Float.parseFloat(l[5]));
            return null;
        }
    
        /**
         * @return the plugin
         */
        public Infected getPlugin() {
            return plugin;
        }
    }
    So I don't know if the problem is with the ':' :L

    Btw, the debug message gave the correct results while loading the data (no null or missing locations or numbers), so it has to be something with the saving part of the arena.
     
  4. Offline

    Abstract97

    --Removed--

    Edit: Forget what I said originally, i actually have no idea what's causing that.
     
    Last edited: Apr 21, 2016
  5. Offline

    I Al Istannen

    @bennie3211
    I normally use a custom Class for that, built like this:
    Code:
    /**
    * A serializable version of the {@link Location}. Needed for < 1.8
    */
    public class LocationSerializable implements ConfigurationSerializable {
    And then register the class with the LocationSerialization before loading the config in the onEnable. The class just has some variables (String world, yaw, pitch, x, y, z) and some methods to convert between the two locations).

    But as a short test was successful, the problem is probably with retrieving the data. Have you tried adding a
    "Sysout str" line right under the declaration of the strToLoc to see what the method actually works with?



    IGNORE the above...
    Just read the off post. These values are there to save space.
    Read this. At first glance they seem to behave a bit odd in your file, but as long as it works... :D
     
Thread Status:
Not open for further replies.

Share This Page