Skip to content

Commit 762a031

Browse files
committed
Refactor Mutability of Volumes
An intention of having Mutable Volumes recyclable and reusable is an effort to have them simplistic with their use and retain their extendability. Effectively, the usages of BiomeVolume.Mutable etc will be limited to their scope, while the other volume types can extend Modifiable entirely. Signed-off-by: Gabriel Harris-Rouquette <[email protected]>
1 parent 74f41ae commit 762a031

File tree

17 files changed

+458
-60
lines changed

17 files changed

+458
-60
lines changed

src/main/java/org/spongepowered/api/world/ProtoWorld.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@
4444

4545
public interface ProtoWorld<P extends ProtoWorld<P>> extends
4646
Region<P>,
47-
BiomeVolume.Mutable<P>, // Because this is mutable
48-
BlockVolume.Mutable<P>, // Because this is mutable
49-
EntityVolume.Mutable<P>, // Because this is mutable
50-
BlockEntityVolume.Mutable<P>, // Because this is mutable
47+
BiomeVolume.Modifiable<P>, // Because this is mutable
48+
BlockVolume.Modifiable<P>, // Because this is mutable
49+
EntityVolume.Modifiable<P>, // Because this is mutable
50+
BlockEntityVolume.Modifiable<P>, // Because this is mutable
5151
GenerationVolume.Mutable,
5252
LocationBaseDataHolder.Mutable,
5353
UpdatableVolume,

src/main/java/org/spongepowered/api/world/World.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,16 @@
5050
* A loaded Minecraft world.
5151
*/
5252
@DoNotStore
53-
public interface World<W extends World<W, L>, L extends Location<W, L>> extends ForwardingAudience, ProtoWorld<W>, LocationCreator<W, L>,
54-
PhysicsAwareMutableBlockVolume<W>, ContextSource, Viewer, ArchetypeVolumeCreator, WeatherUniverse, ScopedRegistryHolder {
53+
public interface World<W extends World<W, L>, L extends Location<W, L>> extends
54+
ForwardingAudience,
55+
ProtoWorld<W>,
56+
LocationCreator<W, L>,
57+
PhysicsAwareMutableBlockVolume<W>,
58+
ContextSource,
59+
Viewer,
60+
ArchetypeVolumeCreator,
61+
WeatherUniverse,
62+
ScopedRegistryHolder {
5563

5664
/**
5765
* Gets the {@link WorldProperties properties}.

src/main/java/org/spongepowered/api/world/chunk/Chunk.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
* <p>In Minecraft, the chunk is 16 by 16 blocks on the X and Z axes. The height
4141
* of each chunk varies between worlds.</p>
4242
*/
43-
public interface Chunk extends ProtoChunk<Chunk>, EntityVolume.Mutable<Chunk> {
43+
public interface Chunk extends ProtoChunk<Chunk>, EntityVolume.Modifiable<Chunk> {
4444

4545
/**
4646
* Gets the world the chunk is in.

src/main/java/org/spongepowered/api/world/chunk/ProtoChunk.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@
5252
*/
5353
@DoNotStore
5454
public interface ProtoChunk<P extends ProtoChunk<P>> extends
55-
BlockVolume.Mutable<P>,
56-
BlockEntityVolume.Mutable<P>,
57-
BiomeVolume.Mutable<P>,
55+
BlockVolume.Modifiable<P>,
56+
BlockEntityVolume.Modifiable<P>,
57+
BiomeVolume.Modifiable<P>,
5858
UpdatableVolume,
5959
LocationBaseDataHolder.Mutable,
6060
HeightAwareVolume {

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,24 @@
4747
import java.util.Objects;
4848
import java.util.function.Supplier;
4949

50-
public interface ArchetypeVolume extends BlockVolume.Mutable<ArchetypeVolume>,
51-
BlockEntityArchetypeVolume.Mutable<ArchetypeVolume>,
52-
EntityArchetypeVolume.Mutable<ArchetypeVolume>,
53-
BiomeVolume.Mutable<ArchetypeVolume> {
54-
55-
Registry<BlockType> blockStateRegistry();
56-
57-
Registry<Biome> biomeRegistry();
50+
public interface ArchetypeVolume extends BlockVolume.Modifiable<ArchetypeVolume>,
51+
BlockEntityArchetypeVolume.Modifiable<ArchetypeVolume>,
52+
EntityArchetypeVolume.Modifiable<ArchetypeVolume>,
53+
BiomeVolume.Modifiable<ArchetypeVolume> {
5854

55+
/**
56+
* Attempts to apply all of the contents of this
57+
* {@link ArchetypeVolume volume} onto the target {@link ServerWorld world}
58+
* with a relative {@code placement}. This default implementation can be
59+
* used as a guide for utilizing
60+
* {@link org.spongepowered.api.world.volume.stream.VolumeStream VolumeStreams}
61+
* and their companion types.
62+
*
63+
* @param target The target world
64+
* @param placement The target origin, where the diff of relative position
65+
* compared to this volume's min position as the offset
66+
* @param spawnContext The context value used for processing spawn entities.
67+
*/
5968
default void applyToWorld(final ServerWorld target, final Vector3i placement, final Supplier<SpawnType> spawnContext) {
6069
Objects.requireNonNull(target, "Target world cannot be null");
6170
Objects.requireNonNull(placement, "Target position cannot be null");
@@ -64,14 +73,10 @@ default void applyToWorld(final ServerWorld target, final Vector3i placement, fi
6473
.apply(VolumeCollectors.of(
6574
target,
6675
VolumePositionTranslators.relativeTo(placement),
67-
VolumeApplicators.applyBlocks(BlockChangeFlags.ALL)
76+
VolumeApplicators.applyBlocks(BlockChangeFlags.DEFAULT_PLACEMENT)
6877
));
6978

7079
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()))
7580
.apply(VolumeCollectors.of(
7681
target,
7782
VolumePositionTranslators.relativeTo(placement),

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ interface Unmodifiable<U extends Unmodifiable<U>> extends BlockEntityArchetypeVo
9191

9292
}
9393

94-
interface Mutable<M extends Mutable<M>> extends Streamable<M>,
95-
BlockVolume.Mutable<M>,
96-
MutableVolume {
94+
interface Modifiable<M extends Modifiable<M>> extends Streamable<M>, BlockVolume.Modifiable<M>, MutableVolume {
9795

9896
default void addBlockEntity(final Vector3i pos, final BlockEntity blockEntity) {
9997
this.addBlockEntity(pos.x(), pos.y(), pos.z(), blockEntity);
@@ -114,6 +112,10 @@ default void removeBlockEntity(final Vector3i pos) {
114112
}
115113

116114
void removeBlockEntity(int x, int y, int z);
115+
}
116+
117+
interface Mutable extends Modifiable<Mutable> {
118+
117119

118120
}
119121

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ interface Unmodifiable<U extends Unmodifiable<U>> extends EntityArchetypeVolume,
9494

9595
}
9696

97-
interface Mutable<M extends Mutable<M>> extends Streamable<M>, MutableVolume {
97+
interface Modifiable<M extends Modifiable<M>> extends Streamable<M>, MutableVolume {
9898

9999
default void addEntity(final EntityArchetype archetype, final Vector3d position) {
100100
this.addEntity(EntityArchetypeEntry.of(
@@ -107,6 +107,10 @@ default void addEntity(final EntityArchetype archetype, final Vector3d position)
107107

108108
}
109109

110+
interface Mutable extends Modifiable<Mutable> {
111+
112+
}
113+
110114
interface Immutable extends Unmodifiable<Immutable>, ImmutableVolume {
111115

112116
}

src/main/java/org/spongepowered/api/world/volume/biome/BiomeVolume.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ interface Unmodifiable<U extends Unmodifiable<U>> extends BiomeVolume, Streamabl
8989

9090
}
9191

92-
interface Mutable<M extends Mutable<M>> extends Streamable<M>, MutableVolume {
92+
interface Modifiable<M extends Modifiable<M>> extends Streamable<M>, MutableVolume {
9393

9494
/**
9595
* Sets the {@link Biome} at the given position in this volume.
@@ -119,6 +119,10 @@ default boolean setBiome(Vector3i position, Biome biome) {
119119

120120
}
121121

122+
interface Mutable extends Modifiable<Mutable> {
123+
124+
}
125+
122126
interface Immutable extends Unmodifiable<Immutable>, ImmutableVolume {
123127

124128
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) SpongePowered <https://www.spongepowered.org>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package org.spongepowered.api.world.volume.biome;
26+
27+
import org.checkerframework.checker.nullness.qual.NonNull;
28+
import org.spongepowered.api.registry.RegistryReference;
29+
import org.spongepowered.api.world.biome.Biome;
30+
import org.spongepowered.api.world.schematic.Palette;
31+
import org.spongepowered.api.world.volume.virtual.UnrealizedBiomeVolume;
32+
import org.spongepowered.math.vector.Vector3i;
33+
34+
public interface BiomeVolumeFactory {
35+
36+
BiomeVolume.Mutable empty(Palette<Biome, Biome> palette, RegistryReference<Biome> defaultBiome, Vector3i min, Vector3i max);
37+
38+
BiomeVolume.Mutable copyFromRange(BiomeVolume.Streamable<@NonNull ?> existing, Vector3i newMin, Vector3i newMax);
39+
40+
BiomeVolume.Mutable copy(BiomeVolume.Streamable<@NonNull ?> existing);
41+
42+
BiomeVolume.Immutable immutableOf(BiomeVolume.Streamable<@NonNull ?> existing);
43+
44+
BiomeVolume.Immutable immutableOf(BiomeVolume.Streamable<@NonNull ?> existing, Vector3i newMin, Vector3i newMax);
45+
46+
UnrealizedBiomeVolume.Mutable empty(Vector3i min, Vector3i max);
47+
48+
}

src/main/java/org/spongepowered/api/world/volume/block/BlockVolume.java

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

27-
import org.checkerframework.checker.nullness.qual.NonNull;
2827
import org.spongepowered.api.Sponge;
2928
import org.spongepowered.api.block.BlockState;
3029
import org.spongepowered.api.block.BlockType;
@@ -47,14 +46,6 @@
4746

4847
public interface BlockVolume extends Volume {
4948

50-
static BlockVolume.Mutable<@NonNull ?> empty(final Vector3i min, final Vector3i max) {
51-
return BlockVolume.empty(PaletteTypes.BLOCK_STATE_PALETTE.get().create(Sponge.game().registries(), RegistryTypes.BLOCK_TYPE), BlockTypes.AIR, min, max);
52-
}
53-
54-
static BlockVolume.Mutable<@NonNull ?> empty(final Palette<BlockState, BlockType> palette, final RegistryReference<BlockType> defaultState, final Vector3i min, final Vector3i max) {
55-
return Sponge.game().factoryProvider().provide(BlockVolumeFactory.class).empty(palette, defaultState, min, max);
56-
}
57-
5849
BlockState block(int x, int y, int z);
5950

6051
default BlockState block(final Vector3i vector3i) {
@@ -138,7 +129,7 @@ interface Immutable extends Unmodifiable<Immutable>, ImmutableVolume {
138129

139130
}
140131

141-
interface Mutable<M extends Mutable<M>> extends Streamable<M>, MutableVolume {
132+
interface Modifiable<M extends Modifiable<M>> extends Streamable<M>, MutableVolume {
142133

143134
/**
144135
* Sets the block at the given position in the world.
@@ -171,6 +162,19 @@ default boolean removeBlock(final Vector3i position) {
171162
}
172163

173164
boolean removeBlock(int x, int y, int z);
165+
}
174166

167+
interface Mutable extends Modifiable<Mutable> {
168+
169+
static Mutable empty(final Vector3i min, final Vector3i max) {
170+
return Mutable.empty(PaletteTypes.BLOCK_STATE_PALETTE.get().create(Sponge.game().registries(), RegistryTypes.BLOCK_TYPE), BlockTypes.AIR, min, max);
171+
}
172+
173+
static Mutable empty(
174+
final Palette<BlockState, BlockType> palette, final RegistryReference<BlockType> defaultState,
175+
final Vector3i min, final Vector3i max
176+
) {
177+
return Sponge.game().factoryProvider().provide(BlockVolumeFactory.class).empty(palette, defaultState, min, max);
178+
}
175179
}
176180
}

0 commit comments

Comments
 (0)