XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEServerManager

【Java】我的世界XFE服务器管理器

公开
关注 0 Fork 0 Star 0
UTF-8
package com.xfestudio.xfeservermanager.core.player;

import com.xfestudio.xfeservermanager.api.command.CommandSourceKind;
import java.util.Objects;
import java.util.UUID;

/** Shared protection against peer/superior actions and unsafe offline-mode game identities. */
public final class PlayerAuthorityGuard {
    public void requireMayTarget(
            ActorAuthority actor, TargetAuthority target, boolean explicitOwnerOverride) {
        Objects.requireNonNull(actor, "actor");
        Objects.requireNonNull(target, "target");
        if (actor.actorId() != null && actor.actorId().equals(target.playerId())) {
            return;
        }
        if (target.authorityWeight() >= actor.authorityWeight()
                && !(actor.owner() && explicitOwnerOverride)) {
            throw new PlayerOperationException(
                    "protected_target", "same-rank and higher-rank targets are protected by default");
        }
    }

    public void requireDangerousAuthority(ActorAuthority actor) {
        Objects.requireNonNull(actor, "actor");
        boolean physicalConsole = actor.source() == CommandSourceKind.CONSOLE;
        boolean webOwner = actor.source() == CommandSourceKind.WEB_CONSOLE && actor.owner();
        if (!actor.onlineMode() && !physicalConsole && !webOwner) {
            throw new PlayerOperationException(
                    "offline_identity_downgrade",
                    "dangerous operations require a web owner or physical console in offline mode");
        }
    }

    public record ActorAuthority(
            UUID actorId,
            int authorityWeight,
            boolean owner,
            boolean onlineMode,
            CommandSourceKind source) {
        public ActorAuthority {
            if (authorityWeight < 0) throw new IllegalArgumentException("authorityWeight must be non-negative");
            Objects.requireNonNull(source, "source");
        }
    }

    public record TargetAuthority(UUID playerId, int authorityWeight) {
        public TargetAuthority {
            Objects.requireNonNull(playerId, "playerId");
            if (authorityWeight < 0) throw new IllegalArgumentException("authorityWeight must be non-negative");
        }
    }
}