Merge pull request #63 from bobby29831/1.5.0

Clean up placeholder expansion (readability)
This commit is contained in:
Tyler Murphy 2022-05-24 15:17:39 -04:00 committed by GitHub
commit a808ec5706
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 38 deletions

View file

@ -58,7 +58,8 @@ public class GameDataTable {
} }
@Nullable @Nullable
public PlayerInfo getInfo(UUID uuid) { public PlayerInfo getInfo(@Nullable UUID uuid) {
if (uuid == null) return null;
if(CACHE.containsKey(uuid)) return CACHE.get(uuid); if(CACHE.containsKey(uuid)) return CACHE.get(uuid);
String sql = "SELECT * FROM hs_data WHERE uuid = ?;"; String sql = "SELECT * FROM hs_data WHERE uuid = ?;";
try(Connection connection = database.connect(); PreparedStatement statement = connection.prepareStatement(sql)) { try(Connection connection = database.connect(); PreparedStatement statement = connection.prepareStatement(sql)) {

View file

@ -6,7 +6,9 @@ import net.tylermurphy.hideAndSeek.database.Database;
import net.tylermurphy.hideAndSeek.database.util.PlayerInfo; import net.tylermurphy.hideAndSeek.database.util.PlayerInfo;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import static net.tylermurphy.hideAndSeek.configuration.Config.placeholderError; import static net.tylermurphy.hideAndSeek.configuration.Config.placeholderError;
@ -38,45 +40,32 @@ public class PAPIExpansion extends PlaceholderExpansion {
public String onRequest(OfflinePlayer player, @NotNull String params) { public String onRequest(OfflinePlayer player, @NotNull String params) {
Database database = Main.getInstance().getDatabase(); Database database = Main.getInstance().getDatabase();
String[] args = params.split("_"); String[] args = params.split("_");
if (args.length < 1) return null; if (args.length < 1) return null;
if (args[0].equals("stats") && (args.length == 2 || args.length == 3)) { if ((args.length == 2 || args.length == 3) && (args[0].equals("stats") || args[0].equals("rank-place"))) {
PlayerInfo info; Optional<PlayerInfo> info = this.getPlayerInfo(args.length == 2 ? player.getUniqueId() : database.getNameData().getUUID(args[2]));
if(args.length == 2) { if (info.isPresent()) {
info = database.getGameData().getInfo(player.getUniqueId()); switch (args[0]) {
} else { case "stats":
UUID uuid; return getValue(info.get(), args[1]);
try { uuid = Main.getInstance().getDatabase().getNameData().getUUID(args[2]); } catch (Exception e) { return placeholderError; } case "rank-place":
info = database.getGameData().getInfo(uuid); 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();
}
} }
if (info == null) return placeholderNoData; }
return getValue(info, args[1]);
} else if ((args[0].equals("rank-score") || args[0].equals("rank-name") ) && args.length == 3) { if ((args[0].equals("rank-score") || args[0].equals("rank-name")) && args.length == 3) {
int place; int place = Integer.parseInt(args[2]);
try { place = Integer.parseInt(args[2]); } catch (NumberFormatException e) { return placeholderError; } if (place < 1 || getRanking(args[1]) == null) return placeholderError;
if (place < 1) { return placeholderError; }
if (getRanking(args[1]) == null) { return placeholderError; }
PlayerInfo info = database.getGameData().getInfoRanking(getRanking(args[1]), place); PlayerInfo info = database.getGameData().getInfoRanking(getRanking(args[1]), place);
if (info == null) return placeholderNoData; if (info == null) return placeholderNoData;
if (args[0].equals("rank-score")) {
return getValue(info, args[1]); return args[0].equals("rank-score") ? getValue(info, args[1]) : Main.getInstance().getServer().getOfflinePlayer(info.getUniqueId()).getName();
} else {
return Main.getInstance().getServer().getOfflinePlayer(info.getUniqueId()).getName();
}
} else if (args[0].equals("rank-place") && (args.length == 2 || args.length == 3)) {
if (getRanking(args[1]) == null) { return placeholderError; }
PlayerInfo info;
if(args.length == 2){
info = database.getGameData().getInfo(player.getUniqueId());
} else {
UUID uuid;
try { uuid = Main.getInstance().getDatabase().getNameData().getUUID(args[2]); } catch (Exception e) { return placeholderError; }
info = database.getGameData().getInfo(uuid);
}
if (info == null) return placeholderNoData;
if (getValue(info, args[1]).equals("0")) { return "-"; }
Integer count = database.getGameData().getRanking(getRanking(args[1]), player.getUniqueId());
if (count == null) { return placeholderNoData; }
return count.toString();
} }
return null; return null;
} }
@ -113,8 +102,7 @@ public class PAPIExpansion extends PlaceholderExpansion {
} }
} }
private String getRanking(String query) { private String getRanking(@NotNull String query) {
if (query == null) return null;
switch (query) { switch (query) {
case "total-wins": case "total-wins":
return "(hider_wins + seeker_wins)"; return "(hider_wins + seeker_wins)";
@ -145,4 +133,8 @@ public class PAPIExpansion extends PlaceholderExpansion {
} }
} }
private Optional<PlayerInfo> getPlayerInfo(@Nullable UUID uniqueId) {
return Optional.ofNullable(Main.getInstance().getDatabase().getGameData().getInfo(uniqueId));
}
} }