Skip to content

Commit

Permalink
Added persist command
Browse files Browse the repository at this point in the history
  • Loading branch information
cheezychicken authored Jan 28, 2023
1 parent 07eb61d commit 0f80494
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
7 changes: 6 additions & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
name: PvPToggle
version: 0.1
version: 0.3
description: Toggles PvP for people
database: true
author: cheezychicken
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: |
/<command> on - Turns PvP on for the player
/<command> on persist - Turns PvP on for the player. This will persist through death.
/<command> off - Turns PvP off for the player
/<command> 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
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</repository>
<repository>
<id>sk89q-repo</id>
<url>https://maven.sk89q.com/repo/com/sk89q/worldguard/</url>
<url>https://maven.enginehub.org/repo/</url>
</repository>
</repositories>
<dependencies>
Expand All @@ -34,9 +34,9 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sk89q</groupId>
<groupId>com.sk89q.worldguard</groupId>
<artifactId>worldguard-bukkit</artifactId>
<version>7.0.5</version>
<version>7.0.7</version>
</dependency>
<dependency>
<groupId>nu.nerd</groupId>
Expand Down
36 changes: 33 additions & 3 deletions src/main/java/com/cheezychicken/pvptoggle/PvPToggle.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,7 +34,17 @@ public class PvPToggle extends JavaPlugin implements Listener {
*/
private static final Set<UUID> ENABLED_PLAYERS = new HashSet<>();

// ------------------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------------------
/**
* Stores the UUIDs of the players with PVP persisting.
*/
private static final Set<UUID> PERSISTED_PLAYERS = new HashSet<>();

// ------------------------------------------------------------------------------------------------------


/**
* @see JavaPlugin#onEnable().
*/
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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");
}
}
Expand Down Expand Up @@ -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);
Expand All @@ -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());
}

}

0 comments on commit 0f80494

Please sign in to comment.