From 0f804943a5bbdcbbe180d928b3a472107cd4a076 Mon Sep 17 00:00:00 2001 From: cheezychicken <53493763+cheezychicken@users.noreply.github.com> Date: Sat, 28 Jan 2023 21:11:25 +0000 Subject: [PATCH] Added persist command --- plugin.yml | 7 +++- pom.xml | 6 ++-- .../cheezychicken/pvptoggle/PvPToggle.java | 36 +++++++++++++++++-- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/plugin.yml b/plugin.yml index 74490ee..77afadf 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,5 +1,5 @@ name: PvPToggle -version: 0.1 +version: 0.3 description: Toggles PvP for people database: true author: cheezychicken @@ -7,15 +7,20 @@ website: ${project.url} main: com.cheezychicken.pvptoggle.PvPToggle api-version: 1.16 +depend: [WorldGuard, NerdBoard] + commands: pvp: description: Toggle a player's AdminHunt mode permission: pvp.toggle usage: | / on - Turns PvP on for the player + / on persist - Turns PvP on for the player. This will persist through death. / off - Turns PvP off for the player / list - Lists players with PvP on permissions: pvp.toggle: description: Allow use of /pvp command + pvp.others: + description: Allow users to change pvp status of other people diff --git a/pom.xml b/pom.xml index c69277c..ae8ac65 100644 --- a/pom.xml +++ b/pom.xml @@ -22,7 +22,7 @@ sk89q-repo - https://maven.sk89q.com/repo/com/sk89q/worldguard/ + https://maven.enginehub.org/repo/ @@ -34,9 +34,9 @@ provided - com.sk89q + com.sk89q.worldguard worldguard-bukkit - 7.0.5 + 7.0.7 nu.nerd diff --git a/src/main/java/com/cheezychicken/pvptoggle/PvPToggle.java b/src/main/java/com/cheezychicken/pvptoggle/PvPToggle.java index d281577..b707bfd 100644 --- a/src/main/java/com/cheezychicken/pvptoggle/PvPToggle.java +++ b/src/main/java/com/cheezychicken/pvptoggle/PvPToggle.java @@ -7,6 +7,7 @@ import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; +import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -33,7 +34,17 @@ public class PvPToggle extends JavaPlugin implements Listener { */ private static final Set ENABLED_PLAYERS = new HashSet<>(); + // ------------------------------------------------------------------------------------------------------ + // ------------------------------------------------------------------------------------------------------ + /** + * Stores the UUIDs of the players with PVP persisting. + */ + private static final Set PERSISTED_PLAYERS = new HashSet<>(); + + // ------------------------------------------------------------------------------------------------------ + + /** * @see JavaPlugin#onEnable(). */ @@ -94,11 +105,20 @@ public boolean onCommand(CommandSender sender, Command command, String name, Str Player player = (Player) sender; pvpStatusOn(player, "Player"); return true; + + } else if (args[1].equalsIgnoreCase("persist")) { + Player player = (Player) sender; + UUID uuid = player.getUniqueId(); + PERSISTED_PLAYERS.add(uuid); + pvpStatusOn(player, "Player"); + return true; + // Admins can change other people's } else if (sender.hasPermission("pvp.others")) { Player player = Bukkit.getPlayer(args[1]); pvpStatusOn(player, "Admin"); - return true; + return true; + } else { return false; } @@ -158,7 +178,7 @@ private void onPVPDamage(DisallowedPVPEvent event) { @EventHandler public void onPlayerDeath(PlayerDeathEvent event) { Player player = event.getEntity(); - if (isActive(player)) { + if (isActive(player) && !isPersisted(player)) { pvpStatusOff(player, "Death"); } } @@ -236,6 +256,9 @@ private void pvpStatusOff(Player player, String reason) { } else if (reason == "Admin") { msg = ChatColor.RED + player.getName() + " has had their PvP turned off." + ChatColor.RESET; } + if(isPersisted(player)) { + PERSISTED_PLAYERS.remove(uuid); + } ENABLED_PLAYERS.remove(uuid); if (msg != "") { getServer().broadcastMessage(msg); @@ -257,6 +280,13 @@ private void pvpStatusOff(Player player, String reason) { public static boolean isActive(Player player) { return ENABLED_PLAYERS.contains(player.getUniqueId()); } - + // ------------------------------------------------------------------------------------------------------ + /** + * @param player the player. + * @return true if the player has pvp persisted + */ + public static boolean isPersisted(Player player) { + return PERSISTED_PLAYERS.contains(player.getUniqueId()); + } }