Solved Config file dupes each time I reload HELP

Discussion in 'Spigot Plugin Development' started by Jumbo_1907, May 25, 2017.

Thread Status:
Not open for further replies.
  1. Hey,
    I made a blockbreaker plugin and I'm saving my locations in the config file but each time I reload the config file dupes. I started with 3 locations and now i'm over 90000 locatations....
    Please help!

    Main class:
    Code (Text):
    package me.jumbo1907.bockbreaker.main;

    import java.util.ArrayList;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.inventory.ItemFlag;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.util.Vector;

    import me.jumbo1907.blockbreaker.commands.GiveBB;
    import me.jumbo1907.blockbreaker.events.AddBB;
    import me.jumbo1907.blockbreaker.events.RemoveBB;
    import me.jumbo1907.blockbreaker.lib.ParticleEffect;

    public class Main extends JavaPlugin {

        public static ArrayList<Block> bb = new ArrayList<Block>();
        public static ArrayList<Material> blacklist = new ArrayList<Material>();
       
        public static Main plugin;
        public void onEnable() {
       

            ArrayList<String> slist = new ArrayList<String>(getConfig().getStringList("Locations"));
            if(!slist.isEmpty()){
            for(String s : slist){
                String[] split = s.split(",");
                for(World wo : Bukkit.getWorlds()){
                World w = wo;
                double x = Double.parseDouble(split[0]);
                double y = Double.parseDouble(split[1]);
                double z = Double.parseDouble(split[2]);
                Location l = new Location(w,x,y,z);
                Main.bb.add(l.getBlock());
            }
            }
            }

           
            registerCommands();
            registerEvents();
            blockBreakRun();
            addBList();
            configManager();

        }
       
        public void onDisable(){
            ArrayList<String> klist = new ArrayList<String>();
            for(Block b : bb){
                Location l = b.getLocation();
                String s = Var.configAdd(l.getX(), l.getY(), l.getZ());
                klist.add(s);
            }
            getConfig().set("Locations", klist);
            saveConfig();


       
        }

        public void registerEvents() {
            getServer().getPluginManager().registerEvents(new AddBB(), this);
            getServer().getPluginManager().registerEvents(new RemoveBB(), this);

        }

        public void registerCommands() {
            getCommand("bb").setExecutor(new GiveBB());
        }
        public void configManager(){
            getConfig().options().copyDefaults(true);
            saveConfig();
           
        }

       
        public void addBList(){
            blacklist.add(Material.BEDROCK);
            blacklist.add(Material.HOPPER);
            blacklist.add(Material.FURNACE);
            blacklist.add(Material.OBSIDIAN);
            blacklist.add(Material.BARRIER);
            blacklist.add(Material.COMMAND);
            blacklist.add(Material.DROPPER);
            blacklist.add(Material.DISPENSER);
            blacklist.add(Material.CHEST);
            blacklist.add(Material.TRAPPED_CHEST);
            blacklist.add(Material.WATER);
            blacklist.add(Material.STATIONARY_WATER);
            blacklist.add(Material.LAVA);
            blacklist.add(Material.STATIONARY_LAVA);

        }
        public static ItemStack items() {
            ItemStack tear = new ItemStack(Material.GHAST_TEAR);
            ItemMeta tearm = tear.getItemMeta();
            tearm.setDisplayName(Var.itemname);
            tearm.addEnchant(Enchantment.SILK_TOUCH, 1, true);
            tearm.addItemFlags(ItemFlag.HIDE_ENCHANTS);

            tear.setItemMeta(tearm);
            return tear;

        }

        public void blockBreakRun() {
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                    if (!(bb.isEmpty())) {
                        for (Block b : bb) {
                            ParticleEffect.BARRIER.display(0, 0, 0, 0, 1, b.getLocation().add(0.5, 1.5, 0.5), 200);

                            Location l = b.getLocation().add(0, 1, 0);
                            if(!b.getType().equals(Material.HOPPER)){
                                Main.bb.remove(b);
                                ArrayList<String> slist = new ArrayList<String>(getConfig().getStringList("Locations"));
                                String s = Var.configAdd(l.getX(), l.getY(), l.getZ());
                                slist.remove(s);
                                getConfig().set("Locations", slist);
                                saveConfig();
                            } else {
                            if(blacklist.contains(l.getBlock().getType())){
                            ParticleEffect.REDSTONE.display(0.02f, 0.02f, 0.02f, 0, 5, b.getLocation().add(0.0,2,0.0), 200);
                            ParticleEffect.REDSTONE.display(0.02f, 0.02f, 0.02f, 0, 5, b.getLocation().add(0.0,2,1), 200);
                            ParticleEffect.REDSTONE.display(0.02f, 0.02f, 0.02f, 0, 5, b.getLocation().add(1,2,0.0), 200);
                            ParticleEffect.REDSTONE.display(0.02f, 0.02f, 0.02f, 0, 5, b.getLocation().add(1,2,1), 200);
                            ParticleEffect.REDSTONE.display(0.02f, 0.02f, 0.02f, 0, 5, b.getLocation().add(0.0,1,0.0), 200);
                            ParticleEffect.REDSTONE.display(0.02f, 0.02f, 0.02f, 0, 5, b.getLocation().add(0.0,1,1), 200);
                            ParticleEffect.REDSTONE.display(0.02f, 0.02f, 0.02f, 0, 5, b.getLocation().add(1,1,0.0), 200);
                            ParticleEffect.REDSTONE.display(0.02f, 0.02f, 0.02f, 0, 5, b.getLocation().add(1,1,1), 200);
                            b.getWorld().playSound(b.getLocation(), Sound.VILLAGER_NO, 1, 1);

                            } else if(b.getLocation().add(0, 1, 0).getBlock().getType().equals(Material.AIR)){
                                Vector v1 = new Vector(0,1,1);
                                Vector v2 = new Vector(0,1,-1);
                                Vector v3 = new Vector(-1,1,0);
                                Vector v4 = new Vector(1,1,0);
                                ParticleEffect.FLAME.display(v1,0.05f, b.getLocation().add(0.5,1,0.5), 200);
                                ParticleEffect.FLAME.display(v2,0.05f, b.getLocation().add(0.5,1,0.5), 200);
                                ParticleEffect.FLAME.display(v3,0.05f, b.getLocation().add(0.5,1,0.5), 200);
                                ParticleEffect.FLAME.display(v4,0.05f, b.getLocation().add(0.5,1,0.5), 200);

                            } else {
                            l.getBlock().breakNaturally();
                            Vector v = new Vector(0,1,0);
                            ParticleEffect.FLAME.display(v,0.05f, b.getLocation().add(0.0,1,0.0), 200);
                            ParticleEffect.FLAME.display(v,0.05f, b.getLocation().add(0.0,1,1), 200);
                            ParticleEffect.FLAME.display(v,0.05f, b.getLocation().add(1,1,0.0), 200);
                            ParticleEffect.FLAME.display(v,0.05f, b.getLocation().add(1,1,1), 200);

                            b.getWorld().playSound(b.getLocation(), Sound.NOTE_PLING, 1, 1);
                        }
                    }
                        }
                    }

                }
            }, 60, 60);
        }

    }
     
     
  2. Please help my config is getting spammed
     
  3. In your onDisable, what is this?
    Code (Text):
    String s = Var.configAdd(l.getX(), l.getY(), l.getZ());
    I don't suppose it could be generating the problem?
     
  4. Its just a stringbuilder for my config (x,y,z)
     
  5. Let me pull your main class into a workspace, I'll take a quick look around. What is "Var"? Is it a separate class? I won't be able to use it if that's the case (I don't have access to it).
     
  6. I will send you the class in PM
     
  7. Bumb
    Please help me no replies so far
     
  8. Strahan

    Benefactor

    It would help if you actually posted the classes you use so we can see what's going on rather than PMing them to people where we can't.
     
  9. Run configManager(); first in your onEnable else it will generate a new config file.
     
  10. Still not working...
     
  11. Can you post a few iterations of your config (i.e. start with no config, run it once, post the config, run it again, add the config. Those two should give us an idea what duplicates exactly.

    Also,
    • Don't make fields public if they're not final and immutable.
    • Don't use static as an access modifier (i.e. to access a field in another class), that's not what static is for.
    • Program against abstractions rather than implementations, i.e. use List as field/variable type rather than ArrayList
     
Thread Status:
Not open for further replies.