refactor placeholders

This commit is contained in:
Tyler Murphy 2022-05-09 16:19:24 -04:00
parent 5da8c8ea0a
commit 5086fb4a00
5 changed files with 150 additions and 94 deletions

View file

@ -53,7 +53,9 @@ public class Config {
exitWorld, exitWorld,
lobbyWorld, lobbyWorld,
locale, locale,
leaveServer; leaveServer,
placeholderError,
placeholderNoData;
public static Vector public static Vector
spawnPosition, spawnPosition,
@ -258,6 +260,8 @@ public class Config {
locale = config.getString("locale", "local"); locale = config.getString("locale", "local");
blockedCommands = config.getStringList("blockedCommands"); blockedCommands = config.getStringList("blockedCommands");
leaveOnEnd = config.getBoolean("leaveOnEnd"); leaveOnEnd = config.getBoolean("leaveOnEnd");
placeholderError = config.getString("placeholder.incorrect");
placeholderNoData = config.getString("placeholder.noData");
try { try {
countdownDisplay = CountdownDisplay.valueOf(config.getString("hideCountdownDisplay")); countdownDisplay = CountdownDisplay.valueOf(config.getString("hideCountdownDisplay"));
} catch (IllegalArgumentException e){ } catch (IllegalArgumentException e){

View file

@ -44,11 +44,11 @@ public class PlayerInfoTable {
+ " uuid BINARY(16) PRIMARY KEY,\n" + " uuid BINARY(16) PRIMARY KEY,\n"
+ " hider_wins int NOT NULL,\n" + " hider_wins int NOT NULL,\n"
+ " seeker_wins int NOT NULL,\n" + " seeker_wins int NOT NULL,\n"
+ " hider_games int NOT NULL\n" + " hider_games int NOT NULL,\n"
+ " seeker_games int NOT NULL\n" + " seeker_games int NOT NULL,\n"
+ " hider_kills int NOT NULL\n" + " hider_kills int NOT NULL,\n"
+ " seeker_kills int NOT NULL\n" + " seeker_kills int NOT NULL,\n"
+ " hider_deaths int NOT NULL\n" + " hider_deaths int NOT NULL,\n"
+ " seeker_deaths int NOT NULL\n" + " seeker_deaths int NOT NULL\n"
+ ");"; + ");";
@ -123,6 +123,38 @@ public class PlayerInfoTable {
return new PlayerInfo(uuid, 0, 0, 0, 0, 0, 0, 0, 0); return new PlayerInfo(uuid, 0, 0, 0, 0, 0, 0, 0, 0);
} }
@Nullable
public PlayerInfo getInfoRanking(String order, int place){
String sql = "SELECT * FROM hs_data ORDER BY "+order+" DESC LIMIT 1 OFFSET ?;";
try(Connection connection = Database.connect(); PreparedStatement statement = connection.prepareStatement(sql)){
statement.setInt(1, place-1);
ResultSet rs = statement.executeQuery();
if(rs.next()){
UUID uuid = decodeUUID(rs.getBytes("uuid"));
PlayerInfo info = new PlayerInfo(
uuid,
rs.getInt("hider_wins"),
rs.getInt("seeker_wins"),
rs.getInt("hider_games"),
rs.getInt("seeker_games"),
rs.getInt("hider_kills"),
rs.getInt("seeker_kills"),
rs.getInt("hider_deaths"),
rs.getInt("seeker_deaths")
);
rs.close();
connection.close();
CACHE.put(uuid, info);
return info;
}
rs.close();
} catch (SQLException e){
Main.plugin.getLogger().severe("SQL Error: " + e.getMessage());
e.printStackTrace();
}
return null;
}
@Nullable @Nullable
public List<PlayerInfo> getInfoPage(int page){ public List<PlayerInfo> getInfoPage(int page){
String sql = "SELECT * FROM hs_data ORDER BY (hider_wins + seeker_wins) DESC LIMIT 10 OFFSET ?;"; String sql = "SELECT * FROM hs_data ORDER BY (hider_wins + seeker_wins) DESC LIMIT 10 OFFSET ?;";

View file

@ -1,21 +1,20 @@
package net.tylermurphy.hideAndSeek.util; package net.tylermurphy.hideAndSeek.util;
import static net.tylermurphy.hideAndSeek.configuration.Config.*;
import me.clip.placeholderapi.expansion.PlaceholderExpansion; import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import net.tylermurphy.hideAndSeek.database.Database; import net.tylermurphy.hideAndSeek.database.Database;
import net.tylermurphy.hideAndSeek.database.PlayerInfo; import net.tylermurphy.hideAndSeek.database.PlayerInfo;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.lang.reflect.Method;
import java.util.Locale;
import java.util.UUID; import java.util.UUID;
public class PAPIExpansion extends PlaceholderExpansion { public class PAPIExpansion extends PlaceholderExpansion {
@Override @Override
public @NotNull String getIdentifier() { public @NotNull String getIdentifier() {
return "kenshinshideandseek"; return "hs";
} }
@Override @Override
@ -35,94 +34,95 @@ public class PAPIExpansion extends PlaceholderExpansion {
@Override @Override
public String onRequest(OfflinePlayer player, @NotNull String params) { public String onRequest(OfflinePlayer player, @NotNull String params) {
switch (params.toLowerCase(Locale.ROOT)) { String[] args = params.split("_");
case "hs_stats_total-wins": System.out.println();
return hs_stats_total_wins(player.getUniqueId()); if(args.length < 1) return null;
case "hs_stats_hider-wins": if(args[0].equals("stats") && args.length == 2){
return hs_stats_hider_wins(player.getUniqueId()); PlayerInfo info = Database.playerInfo.getInfo(player.getUniqueId());
case "hs_stats_seeker-wins": return getValue(info, args[1]);
return hs_stats_seeker_wins(player.getUniqueId()); } else if(args[0].equals("stats") && args.length == 3){
case "hs_stats_total-games": UUID uuid;
return hs_stats_total_games(player.getUniqueId()); try { uuid = UUIDFetcher.getUUID(args[2]); } catch (Exception e) { return placeholderError; }
case "hs_stats_hider-games": PlayerInfo info = Database.playerInfo.getInfo(uuid);
return hs_stats_hider_games(player.getUniqueId()); return getValue(info, args[1]);
case "hs_stats_seeker-games": } else if(args[0].equals("rank") && args.length == 3){
return hs_stats_seeker_games(player.getUniqueId()); int place;
case "hs_stats_total-kills": try { place = Integer.parseInt(args[2]); } catch (NumberFormatException e) { return placeholderError; }
return hs_stats_total_kills(player.getUniqueId()); if(place < 1) { return placeholderError; }
case "hs_stats_hider-kills": if(getRanking(args[1]) == null) { return placeholderError; }
return hs_stats_hider_kills(player.getUniqueId()); PlayerInfo info = Database.playerInfo.getInfoRanking(getRanking(args[1]), place);
case "hs_stats_seeker-kills": if(info == null) return placeholderNoData;
return hs_stats_seeker_kills(player.getUniqueId()); if(args[0].equals("rankStat")){
case "hs_stats_total-deaths": return getValue(info, args[1]);
return hs_stats_total_deaths(player.getUniqueId()); } else {
case "hs_stats_hider-deaths": return UUIDFetcher.getPlayer(info.uuid).getName();
return hs_stats_hider_deaths(player.getUniqueId()); }
case "hs_stats_seeker-deaths": }
return hs_stats_seeker_deaths(player.getUniqueId()); return null;
}
private String getValue(PlayerInfo info, String query){
if(query == null) return null;
switch (query) {
case "total-wins":
return String.valueOf(info.hider_wins + info.seeker_wins);
case "hider-wins":
return String.valueOf(info.hider_wins);
case "seeker-wins":
return String.valueOf(info.seeker_wins);
case "total-games":
return String.valueOf(info.hider_games + info.seeker_games);
case "hider-games":
return String.valueOf(info.hider_games);
case "seeker-games":
return String.valueOf(info.seeker_games);
case "total-kills":
return String.valueOf(info.hider_kills + info.seeker_kills);
case "hider-kills":
return String.valueOf(info.hider_kills);
case "seeker-kills":
return String.valueOf(info.seeker_kills);
case "total-deaths":
return String.valueOf(info.hider_deaths + info.seeker_deaths);
case "hider-deaths":
return String.valueOf(info.hider_deaths);
case "seeker-deaths":
return String.valueOf(info.seeker_deaths);
default: default:
return null; return null;
} }
} }
private String hs_stats_total_wins(UUID uuid){ private String getRanking(String query){
PlayerInfo info = Database.playerInfo.getInfo(uuid); if(query == null) return null;
return String.valueOf(info.hider_wins + info.seeker_wins); switch (query) {
} case "total-wins":
return "(hider_wins + seeker_wins)";
private String hs_stats_hider_wins(UUID uuid){ case "hider-wins":
PlayerInfo info = Database.playerInfo.getInfo(uuid); return "hider_wins";
return String.valueOf(info.hider_wins); case "seeker-wins":
} return "seeker_wins";
case "total-games":
private String hs_stats_seeker_wins(UUID uuid){ return "(hider_games + seeker_games)";
PlayerInfo info = Database.playerInfo.getInfo(uuid); case "hider-games":
return String.valueOf(info.seeker_wins); return "hider_games";
} case "seeker-games":
return "seeker_games";
private String hs_stats_total_games(UUID uuid){ case "total-kills":
PlayerInfo info = Database.playerInfo.getInfo(uuid); return "(hider_kills + seeker_kills)";
return String.valueOf(info.hider_games + info.seeker_games); case "hider-kills":
} return "hider_kills";
case "seeker-kills":
private String hs_stats_hider_games(UUID uuid){ return "seeker_kills";
PlayerInfo info = Database.playerInfo.getInfo(uuid); case "total-deaths":
return String.valueOf(info.hider_games); return "(hider_deaths + seeker_deaths)";
} case "hider-deaths":
return "hider_deaths";
private String hs_stats_seeker_games(UUID uuid){ case "seeker-deaths":
PlayerInfo info = Database.playerInfo.getInfo(uuid); return "seeker_deaths";
return String.valueOf(info.seeker_games); default:
} return null;
}
private String hs_stats_total_kills(UUID uuid){
PlayerInfo info = Database.playerInfo.getInfo(uuid);
return String.valueOf(info.hider_kills + info.seeker_kills);
}
private String hs_stats_hider_kills(UUID uuid){
PlayerInfo info = Database.playerInfo.getInfo(uuid);
return String.valueOf(info.hider_kills);
}
private String hs_stats_seeker_kills(UUID uuid){
PlayerInfo info = Database.playerInfo.getInfo(uuid);
return String.valueOf(info.seeker_kills);
}
private String hs_stats_total_deaths(UUID uuid){
PlayerInfo info = Database.playerInfo.getInfo(uuid);
return String.valueOf(info.hider_deaths + info.seeker_deaths);
}
private String hs_stats_hider_deaths(UUID uuid){
PlayerInfo info = Database.playerInfo.getInfo(uuid);
return String.valueOf(info.hider_deaths);
}
private String hs_stats_seeker_deaths(UUID uuid){
PlayerInfo info = Database.playerInfo.getInfo(uuid);
return String.valueOf(info.seeker_deaths);
} }
} }

View file

@ -20,6 +20,9 @@
package net.tylermurphy.hideAndSeek.util; package net.tylermurphy.hideAndSeek.util;
import net.tylermurphy.hideAndSeek.Main; import net.tylermurphy.hideAndSeek.Main;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.InputStreamReader; import java.io.InputStreamReader;
@ -33,12 +36,16 @@ import java.util.UUID;
public final class UUIDFetcher { public final class UUIDFetcher {
private static final Map<String,UUID> CACHE = new HashMap<>(); 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 final String UUID_URL = "https://api.mojang.com/users/profiles/minecraft/";
private static int cacheTask; private static int cacheTask;
public static void init(){ public static void init(){
cacheTask = Main.plugin.getServer().getScheduler().scheduleSyncRepeatingTask(Main.plugin, CACHE::clear,600*20, 600*20); cacheTask = Main.plugin.getServer().getScheduler().scheduleSyncRepeatingTask(Main.plugin, () -> {
CACHE.clear();
PLAYER_CACHE.clear();
}, 600 * 20, 600 * 20);
} }
public static void cleanup(){ public static void cleanup(){
@ -66,6 +73,13 @@ public final class UUIDFetcher {
return 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) { private static void readData(String toRead, StringBuilder result) {
for (int i = toRead.length() - 3; i >= 0; i--) { for (int i = toRead.length() - 3; i >= 0; i--) {
if (toRead.charAt(i) != '"') { if (toRead.charAt(i) != '"') {

View file

@ -142,6 +142,12 @@ prefix:
gameover: '&aGame Over > &f' gameover: '&aGame Over > &f'
warning: '&cWarning > &f' warning: '&cWarning > &f'
# This plugin has support for custom placeholders with PlaceholderAPI. If you make a mistake when creating a placeholder,
# the plugin may tell you an error. Below you can change what could appear as an error message.
placeholder:
incorrect: "{Error}"
noData: "{No Data}"
# This is the section if you want a standard "waiting for players" lobby. You can specify # This is the section if you want a standard "waiting for players" lobby. You can specify
# the standard countdown length under [countdown] (min 10 seconds). Then once the lobby gets to a size specified # the standard countdown length under [countdown] (min 10 seconds). Then once the lobby gets to a size specified
# by [changeCountdown], the timer will automatically go to 10 seconds. [min] is the minimum players # by [changeCountdown], the timer will automatically go to 10 seconds. [min] is the minimum players