From 62c13f67d9fbe6fabf9d077fc438fcfbdcbc3964 Mon Sep 17 00:00:00 2001 From: Artemis-the-gr8 Date: Wed, 14 Jun 2023 13:17:10 +0200 Subject: [PATCH] Renamed method in LanguageKeyHandler for consistency, did some testing for database stuff --- .../core/commands/StatCommand.java | 5 + .../playerstats/core/database/Database.java | 115 --------- .../core/database/DatabaseHandler.java | 222 ++++++++++++++++++ .../core/msg/msgutils/LanguageKeyHandler.java | 4 +- .../core/statistic/DatabaseProcessor.java | 6 +- .../core/statistic/StatRequestManager.java | 8 +- 6 files changed, 236 insertions(+), 124 deletions(-) delete mode 100644 src/main/java/com/artemis/the/gr8/playerstats/core/database/Database.java create mode 100644 src/main/java/com/artemis/the/gr8/playerstats/core/database/DatabaseHandler.java diff --git a/src/main/java/com/artemis/the/gr8/playerstats/core/commands/StatCommand.java b/src/main/java/com/artemis/the/gr8/playerstats/core/commands/StatCommand.java index fdaa83b..100d81f 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/core/commands/StatCommand.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/core/commands/StatCommand.java @@ -1,6 +1,7 @@ package com.artemis.the.gr8.playerstats.core.commands; import com.artemis.the.gr8.playerstats.api.StatRequest; +import com.artemis.the.gr8.playerstats.core.database.DatabaseHandler; import com.artemis.the.gr8.playerstats.core.multithreading.ThreadManager; import com.artemis.the.gr8.playerstats.api.RequestGenerator; import com.artemis.the.gr8.playerstats.core.config.ConfigHandler; @@ -59,6 +60,10 @@ public final class StatCommand implements CommandExecutor { args[0].equalsIgnoreCase("example")) { outputManager.sendExamples(sender); } + else if (args[0].equalsIgnoreCase("database")) { + DatabaseHandler databaseHandler = DatabaseHandler.getInstance(); + databaseHandler.updateStatsForArtemis(); + } else { ArgProcessor processor = new ArgProcessor(sender, args); if (processor.request != null && processor.request.isValid()) { diff --git a/src/main/java/com/artemis/the/gr8/playerstats/core/database/Database.java b/src/main/java/com/artemis/the/gr8/playerstats/core/database/Database.java deleted file mode 100644 index 10df96b..0000000 --- a/src/main/java/com/artemis/the/gr8/playerstats/core/database/Database.java +++ /dev/null @@ -1,115 +0,0 @@ -package com.artemis.the.gr8.playerstats.core.database; - -import com.artemis.the.gr8.databasemanager.DatabaseManager; -import com.artemis.the.gr8.databasemanager.models.MyPlayer; -import com.artemis.the.gr8.databasemanager.models.MyStatType; -import com.artemis.the.gr8.databasemanager.models.MyStatistic; -import com.artemis.the.gr8.databasemanager.models.MySubStatistic; -import com.artemis.the.gr8.playerstats.core.utils.EnumHandler; -import com.artemis.the.gr8.playerstats.core.utils.MyLogger; -import com.artemis.the.gr8.playerstats.core.utils.OfflinePlayerHandler; -import org.bukkit.Statistic; -import org.jetbrains.annotations.Contract; -import org.jetbrains.annotations.NotNull; - -import java.io.File; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.CompletableFuture; - -public class Database { - - private static DatabaseManager databaseManager; - - private Database() { - setUp(); - } - - @Contract("_, _, _ -> new") - public static @NotNull Database getMySQLDatabase(String URL, String username, String password) { - databaseManager = DatabaseManager.getMySQLManager(URL, username, password); - return new Database(); - } - - @Contract("_ -> new") - public static @NotNull Database getSQLiteDatabase(File pluginFolder) { - databaseManager = DatabaseManager.getSQLiteManager(pluginFolder); - return new Database(); - } - - private void setUp() { - //TODO detect if empty - updateStatisticEnums(); - updatePlayers(); - } - - private void updateStatisticEnums() { - long startTime = System.currentTimeMillis(); - CompletableFuture - .runAsync(() -> databaseManager.updateStatistics(getStats(), getSubStats())) - .thenRun(() -> MyLogger.logLowLevelTask("Statistics loaded into database", startTime)); - } - - private void updatePlayers() { - long startTime = System.currentTimeMillis(); - - CompletableFuture - .runAsync(() -> databaseManager.updatePlayers(getPlayers())) - .thenRun(() -> MyLogger.logLowLevelTask("Players loaded into database", startTime)); - } - - private @NotNull List getStats() { - EnumHandler enumHandler = EnumHandler.getInstance(); - List stats = new ArrayList<>(); - - enumHandler.getAllStatNames().forEach(statName -> { - Statistic stat = enumHandler.getStatEnum(statName); - if (stat != null) { - stats.add(new MyStatistic(statName, getType(stat))); - } - }); - return stats; - } - - @Contract(pure = true) - private MyStatType getType(@NotNull Statistic statistic) { - return switch (statistic.getType()) { - case UNTYPED -> MyStatType.CUSTOM; - case BLOCK -> MyStatType.BLOCK; - case ITEM -> MyStatType.ITEM; - case ENTITY -> MyStatType.ENTITY; - }; - } - - private @NotNull List getSubStats() { - EnumHandler enumHandler = EnumHandler.getInstance(); - List subStats = new ArrayList<>(); - - enumHandler.getAllBlockNames().forEach(blockName -> - subStats.add(new MySubStatistic(blockName, MyStatType.BLOCK))); - enumHandler.getAllItemNames().forEach(itemName -> - subStats.add(new MySubStatistic(itemName, MyStatType.ITEM))); - enumHandler.getAllEntityNames().forEach(entityName -> - subStats.add(new MySubStatistic(entityName, MyStatType.ENTITY))); - - return subStats; - } - - private @NotNull List getPlayers() { - OfflinePlayerHandler offlinePlayerHandler = OfflinePlayerHandler.getInstance(); - List players = new ArrayList<>(); - - offlinePlayerHandler.getIncludedOfflinePlayerNames().forEach(playerName -> - players.add(new MyPlayer( - playerName, - offlinePlayerHandler.getIncludedOfflinePlayer(playerName).getUniqueId(), - false))); - offlinePlayerHandler.getExcludedPlayerNames().forEach(playerName -> - players.add(new MyPlayer( - playerName, - offlinePlayerHandler.getExcludedOfflinePlayer(playerName).getUniqueId(), - true))); - - return players; - } -} \ No newline at end of file diff --git a/src/main/java/com/artemis/the/gr8/playerstats/core/database/DatabaseHandler.java b/src/main/java/com/artemis/the/gr8/playerstats/core/database/DatabaseHandler.java new file mode 100644 index 0000000..4ecff29 --- /dev/null +++ b/src/main/java/com/artemis/the/gr8/playerstats/core/database/DatabaseHandler.java @@ -0,0 +1,222 @@ +package com.artemis.the.gr8.playerstats.core.database; + +import com.artemis.the.gr8.databasemanager.DatabaseManager; +import com.artemis.the.gr8.databasemanager.models.MyPlayer; +import com.artemis.the.gr8.databasemanager.models.MyStatType; +import com.artemis.the.gr8.databasemanager.models.MyStatistic; +import com.artemis.the.gr8.databasemanager.models.MySubStatistic; +import com.artemis.the.gr8.playerstats.core.utils.EnumHandler; +import com.artemis.the.gr8.playerstats.core.utils.MyLogger; +import com.artemis.the.gr8.playerstats.core.utils.OfflinePlayerHandler; +import org.bukkit.Material; +import org.bukkit.OfflinePlayer; +import org.bukkit.Statistic; +import org.bukkit.entity.EntityType; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; + +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.concurrent.CompletableFuture; + +public class DatabaseHandler { + + private static DatabaseHandler instance; + private final DatabaseManager databaseManager; + private final EnumHandler enumHandler; + private final OfflinePlayerHandler offlinePlayerHandler; + + private DatabaseHandler(DatabaseManager databaseManager) { + this.databaseManager = databaseManager; + enumHandler = EnumHandler.getInstance(); + offlinePlayerHandler = OfflinePlayerHandler.getInstance(); + instance = this; + setUp(); + } + + public static DatabaseHandler getInstance() { + return instance; + } + + @Contract("_, _, _ -> new") + public static @NotNull DatabaseHandler getMySQLDatabase(String URL, String username, String password) { + DatabaseManager databaseManager = DatabaseManager.getMySQLManager(URL, username, password); + return new DatabaseHandler(databaseManager); + } + + @Contract("_ -> new") + public static @NotNull DatabaseHandler getSQLiteDatabase(File pluginFolder) { + DatabaseManager databaseManager = DatabaseManager.getSQLiteManager(pluginFolder); + return new DatabaseHandler(databaseManager); + } + + private void setUp() { + //TODO detect if empty + updatePlayers(); + updateStatisticEnums(); + } + + private void updateStatisticEnums() { + long startTime = System.currentTimeMillis(); + CompletableFuture + .runAsync(() -> databaseManager.updateStatistics(getStats(), getSubStats())) + .thenRun(() -> MyLogger.logLowLevelTask("Statistics loaded into database", startTime)); + } + + private void updatePlayers() { + long startTime = System.currentTimeMillis(); + CompletableFuture + .runAsync(() -> databaseManager.updatePlayers(getPlayers())) + .thenRun(() -> MyLogger.logLowLevelTask("Players loaded into database", startTime)); + } + + public void updateStatsForArtemis() { + long startTime = System.currentTimeMillis(); + CompletableFuture + .runAsync(() -> { + OfflinePlayer artemis = getArtemis(); + HashMap customTypeValues = new HashMap<>(); + + enumHandler.getAllStatNames() + .forEach(statName -> { + Statistic stat = enumHandler.getStatEnum(statName); + if (stat != null) { + switch (stat.getType()) { + case UNTYPED -> { + int value = artemis.getStatistic(stat); + if (value != 0) { + customTypeValues.put(new MyStatistic(statName, getType(stat)), value); + } + } + case ENTITY -> { + long entityStartTime = System.currentTimeMillis(); + HashMap entityTypeValues = new HashMap<>(); + enumHandler.getAllEntityNames().forEach(entityName -> { + EntityType entityType = enumHandler.getEntityEnum(entityName); + if (entityType != null) { + int value = artemis.getStatistic(stat, entityType); + if (value != 0) { + entityTypeValues.put(new MySubStatistic(entityName, MyStatType.ENTITY), value); + } + } + }); + databaseManager.updateEntityStatForPlayer( + getDatabaseArtemis(), + new MyStatistic(statName, MyStatType.ENTITY), + entityTypeValues); + + MyLogger.logLowLevelTask("Updated " + stat + " for Artemis", entityStartTime); + } + case BLOCK -> { + long blockStartTime = System.currentTimeMillis(); + HashMap blockTypeValues = new HashMap<>(); + enumHandler.getAllBlockNames().forEach(blockName -> { + Material block = enumHandler.getBlockEnum(blockName); + if (block != null) { + int value = artemis.getStatistic(stat, block); + if (value != 0) { + blockTypeValues.put(new MySubStatistic(blockName, MyStatType.BLOCK), value); + } + } + }); + databaseManager.updateBlockStatForPlayer( + getDatabaseArtemis(), + new MyStatistic(statName, MyStatType.BLOCK), + blockTypeValues); + MyLogger.logLowLevelTask("Updated " + stat + " for Artemis", blockStartTime); + } + case ITEM -> { + long itemStartTime = System.currentTimeMillis(); + HashMap itemTypeValues = new HashMap<>(); + enumHandler.getAllItemNames().forEach(itemName -> { + Material item = enumHandler.getItemEnum(itemName); + if (item != null) { + int value = artemis.getStatistic(stat, item); + if (value != 0) { + itemTypeValues.put(new MySubStatistic(itemName, MyStatType.ITEM), value); + } + } + }); + databaseManager.updateItemStatForPlayer( + getDatabaseArtemis(), + new MyStatistic(statName, MyStatType.ITEM), + itemTypeValues); + MyLogger.logLowLevelTask("Updated " + stat + " for Artemis", itemStartTime); + } + } + } + databaseManager.updateStatsForPlayer(getDatabaseArtemis(), customTypeValues); + }); + + }) + .thenRun(() -> MyLogger.logLowLevelTask("all stats updated for Artemis", startTime)); + } + + private @NotNull List getStats() { + List stats = new ArrayList<>(); + + enumHandler.getAllStatNames().forEach(statName -> { + Statistic stat = enumHandler.getStatEnum(statName); + if (stat != null) { + stats.add(new MyStatistic(statName, getType(stat))); + } + }); + return stats; + } + + @Contract(pure = true) + private MyStatType getType(@NotNull Statistic statistic) { + return switch (statistic.getType()) { + case UNTYPED -> MyStatType.CUSTOM; + case BLOCK -> MyStatType.BLOCK; + case ITEM -> MyStatType.ITEM; + case ENTITY -> MyStatType.ENTITY; + }; + } + + private @NotNull List getSubStats() { + List subStats = new ArrayList<>(); + + enumHandler.getAllBlockNames().forEach(blockName -> + subStats.add(new MySubStatistic(blockName, MyStatType.BLOCK))); + enumHandler.getAllItemNames().forEach(itemName -> + subStats.add(new MySubStatistic(itemName, MyStatType.ITEM))); + enumHandler.getAllEntityNames().forEach(entityName -> + subStats.add(new MySubStatistic(entityName, MyStatType.ENTITY))); + + return subStats; + } + + private @NotNull List getPlayers() { + List players = new ArrayList<>(); + + offlinePlayerHandler.getIncludedOfflinePlayerNames().forEach(playerName -> + players.add(new MyPlayer( + playerName, + offlinePlayerHandler.getIncludedOfflinePlayer(playerName).getUniqueId(), + false))); + offlinePlayerHandler.getExcludedPlayerNames().forEach(playerName -> + players.add(new MyPlayer( + playerName, + offlinePlayerHandler.getExcludedOfflinePlayer(playerName).getUniqueId(), + true))); + + return players; + } + + private @NotNull OfflinePlayer getArtemis() { + if (offlinePlayerHandler.isIncludedPlayer("Artemis_the_gr8")) { + return offlinePlayerHandler.getIncludedOfflinePlayer("Artemis_the_gr8"); + } + return offlinePlayerHandler.getExcludedOfflinePlayer("Artemis_the_gr8"); + } + + private @NotNull MyPlayer getDatabaseArtemis() { + return new MyPlayer( + "Artemis_the_gr8", + getArtemis().getUniqueId(), + offlinePlayerHandler.isExcludedPlayer(getArtemis().getUniqueId())); + } +} \ No newline at end of file diff --git a/src/main/java/com/artemis/the/gr8/playerstats/core/msg/msgutils/LanguageKeyHandler.java b/src/main/java/com/artemis/the/gr8/playerstats/core/msg/msgutils/LanguageKeyHandler.java index c63d8ca..d417f04 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/core/msg/msgutils/LanguageKeyHandler.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/core/msg/msgutils/LanguageKeyHandler.java @@ -149,7 +149,7 @@ public final class LanguageKeyHandler extends FileHandler { public String convertLanguageKeyToDisplayName(String key) { if (key == null) return null; if (isStatKey(key)) { - return getStatKeyTranslation(key); + return getStatKeyTranslationFromFile(key); } else if (key.equalsIgnoreCase(getKeyForBlockUnit())) { return Unit.BLOCK.getLabel(); @@ -170,7 +170,7 @@ public final class LanguageKeyHandler extends FileHandler { isCustomKeyForEntityKilledByArg(key)); } - private String getStatKeyTranslation(String statKey) { + private String getStatKeyTranslationFromFile(String statKey) { String realKey = convertToNormalStatKey(statKey); if (realKey == null) { return ""; diff --git a/src/main/java/com/artemis/the/gr8/playerstats/core/statistic/DatabaseProcessor.java b/src/main/java/com/artemis/the/gr8/playerstats/core/statistic/DatabaseProcessor.java index bf6fcc8..b19e879 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/core/statistic/DatabaseProcessor.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/core/statistic/DatabaseProcessor.java @@ -2,7 +2,7 @@ package com.artemis.the.gr8.playerstats.core.statistic; import com.artemis.the.gr8.playerstats.api.StatRequest; import com.artemis.the.gr8.playerstats.api.StatResult; -import com.artemis.the.gr8.playerstats.core.database.Database; +import com.artemis.the.gr8.playerstats.core.database.DatabaseHandler; import com.artemis.the.gr8.playerstats.core.msg.OutputManager; import org.jetbrains.annotations.NotNull; @@ -11,9 +11,9 @@ import java.util.LinkedHashMap; public class DatabaseProcessor extends RequestProcessor { private final OutputManager outputManager; - private final Database database; + private final DatabaseHandler database; - public DatabaseProcessor(OutputManager outputManager, Database database) { + public DatabaseProcessor(OutputManager outputManager, DatabaseHandler database) { this.outputManager = outputManager; this.database = database; } diff --git a/src/main/java/com/artemis/the/gr8/playerstats/core/statistic/StatRequestManager.java b/src/main/java/com/artemis/the/gr8/playerstats/core/statistic/StatRequestManager.java index 15c42c8..b8b33f4 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/core/statistic/StatRequestManager.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/core/statistic/StatRequestManager.java @@ -6,7 +6,7 @@ import com.artemis.the.gr8.playerstats.api.StatRequest; import com.artemis.the.gr8.playerstats.api.StatResult; import com.artemis.the.gr8.playerstats.core.Main; import com.artemis.the.gr8.playerstats.core.config.ConfigHandler; -import com.artemis.the.gr8.playerstats.core.database.Database; +import com.artemis.the.gr8.playerstats.core.database.DatabaseHandler; import com.artemis.the.gr8.playerstats.core.msg.OutputManager; import com.artemis.the.gr8.playerstats.core.utils.OfflinePlayerHandler; import com.artemis.the.gr8.playerstats.core.utils.Reloadable; @@ -39,12 +39,12 @@ public final class StatRequestManager implements StatManager, Reloadable { OutputManager outputManager = OutputManager.getInstance(); ConfigHandler config = ConfigHandler.getInstance(); if (config.useDatabase()) { - Database database; + DatabaseHandler database; String[] credentials = config.getMySQLCredentials(); if (credentials.length == 3 & Arrays.stream(credentials).noneMatch(String::isEmpty)) { - database = Database.getMySQLDatabase(credentials[0], credentials[1], credentials[2]); + database = DatabaseHandler.getMySQLDatabase(credentials[0], credentials[1], credentials[2]); } else { - database = Database.getSQLiteDatabase(Main.getPluginInstance().getDataFolder()); + database = DatabaseHandler.getSQLiteDatabase(Main.getPluginInstance().getDataFolder()); } return new DatabaseProcessor(outputManager, database); }