Skip to content

Commit

Permalink
Merge pull request #13 from Grzybol/event
Browse files Browse the repository at this point in the history
Event
  • Loading branch information
Grzybol authored Mar 4, 2024
2 parents 4f696e1 + c9ad7b3 commit a2c5768
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 102 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>3.1.55-SNAPSHOT</version>
<version>3.2.2-SNAPSHOT</version>
<packaging>jar</packaging>

<name>BetterElo</name>
Expand Down
108 changes: 75 additions & 33 deletions src/main/java/betterbox/mine/game/betterelo/BetterElo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
public final class BetterElo extends JavaPlugin {
private PluginLogger pluginLogger;
private DataManager dataManager;
public int eventDuration;
public boolean isEventEnabled;
public String eventUnit;
private Placeholders placeholders;
private CheaterCheckScheduler cheaterCheckScheduler;
private BetterRanksCheaters betterRanksCheaters;
Expand All @@ -31,7 +34,7 @@ public final class BetterElo extends JavaPlugin {
private PlayerKillDatabase PKDB;
private BetterEloCommand betterEloCommand;
private ExtendedConfigManager configManager;
private final Map<String, Boolean> rewardStates = new HashMap<>();
public Map<String, Boolean> rewardStates = new HashMap<>();
@Override
public void onEnable() {
// Inicjalizacja PluginLoggera
Expand Down Expand Up @@ -76,7 +79,7 @@ public void onEnable() {
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,configManager);
event = new Event(dataManager, pluginLogger,this,betterRanksCheaters,configManager,this);
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.");
Expand Down Expand Up @@ -125,6 +128,7 @@ public void onDisable() {
dataManager.saveDataToFileDaily();
dataManager.saveDataToFileWeekly();
dataManager.saveDataToFileMonthly();
dataManager.saveDataToFileEvent();
pluginLogger.log(PluginLogger.LogLevel.DEBUG,"BetterElo: onDisable: Plugin BetterElo został wyłączony.");
// Wyłączanie nagród (kod z metody onDisable z klasy RewardManager)
if (dailyTask != null) dailyTask.cancel();
Expand All @@ -148,6 +152,15 @@ public long getRemainingTimeForRewards(String period) {
case "monthly":
useNextMonthTime = true;
break;
case "event":
if(Objects.equals(eventUnit, "h")) {
periodMillis = TimeUnit.HOURS.toMillis(eventDuration);
}else if(Objects.equals(eventUnit, "m")){
periodMillis = TimeUnit.MINUTES.toMillis(eventDuration);
}else{
pluginLogger.log(PluginLogger.LogLevel.ERROR,"BetterElo.getRemainingTimeForRewards: eventUnit: "+eventUnit);
}
break;
default:
throw new IllegalArgumentException("Invalid period: " + period);
}
Expand All @@ -164,7 +177,7 @@ public long getRemainingTimeForRewards(String period) {
}
}

private void loadRewards() {
public void loadRewards() {
for (String rewardType : rewardStates.keySet()) {
File rewardFile = new File(getDataFolder(), rewardType + ".yml");
if (!rewardFile.exists()) {
Expand Down Expand Up @@ -206,7 +219,7 @@ public void updateLastScheduledTime(String period) {
saveConfig();
}

private void scheduleRewards(String period, long periodMillis, boolean useNextMonthTime) {
public void scheduleRewards(String period, long periodMillis, boolean useNextMonthTime) {
FileConfiguration config = getConfig();
long lastScheduledTime = config.getLong(period + "LastScheduledTime", System.currentTimeMillis());
long delay;
Expand All @@ -228,7 +241,7 @@ private void scheduleRewards(String period, long periodMillis, boolean useNextMo
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: scheduleRewards: BukkitRunnable: rewardAndReschedule done");
return;
}

//saveConfig();
new BukkitRunnable() {
@Override
public void run() {
Expand All @@ -248,6 +261,16 @@ private void rewardAndReschedule(String period, long periodMillis, boolean useNe
rewardTopPlayers(period);
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: rewardAndReschedule: rewardTopPlayers done");
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: rewardAndReschedule: Starting updateLastScheduledTime");
if(period.equals("event")){
pluginLogger.log(PluginLogger.LogLevel.INFO, "Event ended - removing data.");
FileConfiguration config = getConfig();
config.set(period + "LastScheduledTime", null);
rewardStates.remove("event");
loadRewards();
isEventEnabled=false;
saveConfig();
return;
}
updateLastScheduledTime(period);
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: rewardAndReschedule: updateLastScheduledTime done");
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: rewardAndReschedule: starting scheduleRewards");
Expand All @@ -258,14 +281,21 @@ public void rewardTopPlayers(String rewardType) {
pluginLogger.log(PluginLogger.LogLevel.INFO, "BetterElo: rewardTopPlayers: rewardType: "+rewardType);
for (int i = 1; i <= 10; i++) {
String playerName = null;
if(rewardType.equals("daily")){
playerName = dataManager.getPlayerAtPosition(i,dataManager.dailyPlayerPoints);
switch (rewardType) {
case "daily":
playerName = dataManager.getPlayerAtPosition(i, dataManager.dailyPlayerPoints);

}else if(rewardType.equals("weekly")){
playerName = dataManager.getPlayerAtPosition(i,dataManager.weeklyPlayerPoints);
break;
case "weekly":
playerName = dataManager.getPlayerAtPosition(i, dataManager.weeklyPlayerPoints);

}else if(rewardType.equals("monthly")){
playerName = dataManager.getPlayerAtPosition(i,dataManager.monthlyPayerPoints);
break;
case "monthly":
playerName = dataManager.getPlayerAtPosition(i, dataManager.monthlyPayerPoints);
break;
case "event":
playerName = dataManager.getPlayerAtPosition(i, dataManager.eventPlayerPoints);
break;
}
if (playerName != null) {
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(playerName);
Expand Down Expand Up @@ -309,27 +339,39 @@ public void rewardTopPlayers(String rewardType) {
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: rewardTopPlayers: No player at position: " + i);
}
}
if(rewardType.equals("daily")){
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: rewardTopPlayers: Clearing the daily ranking");
dataManager.dailyPlayerPoints.clear();
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: rewardTopPlayers: Saving the daily ranking");
dataManager.saveDataToFileDaily();
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: rewardTopPlayers: Resetting the daily ranking timer");
updateLastScheduledTime(rewardType);
}else if(rewardType.equals("weekly")){
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: rewardTopPlayers: Clearing the weekly ranking");
dataManager.weeklyPlayerPoints.clear();
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: rewardTopPlayers: Saving the weekly ranking");
dataManager.saveDataToFileWeekly();
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: rewardTopPlayers: Resetting the weekly ranking timer");
updateLastScheduledTime(rewardType);
}else if(rewardType.equals("monthly")){
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: rewardTopPlayers: Clearing the monthly ranking");
dataManager.monthlyPayerPoints.clear();
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: rewardTopPlayers: Saving the monthly ranking");
dataManager.saveDataToFileMonthly();
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: rewardTopPlayers: Resetting the monthly ranking timer");
updateLastScheduledTime(rewardType);
switch (rewardType) {
case "daily":
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: rewardTopPlayers: Clearing the daily ranking");
dataManager.dailyPlayerPoints.clear();
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: rewardTopPlayers: Saving the daily ranking");
dataManager.saveDataToFileDaily();
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: rewardTopPlayers: Resetting the daily ranking timer");
updateLastScheduledTime(rewardType);
break;
case "weekly":
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: rewardTopPlayers: Clearing the weekly ranking");
dataManager.weeklyPlayerPoints.clear();
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: rewardTopPlayers: Saving the weekly ranking");
dataManager.saveDataToFileWeekly();
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: rewardTopPlayers: Resetting the weekly ranking timer");
updateLastScheduledTime(rewardType);
break;
case "monthly":
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: rewardTopPlayers: Clearing the monthly ranking");
dataManager.monthlyPayerPoints.clear();
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: rewardTopPlayers: Saving the monthly ranking");
dataManager.saveDataToFileMonthly();
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: rewardTopPlayers: Resetting the monthly ranking timer");
updateLastScheduledTime(rewardType);
break;
case "event":
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: rewardTopPlayers: Clearing the event ranking");
dataManager.eventPlayerPoints.clear();
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: rewardTopPlayers: Saving the event ranking");
dataManager.saveDataToFileEvent();
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo: rewardTopPlayers: Resetting the event ranking timer");
//updateLastScheduledTime(rewardType);
break;
}
}
private void saveOfflineReward(String playerName, List<ItemStack> rewardItems) {
Expand Down Expand Up @@ -365,7 +407,7 @@ public void notifyTopPlayers(String period) {
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "BetterElo.notifyTopPlayers called with period=" + period);
DecimalFormat df = new DecimalFormat("#.##");
Duration fadeIn = Duration.ofMillis(500); // czas pojawiania się
Duration stay = Duration.ofMillis(5000); // czas wyświetlania
Duration stay = Duration.ofSeconds(5); // czas wyświetlania
Duration fadeOut = Duration.ofMillis(500); // czas znikania
Title.Times times = Title.Times.times(fadeIn, stay, fadeOut);
Component rankingNotificationTileComponent = Component.text(ChatColor.GOLD + "" + ChatColor.BOLD + period.toUpperCase() + " ranking has ended!");
Expand Down
49 changes: 45 additions & 4 deletions src/main/java/betterbox/mine/game/betterelo/BetterEloCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@

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


public class BetterEloCommand implements CommandExecutor {
Expand Down Expand Up @@ -85,6 +83,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
sender.sendMessage(ChatColor.AQUA + "/be ban <player> " + ChatColor.GREEN + "- resetting the player's rankings to 1000 and redeeming remaining poits to victims.");
sender.sendMessage(ChatColor.AQUA + "/be add <player> <points> <rankingtype> " + ChatColor.GREEN + "- adding points to given player in specific ranking (main,daily,weekly,monthly)");
sender.sendMessage(ChatColor.AQUA + "/be sub <player> <points> <rankingtype> " + ChatColor.GREEN + "- subtracting points from given player in specific ranking (main,daily,weekly,monthly)");
sender.sendMessage(ChatColor.AQUA + "/be event <duration> <timeUnit> " + ChatColor.GREEN + "- setting up event duration and time unit <h/m> ");
}
break;
case "top10":
Expand Down Expand Up @@ -175,9 +174,15 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
long weeklyTimeLeft = betterElo.getRemainingTimeForRewards("weekly");
long monthlyTimeLeft = betterElo.getRemainingTimeForRewards("monthly");


player.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[BetterElo]" + ChatColor.AQUA + " Remaining time for daily rewards: " + ChatColor.GREEN + formatTime(dailyTimeLeft));
player.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[BetterElo]" + ChatColor.AQUA + " Remaining time for weekly rewards: " + ChatColor.GREEN + formatTime(weeklyTimeLeft));
player.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[BetterElo]" + ChatColor.AQUA + " Remaining time for monthly rewards: " + ChatColor.GREEN + formatTime(monthlyTimeLeft));
if(betterElo.isEventEnabled){
long eventTimeLeft = betterElo.getRemainingTimeForRewards("event");
player.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[BetterElo]" + ChatColor.AQUA + " Remaining time for event rewards: " + ChatColor.GREEN + formatTime(eventTimeLeft));

}
break;
case "setrewards":
pluginLogger.log(PluginLogger.LogLevel.INFO, "BetterEloCommand: Player " + sender.getName() + " issued command /be setrewards");
Expand All @@ -197,6 +202,13 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
break;
case "reload":
return handleReloadCommand(sender);
case "event":
if(sender.isOp()){
sender.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[BetterElo]" + ChatColor.DARK_RED + " Usage /be event <duration> <h/m>");
}else{
sender.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[BetterElo]" + ChatColor.DARK_RED + " You don't have permission to use that command!");
return true;
}

default:
// /be <player_name> - Information about a specific player's rank and points
Expand Down Expand Up @@ -256,6 +268,35 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
betterElo.notiyBannedPlayer(args[1]);
}
break;
case 3:
if (sender.isOp()&& Objects.equals(args[0], "event")){
int eventDuration = Integer.parseInt(args[1]);
String eventUnit = args[2];
pluginLogger.log(PluginLogger.LogLevel.DEBUG,"BetterEloCommand.onCommand.event called. Duration:"+args[1]+" timeUnit:"+args[2]);
betterElo.eventDuration= Integer.parseInt(args[1]);
betterElo.eventUnit=args[2];
betterElo.isEventEnabled=true;
pluginLogger.log(PluginLogger.LogLevel.DEBUG,"BetterElo: onEnable: Planowanie nagród dziennych...");
long periodMillis =0;
if(Objects.equals(eventUnit, "h")) {
periodMillis = TimeUnit.HOURS.toMillis(eventDuration);
}else if(Objects.equals(eventUnit, "m")){
periodMillis = TimeUnit.MINUTES.toMillis(eventDuration);
}else{
pluginLogger.log(PluginLogger.LogLevel.ERROR,"BetterEloCommand.onCommand.event: eventUnit: "+eventUnit);
}
pluginLogger.log(PluginLogger.LogLevel.DEBUG,"BetterEloCommand.onCommand.event: scheduling event rewards periodMillis:"+periodMillis);
betterElo.scheduleRewards("event", periodMillis, false);
betterElo.rewardStates.put("event", true);
betterElo.loadRewards();
pluginLogger.log(PluginLogger.LogLevel.DEBUG,"BetterEloCommand.onCommand.event: calling betterElo.updateLastScheduledTime(event)");
betterElo.updateLastScheduledTime("event");
sender.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[BetterElo]" + ChatColor.AQUA + " Event started! Duration "+eventDuration+" "+eventUnit);
}else{
sender.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[BetterElo]" + ChatColor.DARK_RED + " You don't have permission to use that command!");
return true;
}
break;
case 4:
if (args[0].equalsIgnoreCase("add")){
handleAddPointsCommand(sender,args[1], Double.valueOf(args[2]),args[3]);
Expand Down
Loading

0 comments on commit a2c5768

Please sign in to comment.