diff options
author | Tyler Murphy <tylermurphy534@gmail.com> | 2022-05-13 21:17:46 -0400 |
---|---|---|
committer | Tyler Murphy <tylermurphy534@gmail.com> | 2022-05-13 21:17:46 -0400 |
commit | ea8f76493141717296e1f59fbdab21c39f1937be (patch) | |
tree | 3d6ceeb5baf90f857b88d8a09fc9a07caddf75ce /src/main/java/net/tylermurphy/hideAndSeek/util | |
parent | Merge pull request #53 from bobby29831/1.4.3 (diff) | |
download | kenshinshideandseek-ea8f76493141717296e1f59fbdab21c39f1937be.tar.gz kenshinshideandseek-ea8f76493141717296e1f59fbdab21c39f1937be.tar.bz2 kenshinshideandseek-ea8f76493141717296e1f59fbdab21c39f1937be.zip |
refactor and encapsulate classes
Diffstat (limited to 'src/main/java/net/tylermurphy/hideAndSeek/util')
9 files changed, 105 insertions, 309 deletions
diff --git a/src/main/java/net/tylermurphy/hideAndSeek/util/CommandHandler.java b/src/main/java/net/tylermurphy/hideAndSeek/util/CommandHandler.java new file mode 100644 index 0000000..3a05ee5 --- /dev/null +++ b/src/main/java/net/tylermurphy/hideAndSeek/util/CommandHandler.java @@ -0,0 +1,93 @@ +/* + * This file is part of Kenshins Hide and Seek + * + * Copyright (c) 2021 Tyler Murphy. + * + * Kenshins Hide and Seek free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * he Free Software Foundation version 3. + * + * Kenshins Hide and Seek is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +package net.tylermurphy.hideAndSeek.util; + +import net.tylermurphy.hideAndSeek.command.*; +import net.tylermurphy.hideAndSeek.command.location.SetExitLocation; +import net.tylermurphy.hideAndSeek.command.location.SetLobbyLocation; +import net.tylermurphy.hideAndSeek.command.location.SetSpawnLocation; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.Map; + +import static net.tylermurphy.hideAndSeek.configuration.Config.errorPrefix; +import static net.tylermurphy.hideAndSeek.configuration.Config.permissionsRequired; +import static net.tylermurphy.hideAndSeek.configuration.Localization.LOCAL; +import static net.tylermurphy.hideAndSeek.configuration.Localization.message; + +public class CommandHandler { + + public static final Map<String,ICommand> COMMAND_REGISTER = new LinkedHashMap<>(); + + private static void registerCommand(ICommand command) { + if (!COMMAND_REGISTER.containsKey(command.getLabel())) { + COMMAND_REGISTER.put(command.getLabel().toLowerCase(), command); + } + } + + public static void registerCommands() { + registerCommand(new About()); + registerCommand(new Help()); + registerCommand(new Setup()); + registerCommand(new Start()); + registerCommand(new Stop()); + registerCommand(new SetSpawnLocation()); + registerCommand(new SetLobbyLocation()); + registerCommand(new SetExitLocation()); + registerCommand(new SetBorder()); + registerCommand(new Reload()); + registerCommand(new SaveMap()); + registerCommand(new SetBounds()); + registerCommand(new Join()); + registerCommand(new Leave()); + registerCommand(new Top()); + registerCommand(new Wins()); + } + + public static boolean handleCommand(CommandSender sender, String[] args) { + if (!(sender instanceof Player)) { + sender.sendMessage(errorPrefix + message("COMMAND_PLAYER_ONLY")); + } else if (args.length < 1 || !COMMAND_REGISTER.containsKey(args[0].toLowerCase()) ) { + if (permissionsRequired && !sender.hasPermission("hideandseek.about")) { + sender.sendMessage(errorPrefix + LOCAL.get("")); + } else { + COMMAND_REGISTER.get("about").execute(sender, null); + } + } else { + if (!args[0].equalsIgnoreCase("about") && !args[0].equalsIgnoreCase("help") && SaveMap.runningBackup) { + sender.sendMessage(errorPrefix + message("MAPSAVE_INPROGRESS")); + } else if (permissionsRequired && !sender.hasPermission("hideandseek."+args[0].toLowerCase())) { + sender.sendMessage(errorPrefix + message("COMMAND_NOT_ALLOWED")); + } else { + try { + COMMAND_REGISTER.get(args[0].toLowerCase()).execute(sender,Arrays.copyOfRange(args, 1, args.length)); + } catch (Exception e) { + sender.sendMessage(errorPrefix + "An error has occured."); + e.printStackTrace(); + } + } + } + return true; + } + +} diff --git a/src/main/java/net/tylermurphy/hideAndSeek/util/CountdownDisplay.java b/src/main/java/net/tylermurphy/hideAndSeek/util/CountdownDisplay.java deleted file mode 100644 index 477f7ca..0000000 --- a/src/main/java/net/tylermurphy/hideAndSeek/util/CountdownDisplay.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * This file is part of Kenshins Hide and Seek - * - * Copyright (c) 2022 Tyler Murphy. - * - * Kenshins Hide and Seek free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * he Free Software Foundation version 3. - * - * Kenshins Hide and Seek is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * - */ - -package net.tylermurphy.hideAndSeek.util; - -public enum CountdownDisplay { - CHAT, - ACTIONBAR, - TITLE -} diff --git a/src/main/java/net/tylermurphy/hideAndSeek/util/PAPIExpansion.java b/src/main/java/net/tylermurphy/hideAndSeek/util/PAPIExpansion.java index 4af5ff9..b5e7e34 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/util/PAPIExpansion.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/util/PAPIExpansion.java @@ -1,6 +1,7 @@ package net.tylermurphy.hideAndSeek.util; import me.clip.placeholderapi.expansion.PlaceholderExpansion; +import net.tylermurphy.hideAndSeek.Main; import net.tylermurphy.hideAndSeek.database.Database; import net.tylermurphy.hideAndSeek.database.PlayerInfo; import org.bukkit.OfflinePlayer; @@ -35,42 +36,43 @@ public class PAPIExpansion extends PlaceholderExpansion { @Override public String onRequest(OfflinePlayer player, @NotNull String params) { + Database database = Main.getInstance().getDatabase(); String[] args = params.split("_"); if (args.length < 1) return null; if (args[0].equals("stats") && args.length == 2) { - PlayerInfo info = Database.playerInfo.getInfo(player.getUniqueId()); + PlayerInfo info = database.getGameData().getInfo(player.getUniqueId()); return getValue(info, args[1]); } else if (args[0].equals("stats") && args.length == 3) { UUID uuid; - try { uuid = UUIDFetcher.getUUID(args[2]); } catch (Exception e) { return placeholderError; } - PlayerInfo info = Database.playerInfo.getInfo(uuid); + try { uuid = Main.getInstance().getServer().getOfflinePlayer(args[2]).getUniqueId(); } catch (Exception e) { return placeholderError; } + PlayerInfo info = database.getGameData().getInfo(uuid); return getValue(info, args[1]); } else if ((args[0].equals("rank-score") || args[0].equals("rank-name") ) && args.length == 3) { int place; try { place = Integer.parseInt(args[2]); } catch (NumberFormatException e) { return placeholderError; } if (place < 1) { return placeholderError; } if (getRanking(args[1]) == null) { return placeholderError; } - PlayerInfo info = Database.playerInfo.getInfoRanking(getRanking(args[1]), place); + PlayerInfo info = database.getGameData().getInfoRanking(getRanking(args[1]), place); if (info == null) return placeholderNoData; if (args[0].equals("rank-score")) { return getValue(info, args[1]); } else { - return UUIDFetcher.getPlayer(info.uuid).getName(); + return Main.getInstance().getServer().getOfflinePlayer(info.uuid).getName(); } } else if (args[0].equals("rank-place") && args.length == 2) { if (getRanking(args[1]) == null) { return placeholderError; } - PlayerInfo info = Database.playerInfo.getInfo(player.getUniqueId()); + PlayerInfo info = database.getGameData().getInfo(player.getUniqueId()); if (getValue(info, args[1]).equals("0")) { return "-"; } - Integer count = Database.playerInfo.getRanking(getRanking(args[1]), player.getUniqueId()); + Integer count = database.getGameData().getRanking(getRanking(args[1]), player.getUniqueId()); if (count == null) { return placeholderNoData; } return count.toString(); } else if (args[0].equals("rank-place") && args.length == 3) { UUID uuid; - try { uuid = UUIDFetcher.getUUID(args[2]); } catch (Exception e) { return placeholderError; } + try { uuid = Main.getInstance().getServer().getOfflinePlayer(args[2]).getUniqueId(); } catch (Exception e) { return placeholderError; } if (getRanking(args[1]) == null) { return placeholderError; } - PlayerInfo info = Database.playerInfo.getInfo(player.getUniqueId()); + PlayerInfo info = database.getGameData().getInfo(player.getUniqueId()); if (getValue(info, args[1]).equals("0")) { return "-"; } - Integer count = Database.playerInfo.getRanking(getRanking(args[1]), uuid); + Integer count = database.getGameData().getRanking(getRanking(args[1]), uuid); if (count == null) { return placeholderNoData; } return count.toString(); } diff --git a/src/main/java/net/tylermurphy/hideAndSeek/util/Packet.java b/src/main/java/net/tylermurphy/hideAndSeek/util/Packet.java deleted file mode 100644 index bb7219f..0000000 --- a/src/main/java/net/tylermurphy/hideAndSeek/util/Packet.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * This file is part of Kenshins Hide and Seek - * - * Copyright (c) 2021 Tyler Murphy. - * - * Kenshins Hide and Seek free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * he Free Software Foundation version 3. - * - * Kenshins Hide and Seek is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * - */ - -package net.tylermurphy.hideAndSeek.util; - -import com.comphenix.protocol.PacketType; -import com.comphenix.protocol.ProtocolLibrary; -import com.comphenix.protocol.ProtocolManager; -import com.comphenix.protocol.events.PacketContainer; -import com.comphenix.protocol.wrappers.WrappedDataWatcher; -import com.comphenix.protocol.wrappers.WrappedDataWatcher.Registry; -import com.comphenix.protocol.wrappers.WrappedDataWatcher.Serializer; -import org.bukkit.entity.Player; - -import java.lang.reflect.InvocationTargetException; - -public class Packet { - - private static final ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager(); - - public static void setGlow(Player player, Player target, boolean glowing) { - PacketContainer packet = protocolManager.createPacket(PacketType.Play.Server.ENTITY_METADATA); - packet.getIntegers().write(0, target.getEntityId()); - WrappedDataWatcher watcher = new WrappedDataWatcher(); - Serializer serializer = Registry.get(Byte.class); - watcher.setEntity(target); - if (glowing) { - watcher.setObject(0, serializer, (byte) (0x40)); - } else { - watcher.setObject(0, serializer, (byte) (0x0)); - } - packet.getWatchableCollectionModifier().write(0, watcher.getWatchableObjects()); - try { - protocolManager.sendServerPacket(player, packet); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } - } - -} diff --git a/src/main/java/net/tylermurphy/hideAndSeek/util/Status.java b/src/main/java/net/tylermurphy/hideAndSeek/util/Status.java deleted file mode 100644 index 0ffba00..0000000 --- a/src/main/java/net/tylermurphy/hideAndSeek/util/Status.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * This file is part of Kenshins Hide and Seek - * - * Copyright (c) 2021 Tyler Murphy. - * - * Kenshins Hide and Seek free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * he Free Software Foundation version 3. - * - * Kenshins Hide and Seek is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * - */ - -package net.tylermurphy.hideAndSeek.util; - -public enum Status { - - STANDBY, STARTING, PLAYING, ENDING - -} diff --git a/src/main/java/net/tylermurphy/hideAndSeek/util/TabCompleter.java b/src/main/java/net/tylermurphy/hideAndSeek/util/TabCompleter.java index 85b10ee..2605e7b 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/util/TabCompleter.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/util/TabCompleter.java @@ -19,7 +19,6 @@ package net.tylermurphy.hideAndSeek.util; -import net.tylermurphy.hideAndSeek.game.CommandHandler; import org.bukkit.command.CommandSender; import java.util.ArrayList; diff --git a/src/main/java/net/tylermurphy/hideAndSeek/util/UUIDFetcher.java b/src/main/java/net/tylermurphy/hideAndSeek/util/UUIDFetcher.java deleted file mode 100644 index abc3400..0000000 --- a/src/main/java/net/tylermurphy/hideAndSeek/util/UUIDFetcher.java +++ /dev/null @@ -1,119 +0,0 @@ -/* - * This file is part of Kenshins Hide and Seek - * - * Copyright (c) 2021 Tyler Murphy. - * - * Kenshins Hide and Seek free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * he Free Software Foundation version 3. - * - * Kenshins Hide and Seek is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * - */ - -package net.tylermurphy.hideAndSeek.util; - -import net.tylermurphy.hideAndSeek.Main; -import org.bukkit.Bukkit; -import org.bukkit.OfflinePlayer; - -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.net.URL; -import java.net.URLConnection; -import java.nio.charset.Charset; -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; - -public final class UUIDFetcher { - - private static final Map<String,UUID> CACHE = new HashMap<>(); - private static final Map<UUID,OfflinePlayer> PLAYER_CACHE = new HashMap<>(); - - private static final String UUID_URL = "https://api.mojang.com/users/profiles/minecraft/"; - private static int cacheTask; - - public static void init() { - cacheTask = Main.plugin.getServer().getScheduler().scheduleSyncRepeatingTask(Main.plugin, () -> { - CACHE.clear(); - PLAYER_CACHE.clear(); - }, 600 * 20, 600 * 20); - } - - public static void cleanup() { - Main.plugin.getServer().getScheduler().cancelTask(cacheTask); - } - - public static UUID getUUID(String playername) { - - if (CACHE.containsKey(playername)) return CACHE.get(playername); - - String output = callURL(UUID_URL + playername); - StringBuilder result = new StringBuilder(); - readData(output, result); - String u = result.toString(); - StringBuilder uuid = new StringBuilder(); - for (int i = 0; i <= 31; i++) { - uuid.append(u.charAt(i)); - if (i == 7 || i == 11 || i == 15 || i == 19) { - uuid.append('-'); - } - } - - CACHE.put(playername, UUID.fromString(uuid.toString())); - - return UUID.fromString(uuid.toString()); - } - - public static OfflinePlayer getPlayer(UUID uuid) { - if (PLAYER_CACHE.containsKey(uuid)) return PLAYER_CACHE.get(uuid); - OfflinePlayer temp = Bukkit.getOfflinePlayer(uuid); - PLAYER_CACHE.put(uuid, temp); - return temp; - } - - private static void readData(String toRead, StringBuilder result) { - for (int i = toRead.length() - 3; i >= 0; i--) { - if (toRead.charAt(i) != '"') { - result.insert(0, toRead.charAt(i)); - } else { - break; - } - } - } - - private static String callURL(String urlStr) { - StringBuilder sb = new StringBuilder(); - URLConnection urlConn; - InputStreamReader in; - try { - URL url = new URL(urlStr); - urlConn = url.openConnection(); - if (urlConn != null) { - urlConn.setReadTimeout(60 * 1000); - } - if (urlConn != null && urlConn.getInputStream() != null) { - in = new InputStreamReader(urlConn.getInputStream(), - Charset.defaultCharset()); - BufferedReader bufferedReader = new BufferedReader(in); - int cp; - while ((cp = bufferedReader.read()) != -1) { - sb.append((char) cp); - } - bufferedReader.close(); - in.close(); - } - } catch (Exception e) { - e.printStackTrace(); - } - return sb.toString(); - } - -} diff --git a/src/main/java/net/tylermurphy/hideAndSeek/util/Version.java b/src/main/java/net/tylermurphy/hideAndSeek/util/Version.java deleted file mode 100644 index fa66fe2..0000000 --- a/src/main/java/net/tylermurphy/hideAndSeek/util/Version.java +++ /dev/null @@ -1,45 +0,0 @@ -package net.tylermurphy.hideAndSeek.util; - -import org.bukkit.Bukkit; - -import java.util.HashMap; -import java.util.Map; - -public class Version { - - private static final Map<String,Boolean> CACHE = new HashMap<>(); - - public static boolean atLeast(String testVersion) { - - - if (CACHE.containsKey(testVersion)) return CACHE.get(testVersion); - - String[] serverCheckTemp = Bukkit.getBukkitVersion().substring(2,Bukkit.getBukkitVersion().indexOf('-')).split("\\."); - int[] serverCheck = new int[serverCheckTemp.length]; - for(int i=0; i<serverCheck.length; i++) { - serverCheck[i] = Integer.parseInt(serverCheckTemp[i]); - } - - String[] customCheckTemp = testVersion.substring(2).split("\\."); - int[] customCheck = new int[customCheckTemp.length]; - for(int i=0; i<customCheck.length; i++) { - customCheck[i] = Integer.parseInt(customCheckTemp[i]); - } - - boolean result = getResult(customCheck, serverCheck); - CACHE.put(testVersion, result); - return result; - } - - private static boolean getResult(int[] customCheck, int[] serverCheck) { - if (customCheck[0] > serverCheck[0]) return false; - else if (customCheck[0] < serverCheck[0]) return true; - else { - if (customCheck.length == 1 && serverCheck.length == 1) return true; - else if (customCheck.length == 2 && serverCheck.length == 2) { - return customCheck[1] <= serverCheck[1]; - } - else return serverCheck.length == 2; - } - } -} diff --git a/src/main/java/net/tylermurphy/hideAndSeek/util/WinType.java b/src/main/java/net/tylermurphy/hideAndSeek/util/WinType.java deleted file mode 100644 index 1f6ccd8..0000000 --- a/src/main/java/net/tylermurphy/hideAndSeek/util/WinType.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * This file is part of Kenshins Hide and Seek - * - * Copyright (c) 2021 Tyler Murphy. - * - * Kenshins Hide and Seek free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * he Free Software Foundation version 3. - * - * Kenshins Hide and Seek is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * - */ - -package net.tylermurphy.hideAndSeek.util; - -public enum WinType { - - HIDER_WIN, SEEKER_WIN, NONE - -} |