From 34eaefed5224c0f7b546c3a5a475cdef19e31978 Mon Sep 17 00:00:00 2001 From: Grzybol Date: Sun, 26 Nov 2023 17:07:09 +0100 Subject: [PATCH 1/5] - added /be ban command, it "should" delete the player's interactions from database, set his points to 1000 in all rankings and redistribute the points from his interactions to the players he killed/died from - added databes for storing players interactions in all rankings - added /be reload command for reloading the log levels --- pom.xml | 2 +- .../mine/game/betterelo/BetterElo.java | 17 +- .../mine/game/betterelo/BetterEloCommand.java | 87 ++++++- .../game/betterelo/BetterRanksCheaters.java | 54 +++++ .../game/betterelo/CheaterCheckScheduler.java | 31 +++ .../game/betterelo/CheaterInteractions.java | 31 +++ .../mine/game/betterelo/DataManager.java | 1 + .../betterbox/mine/game/betterelo/Event.java | 127 +++++----- .../game/betterelo/ExtendedConfigManager.java | 34 ++- .../game/betterelo/PlayerKillDatabase.java | 225 ++++++++++++++++++ .../mine/game/betterelo/PluginLogger.java | 22 +- src/main/resources/plugin.yml | 6 + 12 files changed, 565 insertions(+), 72 deletions(-) create mode 100644 src/main/java/betterbox/mine/game/betterelo/BetterRanksCheaters.java create mode 100644 src/main/java/betterbox/mine/game/betterelo/CheaterCheckScheduler.java create mode 100644 src/main/java/betterbox/mine/game/betterelo/CheaterInteractions.java create mode 100644 src/main/java/betterbox/mine/game/betterelo/PlayerKillDatabase.java diff --git a/pom.xml b/pom.xml index dcb80cc..32691f5 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ betterbox.mine.game BetterElo - 2.1.16-SNAPSHOT + 3.0.0-SNAPSHOT jar BetterElo diff --git a/src/main/java/betterbox/mine/game/betterelo/BetterElo.java b/src/main/java/betterbox/mine/game/betterelo/BetterElo.java index b9e7223..8fe4870 100644 --- a/src/main/java/betterbox/mine/game/betterelo/BetterElo.java +++ b/src/main/java/betterbox/mine/game/betterelo/BetterElo.java @@ -16,19 +16,23 @@ public final class BetterElo extends JavaPlugin { private PluginLogger pluginLogger; private DataManager dataManager; private Placeholders placeholders; + private CheaterCheckScheduler cheaterCheckScheduler; + private BetterRanksCheaters betterRanksCheaters; private GuiManager guiManager; private FileRewardManager fileRewardManager; private Event event; private BukkitTask dailyTask; private BukkitTask weeklyTask; private BukkitTask monthlyTask; + private PlayerKillDatabase PKDB; private BetterEloCommand betterEloCommand; + private ExtendedConfigManager configManager; private final Map rewardStates = new HashMap<>(); @Override public void onEnable() { // Inicjalizacja PluginLoggera Set defaultLogLevels = EnumSet.of(PluginLogger.LogLevel.INFO,PluginLogger.LogLevel.DEBUG, PluginLogger.LogLevel.WARNING, PluginLogger.LogLevel.ERROR); - pluginLogger = new PluginLogger(getDataFolder().getAbsolutePath(), defaultLogLevels); + pluginLogger = new PluginLogger(getDataFolder().getAbsolutePath(), defaultLogLevels,this); pluginLogger.log(PluginLogger.LogLevel.INFO,"BetterElo: onEnable: Starting BetterElo plugin"); pluginLogger.log(PluginLogger.LogLevel.INFO,"Plugin created by "+this.getDescription().getAuthors()); pluginLogger.log(PluginLogger.LogLevel.INFO,"Plugin version "+this.getDescription().getVersion()); @@ -36,6 +40,7 @@ public void onEnable() { pluginLogger.log(PluginLogger.LogLevel.DEBUG,"BetterElo: onEnable: Loading config.yml"); ExtendedConfigManager configManager = new ExtendedConfigManager(this, pluginLogger); pluginLogger.log(PluginLogger.LogLevel.DEBUG,"BetterElo: onEnable: Zaladowano loggera."); + PlayerKillDatabase PKDB = new PlayerKillDatabase(pluginLogger); // Przekazujemy pluginLogger do innych klas dataManager = new DataManager(this, pluginLogger); pluginLogger.log(PluginLogger.LogLevel.DEBUG,"BetterElo: onEnable: Zaladowano DataManager."); @@ -64,10 +69,11 @@ public void onEnable() { pluginLogger.log(PluginLogger.LogLevel.WARNING,"BetterElo: onEnable: Warning: PlaceholderAPI not found, placeholders will NOT be available."); } // Rejestracja komendy - getCommand("be").setExecutor(new BetterEloCommand(this, dataManager, guiManager, pluginLogger, this)); + // Rejestracja listenera eventów - event = new Event(dataManager, pluginLogger,this); + event = new Event(dataManager, pluginLogger,this,betterRanksCheaters); getServer().getPluginManager().registerEvents(event, this); + getCommand("be").setExecutor(new BetterEloCommand(this, dataManager, guiManager, pluginLogger, this, configManager,event,PKDB)); pluginLogger.log(PluginLogger.LogLevel.DEBUG,"BetterElo: onEnable: Plugin BetterElo został włączony pomyślnie."); // Inicjalizacja RewardManagera (kod z konstruktora RewardManager) rewardStates.put("daily", true); @@ -100,6 +106,8 @@ public void onEnable() { logger.info("[BetterElo] Author " + this.getDescription().getAuthors()); logger.info("[BetterElo] Version " + this.getDescription().getVersion()); logger.info("[BetterElo] " + this.getDescription().getDescription()); + CheaterCheckScheduler cheaterCheckScheduler = new CheaterCheckScheduler(this,betterRanksCheaters, getServer().getScheduler(), pluginLogger); + cheaterCheckScheduler.startScheduler(); } @Override @@ -183,7 +191,10 @@ public void updateLastScheduledTime(String period) { pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: updateLastScheduledTime: period: "+period+" setting current system time as last scheduled time"); FileConfiguration config = getConfig(); config.set(period + "LastScheduledTime", System.currentTimeMillis()); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: updateLastScheduledTime: calling PKDB.clearTable(period) wit parameters: "+period); + PKDB.clearTable(period); pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: updateLastScheduledTime: period: "+period+" starting saveConfig"); + saveConfig(); } diff --git a/src/main/java/betterbox/mine/game/betterelo/BetterEloCommand.java b/src/main/java/betterbox/mine/game/betterelo/BetterEloCommand.java index a016c4c..1ddde80 100644 --- a/src/main/java/betterbox/mine/game/betterelo/BetterEloCommand.java +++ b/src/main/java/betterbox/mine/game/betterelo/BetterEloCommand.java @@ -15,6 +15,8 @@ import java.io.File; import java.io.IOException; import java.util.List; +import java.util.Map; +import java.util.UUID; public class BetterEloCommand implements CommandExecutor { @@ -24,14 +26,20 @@ public class BetterEloCommand implements CommandExecutor { private final PluginLogger pluginLogger; private final GuiManager guiManager; // Dodajemy referencję do GuiManager private final BetterElo betterElo; + private final ExtendedConfigManager configManager; + private final Event event; + private final PlayerKillDatabase PKDB; - public BetterEloCommand(JavaPlugin plugin, DataManager dataManager, GuiManager guiManager, PluginLogger pluginLogger, BetterElo betterElo) { + public BetterEloCommand(JavaPlugin plugin, DataManager dataManager, GuiManager guiManager, PluginLogger pluginLogger, BetterElo betterElo, ExtendedConfigManager configManager,Event event, PlayerKillDatabase PKDB) { this.dataManager = dataManager; this.plugin = plugin; this.guiManager = guiManager; // Inicjalizujemy referencję do GuiManager this.pluginLogger = pluginLogger; this.betterElo = betterElo; // Inicjalizujemy referencję do BetterElo + this.configManager = configManager; + this.event = event; + this.PKDB = PKDB; } @@ -175,6 +183,9 @@ public boolean onCommand(CommandSender sender, Command command, String label, St player = (Player) sender; guiManager.openMainGui(player); // Otwieramy główne menu GUI dla gracza break; + case "reload": + return handleReloadCommand(sender); + default: // /be - Information about a specific player's rank and points playerName = args[0]; @@ -219,10 +230,84 @@ public boolean onCommand(CommandSender sender, Command command, String label, St sender.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[BetterElo]" + ChatColor.DARK_RED + " Please enter a valid ranking position number."); } } + if(args[0].equalsIgnoreCase("ban")){ + handleBanCommand(sender,args[1]); + } break; } return true; } + private boolean handleReloadCommand(CommandSender sender){ + if(sender.hasPermission("betterelo.reload")){ + configManager.ReloadConfig(); + sender.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[BetterElo]" + ChatColor.AQUA + " BetterRanks config reloaded!"); + return true; + }else { + pluginLogger.log(PluginLogger.LogLevel.DEBUG,"BetterEloCommand: handleReloadCommand: sender " + sender + " dont have permission to use /br tl"); + sender.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[BetterElo] " + ChatColor.DARK_RED +"You don't have permission to use this command!"); + return false; + } + } + private boolean handleBanCommand(CommandSender sender, String banName) { + if (sender.hasPermission("betterelo.reload")) { + if (!(sender instanceof Player)) { + sender.sendMessage("Komenda dostępna tylko dla graczy."); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterEloCommand: handleBanCommand: sender " + sender + " dont have permission to use /br tl"); + return false; + } + + Player player = (Player) sender; + + // Pobierz listę interakcji dla gracza, którego chcesz zbanować + Map> interactionsMap = PKDB.getPlayerInteractions(banName); + + if (interactionsMap.isEmpty()) { + sender.sendMessage("Brak interakcji do zbanowania dla gracza " + banName); + return false; + } + + double totalPoints = 0; + + for (List playerInteractions : interactionsMap.values()) { + for (PlayerKillDatabase.PlayerInteraction interaction : playerInteractions) { + totalPoints += interaction.getTotalPoints(); + } + } + + if (totalPoints > 0) { + // Gracz zasługuje na karę (odejmowanie punktów) + for (String rankingType : interactionsMap.keySet()) { + event.subtractPoints(player.getUniqueId().toString(), totalPoints, rankingType); + } + sender.sendMessage("Gracz " + banName + " stracił " + totalPoints + " punktów."); + } else { + // Gracz nie zasługuje na karę (dodawanie punktów) + for (String rankingType : interactionsMap.keySet()) { + event.addPoints(player.getUniqueId().toString(), Math.abs(totalPoints), rankingType); + } + sender.sendMessage("Gracz " + banName + " otrzymał " + Math.abs(totalPoints) + " punktów nagrody."); + } + + // Usuń rekordy gracza z wszystkich tabel + PKDB.deletePlayerRecords(banName); + return true; + } else { + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterEloCommand: handleBanCommand: sender " + sender + " dont have permission to use /br tl"); + return false; + } + } + + public String getOfflinePlayerUUID(String playerName) { + OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(playerName); + + if (offlinePlayer.hasPlayedBefore()) { + String UUID = String.valueOf(offlinePlayer.getUniqueId()); + return UUID; + } else { + // Gracz o podanej nazwie jeszcze nie grał + return null; + } + } private String formatTime(long millis) { long seconds = millis / 1000; diff --git a/src/main/java/betterbox/mine/game/betterelo/BetterRanksCheaters.java b/src/main/java/betterbox/mine/game/betterelo/BetterRanksCheaters.java new file mode 100644 index 0000000..264b845 --- /dev/null +++ b/src/main/java/betterbox/mine/game/betterelo/BetterRanksCheaters.java @@ -0,0 +1,54 @@ +package betterbox.mine.game.betterelo; + +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.plugin.java.JavaPlugin; +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Set; + +public class BetterRanksCheaters { + private JavaPlugin plugin; + private final PluginLogger pluginLogger; + public List cheatersList; + + public BetterRanksCheaters(JavaPlugin plugin, PluginLogger pluginLogger) { + this.plugin = plugin; + this.pluginLogger = pluginLogger; + this.cheatersList = new ArrayList<>(); + } + + public void CheckCheatersFromBetterRanks() { + pluginLogger.log(PluginLogger.LogLevel.DEBUG_LVL2,"CheckCheatersFromBetterRanks called"); + File dataFolder = plugin.getDataFolder(); + if (!dataFolder.exists()) { + pluginLogger.log(PluginLogger.LogLevel.WARNING,"BetterRanks plugin folder doesn't exists!"); + return; // Jeśli folder danych nie istnieje, to nie ma co sprawdzać. + } + + File configFile = new File(dataFolder, "plugins/BetterRanks/database.yml"); + if (!configFile.exists()) { + pluginLogger.log(PluginLogger.LogLevel.WARNING,"BetterRanks database.yml doesn't exists!"); + return; // Jeśli plik database.yml nie istnieje, to nie ma co sprawdzać. + } + + FileConfiguration config = YamlConfiguration.loadConfiguration(configFile); + Set playerNames = config.getKeys(false); + pluginLogger.log(PluginLogger.LogLevel.DEBUG_LVL2,"BetterRanksCheaters: CheckCheatersFromBetterRanks checking.."); + for (String playerName : playerNames) { + String rank = config.getString(playerName + ".rank"); + if (rank != null && rank.equalsIgnoreCase("CHEATER")) { + cheatersList.add(playerName); + pluginLogger.log(PluginLogger.LogLevel.DEBUG_LVL2,"BetterRanksCheaters: CheckCheatersFromBetterRanks: adding cheater "+playerName); + } + } + pluginLogger.log(PluginLogger.LogLevel.DEBUG_LVL2,"BetterRanksCheaters: CheckCheatersFromBetterRanks: Cheaters found: "+ Arrays.toString(cheatersList.toArray())); + } + + public List getCheatersList() { + pluginLogger.log(PluginLogger.LogLevel.DEBUG_LVL2,"BetterRanksCheaters: getCheatersList called"); + return cheatersList; + } +} diff --git a/src/main/java/betterbox/mine/game/betterelo/CheaterCheckScheduler.java b/src/main/java/betterbox/mine/game/betterelo/CheaterCheckScheduler.java new file mode 100644 index 0000000..d04d807 --- /dev/null +++ b/src/main/java/betterbox/mine/game/betterelo/CheaterCheckScheduler.java @@ -0,0 +1,31 @@ +package betterbox.mine.game.betterelo; + +import org.bukkit.plugin.Plugin; +import org.bukkit.scheduler.BukkitScheduler; + +public class CheaterCheckScheduler { + private final BetterRanksCheaters betterRanksCheaters; + private final BukkitScheduler scheduler; + private final PluginLogger pluginLogger; + private final Plugin plugin; // Dodaj zmienną plugin + + public CheaterCheckScheduler(Plugin plugin, BetterRanksCheaters betterRanksCheaters, BukkitScheduler scheduler, PluginLogger pluginLogger) { + + this.plugin = plugin; // Zapisz zmienną plugin + this.betterRanksCheaters = betterRanksCheaters; + this.scheduler = scheduler; + this.pluginLogger = pluginLogger; + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "CheaterCheckScheduler called"); + } + + public void startScheduler() { + pluginLogger.log(PluginLogger.LogLevel.DEBUG_LVL2, "startScheduler called"); + int delay = 0; // Opóźnienie początkowe (0 ticków) + int period = 100; // Okres w tickach (20 ticków to 1 sekunda) + + scheduler.scheduleSyncRepeatingTask(plugin, () -> { + betterRanksCheaters.CheckCheatersFromBetterRanks(); + }, delay, period); + } +} + diff --git a/src/main/java/betterbox/mine/game/betterelo/CheaterInteractions.java b/src/main/java/betterbox/mine/game/betterelo/CheaterInteractions.java new file mode 100644 index 0000000..f518adc --- /dev/null +++ b/src/main/java/betterbox/mine/game/betterelo/CheaterInteractions.java @@ -0,0 +1,31 @@ +package betterbox.mine.game.betterelo; + +public class CheaterInteractions { + private String rankingType; + private String playerName; + private String victimName; + private double totalPoints; + + public CheaterInteractions(String rankingType, String playerName, String victimName, double totalPoints) { + this.rankingType = rankingType; + this.playerName = playerName; + this.victimName = victimName; + this.totalPoints = totalPoints; + } + + public String getRankingType() { + return rankingType; + } + + public String getPlayerName() { + return playerName; + } + + public String getVictimName() { + return victimName; + } + + public double getTotalPoints() { + return totalPoints; + } +} diff --git a/src/main/java/betterbox/mine/game/betterelo/DataManager.java b/src/main/java/betterbox/mine/game/betterelo/DataManager.java index d87fe72..ca11b37 100644 --- a/src/main/java/betterbox/mine/game/betterelo/DataManager.java +++ b/src/main/java/betterbox/mine/game/betterelo/DataManager.java @@ -42,6 +42,7 @@ public void setPoints(String playerUUID, double points, String ranking_type) { saveDataToFileWeekly(); // Zapisz zmienione dane do pliku saveDataToFileMonthly(); // Zapisz zmienione dane do pliku } + public double getPoints(String playerUUID, String ranking_type) { //pluginLogger.log(PluginLogger.LogLevel.DEBUG,"DataManager: getPoints called with parameters: playerUUID "+playerUUID+" ranking_type "+ranking_type); Map pointsMap = allPlayerPoints.get(ranking_type); diff --git a/src/main/java/betterbox/mine/game/betterelo/Event.java b/src/main/java/betterbox/mine/game/betterelo/Event.java index 9ac522c..c8f14af 100644 --- a/src/main/java/betterbox/mine/game/betterelo/Event.java +++ b/src/main/java/betterbox/mine/game/betterelo/Event.java @@ -20,11 +20,13 @@ public class Event implements Listener { private final DataManager dataManager; private final JavaPlugin plugin; private final PluginLogger pluginLogger; + private BetterRanksCheaters cheaters; - public Event(DataManager dataManager, PluginLogger pluginLogger, JavaPlugin plugin) { + public Event(DataManager dataManager, PluginLogger pluginLogger, JavaPlugin plugin, BetterRanksCheaters cheaters) { this.dataManager = dataManager; this.pluginLogger = pluginLogger; this.plugin = plugin; + this.cheaters = cheaters; } @EventHandler @@ -69,27 +71,34 @@ public void onPlayerJoin(PlayerJoinEvent event) { private double handleKillEvent(String rankingType, Player victim, Player killer) { pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: handleKillEvent called with parameters: " + rankingType+" "+victim+" "+killer); double pointsEarned = 0; + if (killer != null && !killer.equals(victim)) { // Gracz zabił innego gracza - double victimElo = getElo(victim.getUniqueId().toString(),rankingType); - double killerElo = getElo(killer.getUniqueId().toString(),rankingType); + double victimElo = getElo(victim.getUniqueId().toString(), rankingType); + double killerElo = getElo(killer.getUniqueId().toString(), rankingType); double maxElo = getMaxElo(rankingType); double minElo = dataManager.getMinElo(rankingType); - pluginLogger.log(PluginLogger.LogLevel.DEBUG,"Event: KillEvent: rankingType: "+rankingType+" loaded variables: maxElo:" + maxElo + " minElo: "+minElo+" victimElo:" + victimElo + " victim name: "+victim.getName()+" killerElo:" + killerElo+" killer name: "+killer.getName()); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: KillEvent: rankingType: " + rankingType + " loaded variables: maxElo:" + maxElo + " minElo: " + minElo + " victimElo:" + victimElo + " victim name: " + victim.getName() + " killerElo:" + killerElo + " killer name: " + killer.getName()); double basePoints = 100; - pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: handleKillEvent calling calculatePointsEarned with parameters: basePoints " + basePoints+" killerElo "+killerElo+" victimElo "+victimElo+" maxElo "+maxElo+" minElo "+minElo); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: handleKillEvent calling calculatePointsEarned with parameters: basePoints " + basePoints + " killerElo " + killerElo + " victimElo " + victimElo + " maxElo " + maxElo + " minElo " + minElo); pointsEarned = calculatePointsEarned(basePoints, killerElo, victimElo, maxElo, minElo); - pluginLogger.log(PluginLogger.LogLevel.INFO,"Event: KillEvent: pointsEarned: " + pointsEarned+"rankingType: "+rankingType); + pluginLogger.log(PluginLogger.LogLevel.INFO, "Event: KillEvent: pointsEarned: " + pointsEarned + "rankingType: " + rankingType); + // Zapisz informacje do bazy danych + PlayerKillDatabase playerKillDatabase = new PlayerKillDatabase(pluginLogger); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: handleKillEvent calling saveKillData"); + playerKillDatabase.saveKillData(rankingType, victim.getName(), killer.getName(), pointsEarned); // Dodaj punkty graczowi, który zabił - pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: handleKillEvent calling addPoints with parameters: " + killer.getUniqueId().toString()+" "+pointsEarned+" "+rankingType); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: handleKillEvent calling addPoints with parameters: " + killer.getUniqueId().toString() + " " + pointsEarned + " " + rankingType); addPoints(killer.getUniqueId().toString(), pointsEarned, rankingType); - pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: handleKillEvent calling subtractPoints with parameters: " + victim.getUniqueId().toString()+" "+pointsEarned+" "+rankingType); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: handleKillEvent calling subtractPoints with parameters: " + victim.getUniqueId().toString() + " " + pointsEarned + " " + rankingType); subtractPoints(victim.getUniqueId().toString(), pointsEarned, rankingType); } return pointsEarned; } + + // Mapa do śledzenia ostatnich uderzeń private final Map lastHitTime = new HashMap<>(); @@ -153,55 +162,62 @@ private Player getLastAttacker(Player victim) { @EventHandler public void onPlayerDeath(PlayerDeathEvent event) { - try - { + try { Player victim = event.getEntity(); Player killer = victim.getKiller(); - pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath called with parameters: " + event); - pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath: victim: " + victim + " killer: " + killer); - pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath calling deathDueToCombat(victim)"); - if (killer == null && deathDueToCombat(victim)) { - pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath killer is null"); - pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath: deathDueToCombat(victim): " + deathDueToCombat(victim) + " victim: " + victim); - // Znajdź ostatniego gracza, który uderzył ofiarę - pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath calling getLastAttacker(victim)"); - Player lastAttacker = getLastAttacker(victim); - pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath: lastAttacker "+lastAttacker); - - if (lastAttacker != null) { - pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath: calling handleKillEvent"+lastAttacker); - // Oblicz punkty zdobyte/wtracone w wyniku "wirtualnej" walki - double pointsEarned = handleKillEvent("main", victim, lastAttacker); - pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath calling handleKillEvent with parameters: daily " + victim+" "+lastAttacker); - handleKillEvent("daily", victim, lastAttacker); - pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath calling handleKillEvent with parameters: weekly " + victim+" "+lastAttacker); - handleKillEvent("weekly", victim, lastAttacker); - pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath calling handleKillEvent with parameters: monthly " + victim+" "+lastAttacker); - handleKillEvent("monthly", victim, lastAttacker); - pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath calling notifyPlayersAboutPoints(lastAttacker, victim, pointsEarned)"); - // Powiadom graczy o zmianie punktów - notifyPlayersAboutPoints(lastAttacker, victim, pointsEarned); - return; + if (!cheaters.getCheatersList().contains(victim.getName()) && !cheaters.getCheatersList().contains(killer.getName())) { + + + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath called with parameters: " + event); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath: victim: " + victim + " killer: " + killer); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath calling deathDueToCombat(victim)"); + if (killer == null && deathDueToCombat(victim)) { + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath killer is null"); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath: deathDueToCombat(victim): " + deathDueToCombat(victim) + " victim: " + victim); + // Znajdź ostatniego gracza, który uderzył ofiarę + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath calling getLastAttacker(victim)"); + Player lastAttacker = getLastAttacker(victim); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath: lastAttacker " + lastAttacker); + + if (lastAttacker != null) { + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath: calling handleKillEvent" + lastAttacker); + // Oblicz punkty zdobyte/wtracone w wyniku "wirtualnej" walki + double pointsEarned = handleKillEvent("main", victim, lastAttacker); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath calling handleKillEvent with parameters: daily " + victim + " " + lastAttacker); + handleKillEvent("daily", victim, lastAttacker); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath calling handleKillEvent with parameters: weekly " + victim + " " + lastAttacker); + handleKillEvent("weekly", victim, lastAttacker); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath calling handleKillEvent with parameters: monthly " + victim + " " + lastAttacker); + handleKillEvent("monthly", victim, lastAttacker); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath calling notifyPlayersAboutPoints(lastAttacker, victim, pointsEarned)"); + // Powiadom graczy o zmianie punktów + notifyPlayersAboutPoints(lastAttacker, victim, pointsEarned); + return; + } + + return; + } + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath calling handleKillEvent with parameters: main " + victim + " " + killer); + double pointsEarned = handleKillEvent("main", victim, killer); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath calling handleKillEvent with parameters: daily " + victim + " " + killer); + handleKillEvent("daily", victim, killer); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath calling handleKillEvent with parameters: weekly " + victim + " " + killer); + handleKillEvent("weekly", victim, killer); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath calling handleKillEvent with parameters: monthly " + victim + " " + killer); + handleKillEvent("monthly", victim, killer); + + assert killer != null; + notifyPlayersAboutPoints(killer, victim, pointsEarned); } - - return; - } - pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath calling handleKillEvent with parameters: main " + victim + " " + killer); - double pointsEarned = handleKillEvent("main", victim, killer); - pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath calling handleKillEvent with parameters: daily " + victim + " " + killer); - handleKillEvent("daily", victim, killer); - pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath calling handleKillEvent with parameters: weekly " + victim + " " + killer); - handleKillEvent("weekly", victim, killer); - pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: onPlayerDeath calling handleKillEvent with parameters: monthly " + victim + " " + killer); - handleKillEvent("monthly", victim, killer); - - assert killer != null; - notifyPlayersAboutPoints(killer, victim, pointsEarned); - - }catch (Exception e){ - pluginLogger.log(PluginLogger.LogLevel.ERROR, "Event: onPlayerDeath exception " + e+" "+e.getMessage()); + else + { + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: handleKillEvent: returning 0 points because either " + victim + " " + cheaters.getCheatersList().contains(victim.getName()) + " or " + killer + " " + cheaters.getCheatersList().contains(killer.getName()) + " has CHEATER rank in BetterRanks."); + } + }catch(Exception e){ + pluginLogger.log(PluginLogger.LogLevel.ERROR, "Event: onPlayerDeath exception " + e + " " + e.getMessage()); } - } +} + private void notifyPlayersAboutPoints(Player killer, Player victim, double pointsEarned) { pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event: notifyPlayersAboutPoints called with parameters: "+killer+" "+victim+" "+pointsEarned); @@ -244,12 +260,13 @@ private void updatePoints(String playerUUID, double points, String rankingType, pluginLogger.log(PluginLogger.LogLevel.DEBUG,String.format("Event: %sPoints: currentPoints: %s ranking saved", (isAdding ? "add" : "subtract"), rankingType)); } - private void addPoints(String playerUUID, double points, String rankingType) { + + public void addPoints(String playerUUID, double points, String rankingType) { pluginLogger.log(PluginLogger.LogLevel.DEBUG,"Event: addPoints: playerUUID: "+playerUUID+" rankingType: "+rankingType+" points: "+points); updatePoints(playerUUID, points, rankingType, true); } - private void subtractPoints(String playerUUID, double points, String rankingType) { + public void subtractPoints(String playerUUID, double points, String rankingType) { pluginLogger.log(PluginLogger.LogLevel.DEBUG,"Event: subtractPoints: playerUUID: "+playerUUID+" rankingType: "+rankingType+" points: "+points); updatePoints(playerUUID, points, rankingType, false); } diff --git a/src/main/java/betterbox/mine/game/betterelo/ExtendedConfigManager.java b/src/main/java/betterbox/mine/game/betterelo/ExtendedConfigManager.java index 5e35a8e..20c89b0 100644 --- a/src/main/java/betterbox/mine/game/betterelo/ExtendedConfigManager.java +++ b/src/main/java/betterbox/mine/game/betterelo/ExtendedConfigManager.java @@ -6,14 +6,13 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; -import java.util.EnumSet; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; public class ExtendedConfigManager { private JavaPlugin plugin; private PluginLogger pluginLogger; + List logLevels = null; + Set enabledLogLevels; public ExtendedConfigManager(JavaPlugin plugin, PluginLogger pluginLogger) { this.plugin = plugin; @@ -45,6 +44,33 @@ private void configureLogger() { // Ustawienie aktywnych poziomów logowania w loggerze pluginLogger.setEnabledLogLevels(enabledLogLevels); } + public void ReloadConfig() { + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "ConfigManager: ReloadConfig called"); + // Odczytanie ustawień log_level z pliku konfiguracyjnego + File configFile = new File(plugin.getDataFolder(), "config.yml"); + plugin.reloadConfig(); + logLevels = plugin.getConfig().getStringList("log_level"); + enabledLogLevels = new HashSet<>(); + if (logLevels == null || logLevels.isEmpty()) { + pluginLogger.log(PluginLogger.LogLevel.ERROR, "ConfigManager: ReloadConfig: no config file or no configured log levels! Saving default settings."); + // Jeśli konfiguracja nie określa poziomów logowania, użyj domyślnych ustawień + enabledLogLevels = EnumSet.of(PluginLogger.LogLevel.INFO, PluginLogger.LogLevel.WARNING, PluginLogger.LogLevel.ERROR); + updateConfig("log_level:\n - INFO\n - WARNING\n - ERROR"); + + } + + for (String level : logLevels) { + try { + pluginLogger.log(PluginLogger.LogLevel.DEBUG_LVL2, "ConfigManager: ReloadConfig: adding " + level.toUpperCase()); + enabledLogLevels.add(PluginLogger.LogLevel.valueOf(level.toUpperCase())); + pluginLogger.log(PluginLogger.LogLevel.DEBUG_LVL2, "ConfigManager: ReloadConfig: current log levels: " + Arrays.toString(enabledLogLevels.toArray())); + + } catch (IllegalArgumentException e) { + // Jeśli podano nieprawidłowy poziom logowania, zaloguj błąd + plugin.getServer().getLogger().warning("Invalid log level in config: " + level); + } + } + } public void updateConfig(String configuration) { diff --git a/src/main/java/betterbox/mine/game/betterelo/PlayerKillDatabase.java b/src/main/java/betterbox/mine/game/betterelo/PlayerKillDatabase.java new file mode 100644 index 0000000..2635fcf --- /dev/null +++ b/src/main/java/betterbox/mine/game/betterelo/PlayerKillDatabase.java @@ -0,0 +1,225 @@ +package betterbox.mine.game.betterelo; + +import java.sql.*; +import java.io.File; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class PlayerKillDatabase { + private static final String DATABASE_URL = "jdbc:sqlite:plugins/BetterElo/player_kill_database.db"; + private PluginLogger pluginLogger; + + public PlayerKillDatabase(PluginLogger pluginLogger) { + this.pluginLogger = pluginLogger; + pluginLogger.log(PluginLogger.LogLevel.DEBUG,"PlayerKillDatabase called"); + createDatabaseIfNeeded(); + } + + private void createDatabaseIfNeeded() { + pluginLogger.log(PluginLogger.LogLevel.DEBUG,"PlayerKillDatabase: createDatabaseIfNeeded called"); + File databaseFile = new File("plugins/BetterElo/player_kill_database.db"); + if (!databaseFile.exists()) { + createNewDatabase(); + } + } + public void saveKillData(String rankingType, String victimName, String killerName, double pointsEarned) { + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "PlayerKillDatabase: saveKillData called with parameters: " + rankingType+" "+victimName+" "+killerName); + try { + Connection connection = DriverManager.getConnection(DATABASE_URL); + + // Wstawienie danych do odpowiedniej tabeli + String insertSQL = "INSERT INTO " + rankingType + " (killername, victimname, pointearned) VALUES (?, ?, ?)"; + PreparedStatement preparedStatement = connection.prepareStatement(insertSQL); + preparedStatement.setString(1, killerName); + preparedStatement.setString(2, victimName); + preparedStatement.setDouble(3, pointsEarned); + + preparedStatement.executeUpdate(); + + preparedStatement.close(); + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + public void deletePlayerRecords(String playerName) { + try { + Connection connection = DriverManager.getConnection(DATABASE_URL); + + // Usuwanie rekordów z tabeli "daily" + deleteRecordsFromTable(connection, "daily", playerName); + + // Usuwanie rekordów z tabeli "weekly" + deleteRecordsFromTable(connection, "weekly", playerName); + + // Usuwanie rekordów z tabeli "monthly" + deleteRecordsFromTable(connection, "monthly", playerName); + + // Usuwanie rekordów z tabeli "main" + deleteRecordsFromTable(connection, "main", playerName); + + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + private void deleteRecordsFromTable(Connection connection, String tableName, String playerName) throws SQLException { + String deleteSQL = "DELETE FROM " + tableName + " WHERE killername = ? OR victimname = ?"; + PreparedStatement preparedStatement = connection.prepareStatement(deleteSQL); + preparedStatement.setString(1, playerName); + preparedStatement.setString(2, playerName); + preparedStatement.executeUpdate(); + preparedStatement.close(); + } + public Map> getPlayerInteractions(String playerName) { + Map> interactionsMap = new HashMap<>(); + + // Tabele do przeszukania + String[] tables = {"daily", "weekly", "monthly", "main"}; + + for (String table : tables) { + List playerInteractions = new ArrayList<>(); + + try { + Connection connection = DriverManager.getConnection(DATABASE_URL); + + // Zapytanie SQL do zliczania sumy zabójstw gracza X na graczu Y + String queryKillsXonY = "SELECT victimname, SUM(pointearned) AS total_points " + + "FROM " + table + " " + + "WHERE killername = ? AND victimname = ?"; + + PreparedStatement preparedStatementKillsXonY = connection.prepareStatement(queryKillsXonY); + preparedStatementKillsXonY.setString(1, playerName); + preparedStatementKillsXonY.setString(2, playerName); + + ResultSet resultSetKillsXonY = preparedStatementKillsXonY.executeQuery(); + + while (resultSetKillsXonY.next()) { + String victimName = resultSetKillsXonY.getString("victimname"); + double totalPoints = resultSetKillsXonY.getDouble("total_points"); + + playerInteractions.add(new PlayerInteraction(victimName, totalPoints)); + } + + resultSetKillsXonY.close(); + preparedStatementKillsXonY.close(); + + // Zapytanie SQL do zliczania sumy zabójstw gracza Y na graczu X + String queryKillsYonX = "SELECT killername, SUM(pointearned) AS total_points " + + "FROM " + table + " " + + "WHERE killername = ? AND victimname = ?"; + + PreparedStatement preparedStatementKillsYonX = connection.prepareStatement(queryKillsYonX); + preparedStatementKillsYonX.setString(1, playerName); + preparedStatementKillsYonX.setString(2, playerName); + + ResultSet resultSetKillsYonX = preparedStatementKillsYonX.executeQuery(); + + while (resultSetKillsYonX.next()) { + String killerName = resultSetKillsYonX.getString("killername"); + double totalPoints = resultSetKillsYonX.getDouble("total_points"); + + playerInteractions.add(new PlayerInteraction(killerName, totalPoints)); + } + + resultSetKillsYonX.close(); + preparedStatementKillsYonX.close(); + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + + interactionsMap.put(table, playerInteractions); + } + + return interactionsMap; + } + public void clearTable(String tableName) { + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "PlayerKillDatabase: clearTable: called with parameters "+tableName); + try { + Connection connection = DriverManager.getConnection(DATABASE_URL); + + // Zapytanie SQL do usunięcia wszystkich rekordów z tabeli + String query = "DELETE FROM " + tableName; + + Statement statement = connection.createStatement(); + statement.executeUpdate(query); + + statement.close(); + connection.close(); + } catch (SQLException e) { + pluginLogger.log(PluginLogger.LogLevel.ERROR, "PlayerKillDatabase: clearTable: "+ e.getMessage()); + } + } + public class PlayerInteraction { + private String playerName; + private double totalPoints; + + public PlayerInteraction(String playerName, double totalPoints) { + pluginLogger.log(PluginLogger.LogLevel.DEBUG_LVL2, "PlayerKillDatabase: PlayerInteraction: called with parameters "+playerName+" "+totalPoints); + this.playerName = playerName; + this.totalPoints = totalPoints; + } + + public String getPlayerName() { + return playerName; + } + + public double getTotalPoints() { + return totalPoints; + } + } + + + + + + private void createNewDatabase() { + pluginLogger.log(PluginLogger.LogLevel.DEBUG,"PlayerKillDatabase: createNewDatabase called"); + try { + Connection connection = DriverManager.getConnection(DATABASE_URL); + + // Tworzenie tabeli "daily" + createTable(connection, "daily"); + + // Tworzenie tabeli "weekly" + createTable(connection, "weekly"); + + // Tworzenie tabeli "monthly" + createTable(connection, "monthly"); + + // Tworzenie tabeli "main" + createTable(connection, "main"); + + // Zamykanie połączenia + connection.close(); + } catch (SQLException e) { + pluginLogger.log(PluginLogger.LogLevel.ERROR,"PlayerKillDatabase: createNewDatabase: "+e.getMessage()); + } + } + + private void createTable(Connection connection, String tableName) throws SQLException { + pluginLogger.log(PluginLogger.LogLevel.DEBUG,"PlayerKillDatabase: createTable called with paraemeters "+connection.toString()+" "+tableName); + Statement statement = connection.createStatement(); + + // Tworzenie tabeli z odpowiednimi kolumnami + String createTableSQL = "CREATE TABLE IF NOT EXISTS " + tableName + " (" + + "id INTEGER PRIMARY KEY AUTOINCREMENT," + + "time TIMESTAMP DEFAULT CURRENT_TIMESTAMP," + + "killername TEXT NOT NULL," + + "victimname TEXT NOT NULL," + + "pointearned DOUBLE NOT NULL" + + ");"; + + statement.execute(createTableSQL); + statement.close(); + } +} diff --git a/src/main/java/betterbox/mine/game/betterelo/PluginLogger.java b/src/main/java/betterbox/mine/game/betterelo/PluginLogger.java index 4f0260a..a44d764 100644 --- a/src/main/java/betterbox/mine/game/betterelo/PluginLogger.java +++ b/src/main/java/betterbox/mine/game/betterelo/PluginLogger.java @@ -1,27 +1,31 @@ package betterbox.mine.game.betterelo; +import org.bukkit.plugin.java.JavaPlugin; + import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.Date; -import java.util.EnumSet; import java.util.Set; +import java.util.logging.*; public class PluginLogger { private final File logFile; + private JavaPlugin plugin; private Set enabledLogLevels; // Zbiór aktywnych poziomów logowania // Enumeracja dla poziomów logowania public enum LogLevel { - INFO, WARNING, ERROR, DEBUG + INFO, WARNING, ERROR, DEBUG, DEBUG_LVL2 } - public PluginLogger(String folderPath, Set enabledLogLevels) { + public PluginLogger(String folderPath, Set enabledLogLevels, JavaPlugin plugin) { this.enabledLogLevels = enabledLogLevels; - + this.plugin = plugin; // Tworzenie folderu dla logów, jeśli nie istnieje File logFolder = new File(folderPath,"logs"); if (!logFolder.exists()) { @@ -40,7 +44,7 @@ public PluginLogger(String folderPath, Set enabledLogLevels) { logFile.createNewFile(); } } catch (IOException e) { - e.printStackTrace(); // Tu można użyć loggera serwera do zalogowania błędu + plugin.getLogger().severe("PluginLogger: Could not create log file! "+e.getMessage()); } } @@ -60,14 +64,16 @@ public void log(LogLevel level, String message) { writer.write(logMessage); writer.newLine(); } catch (IOException e) { - e.printStackTrace(); // Tu można użyć loggera serwera do zalogowania błędu + plugin.getLogger().severe("PluginLogger: log: Could not write to log file!"+e.getMessage()); } } } // Metoda do ustawiania aktywnych poziomów logowania - public void setEnabledLogLevels(Set enabledLogLevels) { - this.enabledLogLevels = enabledLogLevels; + public void setEnabledLogLevels(Set configEnabledLogLevels) { + this.enabledLogLevels = configEnabledLogLevels; + log("Enabled Log levels "+ Arrays.toString(enabledLogLevels.toArray())); + } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 2efc3b4..81e3690 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -58,4 +58,10 @@ permissions: betterelo.monthly: description: Daje dostęp do komendy /be monthly. default: op + betterelo.reload: + description: reloads config. + default: op + betterelo.ban: + description: Deletes player. + default: op From 2d382e36369fab89eda5c80d6f686f68ef7ab9f2 Mon Sep 17 00:00:00 2001 From: Grzybol Date: Sun, 26 Nov 2023 19:57:32 +0100 Subject: [PATCH 2/5] - still not working --- pom.xml | 7 ++- .../mine/game/betterelo/BetterElo.java | 12 +++-- .../mine/game/betterelo/BetterEloCommand.java | 46 +++++++++---------- .../game/betterelo/BetterRanksCheaters.java | 20 ++++---- .../game/betterelo/PlayerKillDatabase.java | 42 +++++++++-------- .../mine/game/betterelo/PluginLogger.java | 2 +- 6 files changed, 72 insertions(+), 57 deletions(-) diff --git a/pom.xml b/pom.xml index 32691f5..441f1ca 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ betterbox.mine.game BetterElo - 3.0.0-SNAPSHOT + 3.0.15-SNAPSHOT jar BetterElo @@ -105,6 +105,11 @@ + + net.kyori + adventure-platform-bukkit + 4.3.1 + org.eclipse.jetty jetty-server diff --git a/src/main/java/betterbox/mine/game/betterelo/BetterElo.java b/src/main/java/betterbox/mine/game/betterelo/BetterElo.java index 8fe4870..4b6c703 100644 --- a/src/main/java/betterbox/mine/game/betterelo/BetterElo.java +++ b/src/main/java/betterbox/mine/game/betterelo/BetterElo.java @@ -38,9 +38,9 @@ public void onEnable() { pluginLogger.log(PluginLogger.LogLevel.INFO,"Plugin version "+this.getDescription().getVersion()); pluginLogger.log(PluginLogger.LogLevel.INFO,"https://github.com/Grzybol/BetterElo"); pluginLogger.log(PluginLogger.LogLevel.DEBUG,"BetterElo: onEnable: Loading config.yml"); - ExtendedConfigManager configManager = new ExtendedConfigManager(this, pluginLogger); + configManager = new ExtendedConfigManager(this, pluginLogger); pluginLogger.log(PluginLogger.LogLevel.DEBUG,"BetterElo: onEnable: Zaladowano loggera."); - PlayerKillDatabase PKDB = new PlayerKillDatabase(pluginLogger); + PKDB = new PlayerKillDatabase(pluginLogger); // Przekazujemy pluginLogger do innych klas dataManager = new DataManager(this, pluginLogger); pluginLogger.log(PluginLogger.LogLevel.DEBUG,"BetterElo: onEnable: Zaladowano DataManager."); @@ -69,7 +69,8 @@ public void onEnable() { pluginLogger.log(PluginLogger.LogLevel.WARNING,"BetterElo: onEnable: Warning: PlaceholderAPI not found, placeholders will NOT be available."); } // Rejestracja komendy - + betterRanksCheaters = new BetterRanksCheaters(this,pluginLogger); + CheaterCheckScheduler cheaterCheckScheduler = new CheaterCheckScheduler(this, betterRanksCheaters, getServer().getScheduler(), pluginLogger); // Rejestracja listenera eventów event = new Event(dataManager, pluginLogger,this,betterRanksCheaters); getServer().getPluginManager().registerEvents(event, this); @@ -106,7 +107,10 @@ public void onEnable() { logger.info("[BetterElo] Author " + this.getDescription().getAuthors()); logger.info("[BetterElo] Version " + this.getDescription().getVersion()); logger.info("[BetterElo] " + this.getDescription().getDescription()); - CheaterCheckScheduler cheaterCheckScheduler = new CheaterCheckScheduler(this,betterRanksCheaters, getServer().getScheduler(), pluginLogger); + // Inicjalizacja BetterRanksCheaters + + + pluginLogger.log(PluginLogger.LogLevel.DEBUG,"BetterElo: onEnable: calling cheaterCheckScheduler.startScheduler()"); cheaterCheckScheduler.startScheduler(); } diff --git a/src/main/java/betterbox/mine/game/betterelo/BetterEloCommand.java b/src/main/java/betterbox/mine/game/betterelo/BetterEloCommand.java index 1ddde80..ec65a6a 100644 --- a/src/main/java/betterbox/mine/game/betterelo/BetterEloCommand.java +++ b/src/main/java/betterbox/mine/game/betterelo/BetterEloCommand.java @@ -231,6 +231,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St } } if(args[0].equalsIgnoreCase("ban")){ + handleBanCommand(sender,args[1]); } break; @@ -249,54 +250,53 @@ private boolean handleReloadCommand(CommandSender sender){ } } private boolean handleBanCommand(CommandSender sender, String banName) { - if (sender.hasPermission("betterelo.reload")) { + if (sender.hasPermission("betterelo.ban")) { if (!(sender instanceof Player)) { + sender.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[BetterElo]" + ChatColor.AQUA + " Banning player " + banName); sender.sendMessage("Komenda dostępna tylko dla graczy."); - pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterEloCommand: handleBanCommand: sender " + sender + " dont have permission to use /br tl"); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterEloCommand: handleBanCommand: sender " + sender + " don't have permission to use /br tl"); return false; } Player player = (Player) sender; // Pobierz listę interakcji dla gracza, którego chcesz zbanować - Map> interactionsMap = PKDB.getPlayerInteractions(banName); + Map interactionsMap = PKDB.getPlayerInteractions(banName); if (interactionsMap.isEmpty()) { sender.sendMessage("Brak interakcji do zbanowania dla gracza " + banName); return false; } - double totalPoints = 0; - - for (List playerInteractions : interactionsMap.values()) { - for (PlayerKillDatabase.PlayerInteraction interaction : playerInteractions) { - totalPoints += interaction.getTotalPoints(); - } - } - - if (totalPoints > 0) { - // Gracz zasługuje na karę (odejmowanie punktów) - for (String rankingType : interactionsMap.keySet()) { - event.subtractPoints(player.getUniqueId().toString(), totalPoints, rankingType); - } - sender.sendMessage("Gracz " + banName + " stracił " + totalPoints + " punktów."); - } else { - // Gracz nie zasługuje na karę (dodawanie punktów) - for (String rankingType : interactionsMap.keySet()) { - event.addPoints(player.getUniqueId().toString(), Math.abs(totalPoints), rankingType); + for (Map.Entry entry : interactionsMap.entrySet()) { + String rankingType = entry.getKey(); + double totalPoints = entry.getValue(); + + if (totalPoints > 0) { + // Gracz zasługuje na karę (odejmowanie punktów) + event.subtractPoints(banName, totalPoints, rankingType); + sender.sendMessage("Gracz " + banName + " stracił " + totalPoints + " punktów w trybie " + rankingType); + } else if (totalPoints < 0) { + // Gracz nie zasługuje na karę (dodawanie punktów) + event.addPoints(banName, Math.abs(totalPoints), rankingType); + sender.sendMessage("Gracz " + banName + " otrzymał " + Math.abs(totalPoints) + " punktów nagrody w trybie " + rankingType); + } else { + sender.sendMessage("Gracz " + banName + " nie ma żadnych punktów do zmiany w trybie " + rankingType); } - sender.sendMessage("Gracz " + banName + " otrzymał " + Math.abs(totalPoints) + " punktów nagrody."); } // Usuń rekordy gracza z wszystkich tabel PKDB.deletePlayerRecords(banName); return true; } else { - pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterEloCommand: handleBanCommand: sender " + sender + " dont have permission to use /br tl"); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterEloCommand: handleBanCommand: sender " + sender + " don't have permission to use /br tl"); return false; } } + + + public String getOfflinePlayerUUID(String playerName) { OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(playerName); diff --git a/src/main/java/betterbox/mine/game/betterelo/BetterRanksCheaters.java b/src/main/java/betterbox/mine/game/betterelo/BetterRanksCheaters.java index 264b845..4f4116c 100644 --- a/src/main/java/betterbox/mine/game/betterelo/BetterRanksCheaters.java +++ b/src/main/java/betterbox/mine/game/betterelo/BetterRanksCheaters.java @@ -21,34 +21,36 @@ public BetterRanksCheaters(JavaPlugin plugin, PluginLogger pluginLogger) { } public void CheckCheatersFromBetterRanks() { - pluginLogger.log(PluginLogger.LogLevel.DEBUG_LVL2,"CheckCheatersFromBetterRanks called"); - File dataFolder = plugin.getDataFolder(); + pluginLogger.log(PluginLogger.LogLevel.DEBUG_LVL3,"CheckCheatersFromBetterRanks called"); + File dataFolder = plugin.getServer().getPluginManager().getPlugin("BetterRanks").getDataFolder(); if (!dataFolder.exists()) { - pluginLogger.log(PluginLogger.LogLevel.WARNING,"BetterRanks plugin folder doesn't exists!"); + pluginLogger.log(PluginLogger.LogLevel.WARNING, "BetterRanks plugin folder doesn't exist!"); return; // Jeśli folder danych nie istnieje, to nie ma co sprawdzać. } - File configFile = new File(dataFolder, "plugins/BetterRanks/database.yml"); + File configFile = new File(dataFolder, "database.yml"); if (!configFile.exists()) { - pluginLogger.log(PluginLogger.LogLevel.WARNING,"BetterRanks database.yml doesn't exists!"); + pluginLogger.log(PluginLogger.LogLevel.WARNING, "BetterRanks database.yml doesn't exist!"); return; // Jeśli plik database.yml nie istnieje, to nie ma co sprawdzać. } + FileConfiguration config = YamlConfiguration.loadConfiguration(configFile); Set playerNames = config.getKeys(false); - pluginLogger.log(PluginLogger.LogLevel.DEBUG_LVL2,"BetterRanksCheaters: CheckCheatersFromBetterRanks checking.."); + pluginLogger.log(PluginLogger.LogLevel.DEBUG_LVL3,"BetterRanksCheaters: CheckCheatersFromBetterRanks checking.."); + cheatersList.clear(); for (String playerName : playerNames) { String rank = config.getString(playerName + ".rank"); if (rank != null && rank.equalsIgnoreCase("CHEATER")) { cheatersList.add(playerName); - pluginLogger.log(PluginLogger.LogLevel.DEBUG_LVL2,"BetterRanksCheaters: CheckCheatersFromBetterRanks: adding cheater "+playerName); + pluginLogger.log(PluginLogger.LogLevel.DEBUG_LVL3,"BetterRanksCheaters: CheckCheatersFromBetterRanks: adding cheater "+playerName); } } - pluginLogger.log(PluginLogger.LogLevel.DEBUG_LVL2,"BetterRanksCheaters: CheckCheatersFromBetterRanks: Cheaters found: "+ Arrays.toString(cheatersList.toArray())); + pluginLogger.log(PluginLogger.LogLevel.DEBUG_LVL3,"BetterRanksCheaters: CheckCheatersFromBetterRanks: Cheaters found: "+ Arrays.toString(cheatersList.toArray())); } public List getCheatersList() { - pluginLogger.log(PluginLogger.LogLevel.DEBUG_LVL2,"BetterRanksCheaters: getCheatersList called"); + pluginLogger.log(PluginLogger.LogLevel.DEBUG_LVL3,"BetterRanksCheaters: getCheatersList called"); return cheatersList; } } diff --git a/src/main/java/betterbox/mine/game/betterelo/PlayerKillDatabase.java b/src/main/java/betterbox/mine/game/betterelo/PlayerKillDatabase.java index 2635fcf..e725149 100644 --- a/src/main/java/betterbox/mine/game/betterelo/PlayerKillDatabase.java +++ b/src/main/java/betterbox/mine/game/betterelo/PlayerKillDatabase.java @@ -30,7 +30,7 @@ private void createDatabaseIfNeeded() { } } public void saveKillData(String rankingType, String victimName, String killerName, double pointsEarned) { - pluginLogger.log(PluginLogger.LogLevel.DEBUG, "PlayerKillDatabase: saveKillData called with parameters: " + rankingType+" "+victimName+" "+killerName); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "PlayerKillDatabase: saveKillData called with parameters: " + rankingType+" "+victimName+" "+killerName+" "+pointsEarned); try { Connection connection = DriverManager.getConnection(DATABASE_URL); @@ -40,13 +40,14 @@ public void saveKillData(String rankingType, String victimName, String killerNam preparedStatement.setString(1, killerName); preparedStatement.setString(2, victimName); preparedStatement.setDouble(3, pointsEarned); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "PlayerKillDatabase: saveKillData: insertSQL "+insertSQL); preparedStatement.executeUpdate(); preparedStatement.close(); connection.close(); } catch (SQLException e) { - e.printStackTrace(); + pluginLogger.log(PluginLogger.LogLevel.ERROR,"PlayerKillDatabase: createDatabaseIfNeeded: "+e.getMessage()); } } public void deletePlayerRecords(String playerName) { @@ -79,43 +80,40 @@ private void deleteRecordsFromTable(Connection connection, String tableName, Str preparedStatement.executeUpdate(); preparedStatement.close(); } - public Map> getPlayerInteractions(String playerName) { - Map> interactionsMap = new HashMap<>(); + public Map getPlayerInteractions(String playerName) { + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "PlayerKillDatabase: getPlayerInteractions called with parameters: " + playerName); + Map interactionsMap = new HashMap<>(); // Tabele do przeszukania String[] tables = {"daily", "weekly", "monthly", "main"}; for (String table : tables) { - List playerInteractions = new ArrayList<>(); - try { Connection connection = DriverManager.getConnection(DATABASE_URL); // Zapytanie SQL do zliczania sumy zabójstw gracza X na graczu Y - String queryKillsXonY = "SELECT victimname, SUM(pointearned) AS total_points " + + String queryKillsXonY = "SELECT SUM(pointearned) AS total_points,victimname " + "FROM " + table + " " + - "WHERE killername = ? AND victimname = ?"; + "WHERE killername = ?"; PreparedStatement preparedStatementKillsXonY = connection.prepareStatement(queryKillsXonY); preparedStatementKillsXonY.setString(1, playerName); - preparedStatementKillsXonY.setString(2, playerName); ResultSet resultSetKillsXonY = preparedStatementKillsXonY.executeQuery(); while (resultSetKillsXonY.next()) { - String victimName = resultSetKillsXonY.getString("victimname"); double totalPoints = resultSetKillsXonY.getDouble("total_points"); - - playerInteractions.add(new PlayerInteraction(victimName, totalPoints)); + interactionsMap.put(table, totalPoints); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "PlayerKillDatabase: getPlayerInteractions: " + playerName + " - Table: " + table + ", Points: " + totalPoints); } resultSetKillsXonY.close(); preparedStatementKillsXonY.close(); // Zapytanie SQL do zliczania sumy zabójstw gracza Y na graczu X - String queryKillsYonX = "SELECT killername, SUM(pointearned) AS total_points " + + String queryKillsYonX = "SELECT SUM(pointearned) AS total_points,killername " + "FROM " + table + " " + - "WHERE killername = ? AND victimname = ?"; + "WHERE victimname = ?"; PreparedStatement preparedStatementKillsYonX = connection.prepareStatement(queryKillsYonX); preparedStatementKillsYonX.setString(1, playerName); @@ -124,24 +122,30 @@ public Map> getPlayerInteractions(String playerN ResultSet resultSetKillsYonX = preparedStatementKillsYonX.executeQuery(); while (resultSetKillsYonX.next()) { - String killerName = resultSetKillsYonX.getString("killername"); double totalPoints = resultSetKillsYonX.getDouble("total_points"); - - playerInteractions.add(new PlayerInteraction(killerName, totalPoints)); + interactionsMap.put(table, interactionsMap.getOrDefault(table, 0.0) + totalPoints); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "PlayerKillDatabase: getPlayerInteractions: " + playerName + " - Table: " + table + ", Points (updated): " + interactionsMap.get(table)); } resultSetKillsYonX.close(); preparedStatementKillsYonX.close(); connection.close(); } catch (SQLException e) { - e.printStackTrace(); + pluginLogger.log(PluginLogger.LogLevel.ERROR, "PlayerKillDatabase: getPlayerInteractions: " + e.getMessage()); } + } - interactionsMap.put(table, playerInteractions); + // Wypisz obecne wyniki/punkty dla każdej tabeli + for (Map.Entry entry : interactionsMap.entrySet()) { + String tableName = entry.getKey(); + double totalPoints = entry.getValue(); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "PlayerKillDatabase: getPlayerInteractions: " + playerName + " - Table: " + tableName + ", Total Points: " + totalPoints); } return interactionsMap; } + + public void clearTable(String tableName) { pluginLogger.log(PluginLogger.LogLevel.DEBUG, "PlayerKillDatabase: clearTable: called with parameters "+tableName); try { diff --git a/src/main/java/betterbox/mine/game/betterelo/PluginLogger.java b/src/main/java/betterbox/mine/game/betterelo/PluginLogger.java index a44d764..d983a11 100644 --- a/src/main/java/betterbox/mine/game/betterelo/PluginLogger.java +++ b/src/main/java/betterbox/mine/game/betterelo/PluginLogger.java @@ -20,7 +20,7 @@ public class PluginLogger { // Enumeracja dla poziomów logowania public enum LogLevel { - INFO, WARNING, ERROR, DEBUG, DEBUG_LVL2 + INFO, WARNING, ERROR, DEBUG, DEBUG_LVL2, DEBUG_LVL3 } public PluginLogger(String folderPath, Set enabledLogLevels, JavaPlugin plugin) { From 5428abd827d3ff5c548ab7773d0b6d87b7bb91dc Mon Sep 17 00:00:00 2001 From: Grzybol Date: Sun, 26 Nov 2023 21:53:10 +0100 Subject: [PATCH 3/5] - it works? still testing but it looks like it works --- pom.xml | 7 +-- .../mine/game/betterelo/BetterEloCommand.java | 41 ++++++++++------- .../game/betterelo/PlayerKillDatabase.java | 45 ++++++++++++------- 3 files changed, 54 insertions(+), 39 deletions(-) diff --git a/pom.xml b/pom.xml index 441f1ca..d1cd24f 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ betterbox.mine.game BetterElo - 3.0.15-SNAPSHOT + 3.0.18-SNAPSHOT jar BetterElo @@ -105,11 +105,6 @@ - - net.kyori - adventure-platform-bukkit - 4.3.1 - org.eclipse.jetty jetty-server diff --git a/src/main/java/betterbox/mine/game/betterelo/BetterEloCommand.java b/src/main/java/betterbox/mine/game/betterelo/BetterEloCommand.java index ec65a6a..fe490e3 100644 --- a/src/main/java/betterbox/mine/game/betterelo/BetterEloCommand.java +++ b/src/main/java/betterbox/mine/game/betterelo/BetterEloCommand.java @@ -14,6 +14,7 @@ import java.io.File; import java.io.IOException; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; @@ -252,8 +253,8 @@ private boolean handleReloadCommand(CommandSender sender){ private boolean handleBanCommand(CommandSender sender, String banName) { if (sender.hasPermission("betterelo.ban")) { if (!(sender instanceof Player)) { - sender.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[BetterElo]" + ChatColor.AQUA + " Banning player " + banName); - sender.sendMessage("Komenda dostępna tylko dla graczy."); + + sender.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[BetterElo] " + ChatColor.DARK_RED +"You don't have permission to use this command!"); pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterEloCommand: handleBanCommand: sender " + sender + " don't have permission to use /br tl"); return false; } @@ -261,27 +262,32 @@ private boolean handleBanCommand(CommandSender sender, String banName) { Player player = (Player) sender; // Pobierz listę interakcji dla gracza, którego chcesz zbanować - Map interactionsMap = PKDB.getPlayerInteractions(banName); + HashMap> interactionsMap = PKDB.getPlayerInteractions(banName); if (interactionsMap.isEmpty()) { sender.sendMessage("Brak interakcji do zbanowania dla gracza " + banName); return false; } - - for (Map.Entry entry : interactionsMap.entrySet()) { + sender.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[BetterElo]" + ChatColor.AQUA + " Banning player " + banName); + for (Map.Entry> entry : interactionsMap.entrySet()) { String rankingType = entry.getKey(); - double totalPoints = entry.getValue(); - - if (totalPoints > 0) { - // Gracz zasługuje na karę (odejmowanie punktów) - event.subtractPoints(banName, totalPoints, rankingType); - sender.sendMessage("Gracz " + banName + " stracił " + totalPoints + " punktów w trybie " + rankingType); - } else if (totalPoints < 0) { - // Gracz nie zasługuje na karę (dodawanie punktów) - event.addPoints(banName, Math.abs(totalPoints), rankingType); - sender.sendMessage("Gracz " + banName + " otrzymał " + Math.abs(totalPoints) + " punktów nagrody w trybie " + rankingType); - } else { - sender.sendMessage("Gracz " + banName + " nie ma żadnych punktów do zmiany w trybie " + rankingType); + HashMap interactions = entry.getValue(); + + for (Map.Entry interactionEntry : interactions.entrySet()) { + String otherPlayer = interactionEntry.getKey(); + double totalPoints = interactionEntry.getValue(); + + if (totalPoints > 0) { + // Gracz zasługuje na karę (odejmowanie punktów) + event.subtractPoints(otherPlayer, totalPoints, rankingType); + sender.sendMessage("Gracz " + otherPlayer + " stracił " + totalPoints + " punktów w trybie " + rankingType); + } else if (totalPoints < 0) { + // Gracz nie zasługuje na karę (dodawanie punktów) + event.addPoints(otherPlayer, Math.abs(totalPoints), rankingType); + sender.sendMessage("Gracz " + otherPlayer + " otrzymał " + Math.abs(totalPoints) + " punktów nagrody w trybie " + rankingType); + } else { + sender.sendMessage("Gracz " + otherPlayer + " nie ma żadnych punktów do zmiany w trybie " + rankingType); + } } } @@ -297,6 +303,7 @@ private boolean handleBanCommand(CommandSender sender, String banName) { + public String getOfflinePlayerUUID(String playerName) { OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(playerName); diff --git a/src/main/java/betterbox/mine/game/betterelo/PlayerKillDatabase.java b/src/main/java/betterbox/mine/game/betterelo/PlayerKillDatabase.java index e725149..ea04b68 100644 --- a/src/main/java/betterbox/mine/game/betterelo/PlayerKillDatabase.java +++ b/src/main/java/betterbox/mine/game/betterelo/PlayerKillDatabase.java @@ -80,9 +80,11 @@ private void deleteRecordsFromTable(Connection connection, String tableName, Str preparedStatement.executeUpdate(); preparedStatement.close(); } - public Map getPlayerInteractions(String playerName) { + public HashMap> getPlayerInteractions(String playerName) + { pluginLogger.log(PluginLogger.LogLevel.DEBUG, "PlayerKillDatabase: getPlayerInteractions called with parameters: " + playerName); - Map interactionsMap = new HashMap<>(); + + HashMap> interactionsMap = new HashMap<>(); // Tabele do przeszukania String[] tables = {"daily", "weekly", "monthly", "main"}; @@ -92,41 +94,44 @@ public Map getPlayerInteractions(String playerName) { Connection connection = DriverManager.getConnection(DATABASE_URL); // Zapytanie SQL do zliczania sumy zabójstw gracza X na graczu Y - String queryKillsXonY = "SELECT SUM(pointearned) AS total_points,victimname " + - "FROM " + table + " " + - "WHERE killername = ?"; + String queryKillsXonY = "SELECT victimname, SUM(pointearned) AS total_points FROM " + table + " WHERE killername = ? GROUP BY victimname"; PreparedStatement preparedStatementKillsXonY = connection.prepareStatement(queryKillsXonY); preparedStatementKillsXonY.setString(1, playerName); ResultSet resultSetKillsXonY = preparedStatementKillsXonY.executeQuery(); + HashMap victimsInteractions = new HashMap<>(); while (resultSetKillsXonY.next()) { + String victimname = resultSetKillsXonY.getString("victimname"); double totalPoints = resultSetKillsXonY.getDouble("total_points"); - interactionsMap.put(table, totalPoints); - pluginLogger.log(PluginLogger.LogLevel.DEBUG, "PlayerKillDatabase: getPlayerInteractions: " + playerName + " - Table: " + table + ", Points: " + totalPoints); + + victimsInteractions.put(victimname, totalPoints); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "PlayerKillDatabase: getPlayerInteractions: " + playerName + " - Table: " + table + ", Victim: " + victimname + ", Points: " + totalPoints); } + //interactionsMap.put(table, victimsInteractions; resultSetKillsXonY.close(); preparedStatementKillsXonY.close(); // Zapytanie SQL do zliczania sumy zabójstw gracza Y na graczu X - String queryKillsYonX = "SELECT SUM(pointearned) AS total_points,killername " + - "FROM " + table + " " + - "WHERE victimname = ?"; + String queryKillsYonX = "SELECT killername, SUM(pointearned) AS total_points FROM " + table + " WHERE victimname = ? GROUP BY killername"; PreparedStatement preparedStatementKillsYonX = connection.prepareStatement(queryKillsYonX); preparedStatementKillsYonX.setString(1, playerName); - preparedStatementKillsYonX.setString(2, playerName); ResultSet resultSetKillsYonX = preparedStatementKillsYonX.executeQuery(); while (resultSetKillsYonX.next()) { + String killername = resultSetKillsYonX.getString("killername"); double totalPoints = resultSetKillsYonX.getDouble("total_points"); - interactionsMap.put(table, interactionsMap.getOrDefault(table, 0.0) + totalPoints); - pluginLogger.log(PluginLogger.LogLevel.DEBUG, "PlayerKillDatabase: getPlayerInteractions: " + playerName + " - Table: " + table + ", Points (updated): " + interactionsMap.get(table)); + + victimsInteractions.put(killername, victimsInteractions.getOrDefault(killername, 0.0) - totalPoints); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "PlayerKillDatabase: getPlayerInteractions: " + playerName + " - Table: " + table + ", Killer: " + killername + ", Points (updated): " + victimsInteractions.get(killername)); } + interactionsMap.put(table, victimsInteractions); + resultSetKillsYonX.close(); preparedStatementKillsYonX.close(); connection.close(); @@ -136,10 +141,18 @@ public Map getPlayerInteractions(String playerName) { } // Wypisz obecne wyniki/punkty dla każdej tabeli - for (Map.Entry entry : interactionsMap.entrySet()) { + for (HashMap.Entry> entry : interactionsMap.entrySet()) + { String tableName = entry.getKey(); - double totalPoints = entry.getValue(); - pluginLogger.log(PluginLogger.LogLevel.DEBUG, "PlayerKillDatabase: getPlayerInteractions: " + playerName + " - Table: " + tableName + ", Total Points: " + totalPoints); + HashMap victimsInteractions = entry.getValue(); + + for (HashMap.Entry entry1 : victimsInteractions.entrySet()) + { + String victimname = entry1.getKey(); + Double totalPoints = entry1.getValue(); + + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "PlayerKillDatabase: getPlayerInteractions: " + playerName + " - Table: " + tableName + ", Victim: " + victimname + ", Total Points: " + totalPoints); + } } return interactionsMap; From 42fa2b6eab94c3e4b9e105a7652b2e0ee55f1515 Mon Sep 17 00:00:00 2001 From: Grzybol Date: Sun, 26 Nov 2023 22:46:10 +0100 Subject: [PATCH 4/5] -it just works --- pom.xml | 2 +- .../mine/game/betterelo/BetterEloCommand.java | 17 +++++++++++++---- .../mine/game/betterelo/PlayerKillDatabase.java | 4 ++-- .../mine/game/betterelo/PluginLogger.java | 2 +- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/pom.xml b/pom.xml index d1cd24f..675e9c8 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ betterbox.mine.game BetterElo - 3.0.18-SNAPSHOT + 3.0.22-SNAPSHOT jar BetterElo diff --git a/src/main/java/betterbox/mine/game/betterelo/BetterEloCommand.java b/src/main/java/betterbox/mine/game/betterelo/BetterEloCommand.java index fe490e3..45e1c72 100644 --- a/src/main/java/betterbox/mine/game/betterelo/BetterEloCommand.java +++ b/src/main/java/betterbox/mine/game/betterelo/BetterEloCommand.java @@ -279,12 +279,12 @@ private boolean handleBanCommand(CommandSender sender, String banName) { if (totalPoints > 0) { // Gracz zasługuje na karę (odejmowanie punktów) - event.subtractPoints(otherPlayer, totalPoints, rankingType); - sender.sendMessage("Gracz " + otherPlayer + " stracił " + totalPoints + " punktów w trybie " + rankingType); + event.addPoints(getOfflinePlayerUUID(otherPlayer), totalPoints, rankingType); + sender.sendMessage("Gracz " + otherPlayer + " otrzymał " + totalPoints + " punktów w trybie " + rankingType); } else if (totalPoints < 0) { // Gracz nie zasługuje na karę (dodawanie punktów) - event.addPoints(otherPlayer, Math.abs(totalPoints), rankingType); - sender.sendMessage("Gracz " + otherPlayer + " otrzymał " + Math.abs(totalPoints) + " punktów nagrody w trybie " + rankingType); + event.subtractPoints(getOfflinePlayerUUID(otherPlayer), Math.abs(totalPoints), rankingType); + sender.sendMessage("Gracz " + otherPlayer + " stracił " + Math.abs(totalPoints) + " punktów nagrody w trybie " + rankingType); } else { sender.sendMessage("Gracz " + otherPlayer + " nie ma żadnych punktów do zmiany w trybie " + rankingType); } @@ -292,7 +292,16 @@ private boolean handleBanCommand(CommandSender sender, String banName) { } // Usuń rekordy gracza z wszystkich tabel + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterEloCommand: handleBanCommand: PKDB.deletePlayerRecords(banName)"); PKDB.deletePlayerRecords(banName); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterEloCommand: handleBanCommand: setting 100 points for "+banName+" in main"); + dataManager.setPoints(getOfflinePlayerUUID(banName),1000d,"main"); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterEloCommand: handleBanCommand: setting 100 points for "+banName+" in daily"); + dataManager.setPoints(getOfflinePlayerUUID(banName),1000d,"daily"); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterEloCommand: handleBanCommand: setting 100 points for "+banName+" in weekly"); + dataManager.setPoints(getOfflinePlayerUUID(banName),1000d,"weekly"); + pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterEloCommand: handleBanCommand: setting 100 points for "+banName+" in monthly"); + dataManager.setPoints(getOfflinePlayerUUID(banName),1000d,"monthly"); return true; } else { pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterEloCommand: handleBanCommand: sender " + sender + " don't have permission to use /br tl"); diff --git a/src/main/java/betterbox/mine/game/betterelo/PlayerKillDatabase.java b/src/main/java/betterbox/mine/game/betterelo/PlayerKillDatabase.java index ea04b68..7745709 100644 --- a/src/main/java/betterbox/mine/game/betterelo/PlayerKillDatabase.java +++ b/src/main/java/betterbox/mine/game/betterelo/PlayerKillDatabase.java @@ -127,7 +127,7 @@ public HashMap> getPlayerInteractions(String pla double totalPoints = resultSetKillsYonX.getDouble("total_points"); victimsInteractions.put(killername, victimsInteractions.getOrDefault(killername, 0.0) - totalPoints); - pluginLogger.log(PluginLogger.LogLevel.DEBUG, "PlayerKillDatabase: getPlayerInteractions: " + playerName + " - Table: " + table + ", Killer: " + killername + ", Points (updated): " + victimsInteractions.get(killername)); + pluginLogger.log(PluginLogger.LogLevel.DEBUG_LVL4, "PlayerKillDatabase: getPlayerInteractions: " + playerName + " - Table: " + table + ", Killer: " + killername + ", Points (updated): " + victimsInteractions.get(killername)); } interactionsMap.put(table, victimsInteractions); @@ -151,7 +151,7 @@ public HashMap> getPlayerInteractions(String pla String victimname = entry1.getKey(); Double totalPoints = entry1.getValue(); - pluginLogger.log(PluginLogger.LogLevel.DEBUG, "PlayerKillDatabase: getPlayerInteractions: " + playerName + " - Table: " + tableName + ", Victim: " + victimname + ", Total Points: " + totalPoints); + pluginLogger.log(PluginLogger.LogLevel.DEBUG_LVL4, "PlayerKillDatabase: getPlayerInteractions: " + playerName + " - Table: " + tableName + ", Victim: " + victimname + ", Total Points: " + totalPoints); } } diff --git a/src/main/java/betterbox/mine/game/betterelo/PluginLogger.java b/src/main/java/betterbox/mine/game/betterelo/PluginLogger.java index d983a11..48baa95 100644 --- a/src/main/java/betterbox/mine/game/betterelo/PluginLogger.java +++ b/src/main/java/betterbox/mine/game/betterelo/PluginLogger.java @@ -20,7 +20,7 @@ public class PluginLogger { // Enumeracja dla poziomów logowania public enum LogLevel { - INFO, WARNING, ERROR, DEBUG, DEBUG_LVL2, DEBUG_LVL3 + INFO, WARNING, ERROR, DEBUG, DEBUG_LVL2, DEBUG_LVL3,DEBUG_LVL4 } public PluginLogger(String folderPath, Set enabledLogLevels, JavaPlugin plugin) { From f0adf351d180021fa4af89e7de13820714c05af1 Mon Sep 17 00:00:00 2001 From: Grzybol Date: Sun, 26 Nov 2023 22:56:43 +0100 Subject: [PATCH 5/5] -fixed /be reload --- pom.xml | 2 +- .../mine/game/betterelo/ExtendedConfigManager.java | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 675e9c8..9ce4ac2 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ betterbox.mine.game BetterElo - 3.0.22-SNAPSHOT + 3.0.23-SNAPSHOT jar BetterElo diff --git a/src/main/java/betterbox/mine/game/betterelo/ExtendedConfigManager.java b/src/main/java/betterbox/mine/game/betterelo/ExtendedConfigManager.java index 20c89b0..096fd8d 100644 --- a/src/main/java/betterbox/mine/game/betterelo/ExtendedConfigManager.java +++ b/src/main/java/betterbox/mine/game/betterelo/ExtendedConfigManager.java @@ -12,6 +12,7 @@ public class ExtendedConfigManager { private JavaPlugin plugin; private PluginLogger pluginLogger; List logLevels = null; + private File configFile = null; Set enabledLogLevels; public ExtendedConfigManager(JavaPlugin plugin, PluginLogger pluginLogger) { @@ -47,7 +48,7 @@ private void configureLogger() { public void ReloadConfig() { pluginLogger.log(PluginLogger.LogLevel.DEBUG, "ConfigManager: ReloadConfig called"); // Odczytanie ustawień log_level z pliku konfiguracyjnego - File configFile = new File(plugin.getDataFolder(), "config.yml"); + configFile = new File(plugin.getDataFolder(), "config.yml"); plugin.reloadConfig(); logLevels = plugin.getConfig().getStringList("log_level"); enabledLogLevels = new HashSet<>(); @@ -70,11 +71,13 @@ public void ReloadConfig() { plugin.getServer().getLogger().warning("Invalid log level in config: " + level); } } + pluginLogger.setEnabledLogLevels(enabledLogLevels); + } public void updateConfig(String configuration) { - File configFile = new File(plugin.getDataFolder(), "config.yml"); + configFile = new File(plugin.getDataFolder(), "config.yml"); if (!configFile.exists()) { pluginLogger.log(PluginLogger.LogLevel.WARNING, "Config file does not exist, creating new one.");