Skip to content

Commit 46c693a

Browse files
committed
Merge BlockVolume enhancements. Merges #4012
Signed-off-by: Gabriel Harris-Rouquette <[email protected]>
2 parents 04df7a4 + cb40847 commit 46c693a

18 files changed

+240
-16
lines changed

src/main/java/org/spongepowered/common/world/level/chunk/SpongeEmptyChunk.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.spongepowered.api.world.chunk.ChunkState;
5757
import org.spongepowered.api.world.chunk.ChunkStates;
5858
import org.spongepowered.api.world.chunk.WorldChunk;
59+
import org.spongepowered.api.world.schematic.Palette;
5960
import org.spongepowered.api.world.volume.stream.StreamOptions;
6061
import org.spongepowered.api.world.volume.stream.VolumeStream;
6162
import org.spongepowered.common.util.VecHelper;
@@ -84,9 +85,14 @@ public SpongeEmptyChunk(Level level, ChunkAccess chunk) {
8485
private @Nullable Vector3i blockMin;
8586
private @Nullable Vector3i blockMax;
8687

88+
@Override
89+
public Palette<BlockState, BlockType> blockPalette() {
90+
return ((World<?, ?>) this.level).blockPalette();
91+
}
92+
8793
@Override
8894
public World<?, ?> world() {
89-
return (World) this.level;
95+
return (World<?, ?>) this.level;
9096
}
9197

9298
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* This file is part of Sponge, 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.common.world.schematic;
26+
27+
import net.minecraft.core.IdMapper;
28+
import org.spongepowered.api.registry.Registry;
29+
import org.spongepowered.api.registry.RegistryHolder;
30+
import org.spongepowered.api.world.schematic.Palette;
31+
import org.spongepowered.api.world.schematic.PaletteReference;
32+
import org.spongepowered.api.world.schematic.PaletteType;
33+
34+
import java.util.Map;
35+
import java.util.Optional;
36+
import java.util.OptionalInt;
37+
import java.util.Spliterator;
38+
import java.util.stream.Stream;
39+
import java.util.stream.StreamSupport;
40+
41+
public class PaletteWrapper<NT, T, R> implements Palette.Immutable<T, R> {
42+
43+
public static <NT, T, R> PaletteWrapper<NT, T, R> of(PaletteType<T, R> type, IdMapper<NT> proxy, Registry<R> registry) {
44+
return new PaletteWrapper<>(type, proxy, registry);
45+
}
46+
47+
private final PaletteType<T, R> type;
48+
private final IdMapper<NT> proxy;
49+
private final Registry<R> registry;
50+
51+
private PaletteWrapper(PaletteType<T, R> type, IdMapper<NT> proxy, Registry<R> registry) {
52+
this.type = type;
53+
this.proxy = proxy;
54+
this.registry = registry;
55+
}
56+
57+
@Override
58+
public PaletteType<T, R> type() {
59+
return this.type;
60+
}
61+
62+
@Override
63+
public int highestId() {
64+
return this.proxy.size();
65+
}
66+
67+
@SuppressWarnings("unchecked")
68+
@Override
69+
public Optional<PaletteReference<T, R>> get(int id) {
70+
final var n = this.proxy.byId(id);
71+
if (n == null) {
72+
return Optional.empty();
73+
}
74+
final var apply = this.type.stringifier().apply(this.registry, (T) n);
75+
return Optional.of(PaletteReference.byString(this.registry.type(), apply));
76+
}
77+
78+
@SuppressWarnings("unchecked")
79+
@Override
80+
public OptionalInt get(T type) {
81+
final var id = this.proxy.getId((NT) type);
82+
if (id >= 0) {
83+
return OptionalInt.of(id);
84+
}
85+
return OptionalInt.empty();
86+
}
87+
88+
@SuppressWarnings("unchecked")
89+
@Override
90+
public Stream<T> stream() {
91+
return StreamSupport.stream(this.proxy::spliterator,
92+
Spliterator.SIZED | Spliterator.ORDERED | Spliterator.IMMUTABLE,
93+
false)
94+
.map(n -> (T) n);
95+
}
96+
97+
@Override
98+
public Stream<Map.Entry<T, Integer>> streamWithIds() {
99+
return Stream.empty();
100+
}
101+
102+
@SuppressWarnings("unchecked")
103+
@Override
104+
public Mutable<T, R> asMutable(RegistryHolder registry) {
105+
final var mutable = new MutableBimapPalette<T, R>(this.type, (Registry<R>) this.registry);
106+
for (NT nt : this.proxy) {
107+
mutable.orAssign((T) nt);
108+
}
109+
return mutable;
110+
}
111+
112+
@Override
113+
public Immutable<T, R> asImmutable() {
114+
return this;
115+
}
116+
}

src/main/java/org/spongepowered/common/world/schematic/SpongeSchematic.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public SpongeSchematic(final Vector3i start, final Vector3i size,
6666

6767
@Override
6868
public Palette<BlockState, BlockType> blockPalette() {
69-
return this.volume.getBlockPalette();
69+
return this.volume.blockPalette();
7070
}
7171

7272
@Override

src/main/java/org/spongepowered/common/world/volume/block/SpongeBlockVolumeFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public BlockVolume.Immutable immutableOf(final BlockVolume.Streamable<@NonNull ?
8888

8989
private ArrayImmutableBlockBuffer createImmutableFromBufferData(final ArrayMutableBlockBuffer arrayBuffer) {
9090
final BlockBackingData data = arrayBuffer.getCopiedBackingData();
91-
final Palette.Immutable<BlockState, BlockType> immutablePalette = arrayBuffer.getPalette().asImmutable();
91+
final Palette.Immutable<BlockState, BlockType> immutablePalette = arrayBuffer.blockPalette().asImmutable();
9292
return new ArrayImmutableBlockBuffer(immutablePalette, arrayBuffer.min(), arrayBuffer.size(), data);
9393
}
9494

src/main/java/org/spongepowered/common/world/volume/buffer/archetype/AbstractReferentArchetypeVolume.java

+7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import org.checkerframework.checker.nullness.qual.Nullable;
2828
import org.spongepowered.api.block.BlockState;
29+
import org.spongepowered.api.block.BlockType;
2930
import org.spongepowered.api.block.entity.BlockEntityArchetype;
3031
import org.spongepowered.api.entity.EntityArchetype;
3132
import org.spongepowered.api.fluid.FluidState;
@@ -35,6 +36,7 @@
3536
import org.spongepowered.api.util.rotation.Rotation;
3637
import org.spongepowered.api.util.transformation.Transformation;
3738
import org.spongepowered.api.world.biome.Biome;
39+
import org.spongepowered.api.world.schematic.Palette;
3840
import org.spongepowered.api.world.volume.Volume;
3941
import org.spongepowered.api.world.volume.archetype.ArchetypeVolume;
4042
import org.spongepowered.api.world.volume.archetype.block.entity.BlockEntityArchetypeVolume;
@@ -118,6 +120,11 @@ protected Vector3d transformStreamBlockPosition(final Vector3d blockPosition) {
118120
return this.transformation.transformPosition(blockPosition);
119121
}
120122

123+
@Override
124+
public Palette<BlockState, BlockType> blockPalette() {
125+
return this.reference.get().blockPalette();
126+
}
127+
121128
@Override
122129
public Vector3i min() {
123130
return this.transformBlockSize(Vector3i::min);

src/main/java/org/spongepowered/common/world/volume/buffer/archetype/SpongeArchetypeVolume.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ private SpongeArchetypeVolume(final Vector3i start, final Vector3i size, final P
105105
this.entities = new ObjectArrayMutableEntityArchetypeBuffer(start, size);
106106
}
107107

108+
@Override
109+
public Palette<BlockState, BlockType> blockPalette() {
110+
return this.blocks.blockPalette();
111+
}
112+
108113
@Override
109114
public Optional<BlockEntityArchetype> blockEntityArchetype(final int x, final int y, final int z) {
110115
return this.blockEntities.blockEntityArchetype(x, y, z);
@@ -217,10 +222,6 @@ public void removeBlockEntity(final int x, final int y, final int z) {
217222
this.blockEntities.removeBlockEntity(x, y, z);
218223
}
219224

220-
public Palette<BlockState, BlockType> getBlockPalette() {
221-
return this.blocks.getPalette();
222-
}
223-
224225
public Palette<Biome, Biome> getBiomePalette() {
225226
return this.biomes.getPalette();
226227
}

src/main/java/org/spongepowered/common/world/volume/buffer/archetype/blockentity/AbstractMutableBlockEntityArchetypeBuffer.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ public boolean removeBlock(final int x, final int y, final int z) {
7979

8080

8181
@Override
82-
public Palette<BlockState, BlockType> getPalette() {
83-
return this.blockBuffer.getPalette();
82+
public Palette<BlockState, BlockType> blockPalette() {
83+
return this.blockBuffer.blockPalette();
8484
}
8585

8686
@Override

src/main/java/org/spongepowered/common/world/volume/buffer/block/AbstractBlockBuffer.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ protected AbstractBlockBuffer(final Vector3i start, final Vector3i size) {
4040
super(start, size);
4141
}
4242

43-
public abstract Palette<BlockState, BlockType> getPalette();
43+
@Override
44+
public abstract Palette<BlockState, BlockType> blockPalette();
4445

4546

4647
}

src/main/java/org/spongepowered/common/world/volume/buffer/block/ArrayImmutableBlockBuffer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public ArrayImmutableBlockBuffer(final Palette.Immutable<BlockState, BlockType>
7979
}
8080

8181
@Override
82-
public Palette<BlockState, BlockType> getPalette() {
82+
public Palette<BlockState, BlockType> blockPalette() {
8383
return this.palette;
8484
}
8585

src/main/java/org/spongepowered/common/world/volume/buffer/block/ArrayMutableBlockBuffer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public ArrayMutableBlockBuffer(final Palette<BlockState, BlockType> palette, fin
118118
}
119119

120120
@Override
121-
public Palette<BlockState, BlockType> getPalette() {
121+
public Palette<BlockState, BlockType> blockPalette() {
122122
return this.palette;
123123
}
124124

src/main/java/org/spongepowered/common/world/volume/buffer/blockentity/AbstractMutableBlockEntityBuffer.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ public boolean removeBlock(final int x, final int y, final int z) {
7474

7575

7676
@Override
77-
public Palette<BlockState, BlockType> getPalette() {
78-
return this.blockBuffer.getPalette();
77+
public Palette<BlockState, BlockType> blockPalette() {
78+
return this.blockBuffer.blockPalette();
7979
}
8080

8181
@Override

src/mixins/java/org/spongepowered/common/mixin/api/minecraft/world/level/LevelAccessorMixin_API.java

+18
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,24 @@
2525
package org.spongepowered.common.mixin.api.minecraft.world.level;
2626

2727
import net.minecraft.core.BlockPos;
28+
import net.minecraft.core.registries.Registries;
2829
import net.minecraft.world.level.Level;
2930
import net.minecraft.world.level.LevelAccessor;
3031
import net.minecraft.world.level.LevelReader;
32+
import net.minecraft.world.level.block.Block;
3133
import net.minecraft.world.level.block.state.BlockState;
3234
import net.minecraft.world.level.chunk.ChunkAccess;
3335
import net.minecraft.world.level.storage.LevelData;
3436
import org.checkerframework.checker.nullness.qual.NonNull;
3537
import org.checkerframework.checker.nullness.qual.Nullable;
38+
import org.spongepowered.api.block.BlockType;
3639
import org.spongepowered.api.entity.Entity;
3740
import org.spongepowered.api.util.PositionOutOfBoundsException;
3841
import org.spongepowered.api.world.BlockChangeFlag;
3942
import org.spongepowered.api.world.WorldLike;
4043
import org.spongepowered.api.world.difficulty.Difficulty;
44+
import org.spongepowered.api.world.schematic.Palette;
45+
import org.spongepowered.api.world.schematic.PaletteTypes;
4146
import org.spongepowered.asm.mixin.Implements;
4247
import org.spongepowered.asm.mixin.Interface;
4348
import org.spongepowered.asm.mixin.Interface.Remap;
@@ -50,6 +55,7 @@
5055
import org.spongepowered.common.event.tracking.phase.plugin.PluginPhase;
5156
import org.spongepowered.common.util.Constants;
5257
import org.spongepowered.common.world.SpongeBlockChangeFlag;
58+
import org.spongepowered.common.world.schematic.PaletteWrapper;
5359
import org.spongepowered.common.world.volume.VolumeStreamUtils;
5460
import org.spongepowered.math.vector.Vector3i;
5561

@@ -68,6 +74,18 @@ public interface LevelAccessorMixin_API<P extends WorldLike<P>> extends WorldLik
6874
@Shadow LevelData shadow$getLevelData();
6975
//@formatter:on
7076

77+
// BlockVolume
78+
79+
@SuppressWarnings("unchecked")
80+
@Override
81+
default Palette<org.spongepowered.api.block.BlockState, BlockType> blockPalette() {
82+
return PaletteWrapper.of(
83+
PaletteTypes.BLOCK_STATE_PALETTE.get(),
84+
Block.BLOCK_STATE_REGISTRY,
85+
(org.spongepowered.api.registry.Registry<BlockType>) ((LevelAccessor) (Object) this).registryAccess().registryOrThrow(Registries.BLOCK)
86+
);
87+
}
88+
7189
// MutableBiomeVolume
7290

7391
@Override

src/mixins/java/org/spongepowered/common/mixin/api/minecraft/world/level/LevelReaderMixin_API.java

+22-1
Original file line numberDiff line numberDiff line change
@@ -26,36 +26,44 @@
2626

2727
import net.minecraft.core.BlockPos;
2828
import net.minecraft.core.Holder;
29+
import net.minecraft.core.RegistryAccess;
30+
import net.minecraft.core.registries.Registries;
2931
import net.minecraft.world.level.BlockAndTintGetter;
3032
import net.minecraft.world.level.CollisionGetter;
3133
import net.minecraft.world.level.LevelReader;
34+
import net.minecraft.world.level.block.Block;
3235
import net.minecraft.world.level.chunk.ChunkAccess;
3336
import net.minecraft.world.level.chunk.status.ChunkStatus;
3437
import net.minecraft.world.level.levelgen.Heightmap;
3538
import org.checkerframework.checker.nullness.qual.NonNull;
3639
import org.checkerframework.checker.nullness.qual.Nullable;
3740
import org.spongepowered.api.block.BlockState;
41+
import org.spongepowered.api.block.BlockType;
3842
import org.spongepowered.api.block.entity.BlockEntity;
3943
import org.spongepowered.api.entity.Entity;
44+
import org.spongepowered.api.registry.Registry;
4045
import org.spongepowered.api.util.AABB;
4146
import org.spongepowered.api.world.HeightType;
4247
import org.spongepowered.api.world.WorldType;
4348
import org.spongepowered.api.world.biome.Biome;
4449
import org.spongepowered.api.world.border.WorldBorder;
4550
import org.spongepowered.api.world.chunk.Chunk;
51+
import org.spongepowered.api.world.schematic.Palette;
52+
import org.spongepowered.api.world.schematic.PaletteTypes;
4653
import org.spongepowered.api.world.volume.game.Region;
4754
import org.spongepowered.api.world.volume.stream.StreamOptions;
4855
import org.spongepowered.api.world.volume.stream.VolumeStream;
4956
import org.spongepowered.asm.mixin.Mixin;
5057
import org.spongepowered.asm.mixin.Shadow;
5158
import org.spongepowered.common.bridge.world.level.border.WorldBorderBridge;
59+
import org.spongepowered.common.world.schematic.PaletteWrapper;
5260
import org.spongepowered.common.world.volume.VolumeStreamUtils;
5361
import org.spongepowered.math.vector.Vector3d;
5462
import org.spongepowered.math.vector.Vector3i;
5563

5664
import java.util.Objects;
5765

58-
@SuppressWarnings({"RedundantTypeArguments", "unchecked", "RedundantCast"})
66+
@SuppressWarnings({"RedundantTypeArguments", "RedundantCast"})
5967
@Mixin(LevelReader.class)
6068
public interface LevelReaderMixin_API<R extends Region<R>> extends Region<R> {
6169

@@ -70,8 +78,21 @@ public interface LevelReaderMixin_API<R extends Region<R>> extends Region<R> {
7078
@Shadow net.minecraft.world.level.dimension.DimensionType shadow$dimensionType();
7179
@Shadow boolean shadow$containsAnyLiquid(net.minecraft.world.phys.AABB bb);
7280
@Shadow Holder<net.minecraft.world.level.biome.Biome> shadow$getBiome(BlockPos p_226691_1_);
81+
@Shadow RegistryAccess shadow$registryAccess();
7382
// @formatter:on
7483

84+
// BlockVolume
85+
86+
@SuppressWarnings("unchecked")
87+
@Override
88+
default Palette<BlockState, BlockType> blockPalette() {
89+
return PaletteWrapper.of(
90+
PaletteTypes.BLOCK_STATE_PALETTE.get(),
91+
Block.BLOCK_STATE_REGISTRY,
92+
(Registry<BlockType>) this.shadow$registryAccess().registryOrThrow(Registries.BLOCK)
93+
);
94+
}
95+
7596
// Region
7697

7798
@Override

0 commit comments

Comments
 (0)