summaryrefslogtreewikicommitdiff
path: root/src/main/java/net/tylermurphy/hideAndSeek/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/net/tylermurphy/hideAndSeek/util')
-rw-r--r--src/main/java/net/tylermurphy/hideAndSeek/util/CommandHandler.java97
-rw-r--r--src/main/java/net/tylermurphy/hideAndSeek/util/PAPIExpansion.java145
-rw-r--r--src/main/java/net/tylermurphy/hideAndSeek/util/Packet.java57
-rw-r--r--src/main/java/net/tylermurphy/hideAndSeek/util/Status.java26
-rw-r--r--src/main/java/net/tylermurphy/hideAndSeek/util/TabCompleter.java17
-rw-r--r--src/main/java/net/tylermurphy/hideAndSeek/util/UUIDFetcher.java106
-rw-r--r--src/main/java/net/tylermurphy/hideAndSeek/util/Version.java45
-rw-r--r--src/main/java/net/tylermurphy/hideAndSeek/util/WinType.java26
8 files changed, 250 insertions, 269 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..1f0c8a3
--- /dev/null
+++ b/src/main/java/net/tylermurphy/hideAndSeek/util/CommandHandler.java
@@ -0,0 +1,97 @@
+/*
+ * 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());
+ registerCommand(new Debug());
+ }
+
+ public static boolean handleCommand(CommandSender sender, String[] args) {
+ if (!(sender instanceof Player)) {
+ sender.sendMessage(errorPrefix + message("COMMAND_PLAYER_ONLY"));
+ return true;
+ }
+ Player player = (Player) sender;
+ 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(player, 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(player,Arrays.copyOfRange(args, 1, args.length));
+ } catch (Exception e) {
+ sender.sendMessage(errorPrefix + "An error has occurred.");
+ e.printStackTrace();
+ }
+ }
+ }
+ return true;
+ }
+
+}
diff --git a/src/main/java/net/tylermurphy/hideAndSeek/util/PAPIExpansion.java b/src/main/java/net/tylermurphy/hideAndSeek/util/PAPIExpansion.java
new file mode 100644
index 0000000..032deca
--- /dev/null
+++ b/src/main/java/net/tylermurphy/hideAndSeek/util/PAPIExpansion.java
@@ -0,0 +1,145 @@
+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.util.PlayerInfo;
+import org.bukkit.OfflinePlayer;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.Optional;
+import java.util.UUID;
+
+import static net.tylermurphy.hideAndSeek.configuration.Config.placeholderError;
+import static net.tylermurphy.hideAndSeek.configuration.Config.placeholderNoData;
+
+public class PAPIExpansion extends PlaceholderExpansion {
+
+ @Override
+ public @NotNull String getIdentifier() {
+ return "hs";
+ }
+
+ @Override
+ public @NotNull String getAuthor() {
+ return "KenshinEto";
+ }
+
+ @Override
+ public @NotNull String getVersion() {
+ return "1.4.3";
+ }
+
+ @Override
+ public boolean persist() {
+ return true;
+ }
+
+ @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.length == 2 || args.length == 3) && (args[0].equals("stats") || args[0].equals("rank-place"))) {
+ Optional<PlayerInfo> info = this.getPlayerInfo(args.length == 2 ? player.getUniqueId() : database.getNameData().getUUID(args[2]));
+ if (info.isPresent()) {
+ switch (args[0]) {
+ case "stats":
+ return getValue(info.get(), args[1]);
+ case "rank-place":
+ if (getRanking(args[1]) == null) return placeholderError;
+ Integer count = database.getGameData().getRanking(getRanking(args[1]), player.getUniqueId());
+ if (getValue(info.get(), args[1]).equals("0")) return "-";
+ if (count == null) return placeholderNoData;
+ return count.toString();
+ }
+ } else switch (args[0]) {
+ case "stats":
+ return placeholderNoData;
+ case "rank-place":
+ return "-";
+ }
+ }
+
+ if ((args[0].equals("rank-score") || args[0].equals("rank-name")) && args.length == 3) {
+ int place = Integer.parseInt(args[2]);
+ if (place < 1 || getRanking(args[1]) == null) return placeholderError;
+
+ PlayerInfo info = database.getGameData().getInfoRanking(getRanking(args[1]), place);
+ if (info == null) return placeholderNoData;
+
+ return args[0].equals("rank-score") ? getValue(info, args[1]) : Main.getInstance().getServer().getOfflinePlayer(info.getUniqueId()).getName();
+ }
+ return null;
+ }
+
+ private String getValue(PlayerInfo info, String query) {
+ if (query == null) return null;
+ switch (query) {
+ case "total-wins":
+ return String.valueOf(info.getHiderWins() + info.getSeekerWins());
+ case "hider-wins":
+ return String.valueOf(info.getHiderWins());
+ case "seeker-wins":
+ return String.valueOf(info.getSeekerWins());
+ case "total-games":
+ return String.valueOf(info.getHiderGames() + info.getSeekerGames());
+ case "hider-games":
+ return String.valueOf(info.getHiderGames());
+ case "seeker-games":
+ return String.valueOf(info.getSeekerGames());
+ case "total-kills":
+ return String.valueOf(info.getHiderKills() + info.getSeekerKills());
+ case "hider-kills":
+ return String.valueOf(info.getHiderKills());
+ case "seeker-kills":
+ return String.valueOf(info.getSeekerKills());
+ case "total-deaths":
+ return String.valueOf(info.getHiderDeaths() + info.getSeekerDeaths());
+ case "hider-deaths":
+ return String.valueOf(info.getHiderDeaths());
+ case "seeker-deaths":
+ return String.valueOf(info.getSeekerDeaths());
+ default:
+ return null;
+ }
+ }
+
+ private String getRanking(@NotNull String query) {
+ switch (query) {
+ case "total-wins":
+ return "(hider_wins + seeker_wins)";
+ case "hider-wins":
+ return "hider_wins";
+ case "seeker-wins":
+ return "seeker_wins";
+ case "total-games":
+ return "(hider_games + seeker_games)";
+ case "hider-games":
+ return "hider_games";
+ case "seeker-games":
+ return "seeker_games";
+ case "total-kills":
+ return "(hider_kills + seeker_kills)";
+ case "hider-kills":
+ return "hider_kills";
+ case "seeker-kills":
+ return "seeker_kills";
+ case "total-deaths":
+ return "(hider_deaths + seeker_deaths)";
+ case "hider-deaths":
+ return "hider_deaths";
+ case "seeker-deaths":
+ return "seeker_deaths";
+ default:
+ return null;
+ }
+ }
+
+ private Optional<PlayerInfo> getPlayerInfo(@Nullable UUID uniqueId) {
+ return Optional.ofNullable(Main.getInstance().getDatabase().getGameData().getInfo(uniqueId));
+ }
+
+}
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 9cb17ce..0000000
--- a/src/main/java/net/tylermurphy/hideAndSeek/util/Packet.java
+++ /dev/null
@@ -1,57 +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 java.lang.reflect.InvocationTargetException;
-
-import org.bukkit.entity.Player;
-
-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;
-
-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 44a3e42..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
-}
diff --git a/src/main/java/net/tylermurphy/hideAndSeek/util/TabCompleter.java b/src/main/java/net/tylermurphy/hideAndSeek/util/TabCompleter.java
index 4c949e1..2605e7b 100644
--- a/src/main/java/net/tylermurphy/hideAndSeek/util/TabCompleter.java
+++ b/src/main/java/net/tylermurphy/hideAndSeek/util/TabCompleter.java
@@ -19,30 +19,29 @@
package net.tylermurphy.hideAndSeek.util;
+import org.bukkit.command.CommandSender;
+
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
-import net.tylermurphy.hideAndSeek.game.CommandHandler;
-import org.bukkit.command.CommandSender;
-
-public class TabCompleter{
+public class TabCompleter {
public static List<String> handleTabComplete(CommandSender sender, String[] args) {
- if(args.length == 1) {
+ if (args.length == 1) {
return new ArrayList<>(CommandHandler.COMMAND_REGISTER.keySet())
.stream()
.filter(handle -> sender.hasPermission("hideandseek."+handle.toLowerCase()) && handle.toLowerCase().startsWith(args[0].toLowerCase(Locale.ROOT)))
.collect(Collectors.toList());
- } else if(args.length > 1) {
- if(!CommandHandler.COMMAND_REGISTER.containsKey(args[0].toLowerCase())) {
+ } else if (args.length > 1) {
+ if (!CommandHandler.COMMAND_REGISTER.containsKey(args[0].toLowerCase())) {
return null;
} else {
String[] usage = CommandHandler.COMMAND_REGISTER.get(args[0].toLowerCase()).getUsage().split(" ");
- if(args.length - 2 < usage.length) {
+ if (args.length - 2 < usage.length) {
String parameter = usage[args.length-2];
- if(parameter.equals("<player>")) {
+ if (parameter.equals("<player>")) {
return null;
} else {
List<String> temp = new 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 ef65af3..0000000
--- a/src/main/java/net/tylermurphy/hideAndSeek/util/UUIDFetcher.java
+++ /dev/null
@@ -1,106 +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 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 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,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());
- }
-
- 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 96854db..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 3c555cc..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
-}