Solved Print all homes

Discussion in 'Plugin Development' started by BananenPupse, May 13, 2017.

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

    BananenPupse

    Maybe some of you can help me?
    I want to list all the homes a player has, but I don't know how to do...

    Here is the code: https://hastebin.com/abekisajow.java

    Code (open)

    Code:
    package de.unbanane.commands;
    
    import java.io.File;
    import java.io.IOException;
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.InvalidConfigurationException;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    
    import de.unbanane.main.Main;
    
    public class HomeCMD implements CommandExecutor {
    
        File homes = new File("plugins//Basics//homes.yml");
        YamlConfiguration cfg = YamlConfiguration.loadConfiguration(homes);
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String arg2, String[] args) {
            Player p = (Player) sender;
    
            if (p.hasPermission("basics.home")) {
                if (args.length == 1) {
                    try {
                        cfg.load(homes);
                    } catch (IOException | InvalidConfigurationException e) {
                        e.printStackTrace();
                    }
                    if (cfg.isSet(p.getName() + "." + args[0] + ".world")) {
                        try {
                            cfg.load(homes);
                        } catch (IOException | InvalidConfigurationException e) {
                            e.printStackTrace();
                        }
                        String world = cfg.getString(p.getName() + "." + args[0] + ".world");
                        double x = cfg.getDouble(p.getName() + "." + args[0] + ".X");
                        double y = cfg.getDouble(p.getName() + "." + args[0] + ".Y");
                        double z = cfg.getDouble(p.getName() + "." + args[0] + ".Z");
                        double yaw = cfg.getDouble(p.getName() + "." + args[0] + ".Yaw");
                        double pitch = cfg.getDouble(p.getName() + "." + args[0] + ".Pitch");
    
                        Location loc = new Location(Bukkit.getWorld(world), x, y, z);
                        loc.setPitch((float) pitch);
                        loc.setYaw((float) yaw);
                        p.teleport(loc);
                        p.sendMessage(Main.prefix + "§6You were teleported to your home: §c" + args[0] + "§6!");
                    } else {
                        p.sendMessage(Main.prefix + "§cThe home " + args[0] + " doesn't exist!");
                    }
                } else {
                    p.sendMessage(Main.prefix + "§cPlease use: /home <name>");
                }
            } else {
                sender.sendMessage(Main.noperm);
            }
            if(args[0].equalsIgnoreCase("list")){
                //     ?
            }
            return false;
        }
    
    }
    


    I hope you can help me so that i can print out all homes from a player
     
    Last edited: May 13, 2017
  2. Offline

    Zombie_Striker

    @BananenPupse
    1. Use File.seperateor instead of slashes. Hard coding the slashes like that may cause problems on different OSs.
    2. Don't blidnly cast sender to a Player. Since plugins, consoles, and command blocks can all send commands, you can't just assume the sender will always be a Player. Add an instanceof check before you cast.
    3. Main problem: Since the path to each home is <Player>.<HOME>, use Config#getConfigurationSection(<PLAYER>).getKeys(false) to return all the homes.
    4. Remember: Always check the args length before getting args.
     
Thread Status:
Not open for further replies.

Share This Page