Skip to content

Commit 74f41ae

Browse files
committed
Expand a bit of Schematic API to support serialiation
Signed-off-by: Gabriel Harris-Rouquette <[email protected]>
1 parent 54b95e0 commit 74f41ae

File tree

5 files changed

+73
-2
lines changed

5 files changed

+73
-2
lines changed

src/main/java/org/spongepowered/api/world/schematic/PaletteTypes.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.spongepowered.api.Sponge;
2929
import org.spongepowered.api.block.BlockState;
3030
import org.spongepowered.api.block.BlockType;
31+
import org.spongepowered.api.block.entity.BlockEntityType;
3132
import org.spongepowered.api.registry.DefaultedRegistryReference;
3233
import org.spongepowered.api.registry.RegistryKey;
3334
import org.spongepowered.api.registry.RegistryScope;
@@ -58,6 +59,8 @@ public final class PaletteTypes {
5859
*/
5960
public static final DefaultedRegistryReference<PaletteType<BlockState, BlockType>> BLOCK_STATE_PALETTE = PaletteTypes.key(ResourceKey.sponge("block_state_palette"));
6061

62+
public static final DefaultedRegistryReference<PaletteType<BlockEntityType, BlockEntityType>> BLOCK_ENTITY_PALETTE = PaletteTypes.key(ResourceKey.sponge("block_entity_type_palette"));
63+
6164
// SORTFIELDS:OFF
6265

6366
// @formatter:on

src/main/java/org/spongepowered/api/world/schematic/Schematic.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ interface Builder extends org.spongepowered.api.util.Builder<Schematic, Builder>
9090

9191
/**
9292
* Specifies an archetype volume for the world data of the schematic.
93+
* This is to take part of the schematic, so it should not be referenced
94+
* outside of the schematic, less modifications are going to be made
95+
* also affecting {@link #blockEntities(BlockEntityVolume) the block
96+
* entity volume}.
97+
*
9398
*
9499
* @param volume The archetype volume
95100
* @return This builder, for chaining

src/main/java/org/spongepowered/api/world/volume/archetype/ArchetypeVolume.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,73 @@
2424
*/
2525
package org.spongepowered.api.world.volume.archetype;
2626

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;
2737
import org.spongepowered.api.world.volume.archetype.block.entity.BlockEntityArchetypeVolume;
2838
import org.spongepowered.api.world.volume.archetype.entity.EntityArchetypeVolume;
2939
import org.spongepowered.api.world.volume.biome.BiomeVolume;
3040
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;
3149

3250
public interface ArchetypeVolume extends BlockVolume.Mutable<ArchetypeVolume>,
3351
BlockEntityArchetypeVolume.Mutable<ArchetypeVolume>,
3452
EntityArchetypeVolume.Mutable<ArchetypeVolume>,
3553
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+
3696
}

src/main/java/org/spongepowered/api/world/volume/archetype/entity/EntityArchetypeVolume.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public interface EntityArchetypeVolume extends Volume {
5353

5454
Collection<EntityArchetype> entityArchetypes();
5555

56+
Collection<EntityArchetypeEntry> entityArchetypesByPosition();
57+
5658
Collection<EntityArchetype> entityArchetypes(Predicate<EntityArchetype> filter);
5759

5860
interface Streamable<B extends Streamable<B>> extends EntityArchetypeVolume {

src/main/java/org/spongepowered/api/world/volume/stream/VolumeApplicators.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
package org.spongepowered.api.world.volume.stream;
2626

27+
import org.checkerframework.checker.nullness.qual.NonNull;
2728
import org.spongepowered.api.block.BlockState;
2829
import org.spongepowered.api.block.entity.BlockEntity;
2930
import org.spongepowered.api.block.entity.BlockEntityArchetype;
@@ -99,7 +100,6 @@ extends BlockEntity>> applyBlockEntityArchetype() {
99100
return (volume, element) -> element.type().apply((ServerLocation) volume.location(element.position()));
100101
}
101102

102-
@SuppressWarnings("ConstantConditions")
103103
public static <M extends BlockEntityArchetypeVolume.Mutable<M>> VolumeApplicator<M, BlockEntityArchetype, Boolean> applyBlockEntityArchetypes() {
104104
return (volume, element) -> {
105105
volume.addBlockEntity(element.position(), element.type());
@@ -115,7 +115,8 @@ public static <M extends BiomeVolume.Mutable<M>> VolumeApplicator<M, Biome, Bool
115115
return (volume, element) -> volume.setBiome(element.position(), element.type());
116116
}
117117

118-
public static <M extends LocationCreator<?, ? extends ServerLocation> & EntityVolume.Mutable<M>> VolumeApplicator<M, EntityArchetype, Optional<?
118+
@SuppressWarnings("RedundantCast")
119+
public static <M extends LocationCreator<@NonNull ?, ? extends ServerLocation> & EntityVolume.Mutable<M>> VolumeApplicator<M, EntityArchetype, Optional<?
119120
extends Entity>> applyEntityArchetype() {
120121
return (volume, element) -> element.type().apply((ServerLocation) volume.location(element.position()));
121122
}

0 commit comments

Comments
 (0)