|
24 | 24 | */ |
25 | 25 | package org.spongepowered.api.world.volume.archetype; |
26 | 26 |
|
| 27 | +import org.spongepowered.api.Sponge; |
| 28 | +import org.spongepowered.api.block.BlockType; |
| 29 | +import org.spongepowered.api.event.CauseStackManager; |
| 30 | +import org.spongepowered.api.event.EventContextKeys; |
| 31 | +import org.spongepowered.api.event.cause.entity.SpawnType; |
| 32 | +import org.spongepowered.api.registry.Registry; |
| 33 | +import org.spongepowered.api.registry.RegistryTypes; |
| 34 | +import org.spongepowered.api.world.BlockChangeFlags; |
| 35 | +import org.spongepowered.api.world.biome.Biome; |
| 36 | +import org.spongepowered.api.world.server.ServerWorld; |
27 | 37 | import org.spongepowered.api.world.volume.archetype.block.entity.BlockEntityArchetypeVolume; |
28 | 38 | import org.spongepowered.api.world.volume.archetype.entity.EntityArchetypeVolume; |
29 | 39 | import org.spongepowered.api.world.volume.biome.BiomeVolume; |
30 | 40 | import org.spongepowered.api.world.volume.block.BlockVolume; |
| 41 | +import org.spongepowered.api.world.volume.stream.StreamOptions; |
| 42 | +import org.spongepowered.api.world.volume.stream.VolumeApplicators; |
| 43 | +import org.spongepowered.api.world.volume.stream.VolumeCollectors; |
| 44 | +import org.spongepowered.api.world.volume.stream.VolumePositionTranslators; |
| 45 | +import org.spongepowered.math.vector.Vector3i; |
| 46 | + |
| 47 | +import java.util.Objects; |
| 48 | +import java.util.function.Supplier; |
31 | 49 |
|
32 | 50 | public interface ArchetypeVolume extends BlockVolume.Mutable<ArchetypeVolume>, |
33 | 51 | BlockEntityArchetypeVolume.Mutable<ArchetypeVolume>, |
34 | 52 | EntityArchetypeVolume.Mutable<ArchetypeVolume>, |
35 | 53 | BiomeVolume.Mutable<ArchetypeVolume> { |
| 54 | + |
| 55 | + Registry<BlockType> blockStateRegistry(); |
| 56 | + |
| 57 | + Registry<Biome> biomeRegistry(); |
| 58 | + |
| 59 | + default void applyToWorld(final ServerWorld target, final Vector3i placement, final Supplier<SpawnType> spawnContext) { |
| 60 | + Objects.requireNonNull(target, "Target world cannot be null"); |
| 61 | + Objects.requireNonNull(placement, "Target position cannot be null"); |
| 62 | + try (final CauseStackManager.StackFrame frame = Sponge.server().causeStackManager().pushCauseFrame()) { |
| 63 | + this.blockStateStream(this.blockMin(), this.blockMax(), StreamOptions.lazily()) |
| 64 | + .apply(VolumeCollectors.of( |
| 65 | + target, |
| 66 | + VolumePositionTranslators.relativeTo(placement), |
| 67 | + VolumeApplicators.applyBlocks(BlockChangeFlags.ALL) |
| 68 | + )); |
| 69 | + |
| 70 | + this.biomeStream(this.blockMin(), this.blockMax(), StreamOptions.lazily()) |
| 71 | + // It's common that we'll have to be translating back and forth between ResourceKeys because a Biome |
| 72 | + // is reloadable between worlds (one biome instance can be limited to that world) |
| 73 | + .map((v, b, x, y, z) -> this.biomeRegistry().valueKey(b.get())) |
| 74 | + .map((v, key, x, y, z) -> target.registries().registry(RegistryTypes.BIOME).<Biome>value(key.get())) |
| 75 | + .apply(VolumeCollectors.of( |
| 76 | + target, |
| 77 | + VolumePositionTranslators.relativeTo(placement), |
| 78 | + VolumeApplicators.applyBiomes() |
| 79 | + )); |
| 80 | + this.blockEntityArchetypeStream(this.blockMin(), this.blockMax(), StreamOptions.lazily()) |
| 81 | + .apply(VolumeCollectors.of( |
| 82 | + target, |
| 83 | + VolumePositionTranslators.relativeTo(placement), |
| 84 | + VolumeApplicators.applyBlockEntityArchetype() |
| 85 | + )); |
| 86 | + frame.addContext(EventContextKeys.SPAWN_TYPE, spawnContext); |
| 87 | + this.entityArchetypeStream(this.blockMin(), this.blockMax(), StreamOptions.lazily()) |
| 88 | + .apply(VolumeCollectors.of( |
| 89 | + target, |
| 90 | + VolumePositionTranslators.relativeTo(placement), |
| 91 | + VolumeApplicators.applyEntityArchetype() |
| 92 | + )); |
| 93 | + } |
| 94 | + } |
| 95 | + |
36 | 96 | } |
0 commit comments