minecraft-src/com/mojang/realmsclient/dto/RealmsWorldOptions.java
2025-07-04 03:45:38 +03:00

192 lines
6.2 KiB
Java

package com.mojang.realmsclient.dto;
import com.google.gson.JsonObject;
import com.mojang.realmsclient.dto.RealmsServer.Compatibility;
import com.mojang.realmsclient.util.JsonUtils;
import java.util.Objects;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.util.StringUtil;
import net.minecraft.world.Difficulty;
import net.minecraft.world.level.GameType;
import net.minecraft.world.level.LevelSettings;
import org.jetbrains.annotations.Nullable;
@Environment(EnvType.CLIENT)
public class RealmsWorldOptions extends ValueObject {
public final boolean pvp;
public final boolean spawnMonsters;
public final int spawnProtection;
public final boolean commandBlocks;
public final boolean forceGameMode;
public final int difficulty;
public final int gameMode;
public final boolean hardcore;
private final String slotName;
public final String version;
public final Compatibility compatibility;
public long templateId;
@Nullable
public String templateImage;
public boolean empty;
private static final boolean DEFAULT_FORCE_GAME_MODE = false;
private static final boolean DEFAULT_PVP = true;
private static final boolean DEFAULT_SPAWN_MONSTERS = true;
private static final int DEFAULT_SPAWN_PROTECTION = 0;
private static final boolean DEFAULT_COMMAND_BLOCKS = false;
private static final int DEFAULT_DIFFICULTY = 2;
private static final int DEFAULT_GAME_MODE = 0;
private static final boolean DEFAULT_HARDCORE_MODE = false;
private static final String DEFAULT_SLOT_NAME = "";
private static final String DEFAULT_VERSION = "";
private static final Compatibility DEFAULT_COMPATIBILITY = Compatibility.UNVERIFIABLE;
private static final long DEFAULT_TEMPLATE_ID = -1L;
private static final String DEFAULT_TEMPLATE_IMAGE = null;
public RealmsWorldOptions(
boolean pvp,
boolean spawnMonsters,
int spawnProtection,
boolean commandBlocks,
int difficulty,
int gameMode,
boolean hardcore,
boolean forceGameMode,
String slotName,
String version,
Compatibility compatibility
) {
this.pvp = pvp;
this.spawnMonsters = spawnMonsters;
this.spawnProtection = spawnProtection;
this.commandBlocks = commandBlocks;
this.difficulty = difficulty;
this.gameMode = gameMode;
this.hardcore = hardcore;
this.forceGameMode = forceGameMode;
this.slotName = slotName;
this.version = version;
this.compatibility = compatibility;
}
public static RealmsWorldOptions createDefaults() {
return new RealmsWorldOptions(true, true, 0, false, 2, 0, false, false, "", "", DEFAULT_COMPATIBILITY);
}
public static RealmsWorldOptions createDefaultsWith(
GameType gameType, boolean commandBlocks, Difficulty difficulty, boolean hardcore, String version, String slotName
) {
return new RealmsWorldOptions(true, true, 0, commandBlocks, difficulty.getId(), gameType.getId(), hardcore, false, slotName, version, DEFAULT_COMPATIBILITY);
}
public static RealmsWorldOptions createFromSettings(LevelSettings levelSettings, boolean commandBlocks, String version) {
return createDefaultsWith(levelSettings.gameType(), commandBlocks, levelSettings.difficulty(), levelSettings.hardcore(), version, levelSettings.levelName());
}
public static RealmsWorldOptions createEmptyDefaults() {
RealmsWorldOptions realmsWorldOptions = createDefaults();
realmsWorldOptions.setEmpty(true);
return realmsWorldOptions;
}
public void setEmpty(boolean empty) {
this.empty = empty;
}
public static RealmsWorldOptions parse(JsonObject json, RealmsSettings realmsSettings) {
RealmsWorldOptions realmsWorldOptions = new RealmsWorldOptions(
JsonUtils.getBooleanOr("pvp", json, true),
JsonUtils.getBooleanOr("spawnMonsters", json, true),
JsonUtils.getIntOr("spawnProtection", json, 0),
JsonUtils.getBooleanOr("commandBlocks", json, false),
JsonUtils.getIntOr("difficulty", json, 2),
JsonUtils.getIntOr("gameMode", json, 0),
realmsSettings.hardcore(),
JsonUtils.getBooleanOr("forceGameMode", json, false),
JsonUtils.getRequiredStringOr("slotName", json, ""),
JsonUtils.getRequiredStringOr("version", json, ""),
RealmsServer.getCompatibility(JsonUtils.getRequiredStringOr("compatibility", json, Compatibility.UNVERIFIABLE.name()))
);
realmsWorldOptions.templateId = JsonUtils.getLongOr("worldTemplateId", json, -1L);
realmsWorldOptions.templateImage = JsonUtils.getStringOr("worldTemplateImage", json, DEFAULT_TEMPLATE_IMAGE);
return realmsWorldOptions;
}
public String getSlotName(int slotIndex) {
if (StringUtil.isBlank(this.slotName)) {
return this.empty ? I18n.get("mco.configure.world.slot.empty") : this.getDefaultSlotName(slotIndex);
} else {
return this.slotName;
}
}
public String getDefaultSlotName(int slotIndex) {
return I18n.get("mco.configure.world.slot", slotIndex);
}
public String toJson() {
JsonObject jsonObject = new JsonObject();
if (!this.pvp) {
jsonObject.addProperty("pvp", this.pvp);
}
if (!this.spawnMonsters) {
jsonObject.addProperty("spawnMonsters", this.spawnMonsters);
}
if (this.spawnProtection != 0) {
jsonObject.addProperty("spawnProtection", this.spawnProtection);
}
if (this.commandBlocks) {
jsonObject.addProperty("commandBlocks", this.commandBlocks);
}
if (this.difficulty != 2) {
jsonObject.addProperty("difficulty", this.difficulty);
}
if (this.gameMode != 0) {
jsonObject.addProperty("gameMode", this.gameMode);
}
if (this.hardcore) {
jsonObject.addProperty("hardcore", this.hardcore);
}
if (this.forceGameMode) {
jsonObject.addProperty("forceGameMode", this.forceGameMode);
}
if (!Objects.equals(this.slotName, "")) {
jsonObject.addProperty("slotName", this.slotName);
}
if (!Objects.equals(this.version, "")) {
jsonObject.addProperty("version", this.version);
}
if (this.compatibility != DEFAULT_COMPATIBILITY) {
jsonObject.addProperty("compatibility", this.compatibility.name());
}
return jsonObject.toString();
}
public RealmsWorldOptions clone() {
return new RealmsWorldOptions(
this.pvp,
this.spawnMonsters,
this.spawnProtection,
this.commandBlocks,
this.difficulty,
this.gameMode,
this.hardcore,
this.forceGameMode,
this.slotName,
this.version,
this.compatibility
);
}
}