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

XFEServerManager

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

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

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.UUID;

/** Opaque, versioned cursor for the stable UUID ordering of online-player pages. */
public final class PlayerPageCursor {
    private static final String PREFIX = "v1:";

    private PlayerPageCursor() {
    }

    public static String encode(UUID playerId) {
        String payload = PREFIX + playerId.toString().toLowerCase(java.util.Locale.ROOT);
        return Base64.getUrlEncoder().withoutPadding()
                .encodeToString(payload.getBytes(StandardCharsets.US_ASCII));
    }

    /** Returns an empty key for the first page and a canonical UUID string otherwise. */
    public static String afterKey(String cursor) {
        if (cursor == null || cursor.isBlank()) {
            return "";
        }
        if (cursor.length() > 128) {
            throw new IllegalArgumentException("player cursor is invalid");
        }
        try {
            String decoded = new String(Base64.getUrlDecoder().decode(cursor), StandardCharsets.US_ASCII);
            if (!decoded.startsWith(PREFIX)) {
                throw new IllegalArgumentException("player cursor version is unsupported");
            }
            return UUID.fromString(decoded.substring(PREFIX.length())).toString();
        } catch (IllegalArgumentException exception) {
            throw new IllegalArgumentException("player cursor is invalid", exception);
        }
    }
}