diff options
Diffstat (limited to 'src/main/java/cat/freya/khs/command')
36 files changed, 2394 insertions, 0 deletions
diff --git a/src/main/java/cat/freya/khs/command/Confirm.java b/src/main/java/cat/freya/khs/command/Confirm.java new file mode 100644 index 0000000..34b145f --- /dev/null +++ b/src/main/java/cat/freya/khs/command/Confirm.java @@ -0,0 +1,62 @@ +package cat.freya.khs.command; + +import cat.freya.khs.command.util.ICommand; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.*; +import java.util.function.Consumer; + +import static cat.freya.khs.configuration.Config.errorPrefix; +import static cat.freya.khs.configuration.Localization.message; + +public class Confirm implements ICommand { + + public static final Map<UUID, Confirmation> confirmations = new HashMap<>(); + + public void execute(Player sender, String[] args) { + Confirmation confirmation = confirmations.get(sender.getUniqueId()); + confirmations.remove(sender.getUniqueId()); + if(confirmation == null) { + sender.sendMessage(errorPrefix + message("NO_CONFIRMATION")); + } else { + long now = System.currentTimeMillis(); + float secs = (now - confirmation.start) / 1000F; + if(secs > 10) { + sender.sendMessage(errorPrefix + message("CONFIRMATION_TIMED_OUT")); + return; + } + confirmation.callback.accept(confirmation.data); + } + } + + public String getLabel() { + return "confirm"; + } + + public String getUsage() { + return ""; + } + + public String getDescription() { + return "Confirm another command if required"; + } + + public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + return null; + } + + public static class Confirmation { + public final Consumer<String> callback; + public final String data; + public final long start; + + public Confirmation(String data, Consumer<String> callback) { + this.callback = callback; + this.data = data; + this.start = System.currentTimeMillis(); + } + + } + +} diff --git a/src/main/java/cat/freya/khs/command/Help.java b/src/main/java/cat/freya/khs/command/Help.java new file mode 100644 index 0000000..6d7a1f3 --- /dev/null +++ b/src/main/java/cat/freya/khs/command/Help.java @@ -0,0 +1,87 @@ +package cat.freya.khs.command; + +import cat.freya.khs.Main; +import cat.freya.khs.command.util.ICommand; +import cat.freya.khs.util.Pair; +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +import static cat.freya.khs.configuration.Config.errorPrefix; +import static cat.freya.khs.configuration.Localization.message; + +public class Help implements ICommand { + + public void execute(Player sender, String[] args) { + final int pageSize = 4; + List<Pair<String, ICommand>> commands = Main.getInstance().getCommandGroup().getCommands(); + int pages = (commands.size() - 1) / pageSize + 1; + int page; + try { + if(args.length < 1) { + page = 1; + } else { + page = Integer.parseInt(args[0]); + if (page < 1) { + throw new IllegalArgumentException("Inavlid Input"); + } + } + } catch (Exception e) { + sender.sendMessage(errorPrefix + message("WORLDBORDER_INVALID_INPUT").addAmount(args[0])); + return; + } + String spacer = ChatColor.GRAY + "?" + ChatColor.WHITE; + StringBuilder message = new StringBuilder(); + message.append(String.format("%s================ %sHelp: Page (%s/%s) %s================", + ChatColor.AQUA, ChatColor.WHITE, page, pages, ChatColor.AQUA)); + int lines = 0; + for(Pair<String, ICommand> pair : commands.stream().skip((long) (page - 1) * pageSize).limit(pageSize).collect(Collectors.toList())) { + ICommand command = pair.getRight(); + String label = pair.getLeft(); + String start = label.substring(0, label.indexOf(" ")); + String invoke = label.substring(label.indexOf(" ")+1); + message.append(String.format("\n%s %s/%s %s%s %s%s\n%s %s%s%s", + spacer, + ChatColor.AQUA, + start, + ChatColor.WHITE, + invoke, + ChatColor.BLUE, + command.getUsage(), + spacer, + ChatColor.GRAY, + ChatColor.ITALIC, + command.getDescription() + )); + lines += 2; + } + if(lines / 2 < pageSize) { + for(int i = 0; i < pageSize * 2 - lines; i++) { + message.append("\n").append(spacer); + } + } + message.append("\n").append(ChatColor.AQUA).append("==============================================="); + sender.sendMessage(message.toString()); + } + + public String getLabel() { + return "help"; + } + + public String getUsage() { + return "<*page>"; + } + + public String getDescription() { + return "Get the commands for the plugin"; + } + + public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + return Collections.singletonList(parameter); + } + +} diff --git a/src/main/java/cat/freya/khs/command/Join.java b/src/main/java/cat/freya/khs/command/Join.java new file mode 100644 index 0000000..237c70e --- /dev/null +++ b/src/main/java/cat/freya/khs/command/Join.java @@ -0,0 +1,49 @@ +package cat.freya.khs.command; + +import cat.freya.khs.Main; +import cat.freya.khs.command.util.ICommand; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +import static cat.freya.khs.configuration.Config.errorPrefix; +import static cat.freya.khs.configuration.Localization.message; + +public class Join implements ICommand { + + public void execute(Player sender, String[] args) { + if (Main.getInstance().getGame().checkCurrentMap()) { + sender.sendMessage(errorPrefix + message("GAME_SETUP")); + return; + } + Player player = Bukkit.getServer().getPlayer(sender.getName()); + if (player == null) { + sender.sendMessage(errorPrefix + message("COMMAND_ERROR")); + return; + } + if (Main.getInstance().getBoard().contains(player)) { + sender.sendMessage(errorPrefix + message("GAME_INGAME")); + return; + } + Main.getInstance().getGame().join(player); + } + + public String getLabel() { + return "join"; + } + + public String getUsage() { + return "<*map>"; + } + + public String getDescription() { + return "Joins the lobby if game is set to manual join/leave"; + } + + public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + return null; + } + +} diff --git a/src/main/java/cat/freya/khs/command/Leave.java b/src/main/java/cat/freya/khs/command/Leave.java new file mode 100644 index 0000000..7b64fe3 --- /dev/null +++ b/src/main/java/cat/freya/khs/command/Leave.java @@ -0,0 +1,49 @@ +package cat.freya.khs.command; + +import cat.freya.khs.Main; +import cat.freya.khs.command.util.ICommand; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +import static cat.freya.khs.configuration.Config.errorPrefix; +import static cat.freya.khs.configuration.Localization.message; + +public class Leave implements ICommand { + + public void execute(Player sender, String[] args) { + if (Main.getInstance().getGame().checkCurrentMap()) { + sender.sendMessage(errorPrefix + message("GAME_SETUP")); + return; + } + Player player = Bukkit.getServer().getPlayer(sender.getName()); + if (player == null) { + sender.sendMessage(errorPrefix + message("COMMAND_ERROR")); + return; + } + if (!Main.getInstance().getBoard().contains(player)) { + sender.sendMessage(errorPrefix + message("GAME_NOT_INGAME")); + return; + } + Main.getInstance().getGame().leave(player); + } + + public String getLabel() { + return "leave"; + } + + public String getUsage() { + return ""; + } + + public String getDescription() { + return "Leaves the lobby if game is set to manual join/leave"; + } + + public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + return null; + } + +}
\ No newline at end of file diff --git a/src/main/java/cat/freya/khs/command/Reload.java b/src/main/java/cat/freya/khs/command/Reload.java new file mode 100644 index 0000000..767d2c5 --- /dev/null +++ b/src/main/java/cat/freya/khs/command/Reload.java @@ -0,0 +1,55 @@ +package cat.freya.khs.command; + +import cat.freya.khs.Main; +import cat.freya.khs.command.util.ICommand; +import cat.freya.khs.configuration.*; +import cat.freya.khs.game.util.Status; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +import static cat.freya.khs.configuration.Config.errorPrefix; +import static cat.freya.khs.configuration.Config.messagePrefix; +import static cat.freya.khs.configuration.Localization.message; + +public class Reload implements ICommand { + + public void execute(Player sender, String[] args) { + + if (Main.getInstance().getGame().getStatus() != Status.STANDBY) { + sender.sendMessage(errorPrefix + message("GAME_INPROGRESS")); + return; + } + + try { + Config.loadConfig(); + Maps.loadMaps(); + Localization.loadLocalization(); + Items.loadItems(); + Leaderboard.loadLeaderboard(); + } catch (Exception e) { + sender.sendMessage(errorPrefix + message("CONFIG_ERROR")); + return; + } + + sender.sendMessage(messagePrefix + message("CONFIG_RELOAD")); + } + + public String getLabel() { + return "reload"; + } + + public String getUsage() { + return ""; + } + + public String getDescription() { + return "Reloads the config"; + } + + public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + return null; + } + +} diff --git a/src/main/java/cat/freya/khs/command/Send.java b/src/main/java/cat/freya/khs/command/Send.java new file mode 100644 index 0000000..1f110b8 --- /dev/null +++ b/src/main/java/cat/freya/khs/command/Send.java @@ -0,0 +1,68 @@ +package cat.freya.khs.command; + +import cat.freya.khs.Main; +import cat.freya.khs.command.util.ICommand; +import cat.freya.khs.configuration.Config; +import cat.freya.khs.configuration.Localization; +import cat.freya.khs.configuration.Map; +import cat.freya.khs.configuration.Maps; +import cat.freya.khs.game.util.Status; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.stream.Collectors; + +public class Send implements ICommand { + + public void execute(Player sender, String[] args) { + + if (Main.getInstance().getGame().getStatus() != Status.STANDBY) { + sender.sendMessage(Config.errorPrefix + Localization.message("GAME_INPROGRESS")); + return; + } + + Map map = Maps.getMap(args[0]); + if(map == null) { + sender.sendMessage(Config.errorPrefix + Localization.message("INVALID_MAP")); + return; + } + + if(map.isNotSetup()){ + sender.sendMessage(Config.errorPrefix + Localization.message("MAP_NOT_SETUP")); + return; + } + + if (!Main.getInstance().getBoard().contains(sender)) { + sender.sendMessage(Config.errorPrefix + Localization.message("GAME_NOT_INGAME")); + return; + } + + Main.getInstance().getGame().setCurrentMap(map); + Main.getInstance().getBoard().reloadLobbyBoards(); + for(Player player : Main.getInstance().getBoard().getPlayers()) { + map.getLobby().teleport(player); + } + + } + + public String getLabel() { + return "send"; + } + + public String getUsage() { + return "<map>"; + } + + public String getDescription() { + return "Set the current lobby to another map"; + } + + public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + if(parameter.equals("map")) { + return Maps.getAllMaps().stream().filter(map -> !map.isNotSetup()).map(Map::getName).collect(Collectors.toList()); + } + return null; + } + +}
\ No newline at end of file diff --git a/src/main/java/cat/freya/khs/command/SetExitLocation.java b/src/main/java/cat/freya/khs/command/SetExitLocation.java new file mode 100644 index 0000000..637088b --- /dev/null +++ b/src/main/java/cat/freya/khs/command/SetExitLocation.java @@ -0,0 +1,42 @@ +package cat.freya.khs.command; + +import cat.freya.khs.command.location.LocationUtils; +import cat.freya.khs.command.location.Locations; +import cat.freya.khs.command.util.ICommand; +import cat.freya.khs.configuration.Config; +import cat.freya.khs.util.Location; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +public class SetExitLocation implements ICommand { + + public void execute(Player sender, String[] args) { + LocationUtils.setLocation(sender, Locations.EXIT, null, map -> { + Config.addToConfig("exit.x", sender.getLocation().getBlockX()); + Config.addToConfig("exit.y", sender.getLocation().getBlockY()); + Config.addToConfig("exit.z", sender.getLocation().getBlockZ()); + Config.addToConfig("exit.world", sender.getLocation().getWorld().getName()); + Config.exitPosition = Location.from(sender); + Config.saveConfig(); + }); + } + + public String getLabel() { + return "setexit"; + } + + public String getUsage() { + return ""; + } + + public String getDescription() { + return "Sets the plugins exit location"; + } + + public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + return null; + } + +} diff --git a/src/main/java/cat/freya/khs/command/Start.java b/src/main/java/cat/freya/khs/command/Start.java new file mode 100644 index 0000000..e28a6cc --- /dev/null +++ b/src/main/java/cat/freya/khs/command/Start.java @@ -0,0 +1,77 @@ +package cat.freya.khs.command; + +import cat.freya.khs.Main; +import cat.freya.khs.command.util.ICommand; +import cat.freya.khs.game.util.Status; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +import static cat.freya.khs.configuration.Config.*; +import static cat.freya.khs.configuration.Localization.message; + +public class Start implements ICommand { + + public void execute(Player sender, String[] args) { + if (Main.getInstance().getGame().checkCurrentMap()) { + sender.sendMessage(errorPrefix + message("GAME_SETUP")); + return; + } + if (Main.getInstance().getGame().getStatus() != Status.STANDBY) { + sender.sendMessage(errorPrefix + message("GAME_INPROGRESS")); + return; + } + if (!Main.getInstance().getBoard().contains(sender)) { + sender.sendMessage(errorPrefix + message("GAME_NOT_INGAME")); + return; + } + if (Main.getInstance().getBoard().size() < minPlayers) { + sender.sendMessage(errorPrefix + message("START_MIN_PLAYERS").addAmount(minPlayers)); + return; + } + + if (args.length < 1) { + Main.getInstance().getGame().start(); + return; + }; + + List<Player> initialSeekers = new ArrayList<>(); + for (int i = 0; i < args.length; i++) { + Player seeker = Bukkit.getPlayer(args[i]); + if (seeker == null || !Main.getInstance().getBoard().contains(seeker) || initialSeekers.contains(seeker)) { + sender.sendMessage(errorPrefix + message("START_INVALID_NAME").addPlayer(args[i])); + return; + } + initialSeekers.add(seeker); + } + + int minHiders = minPlayers - startingSeekerCount; + if (Main.getInstance().getBoard().size() - initialSeekers.size() < minHiders) { + sender.sendMessage(errorPrefix + message("START_MIN_PLAYERS").addAmount(minPlayers)); + return; + } + + Main.getInstance().getGame().start(initialSeekers); + } + + public String getLabel() { + return "start"; + } + + public String getUsage() { + return "<*seekers...>"; + } + + public String getDescription() { + return "Starts the game either with a random set of seekers or a chosen list"; + } + + public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + return Main.getInstance().getBoard().getPlayers().stream().map(Player::getName).collect(Collectors.toList()); + } + +} diff --git a/src/main/java/cat/freya/khs/command/Stop.java b/src/main/java/cat/freya/khs/command/Stop.java new file mode 100644 index 0000000..c272c7b --- /dev/null +++ b/src/main/java/cat/freya/khs/command/Stop.java @@ -0,0 +1,46 @@ +package cat.freya.khs.command; + +import cat.freya.khs.Main; +import cat.freya.khs.command.util.ICommand; +import cat.freya.khs.game.util.Status; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +import static cat.freya.khs.configuration.Config.abortPrefix; +import static cat.freya.khs.configuration.Config.errorPrefix; +import static cat.freya.khs.configuration.Localization.message; + +public class Stop implements ICommand { + + public void execute(Player sender, String[] args) { + if (Main.getInstance().getGame().checkCurrentMap()) { + sender.sendMessage(errorPrefix + message("GAME_SETUP")); + return; + } + if (Main.getInstance().getGame().getStatus() == Status.STARTING || Main.getInstance().getGame().getStatus() == Status.PLAYING) { + Main.getInstance().getGame().broadcastMessage(abortPrefix + message("STOP")); + Main.getInstance().getGame().end(); + } else { + sender.sendMessage(errorPrefix + message("GAME_NOT_INPROGRESS")); + } + } + + public String getLabel() { + return "stop"; + } + + public String getUsage() { + return ""; + } + + public String getDescription() { + return "Stops the game"; + } + + public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + return null; + } + +} diff --git a/src/main/java/cat/freya/khs/command/Top.java b/src/main/java/cat/freya/khs/command/Top.java new file mode 100644 index 0000000..cf2aeaa --- /dev/null +++ b/src/main/java/cat/freya/khs/command/Top.java @@ -0,0 +1,72 @@ +package cat.freya.khs.command; + +import cat.freya.khs.Main; +import cat.freya.khs.command.util.ICommand; +import cat.freya.khs.database.util.PlayerInfo; +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.Collections; +import java.util.List; + +import static cat.freya.khs.configuration.Config.errorPrefix; +import static cat.freya.khs.configuration.Localization.message; + +public class Top implements ICommand { + + public void execute(Player sender, String[] args) { + int page; + if (args.length == 0) page = 1; + else try{ + page = Integer.parseInt(args[0]); + } catch(Exception e) { + sender.sendMessage(errorPrefix + message("WORLDBORDER_INVALID_INPUT").addAmount(args[0])); + return; + } + if (page < 1) { + sender.sendMessage(errorPrefix + message("WORLDBORDER_INVALID_INPUT").addAmount(page)); + return; + } + StringBuilder message = new StringBuilder(String.format( + "%s------- %sLEADERBOARD %s(Page %s) %s-------\n", + ChatColor.WHITE, ChatColor.BOLD, ChatColor.GRAY, page, ChatColor.WHITE)); + List<PlayerInfo> infos = Main.getInstance().getDatabase().getGameData().getInfoPage(page); + int i = 1 + (page-1)*10; + if (infos == null) { + sender.sendMessage(errorPrefix + message("NO_GAME_INFO")); + return; + } + for(PlayerInfo info : infos) { + String name = Main.getInstance().getDatabase().getNameData().getName(info.getUniqueId()); + ChatColor color; + switch (i) { + case 1: color = ChatColor.YELLOW; break; + case 2: color = ChatColor.GRAY; break; + case 3: color = ChatColor.GOLD; break; + default: color = ChatColor.WHITE; break; + } + message.append(String.format("%s%s. %s%s %s%s\n", + color, i, ChatColor.RED, info.getSeekerWins() +info.getHiderWins(), ChatColor.WHITE, name)); + i++; + } + sender.sendMessage(message.toString()); + } + + public String getLabel() { + return "top"; + } + + public String getUsage() { + return "<*page>"; + } + + public String getDescription() { + return "Gets the top players in the server."; + } + + public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + return Collections.singletonList(parameter); + } + +} diff --git a/src/main/java/cat/freya/khs/command/Wins.java b/src/main/java/cat/freya/khs/command/Wins.java new file mode 100644 index 0000000..09e92d1 --- /dev/null +++ b/src/main/java/cat/freya/khs/command/Wins.java @@ -0,0 +1,67 @@ +package cat.freya.khs.command; + +import cat.freya.khs.Main; +import cat.freya.khs.command.util.ICommand; +import cat.freya.khs.configuration.Config; +import cat.freya.khs.configuration.Localization; +import cat.freya.khs.database.util.PlayerInfo; +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.Collections; +import java.util.List; +import java.util.UUID; + +public class Wins implements ICommand { + + public void execute(Player sender, String[] args) { + Main.getInstance().getServer().getScheduler().runTaskAsynchronously(Main.getInstance(), () -> { + + UUID uuid; + String name; + if (args.length == 0) { + uuid = sender.getUniqueId(); + name = sender.getName(); + } + else { + name = args[0]; + uuid = Main.getInstance().getDatabase().getNameData().getUUID(args[0]); + } + if(uuid == null){ + sender.sendMessage(Config.errorPrefix + Localization.message("START_INVALID_NAME").addPlayer(args[0])); + return; + } + PlayerInfo info = Main.getInstance().getDatabase().getGameData().getInfo(uuid); + if (info == null) { + sender.sendMessage(Config.errorPrefix + Localization.message("NO_GAME_INFO")); + return; + } + String message = ChatColor.WHITE + "" + ChatColor.BOLD + "==============================\n"; + message = message + Localization.message("INFORMATION_FOR").addPlayer(name) + "\n"; + message = message + "==============================\n"; + message = message + String.format("%sTOTAL WINS: %s%s\n%sHIDER WINS: %s%s\n%sSEEKER WINS: %s%s\n%sGAMES PLAYED: %s", + ChatColor.YELLOW, ChatColor.WHITE, info.getSeekerWins() +info.getHiderWins(), ChatColor.GOLD, ChatColor.WHITE, info.getHiderWins(), + ChatColor.RED, ChatColor.WHITE, info.getSeekerWins(), ChatColor.WHITE, info.getSeekerGames() +info.getHiderGames()); + message = message + ChatColor.WHITE + "" + ChatColor.BOLD + "\n=============================="; + sender.sendMessage(message); + + }); + } + + public String getLabel() { + return "wins"; + } + + public String getUsage() { + return "<*player>"; + } + + public String getDescription() { + return "Get the win information for yourself or another player."; + } + + public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + return Collections.singletonList(parameter); + } +} diff --git a/src/main/java/cat/freya/khs/command/location/LocationUtils.java b/src/main/java/cat/freya/khs/command/location/LocationUtils.java new file mode 100644 index 0000000..c914944 --- /dev/null +++ b/src/main/java/cat/freya/khs/command/location/LocationUtils.java @@ -0,0 +1,52 @@ +package cat.freya.khs.command.location; + +import cat.freya.khs.Main; +import cat.freya.khs.configuration.Config; +import cat.freya.khs.configuration.Localization; +import cat.freya.khs.configuration.Map; +import cat.freya.khs.configuration.Maps; +import cat.freya.khs.game.util.Status; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.function.Consumer; + +/** + * @author bobby29831 + */ +public class LocationUtils { + + public static void setLocation(@NotNull Player player, @NotNull Locations place, String mapName, @NotNull Consumer<Map> consumer) { + + if (Main.getInstance().getGame().getStatus() != Status.STANDBY) { + player.sendMessage(Config.errorPrefix + Localization.message("GAME_INPROGRESS")); + return; + } + + if (player.getLocation().getBlockX() == 0 || player.getLocation().getBlockZ() == 0 || player.getLocation().getBlockY() == 0){ + player.sendMessage(Config.errorPrefix + Localization.message("NOT_AT_ZERO")); + return; + } + + Map map = null; + if(mapName != null) { + map = Maps.getMap(mapName); + if (map == null) { + player.sendMessage(Config.errorPrefix + Localization.message("INVALID_MAP")); + return; + } + } + + try { + consumer.accept(map); + } catch (Exception e) { + player.sendMessage(Config.errorPrefix + e.getMessage()); + return; + } + + if(map != null) + Maps.setMap(mapName, map); + player.sendMessage(Config.messagePrefix + Localization.message(place.message())); + } + +}
\ No newline at end of file diff --git a/src/main/java/cat/freya/khs/command/location/Locations.java b/src/main/java/cat/freya/khs/command/location/Locations.java new file mode 100644 index 0000000..f40419f --- /dev/null +++ b/src/main/java/cat/freya/khs/command/location/Locations.java @@ -0,0 +1,17 @@ +package cat.freya.khs.command.location; + +/** + * @author bobby29831 + */ +public enum Locations { + + GAME, + LOBBY, + EXIT, + SEEKER; + + public String message() { + return this + "_SPAWN"; + } + +}
\ No newline at end of file diff --git a/src/main/java/cat/freya/khs/command/map/Add.java b/src/main/java/cat/freya/khs/command/map/Add.java new file mode 100644 index 0000000..ddbc384 --- /dev/null +++ b/src/main/java/cat/freya/khs/command/map/Add.java @@ -0,0 +1,53 @@ +package cat.freya.khs.command.map; + +import cat.freya.khs.Main; +import cat.freya.khs.command.util.ICommand; +import cat.freya.khs.configuration.Config; +import cat.freya.khs.configuration.Localization; +import cat.freya.khs.configuration.Map; +import cat.freya.khs.configuration.Maps; +import cat.freya.khs.game.util.Status; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.Collections; +import java.util.List; + +public class Add implements ICommand { + + public void execute(Player sender, String[] args) { + if (Main.getInstance().getGame().getStatus() != Status.STANDBY) { + sender.sendMessage(Config.errorPrefix + Localization.message("GAME_INPROGRESS")); + return; + } + Map map = Maps.getMap(args[0]); + if(map != null) { + sender.sendMessage(Config.errorPrefix + Localization.message("MAP_ALREADY_EXISTS")); + } else if(!args[0].matches("[a-zA-Z0-9]*") || args[0].length() < 1) { + sender.sendMessage(Config.errorPrefix + Localization.message("INVALID_MAP_NAME")); + } else { + Maps.setMap(args[0], new Map(args[0])); + sender.sendMessage(Config.messagePrefix + Localization.message("MAP_CREATED").addAmount(args[0])); + } + } + + public String getLabel() { + return "add"; + } + + public String getUsage() { + return "<name>"; + } + + public String getDescription() { + return "Add a map to the plugin!"; + } + + public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + if(parameter.equals("name")) { + return Collections.singletonList("name"); + } + return null; + } + +} diff --git a/src/main/java/cat/freya/khs/command/map/Debug.java b/src/main/java/cat/freya/khs/command/map/Debug.java new file mode 100644 index 0000000..b6a6515 --- /dev/null +++ b/src/main/java/cat/freya/khs/command/map/Debug.java @@ -0,0 +1,122 @@ +package cat.freya.khs.command.map; + +import com.cryptomorin.xseries.XMaterial; +import cat.freya.khs.Main; +import cat.freya.khs.command.util.ICommand; +import cat.freya.khs.configuration.Config; +import cat.freya.khs.configuration.Localization; +import cat.freya.khs.configuration.Maps; +import cat.freya.khs.game.PlayerLoader; +import cat.freya.khs.game.util.Status; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.jetbrains.annotations.NotNull; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Consumer; +import java.util.stream.Collectors; + +public class Debug implements ICommand { + + private static final Map<Player, Map<Integer, Consumer<Player>>> debugMenuFunctions = new HashMap<>(); + + public void execute(Player sender, String[] args) { + cat.freya.khs.configuration.Map map = Maps.getMap(args[0]); + if(map == null) { + sender.sendMessage(Config.errorPrefix + Localization.message("INVALID_MAP")); + return; + } + Inventory debugMenu = createMenu(map, sender); + sender.openInventory(debugMenu); + } + + private Inventory createMenu(cat.freya.khs.configuration.Map map, Player sender){ + Map<Integer, Consumer<Player>> functions = new HashMap<>(); + Inventory debugMenu = Main.getInstance().getServer().createInventory(null, 9, "Debug Menu"); + debugMenu.setItem(0, createOption(functions, 0, XMaterial.LEATHER_CHESTPLATE.parseMaterial(), "&6Become a &lHider", 1, player -> { + if(Config.mapSaveEnabled) { + if(map.getGameSpawn().getWorld() == null) map.getWorldLoader().loadMap(); + } + Main.getInstance().getBoard().addHider(player); + PlayerLoader.loadHider(player, map); + if(Main.getInstance().getGame().getStatus() != cat.freya.khs.game.util.Status.STARTING) + PlayerLoader.resetPlayer(player, Main.getInstance().getBoard()); + })); + debugMenu.setItem(1, createOption(functions, 1, XMaterial.GOLDEN_CHESTPLATE.parseMaterial(), "&cBecome a &lSeeker", 1, player -> { + if(Config.mapSaveEnabled) { + if(map.getGameSpawn().getWorld() == null) map.getWorldLoader().loadMap(); + } + Main.getInstance().getBoard().addSeeker(player); + PlayerLoader.loadSeeker(player, map); + if(Main.getInstance().getGame().getStatus() != cat.freya.khs.game.util.Status.STARTING) + PlayerLoader.resetPlayer(player, Main.getInstance().getBoard()); + })); + debugMenu.setItem(2, createOption(functions, 2, XMaterial.IRON_CHESTPLATE.parseMaterial(), "&8Become a &lSpectator", 1, player -> { + if(Config.mapSaveEnabled) { + if(map.getGameSpawn().getWorld() == null) map.getWorldLoader().loadMap(); + } + Main.getInstance().getBoard().addSpectator(player); + PlayerLoader.loadSpectator(player, map); + })); + debugMenu.setItem(3, createOption(functions, 3, XMaterial.BARRIER.parseMaterial(), "&cUnload from Game", 1, player -> { + Main.getInstance().getBoard().remove(player); + PlayerLoader.unloadPlayer(player); + Config.exitPosition.teleport(player); + })); + debugMenu.setItem(4, createOption(functions, 4, XMaterial.BARRIER.parseMaterial(), "&cDie In Game", 2, player -> { + if((Main.getInstance().getBoard().isSeeker(player) || Main.getInstance().getBoard().isHider(player)) && Main.getInstance().getGame().getStatus() == Status.PLAYING){ + player.setHealth(0.1); + } + })); + if(map.isBlockHuntEnabled()) { + debugMenu.setItem(7, createOption(functions, 7, XMaterial.GLASS.parseMaterial(), "&dEnable Disguise", 1, player -> { + PlayerLoader.openBlockHuntPicker(player, map); + })); + debugMenu.setItem(8, createOption(functions, 8, XMaterial.PLAYER_HEAD.parseMaterial(), "&dDisable Disguise", 1, player -> Main.getInstance().getDisguiser().reveal(player))); + } + debugMenuFunctions.put(sender, functions); + return debugMenu; + } + + private ItemStack createOption(Map<Integer, Consumer<Player>> functions, int slow, Material material, String name, int amount, Consumer<Player> callback){ + ItemStack temp = new ItemStack(material, amount); + ItemMeta meta = temp.getItemMeta(); + meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name)); + temp.setItemMeta(meta); + functions.put(slow, callback); + return temp; + } + + public static void handleOption(Player player, int slotId){ + Main.getInstance().getServer().getScheduler().scheduleSyncDelayedTask(Main.getInstance(), () -> { + Consumer<Player> callback = debugMenuFunctions.get(player).get(slotId); + if(callback != null) callback.accept(player); + }, 0); + } + + public String getLabel() { + return "debug"; + } + + public String getUsage() { + return "<map>"; + } + + public String getDescription() { + return "Run debug commands"; + } + + public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + if(parameter.equals("map")) { + return Maps.getAllMaps().stream().map(cat.freya.khs.configuration.Map::getName).collect(Collectors.toList()); + } + return null; + } + +} diff --git a/src/main/java/cat/freya/khs/command/map/GoTo.java b/src/main/java/cat/freya/khs/command/map/GoTo.java new file mode 100644 index 0000000..d27153e --- /dev/null +++ b/src/main/java/cat/freya/khs/command/map/GoTo.java @@ -0,0 +1,62 @@ +package cat.freya.khs.command.map; + +import cat.freya.khs.command.util.ICommand; +import cat.freya.khs.configuration.Config; +import cat.freya.khs.configuration.Localization; +import cat.freya.khs.configuration.Map; +import cat.freya.khs.configuration.Maps; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class GoTo implements ICommand { + + public void execute(Player sender, String[] args) { + Map map = Maps.getMap(args[0]); + if(map == null) { + sender.sendMessage(Config.errorPrefix + Localization.message("INVALID_MAP")); + return; + } + if (map.isNotSetup()) { + sender.sendMessage(Config.errorPrefix + Localization.message("MAP_NOT_SETUP").addAmount(map.getName())); + return; + } + switch (args[1].toLowerCase()) { + case "spawn": + map.getSpawn().teleport(sender); break; + case "lobby": + map.getLobby().teleport(sender); break; + case "seekerlobby": + map.getSeekerLobby().teleport(sender); break; + case "exit": + Config.exitPosition.teleport(sender); break; + default: + sender.sendMessage(Config.errorPrefix + Localization.message("COMMAND_INVALID_ARG").addAmount(args[1].toLowerCase())); + } + } + + public String getLabel() { + return "goto"; + } + + public String getUsage() { + return "<map> <spawn>"; + } + + public String getDescription() { + return "Teleport to a map spawn zone"; + } + + public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + if(parameter.equals("map")) { + return Maps.getAllMaps().stream().map(Map::getName).collect(Collectors.toList()); + } else if(parameter.equals("spawn")) { + return Arrays.asList("spawn","lobby","seekerlobby","exit"); + } + return null; + } + +} diff --git a/src/main/java/cat/freya/khs/command/map/List.java b/src/main/java/cat/freya/khs/command/map/List.java new file mode 100644 index 0000000..1f9833a --- /dev/null +++ b/src/main/java/cat/freya/khs/command/map/List.java @@ -0,0 +1,45 @@ +package cat.freya.khs.command.map; + +import cat.freya.khs.command.util.ICommand; +import cat.freya.khs.configuration.Config; +import cat.freya.khs.configuration.Localization; +import cat.freya.khs.configuration.Map; +import cat.freya.khs.configuration.Maps; +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.Collection; + +public class List implements ICommand { + + public void execute(Player sender, String[] args) { + Collection<Map> maps = Maps.getAllMaps(); + if(maps.size() < 1) { + sender.sendMessage(Config.errorPrefix + Localization.message("NO_MAPS")); + return; + } + StringBuilder response = new StringBuilder(Config.messagePrefix + Localization.message("LIST_MAPS")); + for(Map map : maps) { + response.append("\n ").append(map.getName()).append(": ").append(map.isNotSetup() ? ChatColor.RED + "NOT SETUP" : ChatColor.GREEN + "SETUP").append(ChatColor.WHITE); + } + sender.sendMessage(response.toString()); + } + + public String getLabel() { + return "list"; + } + + public String getUsage() { + return ""; + } + + public String getDescription() { + return "List all maps in the plugin"; + } + + public java.util.List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + return null; + } + +}
\ No newline at end of file diff --git a/src/main/java/cat/freya/khs/command/map/Remove.java b/src/main/java/cat/freya/khs/command/map/Remove.java new file mode 100644 index 0000000..7a0cd3f --- /dev/null +++ b/src/main/java/cat/freya/khs/command/map/Remove.java @@ -0,0 +1,52 @@ +package cat.freya.khs.command.map; + +import cat.freya.khs.Main; +import cat.freya.khs.command.util.ICommand; +import cat.freya.khs.configuration.Config; +import cat.freya.khs.configuration.Localization; +import cat.freya.khs.configuration.Map; +import cat.freya.khs.configuration.Maps; +import cat.freya.khs.game.util.Status; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.stream.Collectors; + +public class Remove implements ICommand { + + public void execute(Player sender, String[] args) { + if (Main.getInstance().getGame().getStatus() != Status.STANDBY) { + sender.sendMessage(Config.errorPrefix + Localization.message("GAME_INPROGRESS")); + return; + } + Map map = Maps.getMap(args[0]); + if(map == null) { + sender.sendMessage(Config.errorPrefix + Localization.message("INVALID_MAP")); + } else if(!Maps.removeMap(args[0])){ + sender.sendMessage(Config.errorPrefix + Localization.message("MAP_FAIL_DELETE").addAmount(args[0])); + } else { + sender.sendMessage(Config.messagePrefix + Localization.message("MAP_DELETED").addAmount(args[0])); + } + } + + public String getLabel() { + return "remove"; + } + + public String getUsage() { + return "<map>"; + } + + public String getDescription() { + return "Remove a map from the plugin!"; + } + + public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + if(parameter.equals("map")) { + return Maps.getAllMaps().stream().map(Map::getName).collect(Collectors.toList()); + } + return null; + } + +}
\ No newline at end of file diff --git a/src/main/java/cat/freya/khs/command/map/Save.java b/src/main/java/cat/freya/khs/command/map/Save.java new file mode 100644 index 0000000..7d3a3c0 --- /dev/null +++ b/src/main/java/cat/freya/khs/command/map/Save.java @@ -0,0 +1,83 @@ +package cat.freya.khs.command.map; + +import cat.freya.khs.Main; +import cat.freya.khs.command.util.ICommand; +import cat.freya.khs.configuration.Config; +import cat.freya.khs.configuration.Localization; +import cat.freya.khs.configuration.Map; +import cat.freya.khs.configuration.Maps; +import cat.freya.khs.game.util.Status; +import org.bukkit.World; +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitRunnable; +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.stream.Collectors; + +public class Save implements ICommand { + + public static boolean runningBackup = false; + + public void execute(Player sender, String[] args) { + if (!Config.mapSaveEnabled) { + sender.sendMessage(Config.errorPrefix + Localization.message("MAPSAVE_DISABLED")); + return; + } + if (Main.getInstance().getGame().getStatus() != Status.STANDBY) { + sender.sendMessage(Config.errorPrefix + Localization.message("GAME_INPROGRESS")); + return; + } + Map map = Maps.getMap(args[0]); + if(map == null) { + sender.sendMessage(Config.errorPrefix + Localization.message("INVALID_MAP")); + return; + } + if (map.getSpawn().isNotSetup()) { + sender.sendMessage(Config.errorPrefix + Localization.message("ERROR_GAME_SPAWN")); + return; + } + if (map.isBoundsNotSetup()) { + sender.sendMessage(Config.errorPrefix + Localization.message("ERROR_MAP_BOUNDS")); + return; + } + sender.sendMessage(Config.messagePrefix + Localization.message("MAPSAVE_START")); + sender.sendMessage(Config.warningPrefix + Localization.message("MAPSAVE_WARNING")); + World world = map.getSpawn().load(); + if (world == null) { + sender.sendMessage(Config.warningPrefix + Localization.message("MAPSAVE_FAIL_WORLD")); + return; + } + world.save(); + BukkitRunnable runnable = new BukkitRunnable() { + public void run() { + sender.sendMessage( + map.getWorldLoader().save() + ); + runningBackup = false; + } + }; + runnable.runTaskAsynchronously(Main.getInstance()); + runningBackup = true; + } + + public String getLabel() { + return "save"; + } + + public String getUsage() { + return "<map>"; + } + + public String getDescription() { + return "Saves the map to its own separate playable map"; + } + + public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + if(parameter.equals("map")) { + return Maps.getAllMaps().stream().map(Map::getName).collect(Collectors.toList()); + } + return null; + } + +} diff --git a/src/main/java/cat/freya/khs/command/map/Status.java b/src/main/java/cat/freya/khs/command/map/Status.java new file mode 100644 index 0000000..e62e10d --- /dev/null +++ b/src/main/java/cat/freya/khs/command/map/Status.java @@ -0,0 +1,79 @@ +package cat.freya.khs.command.map; + +import cat.freya.khs.command.util.ICommand; +import cat.freya.khs.configuration.Config; +import cat.freya.khs.configuration.Localization; +import cat.freya.khs.configuration.Map; +import cat.freya.khs.configuration.Maps; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.stream.Collectors; + +public class Status implements ICommand { + + public void execute(Player sender, String[] args) { + + String msg = Localization.message("SETUP").toString(); + int count = 0; + Map map = Maps.getMap(args[0]); + if(map == null) { + sender.sendMessage(Config.errorPrefix + Localization.message("INVALID_MAP")); + return; + } + if (map.getSpawn().getBlockX() == 0 && map.getSpawn().getBlockY() == 0 && map.getSpawn().getBlockZ() == 0 || !map.getSpawn().exists()) { + msg = msg + "\n" + Localization.message("SETUP_GAME"); + count++; + } + if (map.getLobby().getBlockX() == 0 && map.getLobby().getBlockY() == 0 && map.getLobby().getBlockZ() == 0 || !map.getLobby().exists()) { + msg = msg + "\n" + Localization.message("SETUP_LOBBY"); + count++; + } + if (map.getSeekerLobby().getBlockX() == 0 && map.getSeekerLobby().getBlockY() == 0 && map.getSeekerLobby().getBlockZ() == 0 || !map.getSeekerLobby().exists()) { + msg = msg + "\n" + Localization.message("SETUP_SEEKER_LOBBY"); + count++; + } + if (Config.exitPosition.getBlockX() == 0 && Config.exitPosition.getBlockY() == 0 && Config.exitPosition.getBlockZ() == 0 || !Config.exitPosition.exists()) { + msg = msg + "\n" + Localization.message("SETUP_EXIT"); + count++; + } + if (map.isBoundsNotSetup()) { + msg = msg + "\n" + Localization.message("SETUP_BOUNDS"); + count++; + } + if (Config.mapSaveEnabled && !map.getGameSpawn().exists()) { + msg = msg + "\n" + Localization.message("SETUP_SAVEMAP"); + count++; + } + if (map.isBlockHuntEnabled() && map.getBlockHunt().isEmpty()) { + msg = msg + "\n" + Localization.message("SETUP_BLOCKHUNT"); + count++; + } + if (count < 1) { + sender.sendMessage(Config.messagePrefix + Localization.message("SETUP_COMPLETE")); + } else { + sender.sendMessage(msg); + } + } + + public String getLabel() { + return "status"; + } + + public String getUsage() { + return "<map>"; + } + + public String getDescription() { + return "Shows what needs to be setup on a map"; + } + + public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + if(parameter.equals("map")) { + return Maps.getAllMaps().stream().map(Map::getName).collect(Collectors.toList()); + } + return null; + } + +} diff --git a/src/main/java/cat/freya/khs/command/map/blockhunt/Enabled.java b/src/main/java/cat/freya/khs/command/map/blockhunt/Enabled.java new file mode 100644 index 0000000..b97e74b --- /dev/null +++ b/src/main/java/cat/freya/khs/command/map/blockhunt/Enabled.java @@ -0,0 +1,63 @@ +package cat.freya.khs.command.map.blockhunt; + +import cat.freya.khs.Main; +import cat.freya.khs.command.util.ICommand; +import cat.freya.khs.configuration.Config; +import cat.freya.khs.configuration.Localization; +import cat.freya.khs.configuration.Map; +import cat.freya.khs.configuration.Maps; +import cat.freya.khs.game.util.Status; +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class Enabled implements ICommand { + + public void execute(Player sender, String[] args) { + if (!Main.getInstance().supports(9)) { + sender.sendMessage(Config.errorPrefix + Localization.message("BLOCKHUNT_UNSUPPORTED")); + return; + } + if (Main.getInstance().getGame().getStatus() != Status.STANDBY) { + sender.sendMessage(Config.errorPrefix + Localization.message("GAME_INPROGRESS")); + return; + } + Map map = Maps.getMap(args[0]); + if(map == null) { + sender.sendMessage(Config.errorPrefix + Localization.message("INVALID_MAP")); + return; + } + boolean bool = Boolean.parseBoolean(args[1]); + map.setBlockhunt(bool, map.getBlockHunt()); + Maps.setMap(map.getName(), map); + sender.sendMessage(Config.messagePrefix + Localization.message("BLOCKHUNT_SET_TO") + .addAmount(bool ? ChatColor.GREEN + "true" : ChatColor.RED + "false") + ChatColor.WHITE); + } + + public String getLabel() { + return "enabled"; + } + + public String getUsage() { + return "<map> <bool>"; + } + + public String getDescription() { + return "Sets blockhunt enabled or disabled in a current map"; + } + + public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + if(parameter.equals("map")) { + return Maps.getAllMaps().stream().map(Map::getName).collect(Collectors.toList()); + } + if(parameter.equals("bool")) { + return Arrays.asList("true", "false"); + } + return null; + } + +} diff --git a/src/main/java/cat/freya/khs/command/map/blockhunt/blocks/Add.java b/src/main/java/cat/freya/khs/command/map/blockhunt/blocks/Add.java new file mode 100644 index 0000000..052602c --- /dev/null +++ b/src/main/java/cat/freya/khs/command/map/blockhunt/blocks/Add.java @@ -0,0 +1,75 @@ +package cat.freya.khs.command.map.blockhunt.blocks; + +import cat.freya.khs.Main; +import cat.freya.khs.command.util.ICommand; +import cat.freya.khs.configuration.Config; +import cat.freya.khs.configuration.Localization; +import cat.freya.khs.configuration.Map; +import cat.freya.khs.configuration.Maps; +import cat.freya.khs.game.util.Status; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class Add implements ICommand { + + public void execute(Player sender, String[] args) { + if (!Main.getInstance().supports(9)) { + sender.sendMessage(Config.errorPrefix + Localization.message("BLOCKHUNT_UNSUPPORTED")); + return; + } + if (Main.getInstance().getGame().getStatus() != Status.STANDBY) { + sender.sendMessage(Config.errorPrefix + Localization.message("GAME_INPROGRESS")); + return; + } + Map map = Maps.getMap(args[0]); + if(map == null) { + sender.sendMessage(Config.errorPrefix + Localization.message("INVALID_MAP")); + return; + } + Material block; + try { block = Material.valueOf(args[1]); } + catch (IllegalArgumentException e) { + sender.sendMessage(Config.errorPrefix + Localization.message("COMMAND_INVALID_ARG").addAmount(args[1])); + return; + } + List<Material> blocks = map.getBlockHunt(); + if(blocks.contains(block)) { + sender.sendMessage(Config.errorPrefix + Localization.message("BLOCKHUNT_BLOCK_EXISTS").addAmount(args[1])); + } + blocks.add(block); + map.setBlockhunt(map.isBlockHuntEnabled(), blocks); + Maps.setMap(map.getName(), map); + sender.sendMessage(Config.messagePrefix + Localization.message("BLOCKHUNT_BLOCK_ADDED").addAmount(args[1])); + } + + public String getLabel() { + return "add"; + } + + public String getUsage() { + return "<map> <block>"; + } + + public String getDescription() { + return "Add a blockhunt block to a map!"; + } + + public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + if(parameter.equals("map")) { + return Maps.getAllMaps().stream().map(Map::getName).collect(Collectors.toList()); + } else if(parameter.equals("block")) { + return Arrays.stream(Material.values()) + .filter(Material::isBlock) + .map(Material::toString) + .filter(s -> s.toUpperCase().startsWith(typed.toUpperCase())) + .collect(Collectors.toList()); + } + return null; + } + +}
\ No newline at end of file diff --git a/src/main/java/cat/freya/khs/command/map/blockhunt/blocks/List.java b/src/main/java/cat/freya/khs/command/map/blockhunt/blocks/List.java new file mode 100644 index 0000000..96d381c --- /dev/null +++ b/src/main/java/cat/freya/khs/command/map/blockhunt/blocks/List.java @@ -0,0 +1,58 @@ +package cat.freya.khs.command.map.blockhunt.blocks; + +import cat.freya.khs.Main; +import cat.freya.khs.command.util.ICommand; +import cat.freya.khs.configuration.Config; +import cat.freya.khs.configuration.Localization; +import cat.freya.khs.configuration.Map; +import cat.freya.khs.configuration.Maps; + +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.stream.Collectors; + +public class List implements ICommand { + + public void execute(Player sender, String[] args) { + if (!Main.getInstance().supports(9)) { + sender.sendMessage(Config.errorPrefix + Localization.message("BLOCKHUNT_UNSUPPORTED")); + return; + } + Map map = Maps.getMap(args[0]); + if(map == null) { + sender.sendMessage(Config.errorPrefix + Localization.message("INVALID_MAP")); + return; + } + java.util.List<Material> blocks = map.getBlockHunt(); + if(blocks.isEmpty()) { + sender.sendMessage(Config.errorPrefix + Localization.message("NO_BLOCKS")); + return; + } + StringBuilder response = new StringBuilder(Config.messagePrefix + Localization.message("BLOCKHUNT_LIST_BLOCKS")); + for(int i = 0; i < blocks.size(); i++) { + response.append(String.format("\n%s. %s", i, blocks.get(i).toString())); + } + sender.sendMessage(response.toString()); + } + + public String getLabel() { + return "list"; + } + + public String getUsage() { + return "<map>"; + } + + public String getDescription() { + return "List all blockhunt blocks in a map"; + } + + public java.util.List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + if(parameter.equals("map")) { + return Maps.getAllMaps().stream().map(Map::getName).collect(Collectors.toList()); + } + return null; + } +} diff --git a/src/main/java/cat/freya/khs/command/map/blockhunt/blocks/Remove.java b/src/main/java/cat/freya/khs/command/map/blockhunt/blocks/Remove.java new file mode 100644 index 0000000..020d8ca --- /dev/null +++ b/src/main/java/cat/freya/khs/command/map/blockhunt/blocks/Remove.java @@ -0,0 +1,75 @@ +package cat.freya.khs.command.map.blockhunt.blocks; + +import cat.freya.khs.Main; +import cat.freya.khs.command.util.ICommand; +import cat.freya.khs.configuration.Config; +import cat.freya.khs.configuration.Localization; +import cat.freya.khs.configuration.Map; +import cat.freya.khs.configuration.Maps; +import cat.freya.khs.game.util.Status; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class Remove implements ICommand { + + public void execute(Player sender, String[] args) { + if (!Main.getInstance().supports(9)) { + sender.sendMessage(Config.errorPrefix + Localization.message("BLOCKHUNT_UNSUPPORTED")); + return; + } + if (Main.getInstance().getGame().getStatus() != Status.STANDBY) { + sender.sendMessage(Config.errorPrefix + Localization.message("GAME_INPROGRESS")); + return; + } + Map map = Maps.getMap(args[0]); + if(map == null) { + sender.sendMessage(Config.errorPrefix + Localization.message("INVALID_MAP")); + return; + } + Material block; + try { block = Material.valueOf(args[1]); } + catch (IllegalArgumentException e) { + sender.sendMessage(Config.errorPrefix + Localization.message("COMMAND_INVALID_ARG").addAmount(args[1])); + return; + } + java.util.List<Material> blocks = map.getBlockHunt(); + if(!blocks.contains(block)) { + sender.sendMessage(Config.errorPrefix + Localization.message("BLOCKHUNT_BLOCK_DOESNT_EXIT").addAmount(args[1])); + } + blocks.remove(block); + map.setBlockhunt(map.isBlockHuntEnabled(), blocks); + Maps.setMap(map.getName(), map); + sender.sendMessage(Config.messagePrefix + Localization.message("BLOCKHUNT_BLOCK_REMOVED").addAmount(args[1])); + } + + public String getLabel() { + return "remove"; + } + + public String getUsage() { + return "<map> <block>"; + } + + public String getDescription() { + return "Remove a blockhunt block from a map!"; + } + + public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + if(parameter.equals("map")) { + return Maps.getAllMaps().stream().map(Map::getName).collect(Collectors.toList()); + } else if(parameter.equals("block")) { + return Arrays.stream(Material.values()) + .filter(Material::isBlock) + .map(Material::toString) + .filter(s -> s.toUpperCase().startsWith(typed.toUpperCase())) + .collect(Collectors.toList()); + } + return null; + } + +} diff --git a/src/main/java/cat/freya/khs/command/map/set/Border.java b/src/main/java/cat/freya/khs/command/map/set/Border.java new file mode 100644 index 0000000..c40b742 --- /dev/null +++ b/src/main/java/cat/freya/khs/command/map/set/Border.java @@ -0,0 +1,86 @@ +package cat.freya.khs.command.map.set; + +import cat.freya.khs.Main; +import cat.freya.khs.command.util.ICommand; +import cat.freya.khs.configuration.Config; +import cat.freya.khs.configuration.Localization; +import cat.freya.khs.configuration.Map; +import cat.freya.khs.configuration.Maps; +import cat.freya.khs.game.util.Status; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +public class Border implements ICommand { + + public void execute(Player sender, String[] args) { + if (Main.getInstance().getGame().getStatus() != Status.STANDBY) { + sender.sendMessage(Config.errorPrefix + Localization.message("GAME_INPROGRESS")); + return; + } + Map map = Maps.getMap(args[0]); + if(map == null) { + sender.sendMessage(Config.errorPrefix + Localization.message("INVALID_MAP")); + return; + } + if (map.getSpawn().isNotSetup()) { + sender.sendMessage(Config.errorPrefix + Localization.message("ERROR_GAME_SPAWN")); + return; + } + + int num,delay,change; + try { num = Integer.parseInt(args[1]); } catch (Exception e) { + sender.sendMessage(Config.errorPrefix + Localization.message("WORLDBORDER_INVALID_INPUT").addAmount(args[0])); + return; + } + try { delay = Integer.parseInt(args[2]); } catch (Exception e) { + sender.sendMessage(Config.errorPrefix + Localization.message("WORLDBORDER_INVALID_INPUT").addAmount(args[1])); + return; + } + try { change = Integer.parseInt(args[3]); } catch (Exception e) { + sender.sendMessage(Config.errorPrefix + Localization.message("WORLDBORDER_INVALID_INPUT").addAmount(args[2])); + return; + } + if (num < 100) { + sender.sendMessage(Config.errorPrefix + Localization.message("WORLDBORDER_MIN_SIZE")); + return; + } + if (change < 1) { + sender.sendMessage(Config.errorPrefix + Localization.message("WORLDBORDER_CHANGE_SIZE")); + return; + } + map.setWorldBorderData( + sender.getLocation().getBlockX(), + sender.getLocation().getBlockZ(), + num, + delay, + change + ); + Maps.setMap(map.getName(), map); + sender.sendMessage(Config.messagePrefix + Localization.message("WORLDBORDER_ENABLE").addAmount(num).addAmount(delay).addAmount(change)); + map.getWorldBorder().resetWorldBorder(); + } + + public String getLabel() { + return "border"; + } + + public String getUsage() { + return "<map> <size> <delay> <move>"; + } + + public String getDescription() { + return "Sets a maps world border information"; + } + + public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + if(parameter.equals("map")) { + return Maps.getAllMaps().stream().map(Map::getName).collect(Collectors.toList()); + } + return Collections.singletonList(parameter); + } + +} diff --git a/src/main/java/cat/freya/khs/command/map/set/Bounds.java b/src/main/java/cat/freya/khs/command/map/set/Bounds.java new file mode 100644 index 0000000..82adcf1 --- /dev/null +++ b/src/main/java/cat/freya/khs/command/map/set/Bounds.java @@ -0,0 +1,112 @@ +package cat.freya.khs.command.map.set; + +import cat.freya.khs.Main; +import cat.freya.khs.command.util.ICommand; +import cat.freya.khs.configuration.Config; +import cat.freya.khs.configuration.Localization; +import cat.freya.khs.configuration.Map; +import cat.freya.khs.configuration.Maps; +import cat.freya.khs.game.util.Status; +import cat.freya.khs.util.Location; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.stream.Collectors; + +public class Bounds implements ICommand { + + public void execute(Player sender, String[] args) { + if (Main.getInstance().getGame().getStatus() != Status.STANDBY) { + sender.sendMessage(Config.errorPrefix + Localization.message("GAME_INPROGRESS")); + return; + } + Map map = Maps.getMap(args[0]); + if(map == null) { + sender.sendMessage(Config.errorPrefix + Localization.message("INVALID_MAP")); + return; + } + if (map.getSpawn().isNotSetup()) { + sender.sendMessage(Config.errorPrefix + Localization.message("ERROR_GAME_SPAWN")); + return; + } + if (map.getSeekerLobby().isNotSetup()) { + sender.sendMessage(Config.errorPrefix + Localization.message("ERROR_GAME_SEEKER_SPAWN")); + return; + } + if (!sender.getWorld().getName().equals(map.getSpawnName())) { + sender.sendMessage(Config.errorPrefix + Localization.message("BOUNDS_WRONG_WORLD")); + return; + } + if (sender.getLocation().getBlockX() == 0 || sender.getLocation().getBlockZ() == 0) { + sender.sendMessage(Config.errorPrefix + Localization.message("NOT_AT_ZERO")); + return; + } + boolean first = true; + int bxs = map.getBoundsMin().getBlockX(); + int bzs = map.getBoundsMin().getBlockZ(); + int bxl = map.getBoundsMax().getBlockX(); + int bzl = map.getBoundsMax().getBlockZ(); + if (bxs != 0 && bzs != 0 && bxl != 0 && bzl != 0) { + bxs = bzs = bxl = bzl = 0; + } + if (bxl == 0) { + bxl = sender.getLocation().getBlockX(); + } else if (map.getBoundsMax().getX() < sender.getLocation().getBlockX()) { + first = false; + bxs = bxl; + bxl = sender.getLocation().getBlockX(); + } else { + first = false; + bxs = sender.getLocation().getBlockX(); + } + if (bzl == 0) { + bzl = sender.getLocation().getBlockZ(); + } else if (map.getBoundsMax().getZ() < sender.getLocation().getBlockZ()) { + first = false; + bzs = bzl; + bzl = sender.getLocation().getBlockZ(); + } else { + first = false; + bzs = sender.getLocation().getBlockZ(); + } + map.setBoundMin(bxs, bzs); + map.setBoundMax(bxl, bzl); + if(!map.isBoundsNotSetup()) { + if(!map.getSpawn().isNotSetup()) { + if(map.getSpawn().isNotInBounds(bxs, bxl, bzs, bzl)) { + map.setSpawn(Location.getDefault()); + sender.sendMessage(Config.warningPrefix + Localization.message("WARN_SPAWN_RESET")); + } + } + if(!map.getSeekerLobby().isNotSetup()) { + if(map.getSeekerLobby().isNotInBounds(bxs, bxl, bzs, bzl)) { + map.setSeekerLobby(Location.getDefault()); + sender.sendMessage(Config.warningPrefix + Localization.message("WARN_SEEKER_SPAWN_RESET")); + } + } + } + Maps.setMap(map.getName(), map); + sender.sendMessage(Config.messagePrefix + Localization.message("BOUNDS").addAmount(first ? 1 : 2)); + } + + public String getLabel() { + return "bounds"; + } + + public String getUsage() { + return "<map>"; + } + + public String getDescription() { + return "Sets the map bounds for the game."; + } + + public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + if(parameter.equals("map")) { + return Maps.getAllMaps().stream().map(Map::getName).collect(Collectors.toList()); + } + return null; + } + +} diff --git a/src/main/java/cat/freya/khs/command/map/set/Lobby.java b/src/main/java/cat/freya/khs/command/map/set/Lobby.java new file mode 100644 index 0000000..847211c --- /dev/null +++ b/src/main/java/cat/freya/khs/command/map/set/Lobby.java @@ -0,0 +1,42 @@ +package cat.freya.khs.command.map.set; + +import cat.freya.khs.command.location.LocationUtils; +import cat.freya.khs.command.location.Locations; +import cat.freya.khs.command.util.ICommand; +import cat.freya.khs.configuration.Map; +import cat.freya.khs.configuration.Maps; +import cat.freya.khs.util.Location; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.stream.Collectors; + +public class Lobby implements ICommand { + + public void execute(Player sender, String[] args) { + LocationUtils.setLocation(sender, Locations.LOBBY, args[0], map -> { + map.setLobby(Location.from(sender)); + }); + } + + public String getLabel() { + return "lobby"; + } + + public String getUsage() { + return "<map>"; + } + + public String getDescription() { + return "Sets the maps lobby location"; + } + + public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + if(parameter.equals("map")) { + return Maps.getAllMaps().stream().map(Map::getName).collect(Collectors.toList()); + } + return null; + } + +} diff --git a/src/main/java/cat/freya/khs/command/map/set/SeekerLobby.java b/src/main/java/cat/freya/khs/command/map/set/SeekerLobby.java new file mode 100644 index 0000000..4aebb51 --- /dev/null +++ b/src/main/java/cat/freya/khs/command/map/set/SeekerLobby.java @@ -0,0 +1,56 @@ +package cat.freya.khs.command.map.set; + +import cat.freya.khs.command.location.LocationUtils; +import cat.freya.khs.command.location.Locations; +import cat.freya.khs.command.util.ICommand; +import cat.freya.khs.configuration.*; +import cat.freya.khs.util.Location; +import cat.freya.khs.configuration.Maps; +import org.bukkit.entity.Player; +import org.bukkit.util.Vector; +import org.jetbrains.annotations.NotNull; + +import java.util.List;; +import java.util.stream.Collectors; + +public class SeekerLobby implements ICommand { + + public void execute(Player sender, String[] args) { + LocationUtils.setLocation(sender, Locations.SEEKER, args[0], map -> { + if(map.getSpawn().isNotSetup()) { + throw new RuntimeException(Localization.message("GAME_SPAWN_NEEDED").toString()); + } + if(!map.getSpawnName().equals(sender.getLocation().getWorld().getName())) { + throw new RuntimeException(Localization.message("SEEKER_LOBBY_INVALID").toString()); + } + map.setSeekerLobby(Location.from(sender)); + if(!map.isBoundsNotSetup()) { + Vector boundsMin = map.getBoundsMin(); + Vector boundsMax = map.getBoundsMax(); + if(map.getSeekerLobby().isNotInBounds(boundsMin.getBlockX(), boundsMax.getBlockX(), boundsMin.getBlockZ(), boundsMax.getBlockZ())) { + sender.sendMessage(Config.warningPrefix + Localization.message("WARN_MAP_BOUNDS")); + } + } + }); + } + + public String getLabel() { + return "seekerlobby"; + } + + public String getUsage() { + return "<map>"; + } + + public String getDescription() { + return "Sets the maps seeker lobby location"; + } + + public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + if(parameter.equals("map")) { + return Maps.getAllMaps().stream().map(Map::getName).collect(Collectors.toList()); + } + return null; + } + +} diff --git a/src/main/java/cat/freya/khs/command/map/set/Spawn.java b/src/main/java/cat/freya/khs/command/map/set/Spawn.java new file mode 100644 index 0000000..44f4d13 --- /dev/null +++ b/src/main/java/cat/freya/khs/command/map/set/Spawn.java @@ -0,0 +1,67 @@ +package cat.freya.khs.command.map.set; + +import cat.freya.khs.command.location.LocationUtils; +import cat.freya.khs.command.location.Locations; +import cat.freya.khs.command.util.ICommand; +import cat.freya.khs.configuration.*; +import cat.freya.khs.util.Location; +import cat.freya.khs.configuration.Maps; +import org.bukkit.entity.Player; +import org.bukkit.util.Vector; +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.stream.Collectors; + +public class Spawn implements ICommand { + + public void execute(Player sender, String[] args) { + LocationUtils.setLocation(sender, Locations.GAME, args[0], map -> { + + if (map.isWorldBorderEnabled() && + new Vector(sender.getLocation().getX(), 0, sender.getLocation().getZ()).distance(map.getWorldBorderPos()) > 100) { + sender.sendMessage(Config.errorPrefix + Localization.message("WORLDBORDER_POSITION")); + throw new RuntimeException("World border not enabled or not in valid position!"); + } + + map.setSpawn(Location.from(sender)); + + if(!map.isBoundsNotSetup()) { + Vector boundsMin = map.getBoundsMin(); + Vector boundsMax = map.getBoundsMax(); + if(map.getSpawn().isNotInBounds(boundsMin.getBlockX(), boundsMax.getBlockX(), boundsMin.getBlockZ(), boundsMax.getBlockZ())) { + sender.sendMessage(Config.warningPrefix + Localization.message("WARN_MAP_BOUNDS")); + } + } + + if(map.getSeekerLobby().getWorld() != null && !map.getSeekerLobby().getWorld().equals(sender.getLocation().getWorld().getName())) { + sender.sendMessage(Config.warningPrefix + Localization.message("SEEKER_LOBBY_SPAWN_RESET")); + map.setSeekerLobby(Location.getDefault()); + } + + if (!sender.getLocation().getWorld().getName().equals(map.getSpawnName()) && Config.mapSaveEnabled) { + map.getWorldLoader().unloadMap(); + } + }); + } + + public String getLabel() { + return "spawn"; + } + + public String getUsage() { + return "<map>"; + } + + public String getDescription() { + return "Sets the maps game spawn location"; + } + + public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + if(parameter.equals("map")) { + return Maps.getAllMaps().stream().map(Map::getName).collect(Collectors.toList()); + } + return null; + } + +} diff --git a/src/main/java/cat/freya/khs/command/map/unset/Border.java b/src/main/java/cat/freya/khs/command/map/unset/Border.java new file mode 100644 index 0000000..078ad17 --- /dev/null +++ b/src/main/java/cat/freya/khs/command/map/unset/Border.java @@ -0,0 +1,58 @@ +package cat.freya.khs.command.map.unset; + +import cat.freya.khs.Main; +import cat.freya.khs.command.util.ICommand; +import cat.freya.khs.configuration.Config; +import cat.freya.khs.configuration.Localization; +import cat.freya.khs.configuration.Map; +import cat.freya.khs.configuration.Maps; +import cat.freya.khs.game.util.Status; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.stream.Collectors; + +public class Border implements ICommand { + + public void execute(Player sender, String[] args) { + if (Main.getInstance().getGame().getStatus() != Status.STANDBY) { + sender.sendMessage(Config.errorPrefix + Localization.message("GAME_INPROGRESS")); + return; + } + Map map = Maps.getMap(args[0]); + if(map == null) { + sender.sendMessage(Config.errorPrefix + Localization.message("INVALID_MAP")); + return; + } + if (map.getSpawn().isNotSetup()) { + sender.sendMessage(Config.errorPrefix + Localization.message("ERROR_GAME_SPAWN")); + return; + } + map.setWorldBorderData(0, 0, 0, 0, 0); + Config.addToConfig("worldBorder.enabled",false); + Config.saveConfig(); + sender.sendMessage(Config.messagePrefix + Localization.message("WORLDBORDER_DISABLE")); + map.getWorldBorder().resetWorldBorder(); + } + + public String getLabel() { + return "border"; + } + + public String getUsage() { + return "<map>"; + } + + public String getDescription() { + return "Removes a maps world border information"; + } + + public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + if(parameter.equals("map")) { + return Maps.getAllMaps().stream().map(Map::getName).collect(Collectors.toList()); + } + return null; + } + +} diff --git a/src/main/java/cat/freya/khs/command/util/CommandGroup.java b/src/main/java/cat/freya/khs/command/util/CommandGroup.java new file mode 100644 index 0000000..ab5ca43 --- /dev/null +++ b/src/main/java/cat/freya/khs/command/util/CommandGroup.java @@ -0,0 +1,186 @@ +package cat.freya.khs.command.util; + +import cat.freya.khs.configuration.Config; +import cat.freya.khs.configuration.Localization; +import cat.freya.khs.util.Pair; +import cat.freya.khs.util.Tuple; +import cat.freya.khs.command.map.Save; +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.Nullable; + +import java.util.*; +import java.util.stream.Collectors; + +public class CommandGroup { + + private final Map<String, Object> commandRegister; + private final String label; + + public CommandGroup(String label, Object... data) { + this.label = label; + this.commandRegister = new LinkedHashMap<>(); + for(Object o : data) { + registerCommand(o); + } + } + + public String getLabel() { + return label; + } + + private void registerCommand(Object object) { + if (object instanceof ICommand) { + ICommand command = (ICommand) object; + if (!commandRegister.containsKey(command.getLabel())) { + commandRegister.put(command.getLabel().toLowerCase(), command); + } + } else if(object instanceof CommandGroup) { + CommandGroup group = (CommandGroup) object; + if (!commandRegister.containsKey(group.getLabel())) { + commandRegister.put(group.getLabel().toLowerCase(), group); + } + } + } + + public void handleCommand(Player player, String[] args) { + + Tuple<ICommand, String, String[]> data = getCommand(args, this.getLabel()); + + if (data == null) { + player.sendMessage( + String.format("%s%sKenshin's Hide and Seek %s(%s1.7.6%s)\n", ChatColor.AQUA, ChatColor.BOLD, ChatColor.GRAY, ChatColor.WHITE, ChatColor.GRAY) + + String.format("%sAuthor: %s[KenshinEto]\n", ChatColor.GRAY, ChatColor.WHITE) + + String.format("%sHelp Command: %s/hs %shelp", ChatColor.GRAY, ChatColor.AQUA, ChatColor.WHITE) + ); + return; + } + + ICommand command = data.getLeft(); + String permission = data.getCenter(); + String[] parameters = data.getRight(); + + if (Save.runningBackup) { + player.sendMessage(Config.errorPrefix + Localization.message("MAPSAVE_INPROGRESS")); + return; + } + + if (Config.permissionsRequired && !player.hasPermission(permission)) { + player.sendMessage(Config.errorPrefix + Localization.message("COMMAND_NOT_ALLOWED")); + return; + } + + int parameterCount = (int) Arrays.stream(command.getUsage().split(" ")).filter(p -> p.startsWith("<") && !p.startsWith("<*")).count(); + if(parameters.length < parameterCount) { + player.sendMessage(Config.errorPrefix + Localization.message("ARGUMENT_COUNT")); + return; + } + + try { + command.execute(player, parameters); + } catch (Exception e) { + player.sendMessage(Config.errorPrefix + "An error has occurred."); + e.printStackTrace(); + } + } + + @Nullable + private Tuple<ICommand, String, String[]> getCommand(String[] args, String permission) { + if(args.length < 1) { + return null; + } + String invoke = args[0]; + if(commandRegister.containsKey(invoke)) { + Object o = commandRegister.get(invoke); + if (o instanceof CommandGroup) { + CommandGroup group = (CommandGroup) o; + return group.getCommand( + Arrays.copyOfRange(args, 1, args.length), + permission + "." + group.getLabel() + ); + } else if(o instanceof ICommand) { + ICommand command = (ICommand) o; + return new Tuple<>(command, permission + "." + command.getLabel(), Arrays.copyOfRange(args, 1, args.length)); + } + } + return null; + } + + public List<String> handleTabComplete(Player player, String[] args) { + return handleTabComplete(player, this.getLabel(), args); + } + + private List<String> handleTabComplete(Player player, String permission, String[] args) { + String invoke = args[0].toLowerCase(); + if (args.length == 1) { + return new ArrayList<>(commandRegister.keySet()) + .stream() + .filter(handle -> handle.toLowerCase().startsWith(invoke)) + .filter(handle -> { + Object object = commandRegister.get(handle); + if (object instanceof ICommand) { + ICommand command = (ICommand) object; + return !Config.permissionsRequired || player.hasPermission(permission + "." + command.getLabel()); + } else if (object instanceof CommandGroup) { + CommandGroup group = (CommandGroup) object; + return !Config.permissionsRequired || group.hasPermission(player, permission + "." + group.getLabel()); + } + return false; + }) + .collect(Collectors.toList()); + } else { + if (commandRegister.containsKey(invoke)) { + Object object = commandRegister.get(invoke); + if (object instanceof CommandGroup) { + CommandGroup group = (CommandGroup) object; + return group.handleTabComplete(player, permission + "." + group.getLabel(), Arrays.copyOfRange(args, 1, args.length)); + } else if (object instanceof ICommand) { + ICommand command = (ICommand) object; + String[] usage = command.getUsage().split(" "); + if (args.length - 2 < usage.length) { + String parameter = usage[args.length - 2]; + String name = parameter.replace("<", "").replace(">", ""); + List<String> list = command.autoComplete(name, args[args.length - 1]); + if (list != null) { + return list; + } + } + } + } + return new ArrayList<>(); + } + } + + private boolean hasPermission(Player player, String permission) { + for(Object object : commandRegister.values()) { + if(object instanceof ICommand) { + ICommand command = (ICommand) object; + if(player.hasPermission(permission + command.getLabel())) return true; + } else if(object instanceof CommandGroup) { + CommandGroup group = (CommandGroup) object; + if (group.hasPermission(player, permission + this.label + ".")) return true; + } + } + return false; + } + + public List<Pair<String, ICommand>> getCommands() { + return getCommands(this.getLabel()); + } + + private List<Pair<String, ICommand>> getCommands(String prefix) { + List<Pair<String, ICommand>> commands = new LinkedList<>(); + for(Object object : commandRegister.values()) { + if(object instanceof ICommand) { + ICommand command = (ICommand) object; + commands.add(new Pair<>(prefix+" "+command.getLabel(), command)); + } else if(object instanceof CommandGroup) { + CommandGroup group = (CommandGroup) object; + commands.addAll(group.getCommands(prefix+" "+group.getLabel())); + } + } + return commands; + } + + +} diff --git a/src/main/java/cat/freya/khs/command/util/ICommand.java b/src/main/java/cat/freya/khs/command/util/ICommand.java new file mode 100644 index 0000000..99f3e20 --- /dev/null +++ b/src/main/java/cat/freya/khs/command/util/ICommand.java @@ -0,0 +1,20 @@ +package cat.freya.khs.command.util; + +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +public interface ICommand { + + void execute(Player sender, String[] args); + + String getLabel(); + + String getUsage(); + + String getDescription(); + + List<String> autoComplete(@NotNull String parameter, @NotNull String typed); + +} diff --git a/src/main/java/cat/freya/khs/command/world/Create.java b/src/main/java/cat/freya/khs/command/world/Create.java new file mode 100644 index 0000000..8b429b3 --- /dev/null +++ b/src/main/java/cat/freya/khs/command/world/Create.java @@ -0,0 +1,80 @@ +package cat.freya.khs.command.world; + +import cat.freya.khs.Main; +import cat.freya.khs.configuration.Config; +import cat.freya.khs.configuration.Localization; +import cat.freya.khs.util.Location; +import cat.freya.khs.command.util.ICommand; +import org.bukkit.World; +import org.bukkit.WorldType; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +public class Create implements ICommand { + + public void execute(Player sender, String[] args) { + List<String> worlds = Main.getInstance().getWorlds(); + if(worlds.contains(args[0])) { + sender.sendMessage(Config.errorPrefix + Localization.message("WORLD_EXISTS").addAmount(args[0])); + return; + } + WorldType type; + World.Environment environment; + switch (args[1]) { + case "normal": + type = WorldType.NORMAL; + environment = World.Environment.NORMAL; + break; + case "flat": + type = WorldType.FLAT; + environment = World.Environment.NORMAL; + break; + case "nether": + type = WorldType.NORMAL; + environment = World.Environment.NETHER; + break; + case "end": + type = WorldType.NORMAL; + environment = World.Environment.THE_END; + break; + default: + sender.sendMessage(Config.errorPrefix + Localization.message("INVALID_WORLD_TYPE").addAmount(args[1])); + return; + } + + Location temp = new Location(args[0], 0, 0, 0); + + if (temp.load(type, environment) == null) { + sender.sendMessage(Config.errorPrefix + Localization.message("WORLD_ADDED_FAILED")); + } else { + sender.sendMessage(Config.messagePrefix + Localization.message("WORLD_ADDED").addAmount(args[0])); + } + + } + + public String getLabel() { + return "create"; + } + + public String getUsage() { + return "<name> <type>"; + } + + public String getDescription() { + return "Create a new world"; + } + + public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + if(parameter.equals("name")) { + return Collections.singletonList("name"); + } + if(parameter.equals("type")) { + return Arrays.asList("normal", "flat", "nether", "end"); + } + return null; + } +}
\ No newline at end of file diff --git a/src/main/java/cat/freya/khs/command/world/Delete.java b/src/main/java/cat/freya/khs/command/world/Delete.java new file mode 100644 index 0000000..629eef4 --- /dev/null +++ b/src/main/java/cat/freya/khs/command/world/Delete.java @@ -0,0 +1,74 @@ +package cat.freya.khs.command.world; + +import cat.freya.khs.Main; +import cat.freya.khs.command.Confirm; +import cat.freya.khs.command.util.ICommand; +import cat.freya.khs.configuration.Config; +import cat.freya.khs.configuration.Localization; +import cat.freya.khs.world.WorldLoader; +import org.bukkit.Bukkit; +import org.bukkit.World; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.io.File; +import java.util.List; + +public class Delete implements ICommand { + + public void execute(Player sender, String[] args) { + java.util.List<String> worlds = Main.getInstance().getWorlds(); + if(!worlds.contains(args[0])) { + sender.sendMessage(Config.errorPrefix + Localization.message("WORLD_DOESNT_EXIST").addAmount(args[0])); + return; + } + + Confirm.Confirmation confirmation = new Confirm.Confirmation(args[0], world -> { + java.util.List<String> worlds_now = Main.getInstance().getWorlds(); + if(!worlds_now.contains(world)) { + sender.sendMessage(Config.errorPrefix + Localization.message("WORLD_DOESNT_EXIST").addAmount(world)); + return; + } + World bukkit_world = Bukkit.getWorld(world); + if(bukkit_world != null && bukkit_world.getPlayers().size() > 0) { + sender.sendMessage(Config.errorPrefix + Localization.message("WORLD_NOT_EMPTY")); + return; + } + String path = Main.getInstance().getWorldContainer().getPath() + File.separator + world; + if (!Bukkit.getServer().unloadWorld(world, false)) { + sender.sendMessage(Config.errorPrefix + Localization.message("WORLD_REMOVED_FAILED")); + return; + } + try { + WorldLoader.deleteDirectory(new File(path)); + } catch (Exception e) { + sender.sendMessage(Config.errorPrefix + Localization.message("WORLD_REMOVED_FAILED")); + return; + } + sender.sendMessage(Config.messagePrefix + Localization.message("WORLD_REMOVED").addAmount(world)); + }); + + Confirm.confirmations.put(sender.getUniqueId(), confirmation); + sender.sendMessage(Config.messagePrefix + Localization.message("CONFIRMATION")); + + } + + public String getLabel() { + return "delete"; + } + + public String getUsage() { + return "<name>"; + } + + public String getDescription() { + return "Delete a world"; + } + + public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + if(parameter.equals("name")) { + return Main.getInstance().getWorlds(); + } + return null; + } +}
\ No newline at end of file diff --git a/src/main/java/cat/freya/khs/command/world/List.java b/src/main/java/cat/freya/khs/command/world/List.java new file mode 100644 index 0000000..a65930e --- /dev/null +++ b/src/main/java/cat/freya/khs/command/world/List.java @@ -0,0 +1,55 @@ +package cat.freya.khs.command.world; + +import cat.freya.khs.Main; +import cat.freya.khs.configuration.Config; +import cat.freya.khs.configuration.Localization; +import cat.freya.khs.command.util.ICommand; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.World; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +public class List implements ICommand { + + public void execute(Player sender, String[] args) { + java.util.List<String> worlds = Main.getInstance().getWorlds(); + if(worlds.isEmpty()) { + sender.sendMessage(Config.errorPrefix + Localization.message("NO_WORLDS")); + } else { + StringBuilder response = new StringBuilder(Config.messagePrefix + Localization.message("LIST_WORLDS")); + for (String world : worlds) { + String status = ChatColor.GRAY + "NOT LOADED"; + World bukkit_world = Bukkit.getWorld(world); + if(bukkit_world != null) { + if(bukkit_world.getEnvironment() == World.Environment.NETHER) { + status = ChatColor.RED + "NETHER"; + } else if(bukkit_world.getEnvironment() == World.Environment.THE_END) { + status = ChatColor.YELLOW + "THE END"; + } else { + status = ChatColor.GREEN + bukkit_world.getWorldType().toString(); + } + } + response.append("\n ").append(world).append(": ").append(status).append(ChatColor.WHITE); + } + sender.sendMessage(response.toString()); + } + } + + public String getLabel() { + return "list"; + } + + public String getUsage() { + return ""; + } + + public String getDescription() { + return "List all worlds in the server"; + } + + public java.util.List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + return null; + } + +} diff --git a/src/main/java/cat/freya/khs/command/world/Tp.java b/src/main/java/cat/freya/khs/command/world/Tp.java new file mode 100644 index 0000000..ff6190a --- /dev/null +++ b/src/main/java/cat/freya/khs/command/world/Tp.java @@ -0,0 +1,48 @@ +package cat.freya.khs.command.world; + +import cat.freya.khs.Main; +import cat.freya.khs.command.util.ICommand; +import cat.freya.khs.configuration.Config; +import cat.freya.khs.configuration.Localization; +import cat.freya.khs.util.Location; +import org.bukkit.World; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +public class Tp implements ICommand { + public void execute(Player sender, String[] args) { + Location test = new Location(args[0], 0, 0,0); + if(!test.exists()) { + sender.sendMessage(Config.errorPrefix + Localization.message("WORLD_DOESNT_EXIT")); + return; + } + World world = test.load(); + if(world == null) { + sender.sendMessage(Config.errorPrefix + Localization.message("WORLD_LOAD_FAILED")); + return; + } + Location loc = new Location(world.getName(), world.getSpawnLocation()); + loc.teleport(sender); + } + + public String getLabel() { + return "tp"; + } + + public String getUsage() { + return "<world>"; + } + + public String getDescription() { + return "Teleport to another world"; + } + + public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) { + if(parameter.equals("world")) { + return Main.getInstance().getWorlds(); + } + return null; + } +} |