Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
Update PvPCommand.java
Browse files Browse the repository at this point in the history
  • Loading branch information
staFF6773 authored Oct 14, 2023
1 parent 1f707fd commit d61f244
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions src/main/java/staff/commands/PvPCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class PvPCommand implements CommandExecutor, Listener {
private final Map<String, BossBar> bossBars = new HashMap<>();
private int cooldownDuration;
private int cooldownCheckerInterval;
private boolean enableBossBar;

private String noPermissionMessage;
private String incorrectUsageMessage;
Expand All @@ -39,6 +40,7 @@ public class PvPCommand implements CommandExecutor, Listener {
private String cooldownErrorMessage;
private String disabledMessage;
private String cooldownExpiredMessage;
private String bossBarMessage;

public PvPCommand(JavaPlugin plugin) {
this.plugin = plugin;
Expand All @@ -56,6 +58,7 @@ private void loadConfig() {
FileConfiguration config = plugin.getConfig();
cooldownDuration = config.getInt("pvp.cooldown_duration", 600);
cooldownCheckerInterval = config.getInt("pvp.cooldown_checker_interval", 600);
enableBossBar = config.getBoolean("pvp.enable_bossbar", true);
}

private void loadMessages() {
Expand All @@ -67,6 +70,7 @@ private void loadMessages() {
cooldownErrorMessage = ChatColor.translateAlternateColorCodes('&', config.getString("message.cooldownErrorPvP"));
disabledMessage = ChatColor.translateAlternateColorCodes('&', config.getString("message.disabledMessagePvP"));
cooldownExpiredMessage = ChatColor.translateAlternateColorCodes('&', config.getString("message.cooldownExpiredPvP"));
bossBarMessage = ChatColor.translateAlternateColorCodes('&', config.getString("message.bossBarMessage", "&cPvP disabled: &e%remaining_time%s"));
}

@Override
Expand Down Expand Up @@ -94,16 +98,20 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
cooldowns.remove(player.getName()); // Elimina el cooldown existente si hay alguno
player.sendMessage(ChatColor.translateAlternateColorCodes('&', Epicplugin.prefix + " " + activationMessage));

// Elimina la BossBar asociada al jugador si existe
removeBossBar(player);
// Elimina la BossBar asociada al jugador si existe y la BossBar está habilitada
if (enableBossBar) {
removeBossBar(player);
}
} else if (arg.equals("off")) {
if (!cooldowns.containsKey(player.getName()) || System.currentTimeMillis() - cooldowns.get(player.getName()) > cooldownDuration * 1000) {
pvpStates.put(player.getName(), false);
cooldowns.put(player.getName(), System.currentTimeMillis() + (cooldownDuration * 1000));
player.sendMessage(ChatColor.translateAlternateColorCodes('&', Epicplugin.prefix + " " + deactivationMessage.replace("%cooldown%", String.valueOf(cooldownDuration))));

// Muestra la BossBar con el tiempo restante del PvP desactivado
showCooldownBossBar(player);
if (enableBossBar) {
showCooldownBossBar(player);
}
} else {
player.sendMessage(ChatColor.translateAlternateColorCodes('&', Epicplugin.prefix + " " + cooldownErrorMessage));
}
Expand Down Expand Up @@ -135,7 +143,9 @@ public void onEntityDamage(EntityDamageByEntityEvent event) {
damager.sendMessage(disabledMessage.replace("%player%", damaged.getName()));

// Muestra la BossBar con el tiempo restante del PvP desactivado
showCooldownBossBar(damager);
if (enableBossBar) {
showCooldownBossBar(damager);
}
}
}
}
Expand All @@ -150,19 +160,25 @@ public void onPlayerPickupItem(PlayerPickupItemEvent event) {
event.setCancelled(true);

// Muestra la BossBar con el tiempo restante del PvP desactivado
showCooldownBossBar(player);
if (enableBossBar) {
showCooldownBossBar(player);
}
}
}

// Método para mostrar la BossBar con el tiempo restante del PvP desactivado
private void showCooldownBossBar(Player player) {
if (!enableBossBar) {
return; // No mostrar BossBar si está deshabilitada
}

long cooldownEndTime = cooldowns.getOrDefault(player.getName(), 0L);
long currentTime = System.currentTimeMillis();

if (cooldownEndTime > currentTime) {
// Obtiene la BossBar existente o crea una nueva
BossBar bossBar = bossBars.computeIfAbsent(player.getName(),
key -> Bukkit.createBossBar(ChatColor.translateAlternateColorCodes('&', "&cPvP desactivado: &e" + cooldownDuration + "s"), BarColor.RED, BarStyle.SOLID));
key -> Bukkit.createBossBar(parseBossBarMessage(player, cooldownDuration), BarColor.RED, BarStyle.SOLID));

// Añade la BossBar al jugador si no está presente
if (!bossBar.getPlayers().contains(player)) {
Expand All @@ -176,7 +192,7 @@ public void run() {
long remainingTime = (cooldownEndTime - System.currentTimeMillis()) / 1000L;

// Actualiza la BossBar
bossBar.setTitle(ChatColor.translateAlternateColorCodes('&', "&cPvP desactivado: &e" + remainingTime + "s"));
bossBar.setTitle(parseBossBarMessage(player, remainingTime));
bossBar.setProgress((double) remainingTime / cooldownDuration);

// Cancela la tarea si el tiempo restante es menor o igual a cero
Expand All @@ -189,15 +205,17 @@ public void run() {
}
}

// Método para eliminar la BossBar asociada a un jugador
private void removeBossBar(Player player) {
if (!enableBossBar) {
return; // No eliminar BossBar si está deshabilitada
}

BossBar bossBar = bossBars.remove(player.getName());
if (bossBar != null) {
bossBar.removeAll();
}
}

// Eventos de unirse y salir del servidor para limpiar las BossBars
private class PlayerJoinQuitListener implements Listener {

@EventHandler
Expand All @@ -213,7 +231,12 @@ public void onPlayerQuit(PlayerQuitEvent event) {
}
}

// Método para verificar los cooldowns
private String parseBossBarMessage(Player player, long remainingTime) {
return ChatColor.translateAlternateColorCodes('&', bossBarMessage
.replace("%player%", player.getName())
.replace("%remaining_time%", String.valueOf(remainingTime)));
}

private void checkCooldowns() {
long currentTime = System.currentTimeMillis();

Expand All @@ -235,7 +258,6 @@ private void checkCooldowns() {
}
}

// Método para iniciar el verificador de cooldowns
private void startCooldownChecker() {
long ticksInterval = cooldownCheckerInterval * 20L;
plugin.getServer().getScheduler().runTaskTimer(plugin, this::checkCooldowns, 20L, ticksInterval);
Expand Down

0 comments on commit d61f244

Please sign in to comment.