From d55260fe9b413bebb8c858ab35b5faa44cedbc83 Mon Sep 17 00:00:00 2001 From: William Date: Thu, 31 Oct 2024 10:51:59 +0000 Subject: [PATCH] refactor: store invulnerability as tag, fix #687 --- .../william278/huskhomes/BukkitHuskHomes.java | 1 - .../william278/huskhomes/user/BukkitUser.java | 19 ++++++++++++----- .../net/william278/huskhomes/HuskHomes.java | 13 ------------ .../william278/huskhomes/user/OnlineUser.java | 11 +++++----- .../william278/huskhomes/FabricHuskHomes.java | 1 - .../william278/huskhomes/user/FabricUser.java | 21 +++++++++++-------- 6 files changed, 32 insertions(+), 34 deletions(-) diff --git a/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java b/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java index fa4d37bd..80fdc313 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java +++ b/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java @@ -87,7 +87,6 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask private final Set savedUsers = Sets.newHashSet(); private final Set currentlyOnWarmup = Sets.newConcurrentHashSet(); - private final Set currentlyInvulnerable = Sets.newConcurrentHashSet(); private final Map onlineUserMap = Maps.newHashMap(); private final Map> globalUserList = Maps.newConcurrentMap(); private final List commands = Lists.newArrayList(); diff --git a/bukkit/src/main/java/net/william278/huskhomes/user/BukkitUser.java b/bukkit/src/main/java/net/william278/huskhomes/user/BukkitUser.java index c447a012..d8709118 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/user/BukkitUser.java +++ b/bukkit/src/main/java/net/william278/huskhomes/user/BukkitUser.java @@ -25,10 +25,12 @@ import net.william278.huskhomes.position.Location; import net.william278.huskhomes.position.Position; import net.william278.huskhomes.teleport.TeleportationException; +import org.bukkit.NamespacedKey; import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.metadata.MetadataValue; import org.bukkit.permissions.PermissionAttachmentInfo; +import org.bukkit.persistence.PersistentDataType; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; @@ -42,6 +44,7 @@ */ public class BukkitUser extends OnlineUser { + private final NamespacedKey INVULNERABLE_KEY = new NamespacedKey((BukkitHuskHomes) plugin, "invulnerable"); private final Player player; private BukkitUser(@NotNull Player player, @NotNull BukkitHuskHomes plugin) { @@ -145,22 +148,28 @@ public boolean isVanished() { .orElse(false); } + @Override + public boolean hasInvulnerability() { + return player.getPersistentDataContainer().has(INVULNERABLE_KEY, PersistentDataType.INTEGER); + } + @Override public void handleInvulnerability() { - if (plugin.getSettings().getGeneral().getTeleportInvulnerabilityTime() <= 0) { + final long invulnerableTicks = 20L * plugin.getSettings().getGeneral().getTeleportInvulnerabilityTime(); + if (invulnerableTicks <= 0) { return; } - long invulnerabilityTimeInTicks = 20L * plugin.getSettings().getGeneral().getTeleportInvulnerabilityTime(); + player.getPersistentDataContainer().set(INVULNERABLE_KEY, PersistentDataType.INTEGER, 1); player.setInvulnerable(true); - // Remove the invulnerability - plugin.runSyncDelayed(() -> player.setInvulnerable(false), this, invulnerabilityTimeInTicks); + plugin.runSyncDelayed(this::removeInvulnerabilityIfPermitted, this, invulnerableTicks); } @Override public void removeInvulnerabilityIfPermitted() { - if (plugin.isInvulnerable(this.getUuid())) { + if (this.hasInvulnerability()) { player.setInvulnerable(false); } + player.getPersistentDataContainer().remove(INVULNERABLE_KEY); } } diff --git a/common/src/main/java/net/william278/huskhomes/HuskHomes.java b/common/src/main/java/net/william278/huskhomes/HuskHomes.java index 58cefedf..1069a1be 100644 --- a/common/src/main/java/net/william278/huskhomes/HuskHomes.java +++ b/common/src/main/java/net/william278/huskhomes/HuskHomes.java @@ -228,19 +228,6 @@ default boolean isWarmingUp(@NotNull UUID userUuid) { return this.getCurrentlyOnWarmup().contains(userUuid); } - @NotNull - Set getCurrentlyInvulnerable(); - - /** - * Returns if the given user is currently invulnerable and if it should be removed. - * - * @param uuid the user to check. - * @return if the user is currently invulnerable. - */ - default boolean isInvulnerable(@NotNull UUID uuid) { - return this.getCurrentlyInvulnerable().contains(uuid); - } - /** * Log a message to the console. * diff --git a/common/src/main/java/net/william278/huskhomes/user/OnlineUser.java b/common/src/main/java/net/william278/huskhomes/user/OnlineUser.java index 5aeb2a5b..d3559e1c 100644 --- a/common/src/main/java/net/william278/huskhomes/user/OnlineUser.java +++ b/common/src/main/java/net/william278/huskhomes/user/OnlineUser.java @@ -33,6 +33,7 @@ import net.william278.huskhomes.teleport.Teleportable; import net.william278.huskhomes.teleport.TeleportationException; import org.intellij.lang.annotations.Subst; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import java.util.*; @@ -213,13 +214,13 @@ public Audience getAudience() { */ public abstract boolean isVanished(); - /** - * Handles player invulnerability after teleporting. - * - * @since 4.6.2 - */ + @ApiStatus.Internal + public abstract boolean hasInvulnerability(); + + @ApiStatus.Internal public abstract void handleInvulnerability(); + @ApiStatus.Internal public abstract void removeInvulnerabilityIfPermitted(); /** diff --git a/fabric/src/main/java/net/william278/huskhomes/FabricHuskHomes.java b/fabric/src/main/java/net/william278/huskhomes/FabricHuskHomes.java index 5c717f78..c44cab0b 100644 --- a/fabric/src/main/java/net/william278/huskhomes/FabricHuskHomes.java +++ b/fabric/src/main/java/net/william278/huskhomes/FabricHuskHomes.java @@ -100,7 +100,6 @@ public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes private final Set savedUsers = Sets.newHashSet(); private final Set currentlyOnWarmup = Sets.newConcurrentHashSet(); - private final Set currentlyInvulnerable = Sets.newConcurrentHashSet(); private final Map onlineUserMap = Maps.newHashMap(); private final Map> globalUserList = Maps.newConcurrentMap(); private final List commands = Lists.newArrayList(); diff --git a/fabric/src/main/java/net/william278/huskhomes/user/FabricUser.java b/fabric/src/main/java/net/william278/huskhomes/user/FabricUser.java index 8db62f21..da142b45 100644 --- a/fabric/src/main/java/net/william278/huskhomes/user/FabricUser.java +++ b/fabric/src/main/java/net/william278/huskhomes/user/FabricUser.java @@ -41,6 +41,7 @@ public class FabricUser extends OnlineUser { + private final String INVULNERABLE_TAG = plugin.getKey("invulnerable").asString(); private final ServerPlayerEntity player; private FabricUser(@NotNull ServerPlayerEntity player, @NotNull FabricHuskHomes plugin) { @@ -164,26 +165,28 @@ public boolean isVanished() { return false; } - /** - * Handles player invulnerability after teleporting. - */ + @Override + public boolean hasInvulnerability() { + return player.getCommandTags().contains(INVULNERABLE_TAG); + } + @Override public void handleInvulnerability() { - if (plugin.getSettings().getGeneral().getTeleportInvulnerabilityTime() <= 0) { + final long invulnerableTicks = 20L * plugin.getSettings().getGeneral().getTeleportInvulnerabilityTime(); + if (invulnerableTicks <= 0) { return; } - long invulnerabilityTimeInTicks = 20L * plugin.getSettings().getGeneral().getTeleportInvulnerabilityTime(); player.setInvulnerable(true); - - // Remove the invulnerability - plugin.runSyncDelayed(() -> player.setInvulnerable(false), this, invulnerabilityTimeInTicks); + player.getCommandTags().add(INVULNERABLE_TAG); + plugin.runSyncDelayed(this::removeInvulnerabilityIfPermitted, this, invulnerableTicks); } @Override public void removeInvulnerabilityIfPermitted() { - if (plugin.isInvulnerable(this.getUuid())) { + if (this.hasInvulnerability()) { player.setInvulnerable(false); } + player.removeCommandTag(INVULNERABLE_TAG); } @NotNull