Saving into object

Discussion in 'Plugin Development' started by xX4w3s0m3Xx, Jul 30, 2017.

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

    xX4w3s0m3Xx

    I have a method to load the config of my plugin, but ingame it isn't loaded. I think this has to do with the way I am assigning the saved values to a new object.
    This is my load method:
    Code:
    public static void loadHunts() {
            if (plugin.getConfig().getConfigurationSection("Hunts") != null) {
                for (String huntName : plugin.getConfig().getConfigurationSection("Hunts").getKeys(false)) {
                    Hunt hunt = new Hunt(huntName);
                    for (String player: plugin.getConfig().getStringList("Hunts." + huntName + ".PlayersCompleted")) {
                        hunt.getPlayersCompleted().add(Util.StringtoPlayer(player));
                    }
                    for (String pressurePlateNumber : plugin.getConfig().getConfigurationSection("Hunts." + huntName).getKeys(false)) {
                        PressurePlate pressurePlate = new PressurePlate(Util.SringtoLocation(plugin.getConfig().getString("Hunts." + huntName + ".PressurePlates." + pressurePlateNumber + ".Location")));
                        for (String player : plugin.getConfig().getStringList("Hunts." + huntName + ".PressurePlates." + pressurePlateNumber + ".ActivatedBy")) {
                            pressurePlate.addPlayerActivation(Util.StringtoPlayer(player));
                        }
                        hunt.getPressurePlates().add(pressurePlate);
                    }
                    Hunt.hunts.add(hunt);
                }
    
            }
        }
    But for some reason when I go ingame and I check if the hunts are added, they aren't there?
    This is the hunt object:
    Code:
    package me.Ruud.Hunt;
    
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    
    import java.util.ArrayList;
    import java.util.Objects;
    
    /**
    * @author Ruud.
    */
    public class Hunt {
    
        static ArrayList<Hunt> hunts = new ArrayList<>();
        private String name;
        private ArrayList<PressurePlate> pressurePlates = new ArrayList<>();
        private ArrayList<Player> playersCompleted = new ArrayList<>();
    
        public Hunt(String name) {
            this.name = name;
        }
    
        static Hunt getHunt(String name) {
            for (Hunt hunt: hunts) {
                if (name.equals(hunt.getName())) {
                    return hunt;
                }
            }
            return null;
        }
    
        static ArrayList<Hunt> getHunts() {
            return hunts;
        }
    
        static Hunt getHuntByLocation(Location location) {
            for(Hunt hunt: hunts) {
                for (PressurePlate pressurePlate: hunt.getPressurePlates()) {
                    if (pressurePlate.getLocation().equals(location)) {
                        return hunt;
                    }
                }
            }
            return null;
        }
    
        int getPressurePlateAmount() {
            return pressurePlates.size();
        }
    
        int getPlayerActivatedAmount(Player player) {
            int amount = 0;
            for (PressurePlate pressurePlate: pressurePlates) {
                for (Player playersActivated: pressurePlate.getActivatedByPlayers()) {
                    if (playersActivated.equals(player)) {
                        amount++;
                    }
                }
            }
            return amount;
        }
    
        boolean addPressurePlate(Location location) {
            if (hunts.size() > 0) {
                for (Hunt hunt: hunts) {
                    for (PressurePlate pressurePlate: hunt.getHunt(hunt.getName()).getPressurePlates()) {
                        if (pressurePlate.getLocation().equals(location)) {
                            return false;
                        }
                    }
                }
                getHunt(name).pressurePlates.add(new PressurePlate(location));
                return true;
            } else {
                return false;
            }
        }
    
        boolean addPressurePlate(PressurePlate pressurePlate) {
            if (hunts.size() > 0) {
                for (Hunt hunt: hunts) {
                    for (PressurePlate pressurePlateInHunt: hunt.getHunt(name).getPressurePlates()) {
                        if (pressurePlateInHunt.equals(pressurePlate)) {
                            return false;
                        }
                    }
                }
    
                getHunt(name).pressurePlates.add(pressurePlate);
                return true;
            } else {
                return false;
            }
        }
    
        static boolean addHunt(String hunt) {
            if (getHunt(hunt) == null) {
                hunts.add(new Hunt(hunt));
                return true;
            } else {
                return false;
            }
        }
    
        static boolean addHunt(Hunt newHunt) {
            for (Hunt hunt: hunts) {
                if (!Objects.equals(newHunt, hunt.getName())) {
                    hunts.add(new Hunt(newHunt.getName()));
                    return true;
                }
                return false;
            }
            return false;
        }
    
        PressurePlate getPressurePlate(Location location) {
            if (pressurePlates.size() != 0) {
                for (PressurePlate pressurePlate: pressurePlates) {
                    if (pressurePlate.getLocation().equals(location))
                        return pressurePlate;
                }
            }
            return null;
        }
    
        boolean playerCompletedHunt(Player player) {
            if (getPressurePlateAmount() == getPlayerActivatedAmount(player)) {
                if (!getPlayersCompleted().contains(player))
                    getPlayersCompleted().add(player);
                return true;
            }
            return false;
        }
    
        public ArrayList<Player> getPlayersCompleted() {
            return this.playersCompleted;
        }
    
        public void setPlayersCompleted(ArrayList<Player> playersCompleted) {
            this.playersCompleted = playersCompleted;
        }
    
        String getName() {
            return this.name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        ArrayList<PressurePlate> getPressurePlates() {
            return this.pressurePlates;
        }
    
        public void setPressurePlates(ArrayList<PressurePlate> pressurePlates) {
            this.pressurePlates = pressurePlates;
        }
    }
    
     
  2. Offline

    DoggyCode™

  3. Offline

    xX4w3s0m3Xx

    @DoggyCode™ That is something I could try. I still want to know what I did wrong, so I can learn from it though

    UPDATE:
    This works:
    Code:
    public static void loadHunts() {
            if (plugin.getConfig().getConfigurationSection("Hunts") != null) {
                for (String huntName : plugin.getConfig().getConfigurationSection("Hunts").getKeys(false)) {
                    Hunt.hunts.add(new Hunt(huntName));
                    for (String player : plugin.getConfig().getStringList("Hunts." + huntName + ".PlayersCompleted")) {
                        Hunt.getHunt(huntName).playerCompletedHunt(Util.StringtoPlayer(player));
                    }
                    for (String pressurePlateNumber : plugin.getConfig().getConfigurationSection("Hunts." + huntName + ".PressurePlates").getKeys(false)) {
                        Location location = Util.SringtoLocation(plugin.getConfig().getString("Hunts." + huntName + ".PressurePlates." + pressurePlateNumber + ".Location"));
                        PressurePlate pressurePlate = new PressurePlate(location);
                        for (String player : plugin.getConfig().getStringList("Hunts." + huntName + ".PressurePlates." + pressurePlateNumber + ".ActivatedBy")) {
                            pressurePlate.addPlayerActivation(Util.StringtoPlayer(player));
                        }
                        Hunt.getHunt(huntName).addPressurePlate(pressurePlate);
                    }
                }
            }
        }
    And this doesn't:
    Code:
    public static void loadHunts() {
            if (plugin.getConfig().getConfigurationSection("Hunts") != null) {
                for (String huntName : plugin.getConfig().getConfigurationSection("Hunts").getKeys(false)) {
                    Hunt hunt = new Hunt(huntName);
                    for (String player : plugin.getConfig().getStringList("Hunts." + huntName + ".PlayersCompleted")) {
                        hunt.playerCompletedHunt(Util.StringtoPlayer(player));
                    }
                    for (String pressurePlateNumber : plugin.getConfig().getConfigurationSection("Hunts." + huntName + ".PressurePlates").getKeys(false)) {
                        Location location = Util.SringtoLocation(plugin.getConfig().getString("Hunts." + huntName + ".PressurePlates." + pressurePlateNumber + ".Location"));
                        PressurePlate pressurePlate = new PressurePlate(location);
                        for (String player : plugin.getConfig().getStringList("Hunts." + huntName + ".PressurePlates." + pressurePlateNumber + ".ActivatedBy")) {
                            pressurePlate.addPlayerActivation(Util.StringtoPlayer(player));
                        }
                        hunt.addPressurePlate(pressurePlate);
                    }
                    Hunt.hunts.add(hunt);
                }
            }
        }
    Is someone able to explain me why this is?
     
    Last edited: Jul 31, 2017
Thread Status:
Not open for further replies.

Share This Page