Problems with Config.yml

Discussion in 'Plugin Development' started by Zettos, Aug 19, 2020.

  1. Offline

    Zettos

    Hey. i wanted to made a /poslist command but i dont know how to get the names of the Positions of the Config.yml. Please Help



    Code:
    package serverextra.cmds;
    
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import serverextra.main.Main;
    
    public class Poslist implements CommandExecutor{
    
    
       
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player p = (Player)sender;
            if(p.hasPermission("ServerExtras.pos")) {
                if(args.length == 0) {
                   
                   
                   
                    String d = Main.loc.getString("pos."+p.getName()+"");
                   
                   
                p.sendMessage("§9Your names: "+d.charAt(1)+"");
                return true;
                   
                }else {
                    p.sendMessage(Main.pr+"§cUse: /poslist");
                    return true;
                }
            }else {
                p.sendMessage(Main.pr+"§cNo permission!");
                return true;
               
       
               
            }
        }
    
    }
    

    Code:
    package serverextra.cmds;
    
    import org.bukkit.Bukkit;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import serverextra.main.Main;
    
    public class Pos implements CommandExecutor{
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player p =(Player)sender;
            if(p.hasPermission("ServerExtras.pos")) {
               
                if(args.length == 1) {
                if(Main.loc.contains("pos."+p.getName()+"."+args[0]+"")) {
                       
                Double x = Main.loc.getDouble("pos."+p.getName()+"."+args[0]+".x");
                Double y = Main.loc.getDouble("pos."+p.getName()+"."+args[0]+".y");
                Double z = Main.loc.getDouble("pos."+p.getName()+"."+args[0]+".z");
                World w = Bukkit.getWorld(Main.loc.getString("pos."+p.getName()+"."+args[0]+".world"));
               
                //Unused
                @SuppressWarnings("unused")
                Float yaw = (float) Main.loc.getDouble("pos."+p.getName()+"."+args[0]+".yaw");
                @SuppressWarnings("unused")
                Float pitch = (float) Main.loc.getDouble("pos."+p.getName()+"."+args[0]+".pitch");
                //Unused
               
               
                p.sendMessage(Main.pr+"§cPosition "+args[0]+"§8:§9 X: "+x+" | Y: "+y+" | Z: "+z+" | Welt: "+w.getName()+"");
                return true;
                    }else {
                        p.sendMessage(Main.pr+"§cPosition "+args[0]+" existiert nicht!");
                        return true;
                    }
            }else {
                p.sendMessage(Main.pr+"§cBenutze: /pos <Name>");
                return true;
            }
               
            }else {
                p.sendMessage(Main.pr+"§cDazu hast du keine Berechtigung!");
                return true;
            }
       
        }
    
    }
    
    Code:
    package serverextra.cmds;
    
    import java.io.IOException;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import serverextra.main.Main;
    
    public class DelPos implements CommandExecutor{
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player p = (Player)sender;
            if(p.hasPermission("ServerExtras.pos")) {
                if(args.length == 1) {
                    if(Main.loc.contains("pos."+p.getName()+"."+args[0]+"")) {
                       
                       
                       
                        Main.loc.set("pos."+p.getName()+"."+args[0]+".x", null);
                        Main.loc.set("pos."+p.getName()+"."+args[0]+".y", null);
                        Main.loc.set("pos."+p.getName()+"."+args[0]+".z", null);
                        Main.loc.set("pos."+p.getName()+"."+args[0]+".yaw", null);
                        Main.loc.set("pos."+p.getName()+"."+args[0]+".pitch", null);
                        Main.loc.set("pos."+p.getName()+"."+args[0]+".world", null);
                        Main.loc.set("pos."+p.getName()+"."+args[0]+".name", null);
                        Main.loc.set("pos."+p.getName()+"."+args[0]+"", null);
                       
                       
                        try {
                            Main.loc.save(Main.locations);
                            p.sendMessage(Main.pr+"§cDu hast die Position §9"+args[0]+" §cgelöscht!");
                        } catch (IOException e) {
                           
                            e.printStackTrace();
                        }
                        return true;
                    }else {
                        p.sendMessage(Main.pr+"§cPosition "+args[0]+" existiert nicht!");
                        return true;
                    }
                }else {
                    p.sendMessage(Main.pr+"§cBenutze: /delpos <Name>");
                    return true;
                }
            }else {
                p.sendMessage(Main.pr+"§cDazu hast du keine Berechtigung!");
                return true;
            }
        }
    
    }
    Code:
    package serverextra.cmds;
    
    import java.io.IOException;
    
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import serverextra.main.Main;
    
    public class Setpos implements CommandExecutor{
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player p = (Player)sender;
            if(p.hasPermission("ServerExtras.pos")) {
            if(args.length == 1) {
               
                if(Main.loc.contains("pos."+p.getName()+"."+args[0]+"")) {
                    p.sendMessage(Main.pr+"§cDu hast bereits eine Position namens "+args[0]+"!");
                    return true;
                }
               
                Location loc = p.getLocation();
               
                Main.loc.set("pos."+p.getName()+"."+args[0]+".x", loc.getBlockX());
                Main.loc.set("pos."+p.getName()+"."+args[0]+".y", loc.getBlockY());
                Main.loc.set("pos."+p.getName()+"."+args[0]+".z", loc.getBlockZ());
                Main.loc.set("pos."+p.getName()+"."+args[0]+".yaw", loc.getYaw());
                Main.loc.set("pos."+p.getName()+"."+args[0]+".pitch", loc.getPitch());
                Main.loc.set("pos."+p.getName()+"."+args[0]+".world", loc.getWorld().getName());
                Main.loc.set("pos."+p.getName()+"."+args[0]+".name", args[0]);
               
                try {
                   
                    Main.loc.save(Main.locations);
                    p.sendMessage(Main.pr+"§9Position §c"+args[0]+" §9wurde gespeichert!");
                } catch (IOException e) {
                    e.printStackTrace();
                }
               
            }else {
                p.sendMessage(Main.pr+"§cBenutze: /setpos <Name>");
                return true;
            }
            }else {
                p.sendMessage(Main.pr+"§cDazu hast du keine Berechtigung!");
                return true;
            }
            return true;
        }
    
    }
    
    Code:
    package serverextra.main;
    
    import java.io.File;
    import java.util.ArrayList;
    import java.util.HashMap;
    
    import org.bukkit.Location;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import serverextra.cmds.Back;
    import serverextra.cmds.ChatClear;
    import serverextra.cmds.DelPos;
    import serverextra.cmds.Name;
    import serverextra.cmds.Pos;
    import serverextra.cmds.Poslist;
    import serverextra.cmds.Setpos;
    import serverextra.cmds.Tpa;
    import serverextra.cmds.Vanish;
    import serverextra.events.DeathEvent;;
    
    public class Main extends JavaPlugin{
       
        public static File locations;
        public static FileConfiguration loc;
       
        public static String pr = "§8[§bServerExtras§8] §a";
       
        public static HashMap<Player, Player> tpa = new HashMap<Player, Player>();
        public static HashMap<String, Location> back = new HashMap<>();
        public static ArrayList<String> nick = new ArrayList<String>();
        public static ArrayList<String> vanish = new ArrayList<String>();
       
       
    @Override
    public void onEnable() {
        saveDefaultConfig();
       
        Main.locations = new File("plugins/ServerExtras", "config.yml");
        Main.loc = YamlConfiguration.loadConfiguration(Main.locations);
       
        registerCMDS();
        registerEvents();
       
    }
    @Override
        public void onDisable() {
        saveConfig();
           
        }
    public void registerEvents() {
        this.getServer().getPluginManager().registerEvents(new DeathEvent(), this);
    
       
    }
    public void registerCMDS() {
        this.getCommand("tpa").setExecutor(new Tpa());
        this.getCommand("tpaccept").setExecutor(new Tpa());
        this.getCommand("tpac").setExecutor(new Tpa());
        this.getCommand("back").setExecutor(new Back());
        this.getCommand("serverchatclear").setExecutor(new ChatClear());
        this.getCommand("chatclear").setExecutor(new ChatClear());
        this.getCommand("name").setExecutor(new Name());
        this.getCommand("vanish").setExecutor(new Vanish());
        this.getCommand("setpos").setExecutor(new Setpos());
        this.getCommand("pos").setExecutor(new Pos());
        this.getCommand("delpos").setExecutor(new DelPos());
        this.getCommand("poslist").setExecutor(new Poslist());
       
    }
    }
    
     

    Attached Files:

  2. Offline

    KarimAKL

    @Zettos Loop the keys at the section "pos.playerName".
     
  3. Offline

    Zettos

    @KarimAKL Thanks, but i dont understand.
     
  4. Offline

    KarimAKL

    @Zettos Look at the Javadocs for ConfigurationSection.
     
  5. Offline

    Strahan

    https://hub.spigotmc.org/javadocs/spigot/org/bukkit/configuration/ConfigurationSection.html if you aren't aware of where that is.

    Pretty easy, basically create a ConfigurationSection object by getting it from getConfig. Then check and be sure it isn't null, as someone could have messed up the config. Then you can use a method exposed by ConfigurationSection that returns a collection of keys. Tell it you do not want to recurse the keys when you get them and then you just loop over them, doing whatever you need to do with it.

    Well, I kind of misspoke. I don't typically get the keys as a collection then loop it, I loop the return directly.
     

Share This Page