| 1 | package com.mojang.realmsclient.dto;
|
| 2 |
|
| 3 | import com.google.gson.annotations.SerializedName;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.client.resources.language.I18n;
|
| 7 | import net.minecraft.util.StringUtil;
|
| 8 | import net.minecraft.world.Difficulty;
|
| 9 | import net.minecraft.world.level.GameType;
|
| 10 | import net.minecraft.world.level.LevelSettings;
|
| 11 | import org.jspecify.annotations.Nullable;
|
| 12 |
|
| 13 | @Environment(EnvType.CLIENT)
|
| 14 | public class RealmsWorldOptions extends ValueObject implements ReflectionBasedSerialization {
|
| 15 | @SerializedName("spawnProtection")
|
| 16 | public int spawnProtection = 0;
|
| 17 | @SerializedName("forceGameMode")
|
| 18 | public boolean forceGameMode = false;
|
| 19 | @SerializedName("difficulty")
|
| 20 | public int difficulty = 2;
|
| 21 | @SerializedName("gameMode")
|
| 22 | public int gameMode = 0;
|
| 23 | @SerializedName("slotName")
|
| 24 | private String slotName = "";
|
| 25 | @SerializedName("version")
|
| 26 | public String version = "";
|
| 27 | @SerializedName("compatibility")
|
| 28 | public RealmsServer.Compatibility compatibility = RealmsServer.Compatibility.UNVERIFIABLE;
|
| 29 | @SerializedName("worldTemplateId")
|
| 30 | public long templateId = -1L;
|
| 31 | @SerializedName("worldTemplateImage")
|
| 32 | public @Nullable String templateImage = null;
|
| 33 | @Exclude
|
| 34 | public boolean empty;
|
| 35 |
|
| 36 | private RealmsWorldOptions() {
|
| 37 | }
|
| 38 |
|
| 39 | public RealmsWorldOptions(
|
| 40 | final int spawnProtection,
|
| 41 | final int difficulty,
|
| 42 | final int gameMode,
|
| 43 | final boolean forceGameMode,
|
| 44 | final String slotName,
|
| 45 | final String version,
|
| 46 | final RealmsServer.Compatibility compatibility
|
| 47 | ) {
|
| 48 | this.spawnProtection = spawnProtection;
|
| 49 | this.difficulty = difficulty;
|
| 50 | this.gameMode = gameMode;
|
| 51 | this.forceGameMode = forceGameMode;
|
| 52 | this.slotName = slotName;
|
| 53 | this.version = version;
|
| 54 | this.compatibility = compatibility;
|
| 55 | }
|
| 56 |
|
| 57 | public static RealmsWorldOptions createDefaults() {
|
| 58 | return new RealmsWorldOptions();
|
| 59 | }
|
| 60 |
|
| 61 | public static RealmsWorldOptions createDefaultsWith(
|
| 62 | final GameType gameMode, final Difficulty difficulty, final boolean hardcore, final String version, final String worldName
|
| 63 | ) {
|
| 64 | RealmsWorldOptions options = createDefaults();
|
| 65 | options.difficulty = difficulty.getId();
|
| 66 | options.gameMode = gameMode.getId();
|
| 67 | options.slotName = worldName;
|
| 68 | options.version = version;
|
| 69 | return options;
|
| 70 | }
|
| 71 |
|
| 72 | public static RealmsWorldOptions createFromSettings(final LevelSettings settings, final String worldVersion) {
|
| 73 | return createDefaultsWith(
|
| 74 | settings.gameType(), settings.difficultySettings().difficulty(), settings.difficultySettings().hardcore(), worldVersion, settings.levelName()
|
| 75 | );
|
| 76 | }
|
| 77 |
|
| 78 | public static RealmsWorldOptions createEmptyDefaults() {
|
| 79 | RealmsWorldOptions options = createDefaults();
|
| 80 | options.setEmpty(true);
|
| 81 | return options;
|
| 82 | }
|
| 83 |
|
| 84 | public void setEmpty(final boolean empty) {
|
| 85 | this.empty = empty;
|
| 86 | }
|
| 87 |
|
| 88 | public static RealmsWorldOptions parse(final GuardedSerializer gson, final String json) {
|
| 89 | RealmsWorldOptions options = gson.fromJson(json, RealmsWorldOptions.class);
|
| 90 | if (options == null) {
|
| 91 | return createDefaults();
|
| 92 | }
|
| 93 |
|
| 94 | finalize(options);
|
| 95 | return options;
|
| 96 | }
|
| 97 |
|
| 98 | private static void finalize(final RealmsWorldOptions options) {
|
| 99 | if (options.slotName == null) {
|
| 100 | options.slotName = "";
|
| 101 | }
|
| 102 |
|
| 103 | if (options.version == null) {
|
| 104 | options.version = "";
|
| 105 | }
|
| 106 |
|
| 107 | if (options.compatibility == null) {
|
| 108 | options.compatibility = RealmsServer.Compatibility.UNVERIFIABLE;
|
| 109 | }
|
| 110 | }
|
| 111 |
|
| 112 | public String getSlotName(final int i) {
|
| 113 | if (StringUtil.isBlank(this.slotName)) {
|
| 114 | return this.empty ? I18n.get("mco.configure.world.slot.empty") : this.getDefaultSlotName(i);
|
| 115 | } else {
|
| 116 | return this.slotName;
|
| 117 | }
|
| 118 | }
|
| 119 |
|
| 120 | public String getDefaultSlotName(final int i) {
|
| 121 | return I18n.get("mco.configure.world.slot", i);
|
| 122 | }
|
| 123 |
|
| 124 | public RealmsWorldOptions copy() {
|
| 125 | return new RealmsWorldOptions(this.spawnProtection, this.difficulty, this.gameMode, this.forceGameMode, this.slotName, this.version, this.compatibility);
|
| 126 | }
|
| 127 | }
|
| 128 |
|