| 1 | package net.minecraft.world.level.levelgen;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.Optional;
|
| 7 | import java.util.OptionalLong;
|
| 8 | import net.minecraft.util.ExtraCodecs;
|
| 9 | import net.minecraft.util.RandomSource;
|
| 10 | import org.apache.commons.lang3.StringUtils;
|
| 11 |
|
| 12 | public class WorldOptions {
|
| 13 | public static final MapCodec<WorldOptions> CODEC = RecordCodecBuilder.mapCodec(
|
| 14 | i -> i.group(
|
| 15 | Codec.LONG.fieldOf("seed").stable().forGetter(WorldOptions::seed),
|
| 16 | ExtraCodecs.optionalAlwaysPresentFieldOf(Codec.BOOL, "generate_structures", true).stable().forGetter(WorldOptions::generateStructures),
|
| 17 | ExtraCodecs.optionalAlwaysPresentFieldOf(Codec.BOOL, "bonus_chest", false).stable().forGetter(WorldOptions::generateBonusChest),
|
| 18 | Codec.STRING.lenientOptionalFieldOf("legacy_custom_options").stable().forGetter(s -> s.legacyCustomOptions)
|
| 19 | )
|
| 20 | .apply(i, i.stable(WorldOptions::new))
|
| 21 | );
|
| 22 | public static final WorldOptions DEMO_OPTIONS = new WorldOptions("North Carolina".hashCode(), true, true);
|
| 23 | private final long seed;
|
| 24 | private final boolean generateStructures;
|
| 25 | private final boolean generateBonusChest;
|
| 26 | private final Optional<String> legacyCustomOptions;
|
| 27 |
|
| 28 | public WorldOptions(final long seed, final boolean generateStructures, final boolean generateBonusChest) {
|
| 29 | this(seed, generateStructures, generateBonusChest, Optional.empty());
|
| 30 | }
|
| 31 |
|
| 32 | public static WorldOptions defaultWithRandomSeed() {
|
| 33 | return new WorldOptions(randomSeed(), true, false);
|
| 34 | }
|
| 35 |
|
| 36 | public static WorldOptions testWorldWithRandomSeed() {
|
| 37 | return new WorldOptions(randomSeed(), false, false);
|
| 38 | }
|
| 39 |
|
| 40 | private WorldOptions(final long seed, final boolean generateStructures, final boolean generateBonusChest, final Optional<String> legacyCustomOptions) {
|
| 41 | this.seed = seed;
|
| 42 | this.generateStructures = generateStructures;
|
| 43 | this.generateBonusChest = generateBonusChest;
|
| 44 | this.legacyCustomOptions = legacyCustomOptions;
|
| 45 | }
|
| 46 |
|
| 47 | public long seed() {
|
| 48 | return this.seed;
|
| 49 | }
|
| 50 |
|
| 51 | public boolean generateStructures() {
|
| 52 | return this.generateStructures;
|
| 53 | }
|
| 54 |
|
| 55 | public boolean generateBonusChest() {
|
| 56 | return this.generateBonusChest;
|
| 57 | }
|
| 58 |
|
| 59 | public boolean isOldCustomizedWorld() {
|
| 60 | return this.legacyCustomOptions.isPresent();
|
| 61 | }
|
| 62 |
|
| 63 | public WorldOptions withBonusChest(final boolean generateBonusChest) {
|
| 64 | return new WorldOptions(this.seed, this.generateStructures, generateBonusChest, this.legacyCustomOptions);
|
| 65 | }
|
| 66 |
|
| 67 | public WorldOptions withStructures(final boolean generateStructures) {
|
| 68 | return new WorldOptions(this.seed, generateStructures, this.generateBonusChest, this.legacyCustomOptions);
|
| 69 | }
|
| 70 |
|
| 71 | public WorldOptions withSeed(final OptionalLong seed) {
|
| 72 | return new WorldOptions(seed.orElse(randomSeed()), this.generateStructures, this.generateBonusChest, this.legacyCustomOptions);
|
| 73 | }
|
| 74 |
|
| 75 | public static OptionalLong parseSeed(String seedString) {
|
| 76 | seedString = seedString.trim();
|
| 77 | if (StringUtils.isEmpty(seedString)) {
|
| 78 | return OptionalLong.empty();
|
| 79 | }
|
| 80 |
|
| 81 | try {
|
| 82 | return OptionalLong.of(Long.parseLong(seedString));
|
| 83 | } catch (NumberFormatException e) {
|
| 84 | return OptionalLong.of(seedString.hashCode());
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | public static long randomSeed() {
|
| 89 | return RandomSource.create().nextLong();
|
| 90 | }
|
| 91 | }
|
| 92 |
|