diff --git a/src/main/java/de/fanta/challenges/commands/VillageCommand.java b/src/main/java/de/fanta/challenges/commands/VillageCommand.java index c9a5d08..a256688 100644 --- a/src/main/java/de/fanta/challenges/commands/VillageCommand.java +++ b/src/main/java/de/fanta/challenges/commands/VillageCommand.java @@ -7,11 +7,15 @@ import org.bukkit.Bukkit; import org.bukkit.HeightMap; import org.bukkit.Location; -import org.bukkit.StructureType; -import org.bukkit.block.Structure; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.bukkit.generator.structure.Structure; +import org.bukkit.util.StructureSearchResult; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; public class VillageCommand extends SubCommand { @@ -23,32 +27,76 @@ @Override public boolean onCommand(CommandSender sender, Command command, String alias, String commandString, ArgsParser args) { - if (!(sender instanceof Player)) { + if (!(sender instanceof Player player)) { ChatUtil.sendErrorMessage(sender, "You are not a Player :>"); return true; } - if (plugin.isEditor((Player) sender) || sender.hasPermission("Challenges.editor.override")) { - Player p = (Player) sender; - Location village = Bukkit.getWorlds().get(0).locateNearestStructure(p.getLocation(), StructureType.VILLAGE, 10000, true); - int x = village.getBlockX(); - int z = village.getBlockZ(); - Location villagetop = village.getWorld().getHighestBlockAt(x, z, HeightMap.MOTION_BLOCKING).getLocation().add(0, 1, 0); - int y = villagetop.getBlockY(); - Bukkit.getWorlds().get(0).setSpawnLocation(villagetop); - ChatUtil.sendNormalMessage(p, "Dorf: " + ChatUtil.BLUE + x + " " + y + " " + z); - for (Player pp : Bukkit.getOnlinePlayers()) { - pp.teleport(villagetop); - ChatUtil.sendNormalMessage(pp, "Dorfspawn gesetzt!"); + if (plugin.isEditor(player) || player.hasPermission("Challenges.editor.override")) { + if (args.hasNext()) { + String structureString = args.getNext(); + Structure structure; + + try { + structure = VillageTypes.valueOf(structureString.toUpperCase()).getStructure(); + } catch (IllegalArgumentException ex) { + ChatUtil.sendErrorMessage(player, "Das von dir gesuchte Villager Dorf (" + structureString + ") gibt es nicht."); + return true; + } + + StructureSearchResult villageResult = player.getWorld().locateNearestStructure(player.getLocation(), structure, 10000, true); + + if (villageResult == null) { + ChatUtil.sendErrorMessage(player, "Kein Villager Dorf gefunden."); + return true; + } + + Location village = villageResult.getLocation(); + int x = village.getBlockX(); + int z = village.getBlockZ(); + Location villagetop = village.getWorld().getHighestBlockAt(x, z, HeightMap.MOTION_BLOCKING).getLocation().add(0, 1, 0); + int y = villagetop.getBlockY(); + Bukkit.getWorlds().get(0).setSpawnLocation(villagetop); + ChatUtil.sendNormalMessage(player, "Dorf: " + ChatUtil.BLUE + x + " " + y + " " + z); + for (Player pp : Bukkit.getOnlinePlayers()) { + pp.teleport(villagetop); + ChatUtil.sendNormalMessage(pp, "Dorfspawn gesetzt!"); + } + } else { + ChatUtil.sendWarningMessage(player, "/village "); } return true; } else if (plugin.getCurrentEditor() != null) { - ChatUtil.sendErrorMessage(sender, "Du bist kein Editor! nur" + plugin.getCurrentEditor().getName() + " kann nach Dörfern suchen"); + ChatUtil.sendErrorMessage(player, "Du bist kein Editor! nur" + plugin.getCurrentEditor().getName() + " kann nach Dörfern suchen"); return true; } else { - ChatUtil.sendErrorMessage(sender, "Aktuell gibt es keinen Editor!"); - ChatUtil.sendErrorMessage(sender, "Um selbst Editor zu werden musst du dir im Freebuild VIP Kaufen!"); + ChatUtil.sendErrorMessage(player, "Aktuell gibt es keinen Editor!"); + ChatUtil.sendErrorMessage(player, "Um selbst Editor zu werden musst du dir im Freebuild VIP Kaufen!"); return true; } } + + @Override + public Collection onTabComplete(CommandSender sender, Command command, String alias, ArgsParser args) { + String[] villages = new String[] { "Plains", "Desert", "Savanna", "Snowy", "Taiga" }; + return new ArrayList<>(Arrays.asList(villages)); + } + + public enum VillageTypes { + PLAINS(Structure.VILLAGE_PLAINS), + DESERT(Structure.VILLAGE_DESERT), + SAVANNA(Structure.VILLAGE_SAVANNA), + SNOWY(Structure.VILLAGE_SNOWY), + TAIGA(Structure.VILLAGE_TAIGA); + + private final Structure structure; + + VillageTypes(Structure structure) { + this.structure = structure; + } + + public Structure getStructure() { + return structure; + } + } } \ No newline at end of file