Solved Saving to Config loop vs not loop

Discussion in 'Spigot Plugin Development' started by Goofan, Jun 20, 2016.

  1. So kinda confusing title I know but it's a bug??:
    I don't really know why it's not working more or less...

    So I got this Code:
    Code (Text):
     * Save objective children to Data.yml
         * @param obj - Objective
         * @param world - World
         * @return void
         */
        private void saveChildren(Objective obj, World world){
            for (Outpost o : obj.getChildren()){
                data.createSection(world.getName() + ".Objectives." + obj.getID() + ".Children");
            }
        }


        /**
         * Save bjectives to Data.yml
         * @param world - World
         * @return void
         */
        private void saveObjectives(World world){
            for(Objective obj : OBJECTIVES){
                if (!obj.getLocation().getWorld().equals(world))
                    continue;
                data.createSection(world.getName() + ".Objectives." + obj.getID());
                data.set(world.getName() + ".Objectives." + obj.getID() + ".Name", obj.getName());
                if (obj.getOwner() != null){
                    data.set(world.getName() + ".Objectives." + obj.getID() + ".Owner", obj.getOwner().getName());
                }
                data.createSection(world.getName() + ".Objectives." + obj.getID() + ".Children");
                //saveChildren(obj, world);
                data.setLocation(data, world.getName() + ".Objectives." + obj.getID() + ".Location", obj.getLocation());
                data.save();
            }
        }
    So I try to save the data to the config, everything besides the children part works.
    It works outside of the for loop
    but not from within the for loop.

    If I commment out:
    data.createSection(world.getName() + ".Objectives." + obj.getID() + ".Children");

    and un-comment:
    //saveChildren(obj, world);
    It wount save to config... What is the fault in my code here? What am I not seeing?

    Thanks in advance,
    Thomas

    P.S. I hope someone out there got an answer for this :/
     
  2. If it's not being run then the objective doesn't have any children. Verify that it actually has children somewhere
     
  3. With a little more error searching (Alot of it)
    it stops on this:
    obj.getID();
    which is the same on the outside as the inside.

    obj.getID() is a Integer and does exist if I try to show in console...

    @Redrield Yes it does have 1 child.
     
  4. All I see is that your saveChildren is doing an extra for-loop.
     
  5. @Hunky524 Yes just for testing it will have + o.getID(); at the end to add all the Children

    Just wanted to display the "bug" as simple as posible
     
  6. Nvm the Edit was not a solution did not work (other code pushed it through...)
     
  7. To Formulate better:
    Why would this work:
    Code (Text):

                    ArrayList<String> list = new ArrayList<String>();
                    for (int i = 0; i < 5; i++){            
                        list.add("Test");
                    }                
                    data.set("path", list);
                    data.save();
    But not this:
    Code (Text):

                    ArrayList<String> list = new ArrayList<String>();
                    for (Outpost o : obj.getChildren()){
                        list.add("Test");
                    }
                    data.set("path", list);
                    data.save();
    And yes there is 3 children in this example.

    Proof:
    http://prntscr.com/bix4ir

    And the config output:
    http://prntscr.com/bix53i
     
  8. Can't even change this:
    Code (Text):
    for (int i = 0; i < 5; i++){
    To this:
    Code (Text):
    for (int i = 0; i < obj.getChildren().size(); i++){
    Because then once again it will not push anything to config :/

    System.out.println(obj.getChildren().size());
    Prints 3.

    My ArrayList Children that my obj.getChildren() is loaded with Outpost Objects
     
    #8 Goofan, Jun 21, 2016
    Last edited: Jun 21, 2016
  9. This is soooo wierd everytime I introduce:
    obj.getChildren().get(i) it breaks.
    but with only obj.getChildren() it works...
    into the loop (even if unused and in a System.out.println(); message) it breaks and wount push to config...

    what is the issue here? Anyone?

    Example codes:
    Breaking Push to config
    Code (Text):

                    ArrayList<String> list = new ArrayList<String>();
                    for (int i = 0; i < 5; i++){
                        try{
                            System.out.println(obj.getChildren().get(i));
                            list.add("test");
                        }catch(Exception e){
                            break;
                        }
                    }
                    data.set("path", list);
    Push to config
    Code (Text):

                    ArrayList<String> list = new ArrayList<String>();
                    for (int i = 0; i < 5; i++){
                        try{
                            System.out.println(obj.getChildren());
                            list.add("test");
                        }catch(Exception e){
                            break;
                        }
                    }
                    data.set("path", list);
     
  10. Can you show the class that has your obj that contains the getChildren method?
     
  11. Sure:

    Code (Text):
    package eu.kingconquest.conquest.io;

    import static org.bukkit.Material.BEACON;
    import static org.bukkit.Material.STAINED_GLASS;

    import java.util.ArrayList;
    import java.util.UUID;

    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;

    import eu.kingconquest.conquest.utils.ColorConverter;

    public class Objective {
        private String name = "";
       
        private UUID uniqueID;
       
        private Capital owner;
       
        private Location location;
        private Location spawn;
       
        private ArrayList<Outpost> Children = new ArrayList<Outpost>();

       
    //Getters
        /**
         * Get Objective Name
         * @return String
         */
        public String getName() {
            return name;
        }
        /**
         * Get Objective Location
         * @return Location
         */
        public Location getLocation(){
            return location.clone();
        }
        /**
         * Get Objective Spawn
         * @return Location
         */
        public Location getSpawn(){
            return spawn.clone();
        }
        /**
         * Get Objective Owner
         * @return Capital
         */
        public Capital getOwner(){
            return owner;
        }
        /**
         * Get Arraylist of Child Outposts
         * @return Arraylist<Outpost>
         */
        public ArrayList<Outpost> getChildren(){
            return Children;
        }

    //Setters
        /**
         * Set Objective Name
         * @param objName - String
         * @return void
         */
        public void setName(String Name) {
            this.name = Name;
        }
        /**
         * Set Objective Location/Spawn
         * @param location - Location
         * @return void
         */
        public void setLocation(Location location){
            this.location = location;
            this.spawn = location;
        }
        /**
         * Set Objective Spawn
         * @param spawn - Location
         * @return void
         */
        public void setSpawn(Location spawn){
            this.spawn = spawn;
        }
        /**
         * Set Objective Owner
         * @param Owner - Capital
         * @return void
         */
        public void setOwner(Capital Owner){
            this.owner = Owner;
        }
        /**
         * Set Objective Beacoon Colors
         * @param p - Player Instance
         * @reutnr void
         */
        public void setBeaconColor(Player p){
            Location loc = getLocation();
            for (int y = 0; y <= 1; y++){
                loc.setY(loc.getY() - 1);
                setGlass(p, loc, STAINED_GLASS);
                loc.setX(loc.getX() - 1);
                setGlass(p, loc, STAINED_GLASS);
                loc.setX(loc.getX() + 1);
                loc.setZ(loc.getZ() - 1);
                setGlass(p, loc, STAINED_GLASS);
                loc.setZ(loc.getZ() + 1);
                loc.setZ(loc.getZ() + 1);
                setGlass(p, loc, STAINED_GLASS);
                loc.setZ(loc.getZ() - 1);
                loc.setX(loc.getX() + 1);
                setGlass(p, loc, STAINED_GLASS);
                loc.setX(loc.getX() - 1);
            }
            loc.setY(loc.getY() - 1);
            setGlass(p, loc, BEACON);
            loc.setX(loc.getX() - 1);
            setGlass(p, loc, BEACON);
            loc.setX(loc.getX() + 1);
            loc.setZ(loc.getZ() - 1);
            setGlass(p, loc, BEACON);
            loc.setZ(loc.getZ() + 1);
            loc.setZ(loc.getZ() + 1);
            setGlass(p, loc, BEACON);
            loc.setZ(loc.getZ() - 1);
            loc.setX(loc.getX() + 1);
            setGlass(p, loc, BEACON);
            loc.setX(loc.getX() - 1);
            loc.setY(loc.getY() + 3);
        }
        /**
         * Set Glass Color
         * @param p - Player
         * @param loc - Location
         * @param block - Material
         * @return void
         */
        @SuppressWarnings("deprecation")
        public void setGlass(Player p, Location loc, Material block){
            p.getWorld().getBlockAt(loc).setType(block);
            if (owner != null){
                p.getWorld().getBlockAt(loc).setData(ColorConverter.intToByte(owner.getColor()));
                return;
            }
            p.getWorld().getBlockAt(loc).setData(ColorConverter.intToByte(999));
        }
        /**
         * Set Objective Neutral
         * @param p - Player Instance
         * @return void
         */
        public void setNeutral(Player p){
            setOwner(null);
            setBeaconColor(p);
        }
       
    //Removers
        /**
         * Remove Objectives bound Outposts
         * @param op - Outpost
         * @return void
         */
        public void removeChild(Outpost op){
            Children.remove(op);
        }

    //Adders
        /**
         * Add an Outpost bound to Objective
         * @param op - Outpost
         * @return void
         */
        public void addChild(Outpost op){
            Children.add(op);
        }

        /**
         * Get UUID
         * @return String
         */
        public String getUUID(){
            return uniqueID.toString();
        }

        /**
         * Set UUID from String
         * @param ID - String
         * @return void
         */
        public void setUUID(String ID){
            this.uniqueID = UUID.fromString(ID);
        }
       
        /**
         * Generate new UUID (New Objective)
         * @return void
         */
        public void generateUUID(){
            this.uniqueID = UUID.randomUUID();
           
        }
    }
     
     
  12. In your catch-statement can you add e.printStacktrace();. Then post what ever error shows in the console.
     
  13. which catch statement are you reffering to? the one in the for loop?

    Code (Text):

                        try{
                            System.out.println(obj.getChildren().get(i));
                            list.add("test");
                        }catch(Exception e){
                            break;
                        }
    if so then it's only IndexOutOfBoundsException because there isn't 5 elements in the getChildren. And no error if there is ...
     
  14. Then lower how many times your for-loop runs.
     
  15. Why?
    I'm sorry but that still changes nothing.
    It's not the amount of times it runs that is the issue it's that it wount save to config because of some odd reasoning with for loops :/
    As said before:
    "elements in the getChildren. And no error if there is ..."
     

  16. Try saving it once without the for-loop and if it fails, show the whole stacktrace.
     
  17. Still same as the answer before...
    If there is a Child at index position there is no stacktrace...
    if there isn't:
    IndexOutOfBoundsException
    Basic Java knowledge there...

    Sorry if i'm not patience or so just getting irritated by this error...

    I'm going to try and sleep on it.
    If anyone is able to figure this one out or have a diffrent solution for me do pitch in pritty please do pitch in.

    Regards,
    Thomas
     

  18. That means there is nothing at that index, or the entered index is an invalid number.

    If your getting an IndexOutOfBounds, then lower the number on your for loop, or double check to make your array is as long as you think it is.
     
    #18 Hunky524, Jun 21, 2016
    Last edited: Jun 21, 2016
  19. I can't explain it but after I turned of my computer slept a good long while, woke up and turned it on then it just worked ... :/

    No Clue as to why not in the first run and no clue as to why it works now. well atleasat it works now!
     
  20. Very simple solution.
    It won't magically save if you don't set the objectives anywhere in your code :p

    EDIT: Had this open on my phone, forgot to refresh the page before replying.