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.util.ApiChecks;
import java.util.Objects;

public record PolicyRule(
        String id,
        boolean enabled,
        RuleTier tier,
        SubjectMatcher subject,
        CommandMatcher command,
        DecisionEffect effect,
        CommandConstraints constraints,
        String reason,
        String groupId,
        String groupName,
        Boolean groupEnabled
) {
    public static final String DEFAULT_GROUP_ID = "default";
    public static final String DEFAULT_GROUP_NAME = "Default policy group";

    public PolicyRule {
        id = ApiChecks.notBlank(id, "id");
        Objects.requireNonNull(tier, "tier");
        Objects.requireNonNull(subject, "subject");
        Objects.requireNonNull(command, "command");
        Objects.requireNonNull(effect, "effect");
        Objects.requireNonNull(constraints, "constraints");
        reason = ApiChecks.notBlank(reason, "reason");
        groupId = groupId == null || groupId.isBlank() ? DEFAULT_GROUP_ID : groupId.strip();
        groupName = groupName == null || groupName.isBlank() ? DEFAULT_GROUP_NAME : groupName.strip();
        groupEnabled = groupEnabled == null ? Boolean.TRUE : groupEnabled;
    }

    /** Backward-compatible constructor for rules created before named policy groups. */
    public PolicyRule(
            String id,
            boolean enabled,
            RuleTier tier,
            SubjectMatcher subject,
            CommandMatcher command,
            DecisionEffect effect,
            CommandConstraints constraints,
            String reason
    ) {
        this(id, enabled, tier, subject, command, effect, constraints, reason,
                DEFAULT_GROUP_ID, DEFAULT_GROUP_NAME, Boolean.TRUE);
    }

    public int specificity() {
        return subject.specificity() + command.specificity();
    }

    public boolean effectiveEnabled() {
        return enabled && groupEnabled;
    }
}