diff --git a/forge/src/main/java/me/lucko/luckperms/forge/capabilities/UserCapabilityImpl.java b/forge/src/main/java/me/lucko/luckperms/forge/capabilities/UserCapabilityImpl.java index 17a55f3c8..a9cd8d2e1 100644 --- a/forge/src/main/java/me/lucko/luckperms/forge/capabilities/UserCapabilityImpl.java +++ b/forge/src/main/java/me/lucko/luckperms/forge/capabilities/UserCapabilityImpl.java @@ -36,6 +36,7 @@ import net.luckperms.api.query.QueryOptions; import net.luckperms.api.util.Tristate; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.player.Player; +import net.minecraftforge.common.util.LazyOptional; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -44,6 +45,19 @@ import java.util.Locale; public class UserCapabilityImpl implements UserCapability { + private static LazyOptional getCapability(Player player) { + if (!player.isRemoved()) { + return player.getCapability(CAPABILITY); + } else { + player.reviveCaps(); + try { + return player.getCapability(CAPABILITY); + } finally { + player.invalidateCaps(); + } + } + } + /** * Gets a {@link UserCapability} for a given {@link ServerPlayer}. * @@ -51,8 +65,7 @@ public class UserCapabilityImpl implements UserCapability { * @return the capability */ public static @NotNull UserCapabilityImpl get(@NotNull Player player) { - return (UserCapabilityImpl) player.getCapability(CAPABILITY) - .orElseThrow(() -> new IllegalStateException("Capability missing for " + player.getUUID())); + return (UserCapabilityImpl) getCapability(player).orElseThrow(() -> new IllegalStateException("Capability missing for " + player.getUUID())); } /** @@ -62,7 +75,7 @@ public class UserCapabilityImpl implements UserCapability { * @return the capability, or null */ public static @Nullable UserCapabilityImpl getNullable(@NotNull Player player) { - return (UserCapabilityImpl) player.getCapability(CAPABILITY).resolve().orElse(null); + return (UserCapabilityImpl) getCapability(player).resolve().orElse(null); } private boolean initialised = false;