Skip to content

Commit

Permalink
📝bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
staFF6773 authored Apr 1, 2024
1 parent 2d09704 commit 40f3e11
Showing 1 changed file with 28 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,82 +6,55 @@
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.plugin.PluginManager;
import java.util.List;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BetterPvPNoPvPCommand extends BaseCommand implements Listener {
public class BetterPvPReloadCommand extends BaseCommand {
private final BetterPvP betterPvP;
private final Map<Player, Long> cooldowns = new HashMap<>();

private boolean pvpEnabled = true;

public BetterPvPNoPvPCommand(final BetterPvP betterPvP) {
super("pvp", new ArrayList<>(), "betterpvp.pvp", true);
public BetterPvPReloadCommand(final BetterPvP betterPvP) {
// Set the command name and permissions
super("betterpvp", new ArrayList<>(), "betterpvp.main", true);
this.betterPvP = betterPvP;

PluginManager pluginManager = betterPvP.getServer().getPluginManager();
pluginManager.registerEvents(this, betterPvP);
}

@Override
public void execute(CommandSender sender, String[] args) {
// Verify whether the "on" or "off" argument was provided.
if (args.length == 1 && (args[0].equalsIgnoreCase("on") || args[0].equalsIgnoreCase("off"))) {
// Verificar si se proporcionó el argumento "reload"
if (args.length == 1 && args[0].equalsIgnoreCase("reload")) {
// Verify player permissions
if (hasPermission(sender)) {
// Check if the PvP status is already the same as the argument
if (args[0].equalsIgnoreCase("on") && pvpEnabled) {
sender.sendMessage(ChatColorUtil.colorize(BetterPvP.prefix + " " + betterPvP.getMainConfig().getString("messages.pvp-already-enabled")));
return;
} else if (args[0].equalsIgnoreCase("off") && !pvpEnabled) {
sender.sendMessage(ChatColorUtil.colorize(BetterPvP.prefix + " " + betterPvP.getMainConfig().getString("messages.pvp-already-disabled")));
return;
}

if (args[0].equalsIgnoreCase("off") && sender instanceof Player) {
Player player = (Player) sender;
long currentTime = System.currentTimeMillis();
long defaultCooldown = 60L; // Default value in seconds
long cooldownTime = betterPvP.getMainConfig().getLong("cooldown.pvp-cooldown", defaultCooldown) * 1000;
if (cooldowns.containsKey(player) && cooldowns.get(player) + cooldownTime > currentTime) {
String CooldownError = betterPvP.getMainConfig().getString("cooldown-error-message");
sender.sendMessage(ChatColorUtil.colorize(BetterPvP.prefix + " " + CooldownError));
return;
}
cooldowns.put(player, currentTime);
}

// Get the message from the configuration
String pvpToggleMessage = betterPvP.getMainConfig().getString("pvptoggle");
// Reload configuration
betterPvP.getMainConfig().load();

// Replace "%status%" with the status provided in the command
pvpToggleMessage = pvpToggleMessage.replace("%status%", args[0]);

pvpEnabled = args[0].equalsIgnoreCase("on");

// Send success message
sender.sendMessage(ChatColorUtil.colorize(BetterPvP.prefix + " " + pvpToggleMessage));
// Enviar mensajes de éxito
sender.sendMessage(ChatColorUtil.colorize(BetterPvP.prefix + " &aThe configuration file was reloaded."));
sender.sendMessage(ChatColorUtil.colorize("&7(Some options only apply after the server has been restarted.)"));
} else {
// Send message of lack of permissions
sendNoPermissionMessage(sender);
}
} else if (args.length == 1 && args[0].equalsIgnoreCase("help")) {
sender.sendMessage(ChatColorUtil.colorize("&3====== BetterPvP Help ======"));
sender.sendMessage(ChatColorUtil.colorize("&3/betterpvp reload &7- Reload the configuration file"));
sender.sendMessage(ChatColorUtil.colorize("&f"));
sender.sendMessage(ChatColorUtil.colorize("&3/betterpvp help &7- Show this help message"));
sender.sendMessage(ChatColorUtil.colorize("&f"));
sender.sendMessage(ChatColorUtil.colorize("&3/pvp <on/off> &7- activates and deactivates player pvp"));
sender.sendMessage(ChatColorUtil.colorize("&f"));
sender.sendMessage(ChatColorUtil.colorize("&3/pvpworld <on/off> <world> &7- disable and enable global pvp for all players in that world"));
} else {
// Incorrect use message
sender.sendMessage(ChatColorUtil.colorize(BetterPvP.prefix + " Usage: /pvp <on/off>"));
sender.sendMessage(ChatColorUtil.colorize(BetterPvP.prefix + " &cUsage: /betterpvp <command>"));
}
}

public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
List<String> completions = new ArrayList<>();
if (args.length == 1) {
completions.add("on");
completions.add("off");
completions.add("reload");
completions.add("help");
}
return completions;
}
Expand All @@ -93,7 +66,7 @@ public List<String> tabComplete(CommandSender sender, String alias, String[] arg
* @return True if the player has permissions, false otherwise.
*/
private boolean hasPermission(CommandSender sender) {
return sender.hasPermission("betterpvp.pvp") || sender instanceof ConsoleCommandSender || sender.isOp();
return sender.hasPermission("betterpvp.main") || sender instanceof ConsoleCommandSender || sender.isOp();
}

/**
Expand All @@ -102,17 +75,9 @@ private boolean hasPermission(CommandSender sender) {
* @param sender The sender of the command.
*/
private void sendNoPermissionMessage(CommandSender sender) {
// Get the message from the configuration, if it does not exist, use a default one.
String noPermissionMessage = betterPvP.getMainConfig().getString("no-permission");
noPermissionMessage = noPermissionMessage.replace("%player_name%", sender.getName());
sender.sendMessage(ChatColorUtil.colorize(BetterPvP.prefix + " " + noPermissionMessage));
}

@EventHandler
public void onEntityDamage(EntityDamageEvent event) {
if (event.getEntity() instanceof Player) {
if (!pvpEnabled && event.getCause() == EntityDamageEvent.DamageCause.ENTITY_ATTACK) {
event.setCancelled(true);
}
}
}
}

0 comments on commit 40f3e11

Please sign in to comment.