Skip to content

Commit

Permalink
Added option to reset cooldowns when someone becomes king (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
Despical committed Apr 19, 2024
1 parent fa2ebe3 commit 6cc44a1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/main/java/me/despical/kotl/ConfigPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public enum Option {
REMOVE_COOLDOWN_ON_LEAVE("King-Settings.Remove-Cooldown-On.Leave", false),
COOLDOWN_WHEN_ALONE("King-Settings.Cooldown-When-Alone", false),
SHOW_COOLDOWN_ON_REJOIN("King-Settings.Show-Cooldown-If-Rejoin"),
RESET_COOLDOWNS_ON_NEW_KING("King-Settings.Reset-Cooldowns-On-New-King"),
APPLY_KING_DELAY_BAR("King-Settings.Apply-Cooldown-Bar"),
UPDATE_GAME_MODE((config) -> !config.getStringList("Inventory-Manager.Do-Not-Restore").contains("game-mode")),
UPDATE_HUNGER((config) -> !config.getStringList("Inventory-Manager.Do-Not-Restore").contains("hunger")),
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/me/despical/kotl/api/StatsStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ public static int getUserStats(Player player, StatisticType statisticType) {
}

public enum StatisticType {
TOURS_PLAYED("toursplayed"), SCORE("score");

TOURS_PLAYED("toursplayed"),
SCORE("score"),
LOCAL_RESET_COOLDOWN("local_reset_cooldown", false);

final String name;
final boolean persistent;
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/me/despical/kotl/events/ArenaEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.HashSet;

/**
* @author Despical
* <p>
Expand Down Expand Up @@ -88,6 +90,13 @@ public void onInteractWithPlate(PlayerInteractEvent event) {

arena.setKing(player.getName());

if (plugin.getOption(ConfigPreferences.Option.RESET_COOLDOWNS_ON_NEW_KING)) {
var players = new HashSet<>(arena.getPlayers());
players.remove(player);

players.stream().map(plugin.getUserManager()::getUser).forEach(pUser -> pUser.setStat(StatsStorage.StatisticType.LOCAL_RESET_COOLDOWN, 1));
}

chatManager.broadcastAction(arena, player, ActionType.NEW_KING);

user.addStat(StatsStorage.StatisticType.SCORE, 1);
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/me/despical/kotl/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.despical.kotl.ConfigPreferences;
import me.despical.kotl.Main;
import me.despical.kotl.api.StatsStorage;
import me.despical.kotl.user.User;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
Expand Down Expand Up @@ -31,6 +32,14 @@ public static void applyActionBarCooldown(User user, int seconds) {

@Override
public void run() {
if (user.getStat(StatsStorage.StatisticType.LOCAL_RESET_COOLDOWN) == 1) {
cancel();

user.setCooldown("king", 0);
user.setStat(StatsStorage.StatisticType.LOCAL_RESET_COOLDOWN, 0);
return;
}

final var arena = user.getArena();

user.setCooldown("king", seconds - Math.ceil(ticks / 20D));
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ King-Settings:
# Should we show the cooldown bar if player has cooldown and rejoins the arena again?
Show-Cooldown-If-Rejoin: true

# Should we reset players' cooldown, other than new king, when someone becomes king?
Reset-Cooldowns-On-New-King: true

Remove-Cooldown-On:
# Should we remove player's becoming king cooldown when they join an arena, if they have?
Join: false
Expand Down

0 comments on commit 6cc44a1

Please sign in to comment.