mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-01 14:38:53 +00:00
refactor: store invulnerability as tag, fix #687
This commit is contained in:
6 files changed
+32
-34
No files matched your search
@@ -87,7 +87,6 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
|
||||
private final Set<SavedUser> savedUsers = Sets.newHashSet();
|
||||
private final Set<UUID> currentlyOnWarmup = Sets.newConcurrentHashSet();
|
||||
private final Set<UUID> currentlyInvulnerable = Sets.newConcurrentHashSet();
|
||||
private final Map<UUID, OnlineUser> onlineUserMap = Maps.newHashMap();
|
||||
private final Map<String, List<User>> globalUserList = Maps.newConcurrentMap();
|
||||
private final List<Command> commands = Lists.newArrayList();
|
||||
|
||||
@@ -25,10 +25,12 @@ import net.william278.huskhomes.network.PluginMessageBroker;
|
||||
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 @@ import java.util.stream.Collectors;
|
||||
*/
|
||||
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 class BukkitUser extends OnlineUser {
|
||||
.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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -228,19 +228,6 @@ public interface HuskHomes extends Task.Supplier, EventDispatcher, SavePositionP
|
||||
return this.getCurrentlyOnWarmup().contains(userUuid);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
Set<UUID> 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.
|
||||
*
|
||||
|
||||
@@ -33,6 +33,7 @@ import net.william278.huskhomes.position.Position;
|
||||
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 abstract class OnlineUser extends User implements Teleportable, CommandUs
|
||||
*/
|
||||
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();
|
||||
|
||||
/**
|
||||
|
||||
@@ -100,7 +100,6 @@ public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes
|
||||
|
||||
private final Set<SavedUser> savedUsers = Sets.newHashSet();
|
||||
private final Set<UUID> currentlyOnWarmup = Sets.newConcurrentHashSet();
|
||||
private final Set<UUID> currentlyInvulnerable = Sets.newConcurrentHashSet();
|
||||
private final Map<UUID, OnlineUser> onlineUserMap = Maps.newHashMap();
|
||||
private final Map<String, List<User>> globalUserList = Maps.newConcurrentMap();
|
||||
private final List<Command> commands = Lists.newArrayList();
|
||||
|
||||
@@ -41,6 +41,7 @@ import java.util.concurrent.CompletableFuture;
|
||||
|
||||
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 class FabricUser extends OnlineUser {
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user