Update /hg stuck cooldown handling with Duration for improved time management

This commit is contained in:
akastijn 2026-06-15 22:41:45 +02:00
parent 016ab17ef2
commit 4cf4361286
2 changed files with 46 additions and 34 deletions

View File

@ -16,14 +16,13 @@ import org.bukkit.entity.Firework;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.meta.FireworkMeta; import org.bukkit.inventory.meta.FireworkMeta;
import java.util.HashMap; import java.time.Duration;
import java.util.List; import java.time.Instant;
import java.util.Map; import java.util.*;
import java.util.UUID;
public class Stuck extends SubCommand { public class Stuck extends SubCommand {
private final Map<UUID, Long> cooldowns = new HashMap<>(); private final Map<UUID, Instant> cooldowns = new HashMap<>();
@Override @Override
public boolean onCommand(CommandSender commandSender, String[] args) { public boolean onCommand(CommandSender commandSender, String[] args) {
@ -34,67 +33,80 @@ public class Stuck extends SubCommand {
if (player.getLocation().getY() < Config.DESTINATION.STUCK_MIN_HEIGHT) { if (player.getLocation().getY() < Config.DESTINATION.STUCK_MIN_HEIGHT) {
player.sendRichMessage(Messages.STUCK.TOO_LOW, player.sendRichMessage(Messages.STUCK.TOO_LOW,
Placeholder.parsed("height", String.valueOf(Config.DESTINATION.STUCK_MIN_HEIGHT))); Placeholder.parsed("height", String.valueOf(Config.DESTINATION.STUCK_MIN_HEIGHT))
);
return true; return true;
} }
if (isOnCooldown(player)) { if (isOnCooldown(player)) {
long remaining = getRemainingCooldown(player); Duration remaining = getRemainingCooldown(player);
player.sendRichMessage(Messages.STUCK.ON_COOLDOWN, player.sendRichMessage(Messages.STUCK.ON_COOLDOWN,
Placeholder.parsed("time", remaining + "s")); Placeholder.parsed("time", remaining.toSeconds() + "s")
);
return true; return true;
} }
Location safeLocation = findSafeLocation(player.getLocation()); Optional<Location> safeLocation = findSafeLocation(player.getLocation());
if (safeLocation == null) { if (safeLocation.isEmpty()) {
player.sendRichMessage(Messages.STUCK.NO_SAFE_LOCATION); player.sendRichMessage(Messages.STUCK.NO_SAFE_LOCATION);
return true; return true;
} }
player.teleport(safeLocation.add(0.5, 1, 0.5)); player.teleport(safeLocation.get().add(0.5, 1, 0.5));
spawnFireworks(player.getLocation()); spawnFireworks(player.getLocation());
player.sendRichMessage(Messages.STUCK.TELEPORTED); player.sendRichMessage(Messages.STUCK.TELEPORTED);
cooldowns.put(player.getUniqueId(), System.currentTimeMillis()); cooldowns.put(player.getUniqueId(), Instant.now());
return true; return true;
} }
private boolean isOnCooldown(Player player) { private boolean isOnCooldown(Player player) {
if (!cooldowns.containsKey(player.getUniqueId())) { Instant lastUse = cooldowns.get(player.getUniqueId());
if (lastUse == null) {
return false; return false;
} }
long lastUse = cooldowns.get(player.getUniqueId()); return Duration.between(lastUse, Instant.now()).compareTo(Config.DESTINATION.STUCK_COOLDOWN) < 0;
return (System.currentTimeMillis() - lastUse) < (Config.DESTINATION.STUCK_COOLDOWN_SECONDS * 1000L);
} }
private long getRemainingCooldown(Player player) { private Duration getRemainingCooldown(Player player) {
if (!cooldowns.containsKey(player.getUniqueId())) { Instant lastUse = cooldowns.get(player.getUniqueId());
return 0; if (lastUse == null) {
return Duration.ZERO;
} }
long lastUse = cooldowns.get(player.getUniqueId()); Duration elapsed = Duration.between(lastUse, Instant.now());
long elapsed = (System.currentTimeMillis() - lastUse) / 1000; Duration remaining = Config.DESTINATION.STUCK_COOLDOWN.minus(elapsed);
return Math.max(0, Config.DESTINATION.STUCK_COOLDOWN_SECONDS - elapsed); return remaining.isNegative() ? Duration.ZERO : remaining;
} }
private Location findSafeLocation(Location start) { private Optional<Location> findSafeLocation(Location start) {
int radius = 10; int radius = 10;
for (int r = 0; r <= radius; r++) { for (int r = 0; r <= radius; r++) {
for (int x = -r; x <= r; x++) { for (int x = -r; x <= r; x++) {
for (int z = -r; z <= r; z++) { for (int z = -r; z <= r; z++) {
if (Math.abs(x) != r && Math.abs(z) != r) continue; if (Math.abs(x) != r && Math.abs(z) != r) {
continue;
}
Block block = start.clone().add(x, 0, z).getBlock(); Location baseLocation = start.clone().add(x, 0, z);
Optional<Location> safeY = findSafeY(baseLocation);
if (safeY.isPresent()) {
return safeY;
}
}
}
}
return Optional.empty();
}
private Optional<Location> findSafeY(Location base) {
// Search up and down a bit from current Y // Search up and down a bit from current Y
for (int yOffset = -5; yOffset <= 5; yOffset++) { for (int yOffset = -5; yOffset <= 5; yOffset++) {
Block target = block.getRelative(0, yOffset, 0); Block target = base.getBlock().getRelative(0, yOffset, 0);
if (isSafe(target)) { if (isSafe(target)) {
return target.getLocation(); return Optional.of(target.getLocation());
} }
} }
} return Optional.empty();
}
}
return null;
} }
private boolean isSafe(Block block) { private boolean isSafe(Block block) {

View File

@ -112,7 +112,7 @@ public class Config extends AbstractConfig {
public static int STUCK_MIN_HEIGHT = 64; public static int STUCK_MIN_HEIGHT = 64;
public static int STUCK_FIREWORK_COUNT = 3; public static int STUCK_FIREWORK_COUNT = 3;
public static int STUCK_COOLDOWN_SECONDS = 300; public static Duration STUCK_COOLDOWN = Duration.ofMinutes(2);
@SuppressWarnings("unused") @SuppressWarnings("unused")
private static void load() { private static void load() {
@ -129,7 +129,7 @@ public class Config extends AbstractConfig {
STUCK_MIN_HEIGHT = config.getInt(prefix, "stuck-min-height", STUCK_MIN_HEIGHT); STUCK_MIN_HEIGHT = config.getInt(prefix, "stuck-min-height", STUCK_MIN_HEIGHT);
STUCK_FIREWORK_COUNT = config.getInt(prefix, "stuck-firework-count", STUCK_FIREWORK_COUNT); STUCK_FIREWORK_COUNT = config.getInt(prefix, "stuck-firework-count", STUCK_FIREWORK_COUNT);
STUCK_COOLDOWN_SECONDS = config.getInt(prefix, "stuck-cooldown-seconds", STUCK_COOLDOWN_SECONDS); STUCK_COOLDOWN = Duration.ofSeconds(config.getInt(prefix, "stuck-cooldown-seconds", (int) STUCK_COOLDOWN.toSeconds()));
} }
} }
} }