Update /hg stuck cooldown handling with Duration for improved time management
This commit is contained in:
parent
016ab17ef2
commit
4cf4361286
|
|
@ -16,14 +16,13 @@ import org.bukkit.entity.Firework;
|
|||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.meta.FireworkMeta;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.*;
|
||||
|
||||
public class Stuck extends SubCommand {
|
||||
|
||||
private final Map<UUID, Long> cooldowns = new HashMap<>();
|
||||
private final Map<UUID, Instant> cooldowns = new HashMap<>();
|
||||
|
||||
@Override
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
|
||||
if (isOnCooldown(player)) {
|
||||
long remaining = getRemainingCooldown(player);
|
||||
Duration remaining = getRemainingCooldown(player);
|
||||
player.sendRichMessage(Messages.STUCK.ON_COOLDOWN,
|
||||
Placeholder.parsed("time", remaining + "s"));
|
||||
Placeholder.parsed("time", remaining.toSeconds() + "s")
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
Location safeLocation = findSafeLocation(player.getLocation());
|
||||
if (safeLocation == null) {
|
||||
Optional<Location> safeLocation = findSafeLocation(player.getLocation());
|
||||
if (safeLocation.isEmpty()) {
|
||||
player.sendRichMessage(Messages.STUCK.NO_SAFE_LOCATION);
|
||||
return true;
|
||||
}
|
||||
|
||||
player.teleport(safeLocation.add(0.5, 1, 0.5));
|
||||
player.teleport(safeLocation.get().add(0.5, 1, 0.5));
|
||||
spawnFireworks(player.getLocation());
|
||||
player.sendRichMessage(Messages.STUCK.TELEPORTED);
|
||||
cooldowns.put(player.getUniqueId(), System.currentTimeMillis());
|
||||
cooldowns.put(player.getUniqueId(), Instant.now());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isOnCooldown(Player player) {
|
||||
if (!cooldowns.containsKey(player.getUniqueId())) {
|
||||
Instant lastUse = cooldowns.get(player.getUniqueId());
|
||||
if (lastUse == null) {
|
||||
return false;
|
||||
}
|
||||
long lastUse = cooldowns.get(player.getUniqueId());
|
||||
return (System.currentTimeMillis() - lastUse) < (Config.DESTINATION.STUCK_COOLDOWN_SECONDS * 1000L);
|
||||
return Duration.between(lastUse, Instant.now()).compareTo(Config.DESTINATION.STUCK_COOLDOWN) < 0;
|
||||
}
|
||||
|
||||
private long getRemainingCooldown(Player player) {
|
||||
if (!cooldowns.containsKey(player.getUniqueId())) {
|
||||
return 0;
|
||||
private Duration getRemainingCooldown(Player player) {
|
||||
Instant lastUse = cooldowns.get(player.getUniqueId());
|
||||
if (lastUse == null) {
|
||||
return Duration.ZERO;
|
||||
}
|
||||
long lastUse = cooldowns.get(player.getUniqueId());
|
||||
long elapsed = (System.currentTimeMillis() - lastUse) / 1000;
|
||||
return Math.max(0, Config.DESTINATION.STUCK_COOLDOWN_SECONDS - elapsed);
|
||||
Duration elapsed = Duration.between(lastUse, Instant.now());
|
||||
Duration remaining = Config.DESTINATION.STUCK_COOLDOWN.minus(elapsed);
|
||||
return remaining.isNegative() ? Duration.ZERO : remaining;
|
||||
}
|
||||
|
||||
private Location findSafeLocation(Location start) {
|
||||
private Optional<Location> findSafeLocation(Location start) {
|
||||
int radius = 10;
|
||||
for (int r = 0; r <= radius; r++) {
|
||||
for (int x = -r; x <= r; x++) {
|
||||
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
|
||||
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)) {
|
||||
return target.getLocation();
|
||||
return Optional.of(target.getLocation());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
private boolean isSafe(Block block) {
|
||||
|
|
|
|||
|
|
@ -112,7 +112,7 @@ public class Config extends AbstractConfig {
|
|||
|
||||
public static int STUCK_MIN_HEIGHT = 64;
|
||||
public static int STUCK_FIREWORK_COUNT = 3;
|
||||
public static int STUCK_COOLDOWN_SECONDS = 300;
|
||||
public static Duration STUCK_COOLDOWN = Duration.ofMinutes(2);
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
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_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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user