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

XFEServerManager

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

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

import com.xfestudio.xfeservermanager.api.command.CommandSourceKind;
import com.xfestudio.xfeservermanager.api.util.ApiChecks;
import java.util.Map;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Set;
import java.util.UUID;

/** Identity and authority presented to the policy engine. actorId may be null for non-player sources. */
public record ActorContext(
        UUID actorId,
        String displayName,
        Set<String> roles,
        Map<String, Integer> roleWeights,
        int opLevel,
        CommandSourceKind source,
        boolean onlineMode
) {
    public ActorContext {
        displayName = ApiChecks.notBlank(displayName, "displayName");
        roles = ApiChecks.immutableLowercaseSet(roles, "roles");
        Map<String, Integer> normalizedWeights = new LinkedHashMap<>();
        for (Map.Entry<String, Integer> entry : roleWeights.entrySet()) {
            String role = ApiChecks.lowerIdentifier(entry.getKey(), "roleWeights key");
            Integer weight = java.util.Objects.requireNonNull(entry.getValue(), "roleWeights value");
            if (weight < 0) {
                throw new IllegalArgumentException("role weights must be non-negative");
            }
            normalizedWeights.put(role, weight);
        }
        roleWeights = Map.copyOf(normalizedWeights);
        if (opLevel < 0 || opLevel > 4) {
            throw new IllegalArgumentException("opLevel must be between 0 and 4");
        }
        if (source == null) {
            throw new NullPointerException("source");
        }
    }

    public int weightOf(String role) {
        return roleWeights.getOrDefault(role.toLowerCase(Locale.ROOT), 0);
    }
}