Renamed method in LanguageKeyHandler for consistency, did some testing for database stuff

This commit is contained in:
Artemis-the-gr8 2023-06-14 13:17:10 +02:00
parent 0434bc644c
commit 62c13f67d9
6 changed files with 236 additions and 124 deletions

View File

@ -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()) {

View File

@ -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<MyStatistic> getStats() {
EnumHandler enumHandler = EnumHandler.getInstance();
List<MyStatistic> 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<MySubStatistic> getSubStats() {
EnumHandler enumHandler = EnumHandler.getInstance();
List<MySubStatistic> 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<MyPlayer> getPlayers() {
OfflinePlayerHandler offlinePlayerHandler = OfflinePlayerHandler.getInstance();
List <MyPlayer> 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;
}
}

View File

@ -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<MyStatistic, Integer> 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<MySubStatistic, Integer> 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<MySubStatistic, Integer> 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<MySubStatistic, Integer> 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<MyStatistic> getStats() {
List<MyStatistic> 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<MySubStatistic> getSubStats() {
List<MySubStatistic> 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<MyPlayer> getPlayers() {
List<MyPlayer> 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()));
}
}

View File

@ -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 "";

View File

@ -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;
}

View File

@ -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);
}