Fix obtaining UserCapability on Forge for respawned players

This commit is contained in:
Luck 2023-02-12 14:29:08 +00:00
parent 1bfbd27701
commit ce6031ad52
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B

View File

@ -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<UserCapability> 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;