Solved Looping through config

Discussion in 'Plugin Development' started by Bear53, Oct 4, 2015.

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

    Bear53

    So I am making it so when you type the command /sentry <name> it saves in the config this:
    Code:
    @Override
        public boolean onCommand(CommandSender sender, Command cmd,
                String commandLabel, String[] args) {
            Player p = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("sentry")) {
                if (args.length == 0) {
                    p.sendMessage(ChatColor.RED
                            + "Invalid args, use /sentry <name>");
                } else if (args.length == 1) {
                    String name = args[0].toLowerCase();
                    Location loc = p.getLocation();
                    Skeleton b = p.getWorld().spawn(loc, Skeleton.class);
                    b.getEquipment().setItemInHand(new ItemStack(Material.BOW));
                    b.setCustomName(ChatColor.RED + "Sentry");
                    b.setCustomNameVisible(true);
                    addMob(b, p.getLocation());
                    double x = loc.getX();
                    double y = loc.getY();
                    double z = loc.getZ();
                    double pitch = loc.getPitch();
                    double yaw = loc.getYaw();
                    String world = p.getWorld().getName();
                    getConfig().set("sentrys." + name + ".x", Double.valueOf(x));
                    getConfig().set("sentrys." + name + ".y", Double.valueOf(y));
                    getConfig().set("sentrys." + name + ".z", Double.valueOf(z));
                    getConfig()
                            .set("sentrys." + name + ".yaw", Double.valueOf(yaw));
                    getConfig().set("sentrys." + name + ".pitch",
                            Double.valueOf(pitch));
                    getConfig().set("sentrys." + name + ".world", world);
                    saveConfig();
                    p.sendMessage(ChatColor.GREEN + "Created Sentry!");
                }
            }
            return true;
        }
    As you can see this will cause there to be multiple sentrys be able to be created. Then when the server restarts or reloads I want to get all of the sentrys and spawn them at their set location. I have the code for just one sentry without a name but i need to be able to spawn them all in at once. the config would look like
    Code:
    sentrys:
        test:
          x: 59.20903427907063
          y: 69.0
          z: 267.8944008295581
          yaw: 158.36195373535156
          pitch: 5.144016742706299
          world: world
        test2:
          x: 59.20903427907063
          y: 69.0
          z: 267.8944008295581
          yaw: 158.36195373535156
          pitch: 5.144016742706299
          world: world
        test3:
          x: 59.20903427907063
          y: 69.0
          z: 267.8944008295581
          yaw: 158.36195373535156
          pitch: 5.144016742706299
          world: world
    And I need to get all these sentrys and spawn them at their locations. The method I was using for just 1 sentry without a name was this:
    Code:
    public void loadSentrys() {
            String world = getConfig().getString("sentrys.world");
            double x = getConfig().getDouble("sentrys.x");
            double y = getConfig().getDouble("sentrys.y");
            double z = getConfig().getDouble("sentrys.z");
            double yaw = getConfig().getDouble("sentrys.yaw");
            double pitch = getConfig().getDouble("sentrys.pitch");
            Location loc = new Location(Bukkit.getWorld(world), x, y, z);
            loc.setPitch((float) pitch);
            loc.setYaw((float) yaw);
            Skeleton b = Bukkit.getWorld(world).spawn(loc, Skeleton.class);
            b.getEquipment().setItemInHand(new ItemStack(Material.BOW));
            b.setCustomName(ChatColor.RED + "Sentry");
            b.setCustomNameVisible(true);
            addMob(b, loc);
        }
    my full code is this:
    Code:
    package com.bear53.sentry;
    
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Map.Entry;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Skeleton;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Core extends JavaPlugin {
    
        private Map<Entity, Location> frozenMobs = new HashMap<Entity, Location>();
    
        public void onEnable() {
            teleport();
            loadSentrys();
        }
    
        public void onDisable() {
    
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd,
                String commandLabel, String[] args) {
            Player p = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("sentry")) {
                if (args.length == 0) {
                    p.sendMessage(ChatColor.RED
                            + "Invalid args, use /sentry <name>");
                } else if (args.length == 1) {
                    String name = args[0].toLowerCase();
                    Location loc = p.getLocation();
                    Skeleton b = p.getWorld().spawn(loc, Skeleton.class);
                    b.getEquipment().setItemInHand(new ItemStack(Material.BOW));
                    b.setCustomName(ChatColor.RED + "Sentry");
                    b.setCustomNameVisible(true);
                    addMob(b, p.getLocation());
                    double x = loc.getX();
                    double y = loc.getY();
                    double z = loc.getZ();
                    double pitch = loc.getPitch();
                    double yaw = loc.getYaw();
                    String world = p.getWorld().getName();
                    getConfig().set("sentrys." + name + ".x", Double.valueOf(x));
                    getConfig().set("sentrys." + name + ".y", Double.valueOf(y));
                    getConfig().set("sentrys." + name + ".z", Double.valueOf(z));
                    getConfig()
                            .set("sentrys." + name + ".yaw", Double.valueOf(yaw));
                    getConfig().set("sentrys." + name + ".pitch",
                            Double.valueOf(pitch));
                    getConfig().set("sentrys." + name + ".world", world);
                    saveConfig();
                    p.sendMessage(ChatColor.GREEN + "Created Sentry!");
                }
            }
            return true;
        }
    
        public void loadSentrys() {
            String world = getConfig().getString("sentrys.world");
            double x = getConfig().getDouble("sentrys.x");
            double y = getConfig().getDouble("sentrys.y");
            double z = getConfig().getDouble("sentrys.z");
            double yaw = getConfig().getDouble("sentrys.yaw");
            double pitch = getConfig().getDouble("sentrys.pitch");
            Location loc = new Location(Bukkit.getWorld(world), x, y, z);
            loc.setPitch((float) pitch);
            loc.setYaw((float) yaw);
            Skeleton b = Bukkit.getWorld(world).spawn(loc, Skeleton.class);
            b.getEquipment().setItemInHand(new ItemStack(Material.BOW));
            b.setCustomName(ChatColor.RED + "Sentry");
            b.setCustomNameVisible(true);
            addMob(b, loc);
        }
    
        public void addMob(Entity mob, Location loc) {
            frozenMobs.put(mob, loc);
        }
    
        public void removeMob(Entity mob) {
            if (frozenMobs.containsKey(mob)) {
                frozenMobs.remove(mob);
            }
        }
    
        public void teleport() {
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                    for (Entry<Entity, Location> current : frozenMobs.entrySet()) {
                        current.getKey().teleport(current.getValue());
                    }
                }
            }, 10, 10);
        }
    }
    
    Please help
     
  2. Offline

    PDKnight

    Okay, soooo... :D
    ...first of all, in your loadSentry() you get double from config by path sentrys.x/y/z, but notice in your config are this numbers stored as sentrys.test.x/y/z, so you need to get all of keys (test, test2, test3):
    Code:java
    1. Set<String> keys = getConfig().getConfigurationSection("sentrys").getKeys(false);

    then, just for loop all the keys, get doubles and move all of your sentrys to certain locations:
    Code:java
    1. Set<String> sentrys = getConfig().getConfigurationSection("sentrys").getKeys(false);
    2. if (sentrys.size() > 0) {
    3. for (String sentryName : sentrys) {
    4. String world = getConfig().getString("sentrys."+sentryName+".world");
    5. double x = getConfig().getDouble("sentrys."+sentryName+".x");
    6. double y = getConfig().getDouble("sentrys."+sentryName+".y");
    7. double z = getConfig().getDouble("sentrys."+sentryName+".z");
    8. double yaw = getConfig().getDouble("sentrys."+sentryName+".yaw");
    9. double pitch = getConfig().getDouble("sentrys."+sentryName+".pitch");
    10. Location loc = new Location(Bukkit.getWorld(world), x, y, z);
    11. loc.setPitch((float) pitch);
    12. loc.setYaw((float) yaw);
    13. Skeleton b = Bukkit.getWorld(world).spawn(loc, Skeleton.class);
    14. b.getEquipment().setItemInHand(new ItemStack(Material.BOW));
    15. b.setCustomName(ChatColor.RED + "Sentry");
    16. b.setCustomNameVisible(true);
    17. b.teleport(loc); // new line!
    18. addMob(b, loc);
    19. }
    20. }


    I didn't test it yet, but it should work, enjoy! :)
     
  3. Offline

    Bear53

    I am getting this error:

    http://hastebin.com/worowoyiro.vbs

    and this is the code:
    Code:
        public void loadSentrys() {
            Set<String> sentrys = getConfig().getConfigurationSection("sentrys")
                    .getKeys(false);
            if (sentrys.size() > 0) {
                for (String sentryName : sentrys) {
                    String world = getConfig().getString(
                            "sentrys." + sentryName + ".world");
                    double x = getConfig()
                            .getDouble("sentrys." + sentryName + ".x");
                    double y = getConfig()
                            .getDouble("sentrys." + sentryName + ".y");
                    double z = getConfig()
                            .getDouble("sentrys." + sentryName + ".z");
                    double yaw = getConfig().getDouble(
                            "sentrys." + sentryName + ".yaw");
                    double pitch = getConfig().getDouble(
                            "sentrys." + sentryName + ".pitch");
                    Location loc = new Location(Bukkit.getWorld(world), x, y, z);
                    loc.setPitch((float) pitch);
                    loc.setYaw((float) yaw);
                    Skeleton b = Bukkit.getWorld(world).spawn(loc, Skeleton.class);
                    b.getEquipment().setItemInHand(new ItemStack(Material.BOW));
                    b.setCustomName(ChatColor.RED + "Sentry");
                    b.setCustomNameVisible(true);
                    b.teleport(loc);
                    addMob(b, loc);
                }
            }
        }
    
    Line 93 is
    Code:
    Location loc = new Location(Bukkit.getWorld(world), x, y, z);
    Sorry for my noobness, this is my first time using configuration sections

    DISREGARD that post
    I just still had the x y z stuff under sentry. in config xD

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 29, 2015
  4. Offline

    PDKnight

    Sooo it's solved? :D
     
  5. Offline

    Zombie_Striker

    Mark as solved if solved.
     
Thread Status:
Not open for further replies.

Share This Page