How to set max homes for player

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

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

    BananenPupse

    How I can set a maximum of homes a player can have (with config)?
    Pls help me

    This is my SetHome Class (open)
    Code:
    package de.unbanane.commands;
    
    import java.io.File;
    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.configuration.InvalidConfigurationException;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    
    import de.unbanane.main.Main;
    
    public class SetHomeCMD implements CommandExecutor {
    
        File homes = new File("plugins//Basics//homes.yml");
        File config = new File("plugins//Basics//config.yml");
        YamlConfiguration cfg = YamlConfiguration.loadConfiguration(homes);
        YamlConfiguration cfg2 = YamlConfiguration.loadConfiguration(config);
    
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String arg2, String[] args) {
            Player p = (Player) sender;
            if (p.hasPermission("basics.home.set")) {
                if (args.length == 1) {
                    try {
                        cfg.load(homes);
                    } catch (IOException | InvalidConfigurationException e) {
                        e.printStackTrace();
                    }
                    if (!cfg.contains(args[0])) {
                        if(!cfg.isSet(p.getName() + "." + args[0] + ".world")){
                            try {
                                cfg.load(homes);
                            } catch (IOException | InvalidConfigurationException e) {
                                e.printStackTrace();
                            }
                        Location loc = p.getLocation();
                        double x = loc.getX();
                        double y = loc.getY();
                        double z = loc.getZ();
                        double yaw = loc.getYaw();
                        double pitch = loc.getPitch();
                        String world = p.getWorld().getName();
    
                        cfg.set(p.getName() + "." + args[0] + ".world", world);
                        cfg.set(p.getName() + "." + args[0] + ".X", x);
                        cfg.set(p.getName() + "." + args[0] + ".Y", y);
                        cfg.set(p.getName() + "." + args[0] + ".Z", z);
                        cfg.set(p.getName() + "." + args[0] + ".Yaw", yaw);
                        cfg.set(p.getName() + "." + args[0] + ".Pitch", pitch);
                        try {
                            cfg.save(homes);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        p.sendMessage(Main.prefix + "§6Your home was set! (" + args[0] + ")");
                        } else {
                            p.sendMessage(Main.prefix + "§cThe home " + args[0] + " already exists!");
                        }
                    } else {
                        p.sendMessage(Main.prefix + "§cThe home " + args[0] + " already exists!");
                    }
                } else {
                        p.sendMessage(Main.prefix + "§cPlease give your home a name!");
                    }
            }else {
                    sender.sendMessage(Main.noperm);
                }
            return false;
        }
    
    }
    
    


    This is my Home Class (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 (cmd.getName().equalsIgnoreCase("homes")) {
                    if (args.length == 0) {
                        p.sendMessage(Main.prefix + "§c" + cfg.getConfigurationSection(p.getName()).getKeys(false));
                    }
                    return false;
                }
                if (cmd.getName().equalsIgnoreCase("home")) {
                    if (args.length == 1) {
                        if (args[0].equalsIgnoreCase("list")) {
                            p.sendMessage(Main.prefix + cfg.getConfigurationSection(p.getName()).getKeys(false));
                        }
                        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);
                }
            }
            return false;
        }
    
    }
    


    This is my DelHome Class (open)

    Code:
    package de.unbanane.commands;
    
    import java.io.File;
    import java.io.IOException;
    
    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 DelHomeCMD 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.del")) {
                if (args.length == 1) {
                    try {
                        cfg.load(homes);
                    } catch (IOException | InvalidConfigurationException e) {
                        e.printStackTrace();
                    }
                    if (cfg.isSet(p.getName() + "." + args[0] + ".world")) {
                        cfg.set(p.getName() + "." + args[0], null);
                        p.sendMessage(Main.prefix + "§6The home §c" + args[0] + "§6 was deleted!");
                        try {
                            cfg.save(homes);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    } else {
                        p.sendMessage(Main.prefix + "§cThe home " + args[0] + " doesn't exist!");
                    }
                    try {
                        cfg.save(homes);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    p.sendMessage(Main.prefix + "§cPlease use: /delhome <name>");
                }
            } else {
                sender.sendMessage(Main.noperm);
            }
            return false;
        }
    
    }
    
     
  2. Online

    timtower Administrator Administrator Moderator

    @BananenPupse Do you know how to check how many homes somebody has?
     
  3. Offline

    BananenPupse

  4. Online

    timtower Administrator Administrator Moderator

  5. Offline

    BananenPupse

    i don't understand
     
  6. Online

    timtower Administrator Administrator Moderator

    @BananenPupse ConfigurationSection#getKeys(false) returns a set.
    If the configurationsection is the base path of somebodies homes then it will return a set with all homes.
    You can call size() on that, then you know the amount of homes somebody has.
     
    BananenPupse likes this.
  7. Offline

    BananenPupse

    i dont know how to implement that

    @timtower
    i dont know how i should do that

    Who can help me with that? :(

    @timtower
    Can you give me an example based on my classes
     
    Last edited: May 15, 2017
Thread Status:
Not open for further replies.

Share This Page