minecraft-src/com/mojang/realmsclient/dto/RealmsWorldOptions.java
2025-07-04 01:41:11 +03:00

188 lines
5.8 KiB
Java

package com.mojang.realmsclient.dto;
import com.google.gson.JsonObject;
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 org.jetbrains.annotations.Nullable;
@Environment(EnvType.CLIENT)
public class RealmsWorldOptions extends ValueObject {
public final boolean pvp;
public final boolean spawnAnimals;
public final boolean spawnMonsters;
public final boolean spawnNPCs;
public final int spawnProtection;
public final boolean commandBlocks;
public final boolean forceGameMode;
public final int difficulty;
public final int gameMode;
private final String slotName;
public final String version;
public final RealmsServer.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_ANIMALS = true;
private static final boolean DEFAULT_SPAWN_MONSTERS = true;
private static final boolean DEFAULT_SPAWN_NPCS = 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 String DEFAULT_SLOT_NAME = "";
private static final String DEFAULT_VERSION = "";
private static final RealmsServer.Compatibility DEFAULT_COMPATIBILITY = RealmsServer.Compatibility.UNVERIFIABLE;
private static final long DEFAULT_TEMPLATE_ID = -1L;
private static final String DEFAULT_TEMPLATE_IMAGE = null;
public RealmsWorldOptions(
boolean pvp,
boolean spawnAnimals,
boolean spawnMonsters,
boolean spawnNPCs,
int spawnProtection,
boolean commandBlocks,
int difficulty,
int gameMode,
boolean forceGameMode,
String slotName,
String version,
RealmsServer.Compatibility compatibility
) {
this.pvp = pvp;
this.spawnAnimals = spawnAnimals;
this.spawnMonsters = spawnMonsters;
this.spawnNPCs = spawnNPCs;
this.spawnProtection = spawnProtection;
this.commandBlocks = commandBlocks;
this.difficulty = difficulty;
this.gameMode = gameMode;
this.forceGameMode = forceGameMode;
this.slotName = slotName;
this.version = version;
this.compatibility = compatibility;
}
public static RealmsWorldOptions createDefaults() {
return new RealmsWorldOptions(true, true, true, true, 0, false, 2, 0, false, "", "", DEFAULT_COMPATIBILITY);
}
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) {
RealmsWorldOptions realmsWorldOptions = new RealmsWorldOptions(
JsonUtils.getBooleanOr("pvp", json, true),
JsonUtils.getBooleanOr("spawnAnimals", json, true),
JsonUtils.getBooleanOr("spawnMonsters", json, true),
JsonUtils.getBooleanOr("spawnNPCs", json, true),
JsonUtils.getIntOr("spawnProtection", json, 0),
JsonUtils.getBooleanOr("commandBlocks", json, false),
JsonUtils.getIntOr("difficulty", json, 2),
JsonUtils.getIntOr("gameMode", json, 0),
JsonUtils.getBooleanOr("forceGameMode", json, false),
JsonUtils.getRequiredStringOr("slotName", json, ""),
JsonUtils.getRequiredStringOr("version", json, ""),
RealmsServer.getCompatibility(JsonUtils.getRequiredStringOr("compatibility", json, RealmsServer.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.spawnAnimals) {
jsonObject.addProperty("spawnAnimals", this.spawnAnimals);
}
if (!this.spawnMonsters) {
jsonObject.addProperty("spawnMonsters", this.spawnMonsters);
}
if (!this.spawnNPCs) {
jsonObject.addProperty("spawnNPCs", this.spawnNPCs);
}
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.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.spawnAnimals,
this.spawnMonsters,
this.spawnNPCs,
this.spawnProtection,
this.commandBlocks,
this.difficulty,
this.gameMode,
this.forceGameMode,
this.slotName,
this.version,
this.compatibility
);
}
}