diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..515498e --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +/bin/ +/.settings/ +/.classpath +/.project + +/.idea/ +/out/ +/CustomSpawns.iml \ No newline at end of file diff --git a/CustomSpawns/.gitignore b/CustomSpawns/.gitignore deleted file mode 100644 index f3f64eb..0000000 --- a/CustomSpawns/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -/bin/ -/.settings/ -/.classpath -/.project \ No newline at end of file diff --git a/CustomSpawns/src/fr/bencor29/customspawns/Commands.java b/CustomSpawns/src/fr/bencor29/customspawns/Commands.java deleted file mode 100644 index ff3d5e6..0000000 --- a/CustomSpawns/src/fr/bencor29/customspawns/Commands.java +++ /dev/null @@ -1,188 +0,0 @@ -package fr.bencor29.customspawns; - -import java.io.File; -import java.io.IOException; -import java.util.logging.Level; - -import org.bukkit.Location; -import org.bukkit.World; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.entity.Player; - -public class Commands implements CommandExecutor { - - CustomSpawns plugin; - GroupManager gm; - - public Commands(CustomSpawns plugin) { - this.plugin = plugin; - gm = new GroupManager(plugin); - } - - - @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - if(!(sender instanceof Player)) { - sender.sendMessage("Vous devez etre un joueur !"); - return true; - } - final Player player = (Player) sender; - if(label.equalsIgnoreCase("spawn")) { - spawnCommand(player); - } else if(label.equalsIgnoreCase("lobby")) { - lobbyCommand(player); - } else if(label.equalsIgnoreCase("setlobby")) { - setlobbyCommand(player); - } else if(label.equalsIgnoreCase("csreload")) { - reloadCommand(player); - } - return true; - } - - public void reloadCommand(Player player) { - plugin.onReload(); - player.sendMessage("�aPlugin recharg� !"); - } - - - public void setlobbyCommand(Player player) { - Location ploc = player.getLocation(); - World world = ploc.getWorld(); - double x = ploc.getX(), - y = ploc.getY(), - z = ploc.getZ(); - float yaw = ploc.getYaw(), - pitch = ploc.getPitch(); - Location newlobby = new Location(world, x, y, z, yaw, pitch); - plugin.lobby = newlobby; - player.sendMessage("�aLe nouveau lobby a �t� d�finit !"); - saveLobby(world.getName(), x, y, z, yaw, pitch); - } - - - public void saveLobby(String world, double x, double y, double z, float yaw, float pitch) { - plugin.genconf.set("lobby.world", world); - plugin.genconf.set("lobby.x", x); - plugin.genconf.set("lobby.y", y); - plugin.genconf.set("lobby.z", z); - plugin.genconf.set("lobby.yaw", yaw); - plugin.genconf.set("lobby.pitch", pitch); - File file = plugin.getConfigFile(0); - FileConfiguration configs = plugin.getFileConfig(0); - try { - configs.save(file); - } catch (IOException e) { - plugin.getLogger().log(Level.WARNING, "Impossible de sauvegarder les configs !"); - } - } - - - public void lobbyCommand(final Player player) { - final Location lobby = plugin.lobby; - if(lobby == null) { - player.sendMessage("�cErreur: Il n'y a pas de lobby d�finit !"); - return; - } - int secs = plugin.secs; - final Location playerloc = player.getLocation(); - if(player.hasPermission("customspawns.notimer")) { - player.teleport(lobby); - player.sendMessage("�7T�l�portation termin� !"); - return; - } - player.sendMessage("�7T�l�portation dans 5 secondes, ne bougez pas !"); - plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { - public void run() { - int lbx = playerloc.getBlockX(), - lby = playerloc.getBlockY(), - lbz = playerloc.getBlockZ(); - String lworld = playerloc.getWorld().getName(); - int nbx = player.getLocation().getBlockX(), - nby = player.getLocation().getBlockY(), - nbz = player.getLocation().getBlockZ(); - String nworld = player.getLocation().getWorld().getName(); - if( lbx != nbx || - lby != nby || - lbz != nbz || - !lworld.equalsIgnoreCase(nworld) - ) { - player.sendMessage("�cVous avez boug�, veuillez ne pas bouger durant la t�l�portation."); - return; - } - player.teleport(lobby); - player.sendMessage("�7T�l�portation termin� !"); - } - }, 20 * secs); - } - - public void spawnCommand(final Player player) { - Group group = gm.getGroupByWorld(player.getLocation().getWorld().getName()); - if(group == null) { - player.sendMessage("�cErreur: Aucun spawn n'est d�finit pour ce monde !"); - return; - } - Location spawn = group.getSpawn(); - if(spawn == null) { - player.sendMessage("�cErreur: Aucun spawn n'est d�finit pour ce monde !"); - return; - } - final Location tp = gm.getSpawnByNameFromConfig(group.getName()); - plugin.getLogger().log(Level.INFO, "" + tp); - if(player.hasPermission("essentials.teleport.timer.bypass")) { - player.teleport(tp); - player.sendMessage("�7T�l�portation termin� !"); - return; - } - int secs = plugin.secs; - final Location playerloc = player.getLocation(); - plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { - public void run() { - scheduler(playerloc, player, tp); - } - }, 20 * secs); - player.sendMessage("�7T�l�portation dans 5 secondes, ne bouger pas !"); - } - - - public Location getSpawnLocation(Player player) { - for(Group group : plugin.groups) { - if(group.containWorld(player.getLocation().getWorld().getName())) { - return group.getSpawn(); - } - } - return null; - } - - public void scheduler(Location playerloc, Player player, Location spawn) { - try { - int lbx = playerloc.getBlockX(), - lby = playerloc.getBlockY(), - lbz = playerloc.getBlockZ(); - String lworld = playerloc.getWorld().getName(); - int nbx = player.getLocation().getBlockX(), - nby = player.getLocation().getBlockY(), - nbz = player.getLocation().getBlockZ(); - String nworld = player.getLocation().getWorld().getName(); - if( lbx != nbx || - lby != nby || - lbz != nbz || - !lworld.equalsIgnoreCase(nworld) - ) { - player.sendMessage("�cVous avez boug�, veuillez ne pas bouger durant la t�l�portation."); - return; - } - player.teleport(spawn); - player.sendMessage("�7T�l�portation termin� !"); - } catch(Exception e) { - player.sendMessage( - "World: " + spawn.getWorld().getName() + - " / X: " + spawn.getBlockX() + - " / Y: " + spawn.getBlockY() + - " / Z: " + spawn.getBlockZ()); - } - } - -} diff --git a/CustomSpawns/src/fr/bencor29/customspawns/GroupManager.java b/CustomSpawns/src/fr/bencor29/customspawns/GroupManager.java deleted file mode 100644 index ff3d689..0000000 --- a/CustomSpawns/src/fr/bencor29/customspawns/GroupManager.java +++ /dev/null @@ -1,131 +0,0 @@ -package fr.bencor29.customspawns; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.logging.Level; - -import org.bukkit.Location; -import org.bukkit.World; - -public class GroupManager { - - CustomSpawns plugin; - - public GroupManager(CustomSpawns plugin) { - this.plugin = plugin; - } - - public void saveGroup(Group group) { - String name = group.getName(); - ArrayList worlds = group.getWorlds(); - Location spawn = group.getSpawn(); - String w = spawn.getWorld().getName(); - String path = "groups." + name + "."; - String loc = path + "spawn."; - plugin.grpconf.set(path + "worlds", worlds); - plugin.grpconf.set(loc + "world", w); - plugin.grpconf.set(loc + "x", spawn.getX()); - plugin.grpconf.set(loc + "y", spawn.getY()); - plugin.grpconf.set(loc + "z", spawn.getZ()); - plugin.grpconf.set(loc + "yaw", spawn.getYaw()); - plugin.grpconf.set(loc + "pitch", spawn.getPitch()); - File file = new File(plugin.getDataFolder(), "groups.yml"); - try { - plugin.grpconf.save(file); - } catch (IOException e) { - plugin.getLogger().log(Level.INFO, "Impossible de sauvegarder les configs !"); - } - } - - public int getGroupIdByWorld(String worldname) { - for(int i = 0; i < plugin.groups.size(); i++) { - if(plugin.groups.get(i).containWorld(worldname)) return i + 1; - } - return 0; - } - - public Group getGroupByWorld(String worldname) { - for(int i = 0; i < plugin.groups.size(); i++) { - if(plugin.groups.get(i).containWorld(worldname)) return plugin.groups.get(i); - } - return null; - } - - public Group getGroupByWorld(World world) { - return getGroupByWorld(world.getName()); - } - - public int getGroupIdByWorld(World world) { - return getGroupIdByWorld(world.getName()); - } - - public Group getGroupByName(String name) { - for(Group group : plugin.groups) { - if(group.getName().equalsIgnoreCase(name)) - return group; - } - return null; - } - - public int getGroupIdByName(String name) { - for(int i = 0; i < plugin.groups.size(); i++) { - if(plugin.groups.get(i).getName().equalsIgnoreCase(name)) { - return i + 1; - } - } - return 0; - } - - public boolean checkAddWorld(String worldname) { - for(Group group : plugin.groups) - if(group.containWorld(worldname)) return true; - return false; - } - - public boolean checkCreate(String name, String worldname) { - for(Group group : plugin.groups) { - if(group.getName().equalsIgnoreCase(name)) return true; - if(group.containWorld(worldname)) return true; - } - return false; - } - - public ArrayList getGroupsFromFile() { - ArrayList groups = new ArrayList(); - String gpath = "groups.", path = "", loc = ""; - for(String name : plugin.grpconf.getConfigurationSection("groups").getKeys(false)) { - try { - path = gpath + name + "."; - loc = path + "spawn."; - @SuppressWarnings("unchecked") - ArrayList worlds = (ArrayList) plugin.grpconf.getList(path + "worlds"); - World world = plugin.getServer().getWorld(plugin.grpconf.getString(loc + "world")); - double x = plugin.grpconf.getDouble(loc + "x"), - y = plugin.grpconf.getDouble(loc + "y"), - z = plugin.grpconf.getDouble(loc + "z"); - float yaw = plugin.grpconf.getLong(loc + "yaw"), - pitch = plugin.grpconf.getLong(loc + "pitch"); - Location spawn = new Location(world, x, y, z, yaw, pitch); - Group group = new Group(name, worlds, spawn); - groups.add(group); - } catch(Exception e) { - plugin.log(Level.WARNING, "Cannot load group: " + name); - } - } - return groups; - } - - public Location getSpawnByNameFromConfig(String name) { - String path = "groups." + name + "."; - String loc = path + "spawn."; - World world = plugin.getServer().getWorld(plugin.grpconf.getString(loc + "world")); - double x = plugin.grpconf.getDouble(loc + "x"), - y = plugin.grpconf.getDouble(loc + "y"), - z = plugin.grpconf.getDouble(loc + "z"); - float yaw = plugin.grpconf.getLong(loc + "yaw"), - pitch = plugin.grpconf.getLong(loc + "pitch"); - return new Location(world, x, y, z, yaw, pitch); - } - -} diff --git a/CustomSpawns/src/fr/bencor29/customspawns/GroupsCommands.java b/CustomSpawns/src/fr/bencor29/customspawns/GroupsCommands.java deleted file mode 100644 index 52b3a2d..0000000 --- a/CustomSpawns/src/fr/bencor29/customspawns/GroupsCommands.java +++ /dev/null @@ -1,343 +0,0 @@ -package fr.bencor29.customspawns; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.logging.Level; - -import org.bukkit.Location; -import org.bukkit.World; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class GroupsCommands implements CommandExecutor { - - CustomSpawns plugin; - GroupManager gm; - String usage; - - public GroupsCommands(CustomSpawns plugin) { - this.plugin = plugin; - gm = new GroupManager(plugin); - usage = "Usage: /group [list | create | remove | addworld | delworld | listworlds | setworldspawn ]"; - } - - @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - if(args.length == 0) { - sender.sendMessage("�c" + usage); - return true; - } - if( - args.length != 3 && - !args[0].equalsIgnoreCase("setworldspawn") && - !args[0].equalsIgnoreCase("remove") && - !args[0].equalsIgnoreCase("listworlds") && - !args[0].equalsIgnoreCase("list")) { - if(sender instanceof Player) { - commandFail(sender, args); - return true; - } else { - commandFail(sender, args); - return true; - } - } - if( - args.length != 2 && args[0].equalsIgnoreCase("setworldspawn") || - args.length != 2 && args[0].equalsIgnoreCase("remove") || - args.length != 2 && args[0].equalsIgnoreCase("listworlds") || - args.length != 1 && args[0].equalsIgnoreCase("list")) { - if(sender instanceof Player) { - commandFail(sender, args); - return true; - } else { - commandFail(sender, args); - return true; - } - } - String cmd = args[0]; - if(cmd.equalsIgnoreCase("create")) commandCreate(sender, args); - else if(cmd.equalsIgnoreCase("remove")) commandRemove(sender, args); - else if(cmd.equalsIgnoreCase("list")) commandList(sender, args); - else if(cmd.equalsIgnoreCase("listworlds")) commandListWorlds(sender, args); - else if(cmd.equalsIgnoreCase("addworld")) commandAddWorld(sender, args); - else if(cmd.equalsIgnoreCase("delworld")) commandDelWorld(sender, args); - else if(cmd.equalsIgnoreCase("setworldspawn")) commandSetWorldSpawn(sender, args); - else commandFail(sender, args); - return true; - } - - public void commandListWorlds(CommandSender sender, String[] args) { - String name = args[1]; - if(gm.getGroupByName(name) == null) { - if(sender instanceof Player) { - sender.sendMessage("�cCe groupe n'existe pas !"); - return; - } else { - plugin.log(Level.INFO, "Ce groupe n'existe pas !"); - return; - } - } - Group group = gm.getGroupByName(name); - if(sender instanceof Player) { - sender.sendMessage("�6�lListe des mondes du groupe:"); - } else { - plugin.log(Level.INFO, "Liste des mondes du groupe:"); - } - for(String world : group.getWorlds()) { - if(sender instanceof Player) { - sender.sendMessage("�7 - " + world); - } else { - plugin.log(Level.INFO, " - " + world); - } - } - } - - public void commandList(CommandSender sender, String[] args) { - if(sender instanceof Player) { - sender.sendMessage("�6�lListe des groupes:"); - } else { - plugin.log(Level.INFO, "Liste des groupe:"); - } - for(Group group : gm.getGroupsFromFile()) { - try { - if(sender instanceof Player) { - sender.sendMessage(" �7- �e�l" + group.getName() + "�7 -> Mondes: " + group.getWorlds().size() + " / Monde de spawn: " + group.getSpawn().getWorld().getName()); - } else { - plugin.log(Level.INFO, " - " + group.getName() + " -> Mondes: " + group.getWorlds().size() + " / Monde de spawn: " + group.getSpawn().getWorld().getName()); - } - } catch (Exception e) { - if(sender instanceof Player) { - sender.sendMessage("�7 - ERROR"); - return; - } else { - plugin.log(Level.INFO, " - ERROR"); - return; - } - } - } - } - - public void commandRemove(CommandSender sender, String[] args) { - String name = args[1]; - if(gm.getGroupByName(name) == null) { - if(sender instanceof Player) { - sender.sendMessage("�cCe groupe n'existe pas !"); - return; - } else { - plugin.log(Level.INFO, "Ce groupe n'existe pas !"); - return; - } - } - int id = gm.getGroupIdByName(name); - id--; - plugin.groups.remove(id); - plugin.grpconf.set("groups." + name, null); - try { - plugin.grpconf.save(plugin.getConfigFile(1)); - } catch (IOException e) { - if(sender instanceof Player) { - sender.sendMessage("�cImpossible de sauvegarder les groupes !"); - return; - } else { - plugin.log(Level.INFO, "Impossible de sauvegarder les groupes !"); - return; - } - } - if(sender instanceof Player) { - sender.sendMessage("�aGroupe supprim� avec succ�s !"); - return; - } else { - plugin.log(Level.INFO, "Groupe supprima avec succes !"); - return; - } - } - - private void commandFail(CommandSender sender, String[] args) { - if(sender instanceof Player) { - sender.sendMessage("�c" + usage); - return; - } else { - plugin.log(Level.INFO, usage); - return; - } - } - - public void commandSetWorldSpawn(CommandSender sender, String[] args) { - if(!(sender instanceof Player)) { - plugin.log(Level.INFO, "Vous devez etre un joueur pour executer cette commande !"); - return; - } - Player player = (Player) sender; - Location playerloc = player.getLocation(); - World world = player.getWorld(); - double x = playerloc.getX(), - y = playerloc.getY(), - z = playerloc.getZ(); - float yaw = playerloc.getYaw(), - pitch = playerloc.getPitch(); - Location spawn = new Location(world, x, y, z, yaw, pitch); - String name = args[1]; - int id = gm.getGroupIdByName(name); - if(id == 0) { - player.sendMessage("�cCe groupe n'existe pas !"); - return; - } - id--; - Group group = plugin.groups.get(id); - group.setSpawn(spawn); - plugin.groups.set(id, group); - gm.saveGroup(group); - player.sendMessage("�aGroupe modifi� avec succ�s !"); - } - - private void commandDelWorld(CommandSender sender, String[] args) { - String worldname = args[2]; - String name = args[1]; - if(!gm.checkAddWorld(worldname)) { - if(sender instanceof Player) { - sender.sendMessage("�cErreur: Le monde choisit n'est pas utilis� !"); - return; - } else { - plugin.log(Level.INFO, "Erreur: Le monde choisit n'est pas utilis� !"); - return; - } - } - int id = gm.getGroupIdByWorld(worldname); - if(id == 0) { - if(sender instanceof Player) { - sender.sendMessage("�cErreur: Le monde choisit n'est pas utilis� !"); - return; - } else { - plugin.log(Level.INFO, "Erreur: Le monde choisit n'est pas utilis� !"); - return; - } - } - id--; - if(!gm.getGroupByName(name).containWorld(worldname)) { - if(sender instanceof Player) { - sender.sendMessage("�cErreur: Le monde choisit n'est pas utilis� dans ce groupe !"); - return; - } else { - plugin.log(Level.INFO, "Erreur: Le monde choisit n'est pas utilis� dans ce groupe !"); - return; - } - } - plugin.groups.remove(id); - if(sender instanceof Player) { - sender.sendMessage("�aLe monde a �t� supprim� !"); - return; - } else { - plugin.log(Level.INFO, "Le monde a ete supprime !"); - return; - } - } - - public void commandAddWorld(CommandSender sender, String[] args) { - String name = args[1]; - String worldname = args[2]; - if(gm.checkAddWorld(worldname)) { - if(sender instanceof Player) { - sender.sendMessage("�cErreur: Le monde choisit est d�j� utilis� !"); - return; - } else { - plugin.log(Level.INFO, "Erreur: Le monde choisit est d�j� utilis� !"); - return; - } - } - - try { - World world = plugin.getServer().getWorld(worldname); - if(world == null) { - if(sender instanceof Player) { - sender.sendMessage("�cErreur: Ce monde n'existe pas !"); - return; - } else { - plugin.log(Level.INFO, "Erreur: Ce monde n'existe pas !"); - return; - } - } - } catch(Exception e) { - if(sender instanceof Player) { - sender.sendMessage("�cErreur: Ce monde n'existe pas !"); - return; - } else { - plugin.log(Level.INFO, "Erreur: Ce monde n'existe pas !"); - return; - } - } - - int id = gm.getGroupIdByName(name); - if(id == 0) { - if(sender instanceof Player) { - sender.sendMessage("�cErreur: Ce groupe n'existe pas !"); - return; - } else { - plugin.log(Level.INFO, "Erreur: Ce groupe n'existe pas !"); - return; - } - } - id--; - Group group = plugin.groups.get(id); - group.addWorld(worldname); - plugin.groups.set(id, group); - gm.saveGroup(group); - if(sender instanceof Player) { - sender.sendMessage("�aLe monde a �t� ajout� !"); - return; - } else { - plugin.log(Level.INFO, "Le monde a ete ajoute !"); - return; - } - } - - public void commandCreate(CommandSender sender, String[] args) { - String name = args[1]; - String worldname = args[2]; - if(gm.checkCreate(name, worldname)) { - if(sender instanceof Player) { - sender.sendMessage("�cErreur: Le groupe/monde choisit est d�j� utilis� !"); - return; - } else { - plugin.log(Level.INFO, "Erreur: Le groupe/monde choisit est d�j� utilis� !"); - return; - } - } - try { - World world = plugin.getServer().getWorld(worldname); - if(world == null) { - if(sender instanceof Player) { - sender.sendMessage("�cErreur: Le monde '" + worldname + "' n'existe pas !"); - return; - } else { - plugin.log(Level.INFO, "Erreur: Le monde '" + worldname + "' n'existe pas !"); - return; - } - } - Location spawn = new Location(world, 0, 0, 0, 0, 0); - ArrayList worlds = new ArrayList(); - worlds.add(world.getName()); - Group group = new Group(name, worlds, spawn); - plugin.groups.add(group); - gm.saveGroup(group); - } catch(Exception e) { - if(sender instanceof Player) { - sender.sendMessage("�cErreur: Le monde '" + worldname + "' n'existe pas !"); - return; - } else { - plugin.log(Level.INFO, "Erreur: Le monde '" + worldname + "' n'existe pas !"); - return; - } - } - if(sender instanceof Player) { - sender.sendMessage("�aLe groupe a �t� cr�er !"); - return; - } else { - plugin.log(Level.INFO, "Le groupe a ete creer !"); - return; - } - - } - -} diff --git a/CustomSpawns/src/config.yml b/src/config.yml similarity index 95% rename from CustomSpawns/src/config.yml rename to src/config.yml index 239bcd8..e713976 100644 --- a/CustomSpawns/src/config.yml +++ b/src/config.yml @@ -1,16 +1,16 @@ -# Voici la configuration par defaut du plugin. -# Vous pouvez la modifier afin de personnaliser le plugin. -# Si vous faites une erreur et que le plugin detecte un erreur : -# Veuillez surpprimer ce fichier et le plugin le recrera ! - -# Secondes a attendre pour un tp -seconds: 5 - -# Configuration du lobby -lobby: - world: lobby_non_definit - x: 0 - y: 0 - z: 0 - yaw: 0 - pitch: 0 +# Voici la configuration par defaut du plugin. +# Vous pouvez la modifier afin de personnaliser le plugin. +# Si vous faites une erreur et que le plugin detecte un erreur : +# Veuillez surpprimer ce fichier et le plugin le recrera ! + +# Secondes a attendre pour un tp +seconds: 5 + +# Configuration du lobby +lobby: + world: lobby_non_definit + x: 0 + y: 0 + z: 0 + yaw: 0 + pitch: 0 diff --git a/src/fr/bencor29/customspawns/Commands.java b/src/fr/bencor29/customspawns/Commands.java new file mode 100644 index 0000000..966b02c --- /dev/null +++ b/src/fr/bencor29/customspawns/Commands.java @@ -0,0 +1,158 @@ +package fr.bencor29.customspawns; + +import java.io.File; +import java.io.IOException; +import java.util.logging.Level; + +import org.bukkit.Location; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.entity.Player; + +public class Commands implements CommandExecutor { + + private final CustomSpawns plugin; + private final GroupManager gm; + + public Commands(CustomSpawns plugin) { + this.plugin = plugin; + gm = GroupManager.getInstance(); + } + + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if(!(sender instanceof Player)) { + sender.sendMessage("§cYou must be a player."); + return true; + } + final Player player = (Player) sender; + if(label.equalsIgnoreCase("spawn")) { + spawnCommand(player); + } else if(label.equalsIgnoreCase("lobby")) { + lobbyCommand(player); + } else if(label.equalsIgnoreCase("setlobby")) { + setLobbyCommand(player); + } else if(label.equalsIgnoreCase("csreload")) { + reloadCommand(player); + } + return true; + } + + public void reloadCommand(Player player) { + plugin.onReload(); + player.sendMessage("§aPlugin reloaded !"); + } + + + public void setLobbyCommand(Player player) { + Location lobby = Utils.getLocation(player); + plugin.setLobby(lobby); + player.sendMessage("§aNew lobby's location defined."); + + saveLobby( + lobby.getWorld().getName(), + lobby.getX(), + lobby.getY(), + lobby.getZ(), + lobby.getYaw(), + lobby.getPitch() + ); + } + + + public void saveLobby(String world, double x, double y, double z, float yaw, float pitch) { + plugin.getGenconf().set("lobby.world", world); + plugin.getGenconf().set("lobby.x", x); + plugin.getGenconf().set("lobby.y", y); + plugin.getGenconf().set("lobby.z", z); + plugin.getGenconf().set("lobby.yaw", yaw); + plugin.getGenconf().set("lobby.pitch", pitch); + File file = plugin.getConfigFile(0); + FileConfiguration configs = plugin.getFileConfig(0); + try { + configs.save(file); + } catch (IOException e) { + plugin.getLogger().log(Level.SEVERE, "Failed to save configs."); + } + } + + + public void lobbyCommand(final Player player) { + final Location lobby = plugin.getLobby(); + if(lobby == null) { + player.sendMessage("§cAucun lobby n'est définit"); + return; + } + int secs = plugin.getSecs(); + final Location playerloc = player.getLocation(); + if(player.hasPermission("customspawns.notimer")) { + player.teleport(lobby); + player.sendMessage("§7Téléportation terminée !"); + return; + } + player.sendMessage("§7Téléportation dans 5 secondes, ne bougez pas !"); + plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, () -> scheduler(playerloc, player, lobby), 20 * secs); + } + + public void spawnCommand(final Player player) { + Group group = gm.getGroupByWorld(player.getLocation().getWorld().getName()); + if(group == null) { + player.sendMessage("§cAucun spawn n'est définit pour ce monde !"); + return; + } + Location spawn = group.getSpawn(); + if(spawn == null) { + player.sendMessage("§cAucun spawn n'est définit pour ce monde !"); + return; + } + final Location tp = gm.getSpawnByNameFromConfig(group.getName()); + plugin.getLogger().log(Level.INFO, "" + tp); + if(player.hasPermission("essentials.teleport.timer.bypass")) { + player.teleport(tp); + player.sendMessage("§7Téléportation terminée !"); + return; + } + int secs = plugin.getSecs(); + final Location playerloc = player.getLocation(); + plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, () -> scheduler(playerloc, player, tp), 20 * secs); + player.sendMessage("§7Téléportation dans 5 secondes, ne bougez pas !"); + } + + public void scheduler(Location playerloc, Player player, Location spawn) { + try { + if (checkBeforeTP(playerloc, player, spawn)) return; + player.sendMessage("§7Téléportation terminée !"); + } catch(Exception e) { + player.sendMessage( + "World: " + spawn.getWorld().getName() + + " / X: " + spawn.getBlockX() + + " / Y: " + spawn.getBlockY() + + " / Z: " + spawn.getBlockZ()); + } + } + + private boolean checkBeforeTP(Location playerloc, Player player, Location spawn) { + int lbx = playerloc.getBlockX(), + lby = playerloc.getBlockY(), + lbz = playerloc.getBlockZ(); + String lworld = playerloc.getWorld().getName(); + int nbx = player.getLocation().getBlockX(), + nby = player.getLocation().getBlockY(), + nbz = player.getLocation().getBlockZ(); + String nworld = player.getLocation().getWorld().getName(); + if( lbx != nbx || + lby != nby || + lbz != nbz || + !lworld.equalsIgnoreCase(nworld) + ) { + player.sendMessage("§cVous avez bougé, veuillez ne pas bouger durant la téléportation."); + return true; + } + player.teleport(spawn); + return false; + } + +} diff --git a/CustomSpawns/src/fr/bencor29/customspawns/CustomSpawns.java b/src/fr/bencor29/customspawns/CustomSpawns.java similarity index 78% rename from CustomSpawns/src/fr/bencor29/customspawns/CustomSpawns.java rename to src/fr/bencor29/customspawns/CustomSpawns.java index c3383a7..3edeb3c 100644 --- a/CustomSpawns/src/fr/bencor29/customspawns/CustomSpawns.java +++ b/src/fr/bencor29/customspawns/CustomSpawns.java @@ -1,175 +1,205 @@ -package fr.bencor29.customspawns; - -import java.io.File; -import java.util.ArrayList; -import java.util.List; -import java.util.logging.Level; - -import org.bukkit.Location; -import org.bukkit.World; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.plugin.java.JavaPlugin; - -public class CustomSpawns extends JavaPlugin { - - FileConfiguration genconf; - FileConfiguration grpconf; - - ArrayList groups = new ArrayList(); - int secs; - Location lobby; - - - public void onEnable() { - - String[] commands = {"spawn", "lobby", "setlobby", "csreload"}; - for(String cmd : commands) { - getCommand(cmd).setExecutor(new Commands(this)); - log(Level.INFO, "Loading command: " + cmd); - } - getCommand("group").setExecutor(new GroupsCommands(this)); - log(Level.INFO, "Loading command: group"); - - loadConfig(); - - log(Level.INFO, "Registering events"); - getServer().getPluginManager().registerEvents(new Events(this), this); - } - - public void onDisable() { - //grpconf.set("", arg1); - } - - public void loadConfig() { - - log(Level.INFO, "Loading configurations is starting."); - - File folder = getDataFolder(); - File config = new File(getDataFolder(), "config.yml"); - File groups = new File(getDataFolder(), "groups.yml"); - - if(!folder.exists()) folder.mkdirs(); - if(folder.isFile()) { - folder.renameTo(new File(folder.getName() + ".broken")); - folder = new File("plugins/CustomSpawns/"); - folder.mkdirs(); - } - - if(!config.exists()) { - saveResource("config.yml", true); - } - - if(config.isDirectory()) { - String temp_name = config.getName(); - config.renameTo(new File(temp_name + ".broken")); - config = new File(getDataFolder(), "config.yml"); - saveResource("config.yml", true); - } - - if(!groups.exists()) { - saveResource("groups.yml", true); - } - - if(groups.isDirectory()) { - String temp_name = groups.getName(); - groups.renameTo(new File(temp_name + ".broken")); - groups = new File(getDataFolder(), "groups.yml"); - saveResource("groups.yml", true); - } - - genconf = YamlConfiguration.loadConfiguration(config); - grpconf = YamlConfiguration.loadConfiguration(groups); - - secs = genconf.getInt("seconds"); - log(Level.INFO, "Loading configurations finished."); - - log(Level.INFO, "Loading lobby is starting."); - lobby = getLobby(); - log(Level.INFO, "Loading lobby finished."); - - log(Level.INFO, "Loading groups starting."); - getGroups(); - log(Level.INFO, "Loading groups finished."); - } - - public void onReload() { - loadConfig(); - } - - - public Location getLobby() { - try { - try { - World world = getServer().getWorld(genconf.getString("lobby.world")); - if(world == null) { - log(Level.WARNING, "Attetion: Le monde du lobby n'est pas correct !"); - return null; - } - double x = genconf.getDouble("lobby.x"), - y = genconf.getDouble("lobby.y"), - z = genconf.getDouble("lobby.z"); - float yaw = genconf.getLong("lobby.yaw"), - pitch = genconf.getLong("lobby.pitch"); - return new Location(world, x, y, z, yaw, pitch); - } catch(Exception e) { - log(Level.WARNING, "Attetion: Le monde du lobby n'est pas correct !"); - return null; - } - } catch(Exception e) { - log(Level.WARNING, "Attetion: Le lobby n'est pas d�finit !"); - return null; - } - } - - public File getConfigFile(int i) { - if(i == 0) { - return new File("plugins/CustomSpawns/config.yml"); - } else if(i == 1) { - return new File("plugins/CustomSpawns/groups.yml"); - } else { - return null; - } - } - - public FileConfiguration getFileConfig(int i) { - if(i == 0) { - return genconf; - } else if(i == 1) { - return grpconf; - } else { - return null; - } - } - - public void getGroups() { - try { - for(String name : grpconf.getConfigurationSection("groups").getKeys(false)) { - String path = "groups." + name + "."; - String loc = path + "spawn."; - try { - World world = getServer().getWorld(grpconf.getString(loc + "world")); - double x = grpconf.getDouble(loc + "x"), - y = grpconf.getDouble(loc + "y"), - z = grpconf.getDouble(loc + "z"); - float yaw = grpconf.getLong(loc + "yaw"), - pitch = grpconf.getLong(loc + "pitch"); - Location spawn = new Location(world, x, y, z, yaw, pitch); - List ttworlds = grpconf.getList(path + "worlds"); - ArrayList tworlds = new ArrayList(); - for(Object tworld : ttworlds) { - tworlds.add(tworld.toString()); - } - ArrayList worlds = tworlds; - Group group = new Group(name, worlds, spawn); - groups.add(group); - log(Level.INFO, "Loading group " + group.getName() + " / worlds: " + worlds + " / Spawn: " + spawn); - } catch (Exception e) {} - } - } catch(Exception e) {} - } - - public void log(Level level, String msg) { - getLogger().log(level, msg); - } +package fr.bencor29.customspawns; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; + +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.plugin.java.JavaPlugin; + +public class CustomSpawns extends JavaPlugin { + + private static CustomSpawns singleton; + + public static CustomSpawns getInstance() { + return singleton; + } + + private FileConfiguration genconf; + private FileConfiguration grpconf; + + private final ArrayList groups = new ArrayList(); + private int secs; + private Location lobby; + + public Location getLobby() { + return lobby; + } + + public void setLobby(Location lobby) { + this.lobby = lobby; + } + + public int getSecs() { + return secs; + } + + public FileConfiguration getGenconf() { + return genconf; + } + + public FileConfiguration getGrpconf() { + return grpconf; + } + + public ArrayList getGroups() { + return groups; + } + + public void onEnable() { + singleton = this; + GroupManager.initSingleton(this); + + String[] commands = {"spawn", "lobby", "setlobby", "csreload"}; + for(String cmd : commands) { + getCommand(cmd).setExecutor(new Commands(this)); + log(Level.INFO, "Loading command: " + cmd); + } + getCommand("group").setExecutor(new GroupsCommands(this)); + log(Level.INFO, "Loading command: group"); + + loadConfig(); + + log(Level.INFO, "Registering events"); + getServer().getPluginManager().registerEvents(new Events(), this); + } + + public void onDisable() { + //grpconf.set("", arg1); + } + + public void loadConfig() { + + log(Level.INFO, "Loading configurations is starting."); + + File folder = getDataFolder(); + File config = new File(getDataFolder(), "config.yml"); + File groups = new File(getDataFolder(), "groups.yml"); + + if(!folder.exists()) folder.mkdirs(); + if(folder.isFile()) { + folder.renameTo(new File(folder.getName() + ".broken")); + folder = new File("plugins/CustomSpawns/"); + folder.mkdirs(); + } + + if(!config.exists()) { + saveResource("config.yml", true); + } + + if(config.isDirectory()) { + String temp_name = config.getName(); + config.renameTo(new File(temp_name + ".broken")); + config = new File(getDataFolder(), "config.yml"); + saveResource("config.yml", true); + } + + if(!groups.exists()) { + saveResource("groups.yml", true); + } + + if(groups.isDirectory()) { + String temp_name = groups.getName(); + groups.renameTo(new File(temp_name + ".broken")); + groups = new File(getDataFolder(), "groups.yml"); + saveResource("groups.yml", true); + } + + genconf = YamlConfiguration.loadConfiguration(config); + grpconf = YamlConfiguration.loadConfiguration(groups); + + secs = genconf.getInt("seconds"); + log(Level.INFO, "Loading configurations finished."); + + log(Level.INFO, "Loading lobby is starting."); + lobby = loadLobby(); + log(Level.INFO, "Loading lobby finished."); + + log(Level.INFO, "Loading groups starting."); + loadGroups(); + log(Level.INFO, "Loading groups finished."); + } + + public void onReload() { + loadConfig(); + } + + + public Location loadLobby() { + try { + try { + World world = getServer().getWorld(genconf.getString("lobby.world")); + if(world == null) { + log(Level.SEVERE, "Error: Incorrect lobby world."); + return null; + } + double x = genconf.getDouble("lobby.x"), + y = genconf.getDouble("lobby.y"), + z = genconf.getDouble("lobby.z"); + float yaw = genconf.getLong("lobby.yaw"), + pitch = genconf.getLong("lobby.pitch"); + return new Location(world, x, y, z, yaw, pitch); + } catch(Exception e) { + log(Level.SEVERE, "Error: Incorrect lobby world."); + return null; + } + } catch(Exception e) { + log(Level.WARNING, "Lobby is not defined."); + return null; + } + } + + public File getConfigFile(int i) { + if(i == 0) { + return new File("plugins/CustomSpawns/config.yml"); + } else if(i == 1) { + return new File("plugins/CustomSpawns/groups.yml"); + } else { + return null; + } + } + + public FileConfiguration getFileConfig(int i) { + if(i == 0) { + return genconf; + } else if(i == 1) { + return grpconf; + } else { + return null; + } + } + + public void loadGroups() { + try { + for(String name : grpconf.getConfigurationSection("groups").getKeys(false)) { + String path = "groups." + name + "."; + String loc = path + "spawn."; + try { + World world = getServer().getWorld(grpconf.getString(loc + "world")); + double x = grpconf.getDouble(loc + "x"), + y = grpconf.getDouble(loc + "y"), + z = grpconf.getDouble(loc + "z"); + float yaw = grpconf.getLong(loc + "yaw"), + pitch = grpconf.getLong(loc + "pitch"); + Location spawn = new Location(world, x, y, z, yaw, pitch); + List ttworlds = grpconf.getList(path + "worlds"); + ArrayList tworlds = new ArrayList(); + for(Object tworld : ttworlds) { + tworlds.add(tworld.toString()); + } + Group group = new Group(name, tworlds, spawn); + groups.add(group); + log(Level.INFO, "Loading group " + group.getName() + " / worlds: " + tworlds + " / Spawn: " + spawn); + } catch (Exception ignored) {} + } + } catch(Exception ignored) {} + } + + public void log(Level level, String msg) { + getLogger().log(level, msg); + } } \ No newline at end of file diff --git a/CustomSpawns/src/fr/bencor29/customspawns/Events.java b/src/fr/bencor29/customspawns/Events.java similarity index 78% rename from CustomSpawns/src/fr/bencor29/customspawns/Events.java rename to src/fr/bencor29/customspawns/Events.java index c4ef2ed..aebb205 100644 --- a/CustomSpawns/src/fr/bencor29/customspawns/Events.java +++ b/src/fr/bencor29/customspawns/Events.java @@ -1,27 +1,25 @@ -package fr.bencor29.customspawns; - -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerRespawnEvent; - -public class Events implements Listener { - - CustomSpawns plugin; - GroupManager gm; - - public Events(CustomSpawns plugin) { - this.plugin = plugin; - gm = new GroupManager(plugin); - } - - @EventHandler(priority = EventPriority.HIGH) - public void onDeath(PlayerRespawnEvent event) { - Group group = gm.getGroupByWorld(event.getPlayer().getWorld()); - if(group == null) return; - if(group.getSpawn() == null) return; - if(!group.containWorld(event.getPlayer().getWorld().getName())) return; - event.setRespawnLocation(group.getSpawn()); - } - -} +package fr.bencor29.customspawns; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerRespawnEvent; + +public class Events implements Listener { + + private final GroupManager gm; + + public Events() { + gm = GroupManager.getInstance(); + } + + @EventHandler(priority = EventPriority.HIGH) + public void onDeath(PlayerRespawnEvent event) { + Group group = gm.getGroupByWorld(event.getPlayer().getWorld()); + if(group == null) return; + if(group.getSpawn() == null) return; + if(!group.containWorld(event.getPlayer().getWorld().getName())) return; + event.setRespawnLocation(group.getSpawn()); + } + +} diff --git a/CustomSpawns/src/fr/bencor29/customspawns/Group.java b/src/fr/bencor29/customspawns/Group.java similarity index 88% rename from CustomSpawns/src/fr/bencor29/customspawns/Group.java rename to src/fr/bencor29/customspawns/Group.java index 70b09b4..f2c3933 100644 --- a/CustomSpawns/src/fr/bencor29/customspawns/Group.java +++ b/src/fr/bencor29/customspawns/Group.java @@ -1,51 +1,51 @@ -package fr.bencor29.customspawns; - -import java.util.ArrayList; - -import org.bukkit.Location; - -public class Group { - - String name; - ArrayList worlds; - Location spawn; - - public Group(String name, ArrayList worlds, Location spawn) { - this.name = name; - this.worlds = worlds; - this.spawn = spawn; - } - - public void setName(String name) { - this.name = name; - } - - public void addWorld(String world) { - worlds.add(world); - } - - public void setSpawn(Location spawn) { - this.spawn = spawn; - } - - public void setWorlds(ArrayList worlds) { - this.worlds = worlds; - } - - public String getName() { - return name; - } - - public ArrayList getWorlds() { - return worlds; - } - - public Location getSpawn() { - return spawn; - } - - public boolean containWorld(String world) { - return (worlds.contains(world) ? true : false); - } - -} +package fr.bencor29.customspawns; + +import java.util.ArrayList; + +import org.bukkit.Location; + +public class Group { + + String name; + ArrayList worlds; + Location spawn; + + public Group(String name, ArrayList worlds, Location spawn) { + this.name = name; + this.worlds = worlds; + this.spawn = spawn; + } + + public void setName(String name) { + this.name = name; + } + + public void addWorld(String world) { + worlds.add(world); + } + + public void setSpawn(Location spawn) { + this.spawn = spawn; + } + + public void setWorlds(ArrayList worlds) { + this.worlds = worlds; + } + + public String getName() { + return name; + } + + public ArrayList getWorlds() { + return worlds; + } + + public Location getSpawn() { + return spawn; + } + + public boolean containWorld(String world) { + return worlds.contains(world); + } + +} diff --git a/src/fr/bencor29/customspawns/GroupManager.java b/src/fr/bencor29/customspawns/GroupManager.java new file mode 100644 index 0000000..3f0fcd5 --- /dev/null +++ b/src/fr/bencor29/customspawns/GroupManager.java @@ -0,0 +1,142 @@ +package fr.bencor29.customspawns; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.logging.Level; + +import org.bukkit.Location; +import org.bukkit.World; + +public class GroupManager { + + private static GroupManager singleton; + + public static GroupManager getInstance() { + return singleton; + } + + public static void initSingleton(CustomSpawns plugin) { + if (singleton != null) { + return; + } + + singleton = new GroupManager(plugin); + } + + + private final CustomSpawns plugin; + + private GroupManager(CustomSpawns plugin) { + this.plugin = plugin; + } + + public void saveGroup(Group group) { + String name = group.getName(); + ArrayList worlds = group.getWorlds(); + Location spawn = group.getSpawn(); + String w = spawn.getWorld().getName(); + String path = "groups." + name + "."; + String loc = path + "spawn."; + plugin.getGrpconf().set(path + "worlds", worlds); + plugin.getGrpconf().set(loc + "world", w); + plugin.getGrpconf().set(loc + "x", spawn.getX()); + plugin.getGrpconf().set(loc + "y", spawn.getY()); + plugin.getGrpconf().set(loc + "z", spawn.getZ()); + plugin.getGrpconf().set(loc + "yaw", spawn.getYaw()); + plugin.getGrpconf().set(loc + "pitch", spawn.getPitch()); + File file = new File(plugin.getDataFolder(), "groups.yml"); + try { + plugin.getGrpconf().save(file); + } catch (IOException e) { + plugin.getLogger().log(Level.INFO, "Cannot save configs."); + } + } + + public int getGroupIdByWorld(String worldname) { + for(int i = 0; i < plugin.getGroups().size(); i++) { + if(plugin.getGroups().get(i).containWorld(worldname)) return i + 1; + } + return 0; + } + + public Group getGroupByWorld(String worldname) { + for(int i = 0; i < plugin.getGroups().size(); i++) { + if(plugin.getGroups().get(i).containWorld(worldname)) return plugin.getGroups().get(i); + } + return null; + } + + public Group getGroupByWorld(World world) { + return getGroupByWorld(world.getName()); + } + + public Group getGroupByName(String name) { + for(Group group : plugin.getGroups()) { + if(group.getName().equalsIgnoreCase(name)) + return group; + } + return null; + } + + public int getGroupIdByName(String name) { + for(int i = 0; i < plugin.getGroups().size(); i++) { + if(plugin.getGroups().get(i).getName().equalsIgnoreCase(name)) { + return i + 1; + } + } + return 0; + } + + public boolean checkAddWorld(String worldname) { + for(Group group : plugin.getGroups()) + if(group.containWorld(worldname)) return true; + return false; + } + + public boolean checkCreate(String name, String worldname) { + for(Group group : plugin.getGroups()) { + if(group.getName().equalsIgnoreCase(name)) return true; + if(group.containWorld(worldname)) return true; + } + return false; + } + + public ArrayList getGroupsFromFile() { + ArrayList groups = new ArrayList(); + String gpath = "groups.", path = "", loc = ""; + for(String name : plugin.getGrpconf().getConfigurationSection("groups").getKeys(false)) { + try { + path = gpath + name + "."; + loc = path + "spawn."; + @SuppressWarnings("unchecked") + ArrayList worlds = (ArrayList) plugin.getGrpconf().getList(path + "worlds"); + World world = plugin.getServer().getWorld(plugin.getGrpconf().getString(loc + "world")); + double x = plugin.getGrpconf().getDouble(loc + "x"), + y = plugin.getGrpconf().getDouble(loc + "y"), + z = plugin.getGrpconf().getDouble(loc + "z"); + float yaw = plugin.getGrpconf().getLong(loc + "yaw"), + pitch = plugin.getGrpconf().getLong(loc + "pitch"); + Location spawn = new Location(world, x, y, z, yaw, pitch); + Group group = new Group(name, worlds, spawn); + groups.add(group); + } catch(Exception e) { + plugin.log(Level.WARNING, "Cannot load group: " + name); + } + } + return groups; + } + + public Location getSpawnByNameFromConfig(String name) { + String path = "groups." + name + "."; + String loc = path + "spawn."; + World world = plugin.getServer().getWorld(plugin.getGrpconf().getString(loc + "world")); + double x = plugin.getGrpconf().getDouble(loc + "x"), + y = plugin.getGrpconf().getDouble(loc + "y"), + z = plugin.getGrpconf().getDouble(loc + "z"); + float yaw = plugin.getGrpconf().getLong(loc + "yaw"), + pitch = plugin.getGrpconf().getLong(loc + "pitch"); + return new Location(world, x, y, z, yaw, pitch); + } + +} diff --git a/src/fr/bencor29/customspawns/GroupsCommands.java b/src/fr/bencor29/customspawns/GroupsCommands.java new file mode 100644 index 0000000..ac45936 --- /dev/null +++ b/src/fr/bencor29/customspawns/GroupsCommands.java @@ -0,0 +1,235 @@ +package fr.bencor29.customspawns; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.logging.Level; + +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class GroupsCommands implements CommandExecutor { + + private final CustomSpawns plugin; + private final GroupManager gm; + + public GroupsCommands(CustomSpawns plugin) { + this.plugin = plugin; + gm = GroupManager.getInstance(); + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (args.length == 0) { + commandFail(sender); + return true; + } + if ( + args.length != 3 && + !args[0].equalsIgnoreCase("setworldspawn") && + !args[0].equalsIgnoreCase("remove") && + !args[0].equalsIgnoreCase("listworlds") && + !args[0].equalsIgnoreCase("list")) { + commandFail(sender); + return true; + } + if ( + args.length != 2 && args[0].equalsIgnoreCase("setworldspawn") || + args.length != 2 && args[0].equalsIgnoreCase("remove") || + args.length != 2 && args[0].equalsIgnoreCase("listworlds") || + args.length != 1 && args[0].equalsIgnoreCase("list")) { + commandFail(sender); + return true; + } + String cmd = args[0]; + if (cmd.equalsIgnoreCase("create")) commandCreate(sender, args); + else if (cmd.equalsIgnoreCase("remove")) commandRemove(sender, args); + else if (cmd.equalsIgnoreCase("list")) commandList(sender); + else if (cmd.equalsIgnoreCase("listworlds")) commandListWorlds(sender, args); + else if (cmd.equalsIgnoreCase("addworld")) commandAddWorld(sender, args); + else if (cmd.equalsIgnoreCase("delworld")) commandDelWorld(sender, args); + else if (cmd.equalsIgnoreCase("setworldspawn")) commandSetWorldSpawn(sender, args); + else commandFail(sender); + return true; + } + + public void commandListWorlds(CommandSender sender, String[] args) { + String name = args[1]; + if (gm.getGroupByName(name) == null) { + Utils.sendMessage(sender, "§cThat group doesn't exists."); + return; + } + Group group = gm.getGroupByName(name); + Utils.sendMessage(sender, "Worlds in group:"); + for(String world : group.getWorlds()) { + if (sender instanceof Player) { + sender.sendMessage("§7 - " + world); + } else { + plugin.log(Level.INFO, " - " + world); + } + } + } + + public void commandList(CommandSender sender) { + Utils.sendMessage(sender, "§6§lGroups list:"); + for(Group group : gm.getGroupsFromFile()) { + try { + Utils.sendMessage( + sender, + String.format( + " §7- §e§l%s§7 -> Worlds: %d / Spawn world: %s", + group.getName(), group.getWorlds().size(), group.getSpawn().getWorld().getName() + ) + ); + } catch (Exception e) { + Utils.sendMessage( + sender, + String.format( + " §7- §e§l%s§7 -> Worlds: %d / Spawn world: §cInvalid world", + group.getName(), group.getWorlds().size() + ) + ); + } + } + } + + public void commandRemove(CommandSender sender, String[] args) { + String name = args[1]; + if (gm.getGroupByName(name) == null) { + Utils.sendMessage(sender, "§cThat group doesn't exists."); + return; + } + + int id = gm.getGroupIdByName(name) - 1; + plugin.getGroups().remove(id); + plugin.getGrpconf().set("groups." + name, null); + + try { + plugin.getGrpconf().save(plugin.getConfigFile(1)); + } catch (IOException e) { + Utils.sendMessage(sender, "§cFailed to save group."); + return; + } + + Utils.sendMessage(sender, "§aGroup removed."); + } + + private void commandFail(CommandSender sender) { + String[] help = new String[] { + "§cUsage:", + "§7 §o /group §elist", + "§7 §o /group §ecreate ", + "§7 §o /group §eremove ", + "§7 §o /group §eaddworld ", + "§7 §o /group §edelworld ", + "§7 §o /group §elistworlds ", + "§7 §o /group §esetworldspawn " + }; + + Utils.sendMessage(sender, help); + } + + public void commandSetWorldSpawn(CommandSender sender, String[] args) { + if (!(sender instanceof Player)) { + Utils.sendMessage(sender, "§cOnly players can execute this command."); + return; + } + Player player = (Player) sender; + String name = args[1]; + int id = gm.getGroupIdByName(name); + if (id == 0) { + Utils.sendMessage(player, "§cThat group doesn't exists."); + return; + } + id--; + Group group = plugin.getGroups().get(id); + group.setSpawn(Utils.getLocation(player)); + plugin.getGroups().set(id, group); + gm.saveGroup(group); + Utils.sendMessage(player, "§aGroup successfully modified."); + } + + private void commandDelWorld(CommandSender sender, String[] args) { + String worldname = args[2]; + String name = args[1]; + if (!gm.checkAddWorld(worldname)) { + Utils.sendMessage(sender, "§cThat world isn't in any group."); + return; + } + + int id = gm.getGroupIdByWorld(worldname); + if (id == 0) { + Utils.sendMessage(sender, "§cThat world isn't in any group."); + return; + } + id--; + if (!gm.getGroupByName(name).containWorld(worldname)) { + Utils.sendMessage(sender, "§cThat world isn't in that group."); + return; + } + plugin.getGroups().remove(id); + Utils.sendMessage(sender, "§aWorld removed from group."); + } + + public void commandAddWorld(CommandSender sender, String[] args) { + String name = args[1]; + String worldname = args[2]; + if (gm.checkAddWorld(worldname)) { + Utils.sendMessage(sender, "§cThat world is already in a group."); + return; + } + + try { + World world = plugin.getServer().getWorld(worldname); + if (world == null) { + Utils.sendMessage(sender, "§cThat world doesn't exists."); + return; + } + } catch(Exception e) { + Utils.sendMessage(sender, "§cThat world doesn't exists."); + return; + } + + int id = gm.getGroupIdByName(name); + if (id == 0) { + Utils.sendMessage(sender, "§cThat group doesn't exists."); + return; + } + id--; + Group group = plugin.getGroups().get(id); + group.addWorld(worldname); + plugin.getGroups().set(id, group); + gm.saveGroup(group); + Utils.sendMessage(sender, "§aWorld added."); + } + + public void commandCreate(CommandSender sender, String[] args) { + String name = args[1]; + String worldname = args[2]; + if (gm.checkCreate(name, worldname)) { + Utils.sendMessage(sender, "§cThat group/world is already used."); + return; + } + try { + World world = plugin.getServer().getWorld(worldname); + if (world == null) { + Utils.sendMessage(sender, "§cThat group doesn't exists."); + return; + } + Location spawn = new Location(world, 0, 0, 0, 0, 0); + ArrayList worlds = new ArrayList<>(); + worlds.add(world.getName()); + Group group = new Group(name, worlds, spawn); + plugin.getGroups().add(group); + gm.saveGroup(group); + } catch(Exception e) { + Utils.sendMessage(sender, "§cThat group doesn't exists."); + return; + } + + Utils.sendMessage(sender, "§aGroup created."); + } +} diff --git a/src/fr/bencor29/customspawns/Utils.java b/src/fr/bencor29/customspawns/Utils.java new file mode 100644 index 0000000..36b19a9 --- /dev/null +++ b/src/fr/bencor29/customspawns/Utils.java @@ -0,0 +1,40 @@ +package fr.bencor29.customspawns; + +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.util.logging.Level; + +public class Utils { + + public static Location getLocation(Player player) { + Location ploc = player.getLocation(); + World world = ploc.getWorld(); + + double x = ploc.getX(), + y = ploc.getY(), + z = ploc.getZ(); + + float yaw = ploc.getYaw(), + pitch = ploc.getPitch(); + + return new Location(world, x, y, z, yaw, pitch); + } + + public static void sendMessage(CommandSender sender, String message) { + if (sender instanceof Player) { + sender.sendMessage(message); + } else { + CustomSpawns.getInstance().log(Level.INFO, message); + } + } + + public static void sendMessage(CommandSender sender, String[] messages) { + for (String message : messages) { + sendMessage(sender, message); + } + } + +} diff --git a/CustomSpawns/src/groups.yml b/src/groups.yml similarity index 93% rename from CustomSpawns/src/groups.yml rename to src/groups.yml index 22ecf74..11f6a3c 100644 --- a/CustomSpawns/src/groups.yml +++ b/src/groups.yml @@ -1,27 +1,27 @@ -# Configuration des groupes -# -#groups: -# nom_du_monde: -# worlds: -# - list_de_tout -# - les_differents -# - monde_du_groupe -# - monde_de_spawn -# spawn: -# world: monde_de_spawn -# x: 0 -# y: 1 -# z: 0 -# yaw: 0 -# pitch: 0 -groups: - groupe: - worlds: - - world - spawn: - world: world - x: 0 - y: 100 - z: 0 - yaw: 0 +# Configuration des groupes +# +#groups: +# nom_du_monde: +# worlds: +# - list_de_tout +# - les_differents +# - monde_du_groupe +# - monde_de_spawn +# spawn: +# world: monde_de_spawn +# x: 0 +# y: 1 +# z: 0 +# yaw: 0 +# pitch: 0 +groups: + groupe: + worlds: + - world + spawn: + world: world + x: 0 + y: 100 + z: 0 + yaw: 0 pitch: 0 \ No newline at end of file diff --git a/CustomSpawns/src/plugin.yml b/src/plugin.yml similarity index 97% rename from CustomSpawns/src/plugin.yml rename to src/plugin.yml index cb6f293..241b785 100644 --- a/CustomSpawns/src/plugin.yml +++ b/src/plugin.yml @@ -1,25 +1,25 @@ -name: CustomSpawns -author: Bencor29 -website: http://bencor29.livehost.fr -version: 1.4.3 -softdepend: [Multiverse-Core, WorldGuard] -main: fr.bencor29.customspawns.CustomSpawns -commands: - spawn: - description: Vous tp vers le spawn du monde. - usage: /spawn - lobby: - description: Vous tp vers le lobby. - usage: /lobby - group: - description: Gere les groupes. - usage: /group [list | create | remove | addworld | delworld | listworlds | setworldspawn ] - permission: customspawns.admin - setlobby: - description: Gere le lobby. - usage: /setlobby - permission: customspawns.admin - csreload: - description: Recharge le plugin. - usage: /csreload +name: CustomSpawns +author: Bencor29 +website: http://bencor29.livehost.fr +version: 1.4.3 +softdepend: [Multiverse-Core, WorldGuard] +main: fr.bencor29.customspawns.CustomSpawns +commands: + spawn: + description: Vous tp vers le spawn du monde. + usage: /spawn + lobby: + description: Vous tp vers le lobby. + usage: /lobby + group: + description: Gere les groupes. + usage: /group [list | create | remove | addworld | delworld | listworlds | setworldspawn ] + permission: customspawns.admin + setlobby: + description: Gere le lobby. + usage: /setlobby + permission: customspawns.admin + csreload: + description: Recharge le plugin. + usage: /csreload permission: customspawns.admin \ No newline at end of file