Solved Restart is bugging my spigot

Discussion in 'Spigot Plugin Development' started by Alex0589, Jul 13, 2018.

  1. Hi spigot Users., I'm devloping a plugin for a user that wants it in 1.8( I also hate 1.8) and there's something weird going on: I have some objects and every time a new one is created I save them in a config (
    Code (YAML):
    04c971e6-7d57-44d2-8714-234a423b0846:
      uuid
    : 04c971e6-7d57-44d2-8714-234a423b0846
      chest
    :
        world
    : world
        x
    : 176.0
        y
    : 64.0
        z
    : 188.0
      doubleChest
    :
        world
    : world
        x
    : 176.0
        y
    : 64.0
        z
    : 187.0
      sign
    :
        world
    : world
        x
    : 175.0
        y
    : 64.0
        z
    : 188.0
      faction
    : '6'
      role
    : ADMIN
      isDoubleChest
    : true
      isRaiadable
    : false
    UUIDs-List
    :
    - 04c971e6-7d57-44d2-8714-234a423b0846
    - 87085535-2707-4595-a3e7-8ff6c994e8d9
    - 6c4fa0b3-ac93-498e-a527-2eb1c66e2050
    - 318079ea-a62f-4a42-bb53-de12494f0bc3
    - 67319913-3bc7-4861-962b-286531e733a7
    87085535-2707-4595-a3e7-8ff6c994e8d9
    :
      uuid
    : 87085535-2707-4595-a3e7-8ff6c994e8d9
      chest
    :
        world
    : world
        x
    : 172.0
        y
    : 64.0
        z
    : 188.0
      doubleChest
    :
        world
    : world
        x
    : 172.0
        y
    : 64.0
        z
    : 187.0
      sign
    :
        world
    : world
        x
    : 173.0
        y
    : 64.0
        z
    : 188.0
      faction
    : '6'
      role
    : ADMIN
      isDoubleChest
    : true
      isRaiadable
    : false
    6c4fa0b3-ac93-498e-a527-2eb1c66e2050
    :
      uuid
    : 6c4fa0b3-ac93-498e-a527-2eb1c66e2050
      chest
    :
        world
    : world
        x
    : 172.0
        y
    : 64.0
        z
    : 187.0
      doubleChest
    :
        world
    : world
        x
    : 172.0
        y
    : 64.0
        z
    : 188.0
      sign
    :
        world
    : world
        x
    : 173.0
        y
    : 64.0
        z
    : 187.0
      faction
    : '6'
      role
    : MODERATOR
      isDoubleChest
    : true
      isRaiadable
    : false
    318079ea-a62f-4a42-bb53-de12494f0bc3
    :
      uuid
    : 318079ea-a62f-4a42-bb53-de12494f0bc3
      chest
    :
        world
    : world
        x
    : 173.0
        y
    : 64.0
        z
    : 189.0
      doubleChest
    :
        world
    : world
        x
    : 174.0
        y
    : 64.0
        z
    : 189.0
      sign
    :
        world
    : world
        x
    : 173.0
        y
    : 64.0
        z
    : 188.0
      faction
    : '6'
      role
    : ADMIN
      isDoubleChest
    : true
      isRaiadable
    : false
    67319913-3bc7-4861-962b-286531e733a7
    :
      uuid
    : 67319913-3bc7-4861-962b-286531e733a7
      chest
    :
        world
    : world
        x
    : 173.0
        y
    : 63.0
        z
    : 185.0
      doubleChest
    :
        world
    : world
        x
    : 172.0
        y
    : 63.0
        z
    : 185.0
      sign
    :
        world
    : world
        x
    : 173.0
        y
    : 63.0
        z
    : 186.0
      faction
    : '6'
      role
    : ADMIN
      isDoubleChest
    : true
      isRaiadable
    : false
    and I save this data in this way(
    Code (Text):
     File file = new File(plugin.getDataFolder(), "Signs.yml");
            YamlConfiguration fc = YamlConfiguration.loadConfiguration(file);
            List<String> uuids = fc.getStringList("UUIDs-List");

            fc.set(customSign.geId().toString() + ".uuid", customSign.geId().toString());
            if(customSign.getChest() != null) {
                fc.set(customSign.geId().toString() + ".chest.world", customSign.getChest().getLocation().getWorld().getName());
                fc.set(customSign.geId().toString() + ".chest.x", customSign.getChest().getLocation().getX());
                fc.set(customSign.geId().toString() + ".chest.y", customSign.getChest().getLocation().getY());
                fc.set(customSign.geId().toString() + ".chest.z", customSign.getChest().getLocation().getZ());
            }
            if(customSign.getSecondChest() != null) {
                fc.set(customSign.geId().toString() + ".doubleChest.world", customSign.getSecondChest().getLocation().getWorld().getName());
                fc.set(customSign.geId().toString() + ".doubleChest.x", customSign.getSecondChest().getLocation().getX());
                fc.set(customSign.geId().toString() + ".doubleChest.y", customSign.getSecondChest().getLocation().getY());
                fc.set(customSign.geId().toString() + ".doubleChest.z", customSign.getSecondChest().getLocation().getZ());
            }
            fc.set(customSign.geId().toString() + ".sign.world", customSign.getSign().getLocation().getWorld().getName());
            fc.set(customSign.geId().toString() + ".sign.x", customSign.getSign().getLocation().getX());
            fc.set(customSign.geId().toString() + ".sign.y", customSign.getSign().getLocation().getY());
            fc.set(customSign.geId().toString() + ".sign.z", customSign.getSign().getLocation().getZ());

            fc.set(customSign.geId().toString() + ".faction", customSign.getFaction().getId());
            fc.set(customSign.geId().toString() + ".role", customSign.getRole().name());
            fc.set(customSign.geId().toString() + ".isDoubleChest", customSign.isDoubleChest());
            fc.set(customSign.geId().toString() + ".isRaiadable", customSign.isRaidable());

            uuids.add(customSign.geId().toString());
            fc.set("UUIDs-List",uuids);

            saveConfig(file,fc);
    and here's the saveCOnfig method
    Code (Text):
        private void saveConfig(File file,YamlConfiguration fc){
            try {
                fc.save(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    . Now everytime that the server is reloaded or restarted I ru this method in my onEnable(
    Code (Text):
      //Run every time in the onEnable to recreate the objects
        private void recreateObjects(){
            //Configs
            File file = new File(getDataFolder(), "Signs.yml");
            YamlConfiguration fc = YamlConfiguration.loadConfiguration(file);
            for (String temp : fc.getStringList("UUIDs-List")) {
                System.out.println(temp);
                UUID uuid = UUID.fromString(fc.getString(temp + ".uuid"));
                Chest chest;
                Chest doubleChest = null;
                boolean isDoubleChest = fc.getBoolean(temp + ".isDoubleChest");

                if (isDoubleChest) {
                    Block tempChest = Bukkit.getWorld(fc.getString(temp + ".doubleChest.world")).getBlockAt(new Location(Bukkit.getWorld(fc.getString(temp + ".doubleChest.world")), fc.getDouble(temp + ".doubleChest.x"), fc.getDouble(temp + ".doubleChest.y"), fc.getDouble(temp + ".doubleChest.z")));
                    if (tempChest.getType() != Material.CHEST) {
                        return;
                    }
                    doubleChest = (Chest) tempChest.getState();

                    Block chestBlock = Bukkit.getWorld(fc.getString(temp + ".chest.world")).getBlockAt(new Location(Bukkit.getWorld(fc.getString(temp + ".chest.world")), fc.getDouble(temp + ".chest.x"), fc.getDouble(temp + ".chest.y"), fc.getDouble(temp + ".chest.z")));
                    if (chestBlock.getType() != Material.CHEST) {
                        return;
                    }
                    chest = (Chest) chestBlock.getState();

                } else {
                    Block chestBlock = Bukkit.getWorld(fc.getString(temp + ".chest.world")).getBlockAt(new Location(Bukkit.getWorld(fc.getString(temp + ".chest.world")), fc.getDouble(temp + ".chest.x"), fc.getDouble(temp + ".chest.y"), fc.getDouble(temp + ".chest.z")));
                    if (chestBlock.getType() != Material.CHEST) {
                        return;
                    }
                    chest = (Chest) chestBlock.getState();
                }

                Block block = Bukkit.getWorld(fc.getString(temp + ".sign.world")).getBlockAt(new Location(Bukkit.getWorld(fc.getString(temp + ".sign.world")), fc.getDouble(temp + ".sign.x"), fc.getDouble(temp + ".sign.y"), fc.getDouble(temp + ".sign.z")));

                CustomSign sign = new CustomSign(uuid, chest, doubleChest, block, Factions.getInstance().getFactionById(fc.getString(temp + ".faction")), Role.valueOf(fc.getString(temp + ".role")), isDoubleChest, fc.getBoolean(temp + ".isRaidable"));
                manager.addCustomSign(sign);
            }
     
    and the omfg comes here: my list contains lots of strings but the System.out.println() only prints out the first one. Even more funny is the fact that if I try to get what's inside the same list with a runTaskLater() all the uuids are there. Can someone help me?
     
  2. From a quick look it looks like in recreateObjects(), you should use 'continue' instead of 'return' since return exits the whole method. You could try printing a debug message on the chest material checks to see if those are in fact failing (so block is not a chest).

    But also, it looks like you don't even need the whole UUIDs-List in the config, you could just loop over all the sections by getting the keys with configuration.getKeys(false). You also don't need to save the UUID in the 'uuid' key of each section, you can just use the section name as the UUID.

    So:
    Code (Java):
    Set<String> keys = config.getKeys(false);
    for (String key : keys) {
        UUID uid = UUID.fromString(key);
        // do whatever you'd normally do when looping over each entry in UUIDs-List
    }
     
    • Informative Informative x 1
  3. Thanks for the help I'll try
     
  4. Thanks so much everything is working now
     

Share This Page