Skip to content

Commit

Permalink
Merge pull request #1 from Grzybol/Ban-command
Browse files Browse the repository at this point in the history
Ban command
  • Loading branch information
Grzybol authored Nov 26, 2023
2 parents 91fcb0f + f0adf35 commit 8e80df0
Show file tree
Hide file tree
Showing 12 changed files with 609 additions and 74 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>betterbox.mine.game</groupId>
<artifactId>BetterElo</artifactId>
<version>2.1.16-SNAPSHOT</version>
<version>3.0.23-SNAPSHOT</version>
<packaging>jar</packaging>

<name>BetterElo</name>
Expand Down
23 changes: 19 additions & 4 deletions src/main/java/betterbox/mine/game/betterelo/BetterElo.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,31 @@ 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<String, Boolean> rewardStates = new HashMap<>();
@Override
public void onEnable() {
// Inicjalizacja PluginLoggera
Set<PluginLogger.LogLevel> 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());
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.");
PKDB = new PlayerKillDatabase(pluginLogger);
// Przekazujemy pluginLogger do innych klas
dataManager = new DataManager(this, pluginLogger);
pluginLogger.log(PluginLogger.LogLevel.DEBUG,"BetterElo: onEnable: Zaladowano DataManager.");
Expand Down Expand Up @@ -64,10 +69,12 @@ 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));
betterRanksCheaters = new BetterRanksCheaters(this,pluginLogger);
CheaterCheckScheduler cheaterCheckScheduler = new CheaterCheckScheduler(this, betterRanksCheaters, getServer().getScheduler(), pluginLogger);
// 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);
Expand Down Expand Up @@ -100,6 +107,11 @@ public void onEnable() {
logger.info("[BetterElo] Author " + this.getDescription().getAuthors());
logger.info("[BetterElo] Version " + this.getDescription().getVersion());
logger.info("[BetterElo] " + this.getDescription().getDescription());
// Inicjalizacja BetterRanksCheaters


pluginLogger.log(PluginLogger.LogLevel.DEBUG,"BetterElo: onEnable: calling cheaterCheckScheduler.startScheduler()");
cheaterCheckScheduler.startScheduler();

}
@Override
Expand Down Expand Up @@ -183,7 +195,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();
}

Expand Down
103 changes: 102 additions & 1 deletion src/main/java/betterbox/mine/game/betterelo/BetterEloCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


public class BetterEloCommand implements CommandExecutor {
Expand All @@ -24,14 +27,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;


}
Expand Down Expand Up @@ -175,6 +184,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 <player_name> - Information about a specific player's rank and points
playerName = args[0];
Expand Down Expand Up @@ -219,10 +231,99 @@ 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.ban")) {
if (!(sender instanceof Player)) {

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

Player player = (Player) sender;

// Pobierz listę interakcji dla gracza, którego chcesz zbanować
HashMap<String, HashMap<String, Double>> interactionsMap = PKDB.getPlayerInteractions(banName);

if (interactionsMap.isEmpty()) {
sender.sendMessage("Brak interakcji do zbanowania dla gracza " + banName);
return false;
}
sender.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[BetterElo]" + ChatColor.AQUA + " Banning player " + banName);
for (Map.Entry<String, HashMap<String, Double>> entry : interactionsMap.entrySet()) {
String rankingType = entry.getKey();
HashMap<String, Double> interactions = entry.getValue();

for (Map.Entry<String, Double> interactionEntry : interactions.entrySet()) {
String otherPlayer = interactionEntry.getKey();
double totalPoints = interactionEntry.getValue();

if (totalPoints > 0) {
// Gracz zasługuje na karę (odejmowanie punktów)
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.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);
}
}
}

// 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");
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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<String> 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_LVL3,"CheckCheatersFromBetterRanks called");
File dataFolder = plugin.getServer().getPluginManager().getPlugin("BetterRanks").getDataFolder();
if (!dataFolder.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, "database.yml");
if (!configFile.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<String> playerNames = config.getKeys(false);
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_LVL3,"BetterRanksCheaters: CheckCheatersFromBetterRanks: adding cheater "+playerName);
}
}
pluginLogger.log(PluginLogger.LogLevel.DEBUG_LVL3,"BetterRanksCheaters: CheckCheatersFromBetterRanks: Cheaters found: "+ Arrays.toString(cheatersList.toArray()));
}

public List<String> getCheatersList() {
pluginLogger.log(PluginLogger.LogLevel.DEBUG_LVL3,"BetterRanksCheaters: getCheatersList called");
return cheatersList;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Double> pointsMap = allPlayerPoints.get(ranking_type);
Expand Down
Loading

0 comments on commit 8e80df0

Please sign in to comment.