PlayerDataCommon.java
package team.aura_dev.aurasudo.platform.common.player;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Optional;
import java.util.UUID;
import javax.annotation.Nonnull;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import net.kyori.adventure.text.TextComponent;
import net.luckperms.api.LuckPerms;
import net.luckperms.api.LuckPermsProvider;
import net.luckperms.api.model.user.User;
import team.aura_dev.aurasudo.api.AuraSudo;
import team.aura_dev.aurasudo.api.player.PlayerData;
import team.aura_dev.aurasudo.platform.common.AuraSudoBase;
import team.aura_dev.aurasudo.platform.common.permission.Permission;
/**
* Simple class to represent players in a platform independent way.
*
* <p>If the platform offers a way to display a display name (like with added prefixes/suffixes or a
* nickname) then it needs to override this class.
*/
@SuppressFBWarnings(
value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE",
justification = "Code generated by lombok.")
@Data
@Getter(onMethod = @__({@Nonnull}))
@EqualsAndHashCode(of = "uuid")
public abstract class PlayerDataCommon implements PlayerData {
protected final LuckPerms luckPerms = LuckPermsProvider.get();
@NonNull protected final UUID uuid;
@NonNull protected final String playerName;
protected int sudoLevel = 0;
/**
* A nice name for the player.<br>
* Can be overridden to allow showing of prefixes and nicknames.
*
* @return a nicer variant of the player name
*/
@Override
@Nonnull
public String getDisplayName() {
return playerName;
}
public void setSudoLevel(int sudoLevel) {
final int maxSudoLevel = AuraSudo.getApi().getMaxSudoLevel();
if ((sudoLevel < 0) || (sudoLevel > maxSudoLevel))
throw new IllegalArgumentException(
"sudoLevel was " + sudoLevel + ". But must be between 0 and " + maxSudoLevel);
this.sudoLevel = sudoLevel;
// Inform LP about the change
luckPerms.getContextManager().signalContextUpdate(getNativePlayer());
}
public boolean isConsole() {
return false;
}
public boolean hasPermission(Permission permission) {
return getLuckPermsUser()
.map(
user ->
user.getCachedData()
.getPermissionData()
.checkPermission(permission.getPermission())
.asBoolean())
.orElse(false);
}
public abstract void sendMessage(TextComponent message);
protected abstract Object getNativePlayer();
protected final Optional<User> getLuckPermsUser() {
final User user = luckPerms.getUserManager().getUser(uuid);
// Shouldn't happen
if (user == null) {
AuraSudoBase.logger.warn("LuckPerms had no data loaded for UUID " + uuid);
}
return Optional.ofNullable(user);
}
}