RealmsServer.java

com.mojang.realmsclient.dto.RealmsServer

信息

  • 全限定名:com.mojang.realmsclient.dto.RealmsServer
  • 类型:public class
  • 包:com.mojang.realmsclient.dto
  • 源码路径:src/main/java/com/mojang/realmsclient/dto/RealmsServer.java
  • 起始行号:L30
  • 继承:ValueObject
  • 实现:ReflectionBasedSerialization
  • 职责:

    TODO

字段/常量

  • LOGGER

    • 类型: Logger
    • 修饰符: private static final
    • 源码定位: L31
    • 说明:

      TODO

  • NO_VALUE

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L32
    • 说明:

      TODO

  • WORLD_CLOSED_COMPONENT

    • 类型: Component
    • 修饰符: public static final
    • 源码定位: L33
    • 说明:

      TODO

  • id

    • 类型: long
    • 修饰符: public
    • 源码定位: L34
    • 说明:

      TODO

  • remoteSubscriptionId

    • 类型: String
    • 修饰符: public
    • 源码定位: L36
    • 说明:

      TODO

  • name

    • 类型: String
    • 修饰符: public
    • 源码定位: L38
    • 说明:

      TODO

  • motd

    • 类型: String
    • 修饰符: public
    • 源码定位: L40
    • 说明:

      TODO

  • state

    • 类型: RealmsServer.State
    • 修饰符: public
    • 源码定位: L42
    • 说明:

      TODO

  • owner

    • 类型: String
    • 修饰符: public
    • 源码定位: L44
    • 说明:

      TODO

  • ownerUUID

    • 类型: UUID
    • 修饰符: public
    • 源码定位: L46
    • 说明:

      TODO

  • players

    • 类型: List<PlayerInfo>
    • 修饰符: public
    • 源码定位: L49
    • 说明:

      TODO

  • slotList

    • 类型: List<RealmsSlot>
    • 修饰符: private
    • 源码定位: L51
    • 说明:

      TODO

  • slots

    • 类型: Map<Integer,RealmsSlot>
    • 修饰符: public
    • 源码定位: L53
    • 说明:

      TODO

  • expired

    • 类型: boolean
    • 修饰符: public
    • 源码定位: L55
    • 说明:

      TODO

  • expiredTrial

    • 类型: boolean
    • 修饰符: public
    • 源码定位: L57
    • 说明:

      TODO

  • daysLeft

    • 类型: int
    • 修饰符: public
    • 源码定位: L59
    • 说明:

      TODO

  • worldType

    • 类型: RealmsServer.WorldType
    • 修饰符: public
    • 源码定位: L61
    • 说明:

      TODO

  • isHardcore

    • 类型: boolean
    • 修饰符: public
    • 源码定位: L63
    • 说明:

      TODO

  • gameMode

    • 类型: int
    • 修饰符: public
    • 源码定位: L65
    • 说明:

      TODO

  • activeSlot

    • 类型: int
    • 修饰符: public
    • 源码定位: L67
    • 说明:

      TODO

  • minigameName

    • 类型: String
    • 修饰符: public
    • 源码定位: L69
    • 说明:

      TODO

  • minigameId

    • 类型: int
    • 修饰符: public
    • 源码定位: L71
    • 说明:

      TODO

  • minigameImage

    • 类型: String
    • 修饰符: public
    • 源码定位: L73
    • 说明:

      TODO

  • parentRealmId

    • 类型: long
    • 修饰符: public
    • 源码定位: L75
    • 说明:

      TODO

  • parentWorldName

    • 类型: String
    • 修饰符: public
    • 源码定位: L77
    • 说明:

      TODO

  • activeVersion

    • 类型: String
    • 修饰符: public
    • 源码定位: L79
    • 说明:

      TODO

  • compatibility

    • 类型: RealmsServer.Compatibility
    • 修饰符: public
    • 源码定位: L81
    • 说明:

      TODO

  • regionSelectionPreference

    • 类型: RegionSelectionPreferenceDto
    • 修饰符: public
    • 源码定位: L83
    • 说明:

      TODO

内部类/嵌套类型

  • com.mojang.realmsclient.dto.RealmsServer.Compatibility

    • 类型: enum
    • 修饰符: public static
    • 源码定位: L290
    • 说明:

      TODO

  • com.mojang.realmsclient.dto.RealmsServer.McoServerComparator

    • 类型: class
    • 修饰符: public static
    • 源码定位: L312
    • 说明:

      TODO

  • com.mojang.realmsclient.dto.RealmsServer.State

    • 类型: enum
    • 修饰符: public static
    • 源码定位: L333
    • 说明:

      TODO

  • com.mojang.realmsclient.dto.RealmsServer.WorldType

    • 类型: enum
    • 修饰符: public static
    • 源码定位: L340
    • 说明:

      TODO

构造器

方法

下面的方法块按源码顺序生成。

public String getDescription() @ L86

  • 方法名:getDescription
  • 源码定位:L86
  • 返回类型:String
  • 修饰符:public

参数:

说明:

TODO

public String getName() @ L90

  • 方法名:getName
  • 源码定位:L90
  • 返回类型:String
  • 修饰符:public

参数:

说明:

TODO

public String getMinigameName() @ L94

  • 方法名:getMinigameName
  • 源码定位:L94
  • 返回类型:String
  • 修饰符:public

参数:

说明:

TODO

public void setName(String name) @ L98

  • 方法名:setName
  • 源码定位:L98
  • 返回类型:void
  • 修饰符:public

参数:

  • name: String

说明:

TODO

public void setDescription(String motd) @ L102

  • 方法名:setDescription
  • 源码定位:L102
  • 返回类型:void
  • 修饰符:public

参数:

  • motd: String

说明:

TODO

public static RealmsServer parse(GuardedSerializer gson, String json) @ L106

  • 方法名:parse
  • 源码定位:L106
  • 返回类型:RealmsServer
  • 修饰符:public static

参数:

  • gson: GuardedSerializer
  • json: String

说明:

TODO

public static void finalize(RealmsServer server) @ L122

  • 方法名:finalize
  • 源码定位:L122
  • 返回类型:void
  • 修饰符:public static

参数:

  • server: RealmsServer

说明:

TODO

private static void sortInvited(RealmsServer server) @ L155

  • 方法名:sortInvited
  • 源码定位:L155
  • 返回类型:void
  • 修饰符:private static

参数:

  • server: RealmsServer

说明:

TODO

private static void finalizeSlots(RealmsServer server) @ L165

  • 方法名:finalizeSlots
  • 源码定位:L165
  • 返回类型:void
  • 修饰符:private static

参数:

  • server: RealmsServer

说明:

TODO

private static List<RealmsSlot> createEmptySlots() @ L175

  • 方法名:createEmptySlots
  • 源码定位:L175
  • 返回类型:List
  • 修饰符:private static

参数:

说明:

TODO

public boolean isCompatible() @ L183

  • 方法名:isCompatible
  • 源码定位:L183
  • 返回类型:boolean
  • 修饰符:public

参数:

说明:

TODO

public boolean needsUpgrade() @ L187

  • 方法名:needsUpgrade
  • 源码定位:L187
  • 返回类型:boolean
  • 修饰符:public

参数:

说明:

TODO

public boolean needsDowngrade() @ L191

  • 方法名:needsDowngrade
  • 源码定位:L191
  • 返回类型:boolean
  • 修饰符:public

参数:

说明:

TODO

public boolean shouldPlayButtonBeActive() @ L195

  • 方法名:shouldPlayButtonBeActive
  • 源码定位:L195
  • 返回类型:boolean
  • 修饰符:public

参数:

说明:

TODO

private boolean isSelfOwnedServer() @ L200

  • 方法名:isSelfOwnedServer
  • 源码定位:L200
  • 返回类型:boolean
  • 修饰符:private

参数:

说明:

TODO

public int hashCode() @ L204

  • 方法名:hashCode
  • 源码定位:L204
  • 返回类型:int
  • 修饰符:public

参数:

说明:

TODO

public boolean equals(Object obj) @ L209

  • 方法名:equals
  • 源码定位:L209
  • 返回类型:boolean
  • 修饰符:public

参数:

  • obj: Object

说明:

TODO

public RealmsServer copy() @ L231

  • 方法名:copy
  • 源码定位:L231
  • 返回类型:RealmsServer
  • 修饰符:public

参数:

说明:

TODO

public Map<Integer,RealmsSlot> cloneSlots(Map<Integer,RealmsSlot> slots) @ L261

  • 方法名:cloneSlots
  • 源码定位:L261
  • 返回类型:Map<Integer,RealmsSlot>
  • 修饰符:public

参数:

  • slots: Map<Integer,RealmsSlot>

说明:

TODO

public boolean isSnapshotRealm() @ L271

  • 方法名:isSnapshotRealm
  • 源码定位:L271
  • 返回类型:boolean
  • 修饰符:public

参数:

说明:

TODO

public boolean isMinigameActive() @ L275

  • 方法名:isMinigameActive
  • 源码定位:L275
  • 返回类型:boolean
  • 修饰符:public

参数:

说明:

TODO

public String getWorldName(int slotId) @ L279

  • 方法名:getWorldName
  • 源码定位:L279
  • 返回类型:String
  • 修饰符:public

参数:

  • slotId: int

说明:

TODO

public ServerData toServerData(String ip) @ L285

  • 方法名:toServerData
  • 源码定位:L285
  • 返回类型:ServerData
  • 修饰符:public

参数:

  • ip: String

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public class RealmsServer extends ValueObject implements ReflectionBasedSerialization {
    private static final Logger LOGGER = LogUtils.getLogger();
    private static final int NO_VALUE = -1;
    public static final Component WORLD_CLOSED_COMPONENT = Component.translatable("mco.play.button.realm.closed");
    @SerializedName("id")
    public long id = -1L;
    @SerializedName("remoteSubscriptionId")
    public @Nullable String remoteSubscriptionId;
    @SerializedName("name")
    public @Nullable String name;
    @SerializedName("motd")
    public String motd = "";
    @SerializedName("state")
    public RealmsServer.State state = RealmsServer.State.CLOSED;
    @SerializedName("owner")
    public @Nullable String owner;
    @SerializedName("ownerUUID")
    @JsonAdapter(UUIDTypeAdapter.class)
    public UUID ownerUUID = Util.NIL_UUID;
    @SerializedName("players")
    public List<PlayerInfo> players = Lists.newArrayList();
    @SerializedName("slots")
    private List<RealmsSlot> slotList = createEmptySlots();
    @Exclude
    public Map<Integer, RealmsSlot> slots = new HashMap<>();
    @SerializedName("expired")
    public boolean expired;
    @SerializedName("expiredTrial")
    public boolean expiredTrial = false;
    @SerializedName("daysLeft")
    public int daysLeft;
    @SerializedName("worldType")
    public RealmsServer.WorldType worldType = RealmsServer.WorldType.NORMAL;
    @SerializedName("isHardcore")
    public boolean isHardcore = false;
    @SerializedName("gameMode")
    public int gameMode = -1;
    @SerializedName("activeSlot")
    public int activeSlot = -1;
    @SerializedName("minigameName")
    public @Nullable String minigameName;
    @SerializedName("minigameId")
    public int minigameId = -1;
    @SerializedName("minigameImage")
    public @Nullable String minigameImage;
    @SerializedName("parentWorldId")
    public long parentRealmId = -1L;
    @SerializedName("parentWorldName")
    public @Nullable String parentWorldName;
    @SerializedName("activeVersion")
    public String activeVersion = "";
    @SerializedName("compatibility")
    public RealmsServer.Compatibility compatibility = RealmsServer.Compatibility.UNVERIFIABLE;
    @SerializedName("regionSelectionPreference")
    public @Nullable RegionSelectionPreferenceDto regionSelectionPreference;
 
    public String getDescription() {
        return this.motd;
    }
 
    public @Nullable String getName() {
        return this.name;
    }
 
    public @Nullable String getMinigameName() {
        return this.minigameName;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public void setDescription(String motd) {
        this.motd = motd;
    }
 
    public static RealmsServer parse(GuardedSerializer gson, String json) {
        try {
            RealmsServer server = gson.fromJson(json, RealmsServer.class);
            if (server == null) {
                LOGGER.error("Could not parse McoServer: {}", json);
                return new RealmsServer();
            } else {
                finalize(server);
                return server;
            }
        } catch (Exception var3) {
            LOGGER.error("Could not parse McoServer", (Throwable)var3);
            return new RealmsServer();
        }
    }
 
    public static void finalize(RealmsServer server) {
        if (server.players == null) {
            server.players = Lists.newArrayList();
        }
 
        if (server.slotList == null) {
            server.slotList = createEmptySlots();
        }
 
        if (server.slots == null) {
            server.slots = new HashMap<>();
        }
 
        if (server.worldType == null) {
            server.worldType = RealmsServer.WorldType.NORMAL;
        }
 
        if (server.activeVersion == null) {
            server.activeVersion = "";
        }
 
        if (server.compatibility == null) {
            server.compatibility = RealmsServer.Compatibility.UNVERIFIABLE;
        }
 
        if (server.regionSelectionPreference == null) {
            server.regionSelectionPreference = RegionSelectionPreferenceDto.DEFAULT;
        }
 
        sortInvited(server);
        finalizeSlots(server);
    }
 
    private static void sortInvited(RealmsServer server) {
        server.players
            .sort(
                (o1, o2) -> ComparisonChain.start()
                    .compareFalseFirst(o2.accepted, o1.accepted)
                    .compare(o1.name.toLowerCase(Locale.ROOT), o2.name.toLowerCase(Locale.ROOT))
                    .result()
            );
    }
 
    private static void finalizeSlots(RealmsServer server) {
        server.slotList.forEach(s -> server.slots.put(s.slotId, s));
 
        for (int i = 1; i <= 3; i++) {
            if (!server.slots.containsKey(i)) {
                server.slots.put(i, RealmsSlot.defaults(i));
            }
        }
    }
 
    private static List<RealmsSlot> createEmptySlots() {
        List<RealmsSlot> slots = new ArrayList<>();
        slots.add(RealmsSlot.defaults(1));
        slots.add(RealmsSlot.defaults(2));
        slots.add(RealmsSlot.defaults(3));
        return slots;
    }
 
    public boolean isCompatible() {
        return this.compatibility.isCompatible();
    }
 
    public boolean needsUpgrade() {
        return this.compatibility.needsUpgrade();
    }
 
    public boolean needsDowngrade() {
        return this.compatibility.needsDowngrade();
    }
 
    public boolean shouldPlayButtonBeActive() {
        boolean active = !this.expired && this.state == RealmsServer.State.OPEN;
        return active && (this.isCompatible() || this.needsUpgrade() || this.isSelfOwnedServer());
    }
 
    private boolean isSelfOwnedServer() {
        return Minecraft.getInstance().isLocalPlayer(this.ownerUUID);
    }
 
    @Override
    public int hashCode() {
        return Objects.hash(this.id, this.name, this.motd, this.state, this.owner, this.expired);
    }
 
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        } else if (obj == this) {
            return true;
        } else if (obj.getClass() != this.getClass()) {
            return false;
        } else {
            RealmsServer rhs = (RealmsServer)obj;
            return new EqualsBuilder()
                .append(this.id, rhs.id)
                .append(this.name, rhs.name)
                .append(this.motd, rhs.motd)
                .append(this.state, rhs.state)
                .append(this.owner, rhs.owner)
                .append(this.expired, rhs.expired)
                .append(this.worldType, this.worldType)
                .isEquals();
        }
    }
 
    public RealmsServer copy() {
        RealmsServer server = new RealmsServer();
        server.id = this.id;
        server.remoteSubscriptionId = this.remoteSubscriptionId;
        server.name = this.name;
        server.motd = this.motd;
        server.state = this.state;
        server.owner = this.owner;
        server.players = this.players;
        server.slotList = this.slotList.stream().map(RealmsSlot::copy).toList();
        server.slots = this.cloneSlots(this.slots);
        server.expired = this.expired;
        server.expiredTrial = this.expiredTrial;
        server.daysLeft = this.daysLeft;
        server.worldType = this.worldType;
        server.isHardcore = this.isHardcore;
        server.gameMode = this.gameMode;
        server.ownerUUID = this.ownerUUID;
        server.minigameName = this.minigameName;
        server.activeSlot = this.activeSlot;
        server.minigameId = this.minigameId;
        server.minigameImage = this.minigameImage;
        server.parentWorldName = this.parentWorldName;
        server.parentRealmId = this.parentRealmId;
        server.activeVersion = this.activeVersion;
        server.compatibility = this.compatibility;
        server.regionSelectionPreference = this.regionSelectionPreference != null ? this.regionSelectionPreference.copy() : null;
        return server;
    }
 
    public Map<Integer, RealmsSlot> cloneSlots(Map<Integer, RealmsSlot> slots) {
        Map<Integer, RealmsSlot> newSlots = Maps.newHashMap();
 
        for (Entry<Integer, RealmsSlot> entry : slots.entrySet()) {
            newSlots.put(entry.getKey(), new RealmsSlot(entry.getKey(), entry.getValue().options.copy(), entry.getValue().settings));
        }
 
        return newSlots;
    }
 
    public boolean isSnapshotRealm() {
        return this.parentRealmId != -1L;
    }
 
    public boolean isMinigameActive() {
        return this.worldType == RealmsServer.WorldType.MINIGAME;
    }
 
    public String getWorldName(int slotId) {
        return this.name == null
            ? this.slots.get(slotId).options.getSlotName(slotId)
            : this.name + " (" + this.slots.get(slotId).options.getSlotName(slotId) + ")";
    }
 
    public ServerData toServerData(String ip) {
        return new ServerData(Objects.requireNonNullElse(this.name, "unknown server"), ip, ServerData.Type.REALM);
    }
 
    @OnlyIn(Dist.CLIENT)
    public static enum Compatibility {
        UNVERIFIABLE,
        INCOMPATIBLE,
        RELEASE_TYPE_INCOMPATIBLE,
        NEEDS_DOWNGRADE,
        NEEDS_UPGRADE,
        COMPATIBLE;
 
        public boolean isCompatible() {
            return this == COMPATIBLE;
        }
 
        public boolean needsUpgrade() {
            return this == NEEDS_UPGRADE;
        }
 
        public boolean needsDowngrade() {
            return this == NEEDS_DOWNGRADE;
        }
    }
 
    @OnlyIn(Dist.CLIENT)
    public static class McoServerComparator implements Comparator<RealmsServer> {
        private final String refOwner;
 
        public McoServerComparator(String owner) {
            this.refOwner = owner;
        }
 
        public int compare(RealmsServer server1, RealmsServer server2) {
            return ComparisonChain.start()
                .compareTrueFirst(server1.isSnapshotRealm(), server2.isSnapshotRealm())
                .compareTrueFirst(server1.state == RealmsServer.State.UNINITIALIZED, server2.state == RealmsServer.State.UNINITIALIZED)
                .compareTrueFirst(server1.expiredTrial, server2.expiredTrial)
                .compareTrueFirst(Objects.equals(server1.owner, this.refOwner), Objects.equals(server2.owner, this.refOwner))
                .compareFalseFirst(server1.expired, server2.expired)
                .compareTrueFirst(server1.state == RealmsServer.State.OPEN, server2.state == RealmsServer.State.OPEN)
                .compare(server1.id, server2.id)
                .result();
        }
    }
 
    @OnlyIn(Dist.CLIENT)
    public static enum State {
        CLOSED,
        OPEN,
        UNINITIALIZED;
    }
 
    @OnlyIn(Dist.CLIENT)
    public static enum WorldType {
        NORMAL("normal"),
        MINIGAME("minigame"),
        ADVENTUREMAP("adventureMap"),
        EXPERIENCE("experience"),
        INSPIRATION("inspiration"),
        UNKNOWN("unknown");
 
        private static final String TRANSLATION_PREFIX = "mco.backup.entry.worldType.";
        private final Component displayName;
 
        private WorldType(String translationKey) {
            this.displayName = Component.translatable("mco.backup.entry.worldType." + translationKey);
        }
 
        public Component getDisplayName() {
            return this.displayName;
        }
    }
}

引用的其他类