Skip to content

Commit

Permalink
refactor: store invulnerability as tag, fix #687
Browse files Browse the repository at this point in the history
  • Loading branch information
WiIIiam278 committed Oct 31, 2024
1 parent aa86dc5 commit d55260f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
19 changes: 14 additions & 5 deletions bukkit/src/main/java/net/william278/huskhomes/user/BukkitUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}

}
13 changes: 0 additions & 13 deletions common/src/main/java/net/william278/huskhomes/HuskHomes.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,19 +228,6 @@ default boolean isWarmingUp(@NotNull UUID userUuid) {
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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
21 changes: 12 additions & 9 deletions fabric/src/main/java/net/william278/huskhomes/user/FabricUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d55260f

Please sign in to comment.