| 1 | package net.minecraft.world.level;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.DataResult;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import java.util.Optional;
|
| 8 | import net.minecraft.core.BlockPos;
|
| 9 | import net.minecraft.nbt.CompoundTag;
|
| 10 | import net.minecraft.resources.Identifier;
|
| 11 | import net.minecraft.server.level.ServerLevel;
|
| 12 | import net.minecraft.util.InclusiveRange;
|
| 13 | import net.minecraft.util.random.WeightedList;
|
| 14 | import net.minecraft.world.entity.EquipmentTable;
|
| 15 |
|
| 16 | public record SpawnData(CompoundTag entityToSpawn, Optional<SpawnData.CustomSpawnRules> customSpawnRules, Optional<EquipmentTable> equipment) {
|
| 17 | public static final String ENTITY_TAG = "entity";
|
| 18 | public static final Codec<SpawnData> CODEC = RecordCodecBuilder.create(
|
| 19 | i -> i.group(
|
| 20 | CompoundTag.CODEC.fieldOf("entity").forGetter(s -> s.entityToSpawn),
|
| 21 | SpawnData.CustomSpawnRules.CODEC.optionalFieldOf("custom_spawn_rules").forGetter(o -> o.customSpawnRules),
|
| 22 | EquipmentTable.CODEC.optionalFieldOf("equipment").forGetter(o -> o.equipment)
|
| 23 | )
|
| 24 | .apply(i, SpawnData::new)
|
| 25 | );
|
| 26 | public static final Codec<WeightedList<SpawnData>> LIST_CODEC = WeightedList.codec(CODEC);
|
| 27 |
|
| 28 | public SpawnData() {
|
| 29 | this(new CompoundTag(), Optional.empty(), Optional.empty());
|
| 30 | }
|
| 31 |
|
| 32 | public SpawnData {
|
| 33 | Optional<Identifier> id = entityToSpawn.read("id", Identifier.CODEC);
|
| 34 | if (id.isPresent()) {
|
| 35 | entityToSpawn.store("id", Identifier.CODEC, id.get());
|
| 36 | } else {
|
| 37 | entityToSpawn.remove("id");
|
| 38 | }
|
| 39 | }
|
| 40 |
|
| 41 | public CompoundTag getEntityToSpawn() {
|
| 42 | return this.entityToSpawn;
|
| 43 | }
|
| 44 |
|
| 45 | public Optional<SpawnData.CustomSpawnRules> getCustomSpawnRules() {
|
| 46 | return this.customSpawnRules;
|
| 47 | }
|
| 48 |
|
| 49 | public Optional<EquipmentTable> getEquipment() {
|
| 50 | return this.equipment;
|
| 51 | }
|
| 52 |
|
| 53 | public record CustomSpawnRules(InclusiveRange<Integer> blockLightLimit, InclusiveRange<Integer> skyLightLimit) {
|
| 54 | private static final InclusiveRange<Integer> LIGHT_RANGE = new InclusiveRange<>(0, 15);
|
| 55 | public static final Codec<SpawnData.CustomSpawnRules> CODEC = RecordCodecBuilder.create(
|
| 56 | i -> i.group(lightLimit("block_light_limit").forGetter(o -> o.blockLightLimit), lightLimit("sky_light_limit").forGetter(o -> o.skyLightLimit))
|
| 57 | .apply(i, SpawnData.CustomSpawnRules::new)
|
| 58 | );
|
| 59 |
|
| 60 | private static DataResult<InclusiveRange<Integer>> checkLightBoundaries(final InclusiveRange<Integer> range) {
|
| 61 | return !LIGHT_RANGE.contains(range) ? DataResult.error(() -> "Light values must be withing range " + LIGHT_RANGE) : DataResult.success(range);
|
| 62 | }
|
| 63 |
|
| 64 | private static MapCodec<InclusiveRange<Integer>> lightLimit(final String name) {
|
| 65 | return InclusiveRange.INT.lenientOptionalFieldOf(name, LIGHT_RANGE).validate(SpawnData.CustomSpawnRules::checkLightBoundaries);
|
| 66 | }
|
| 67 |
|
| 68 | public boolean isValidPosition(final BlockPos blockSpawnPos, final ServerLevel level) {
|
| 69 | return this.blockLightLimit.isValueInRange(level.getBrightness(LightLayer.BLOCK, blockSpawnPos))
|
| 70 | && this.skyLightLimit.isValueInRange(level.getEffectiveSkyBrightness(blockSpawnPos));
|
| 71 | }
|
| 72 | }
|
| 73 | }
|
| 74 |
|