Skip to content

Commit

Permalink
feat: Add BlockVolume.blockPalette accessor
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel Harris-Rouquette <[email protected]>
  • Loading branch information
gabizou committed May 31, 2024
1 parent 04df7a4 commit 847c811
Show file tree
Hide file tree
Showing 18 changed files with 241 additions and 17 deletions.
2 changes: 1 addition & 1 deletion SpongeAPI
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.spongepowered.api.world.chunk.ChunkState;
import org.spongepowered.api.world.chunk.ChunkStates;
import org.spongepowered.api.world.chunk.WorldChunk;
import org.spongepowered.api.world.schematic.Palette;
import org.spongepowered.api.world.volume.stream.StreamOptions;
import org.spongepowered.api.world.volume.stream.VolumeStream;
import org.spongepowered.common.util.VecHelper;
Expand Down Expand Up @@ -84,9 +85,14 @@ public SpongeEmptyChunk(Level level, ChunkAccess chunk) {
private @Nullable Vector3i blockMin;
private @Nullable Vector3i blockMax;

@Override
public Palette<BlockState, BlockType> blockPalette() {
return ((World<?, ?>) this.level).blockPalette();
}

@Override
public World<?, ?> world() {
return (World) this.level;
return (World<?, ?>) this.level;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.common.world.schematic;

import net.minecraft.core.IdMapper;
import org.spongepowered.api.registry.Registry;
import org.spongepowered.api.registry.RegistryHolder;
import org.spongepowered.api.world.schematic.Palette;
import org.spongepowered.api.world.schematic.PaletteReference;
import org.spongepowered.api.world.schematic.PaletteType;

import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.Spliterator;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class PaletteWrapper<NT, T, R> implements Palette.Immutable<T, R> {

public static <NT, T, R> PaletteWrapper<NT, T, R> of(PaletteType<T, R> type, IdMapper<NT> proxy, Registry<R> registry) {
return new PaletteWrapper<>(type, proxy, registry);
}

private final PaletteType<T, R> type;
private final IdMapper<NT> proxy;
private final Registry<R> registry;

private PaletteWrapper(PaletteType<T, R> type, IdMapper<NT> proxy, Registry<R> registry) {
this.type = type;
this.proxy = proxy;
this.registry = registry;
}

@Override
public PaletteType<T, R> type() {
return this.type;
}

@Override
public int highestId() {
return this.proxy.size();
}

@SuppressWarnings("unchecked")
@Override
public Optional<PaletteReference<T, R>> get(int id) {
final var n = this.proxy.byId(id);
if (n == null) {
return Optional.empty();
}
final var apply = this.type.stringifier().apply(this.registry, (T) n);
return Optional.of(PaletteReference.byString(this.registry.type(), apply));
}

@SuppressWarnings("unchecked")
@Override
public OptionalInt get(T type) {
final var id = this.proxy.getId((NT) type);
if (id <= 0) {
return OptionalInt.empty();
}
return OptionalInt.of(id);
}

@SuppressWarnings("unchecked")
@Override
public Stream<T> stream() {
return StreamSupport.stream(this.proxy::spliterator,
Spliterator.SIZED | Spliterator.ORDERED | Spliterator.IMMUTABLE,
false)
.map(n -> (T) n);
}

@Override
public Stream<Map.Entry<T, Integer>> streamWithIds() {
return Stream.empty();
}

@SuppressWarnings("unchecked")
@Override
public Mutable<T, R> asMutable(RegistryHolder registry) {
final var mutable = new MutableBimapPalette<T, R>(this.type, (Registry<R>) this.registry);
for (NT nt : this.proxy) {
mutable.orAssign((T) nt);
}
return mutable;
}

@Override
public Immutable<T, R> asImmutable() {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public SpongeSchematic(final Vector3i start, final Vector3i size,

@Override
public Palette<BlockState, BlockType> blockPalette() {
return this.volume.getBlockPalette();
return this.volume.blockPalette();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public BlockVolume.Immutable immutableOf(final BlockVolume.Streamable<@NonNull ?

private ArrayImmutableBlockBuffer createImmutableFromBufferData(final ArrayMutableBlockBuffer arrayBuffer) {
final BlockBackingData data = arrayBuffer.getCopiedBackingData();
final Palette.Immutable<BlockState, BlockType> immutablePalette = arrayBuffer.getPalette().asImmutable();
final Palette.Immutable<BlockState, BlockType> immutablePalette = arrayBuffer.blockPalette().asImmutable();
return new ArrayImmutableBlockBuffer(immutablePalette, arrayBuffer.min(), arrayBuffer.size(), data);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.block.BlockState;
import org.spongepowered.api.block.BlockType;
import org.spongepowered.api.block.entity.BlockEntityArchetype;
import org.spongepowered.api.entity.EntityArchetype;
import org.spongepowered.api.fluid.FluidState;
Expand All @@ -35,6 +36,7 @@
import org.spongepowered.api.util.rotation.Rotation;
import org.spongepowered.api.util.transformation.Transformation;
import org.spongepowered.api.world.biome.Biome;
import org.spongepowered.api.world.schematic.Palette;
import org.spongepowered.api.world.volume.Volume;
import org.spongepowered.api.world.volume.archetype.ArchetypeVolume;
import org.spongepowered.api.world.volume.archetype.block.entity.BlockEntityArchetypeVolume;
Expand Down Expand Up @@ -118,6 +120,11 @@ protected Vector3d transformStreamBlockPosition(final Vector3d blockPosition) {
return this.transformation.transformPosition(blockPosition);
}

@Override
public Palette<BlockState, BlockType> blockPalette() {
return this.reference.get().blockPalette();
}

@Override
public Vector3i min() {
return this.transformBlockSize(Vector3i::min);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ private SpongeArchetypeVolume(final Vector3i start, final Vector3i size, final P
this.entities = new ObjectArrayMutableEntityArchetypeBuffer(start, size);
}

@Override
public Palette<BlockState, BlockType> blockPalette() {
return this.blocks.blockPalette();
}

@Override
public Optional<BlockEntityArchetype> blockEntityArchetype(final int x, final int y, final int z) {
return this.blockEntities.blockEntityArchetype(x, y, z);
Expand Down Expand Up @@ -217,10 +222,6 @@ public void removeBlockEntity(final int x, final int y, final int z) {
this.blockEntities.removeBlockEntity(x, y, z);
}

public Palette<BlockState, BlockType> getBlockPalette() {
return this.blocks.getPalette();
}

public Palette<Biome, Biome> getBiomePalette() {
return this.biomes.getPalette();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public boolean removeBlock(final int x, final int y, final int z) {


@Override
public Palette<BlockState, BlockType> getPalette() {
return this.blockBuffer.getPalette();
public Palette<BlockState, BlockType> blockPalette() {
return this.blockBuffer.blockPalette();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ protected AbstractBlockBuffer(final Vector3i start, final Vector3i size) {
super(start, size);
}

public abstract Palette<BlockState, BlockType> getPalette();
@Override
public abstract Palette<BlockState, BlockType> blockPalette();


}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public ArrayImmutableBlockBuffer(final Palette.Immutable<BlockState, BlockType>
}

@Override
public Palette<BlockState, BlockType> getPalette() {
public Palette<BlockState, BlockType> blockPalette() {
return this.palette;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public ArrayMutableBlockBuffer(final Palette<BlockState, BlockType> palette, fin
}

@Override
public Palette<BlockState, BlockType> getPalette() {
public Palette<BlockState, BlockType> blockPalette() {
return this.palette;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public boolean removeBlock(final int x, final int y, final int z) {


@Override
public Palette<BlockState, BlockType> getPalette() {
return this.blockBuffer.getPalette();
public Palette<BlockState, BlockType> blockPalette() {
return this.blockBuffer.blockPalette();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,24 @@
package org.spongepowered.common.mixin.api.minecraft.world.level;

import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.storage.LevelData;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.block.BlockType;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.util.PositionOutOfBoundsException;
import org.spongepowered.api.world.BlockChangeFlag;
import org.spongepowered.api.world.WorldLike;
import org.spongepowered.api.world.difficulty.Difficulty;
import org.spongepowered.api.world.schematic.Palette;
import org.spongepowered.api.world.schematic.PaletteTypes;
import org.spongepowered.asm.mixin.Implements;
import org.spongepowered.asm.mixin.Interface;
import org.spongepowered.asm.mixin.Interface.Remap;
Expand All @@ -50,6 +55,7 @@
import org.spongepowered.common.event.tracking.phase.plugin.PluginPhase;
import org.spongepowered.common.util.Constants;
import org.spongepowered.common.world.SpongeBlockChangeFlag;
import org.spongepowered.common.world.schematic.PaletteWrapper;
import org.spongepowered.common.world.volume.VolumeStreamUtils;
import org.spongepowered.math.vector.Vector3i;

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

// BlockVolume

@SuppressWarnings("unchecked")
@Override
default Palette<org.spongepowered.api.block.BlockState, BlockType> blockPalette() {
return PaletteWrapper.of(
PaletteTypes.BLOCK_STATE_PALETTE.get(),
Block.BLOCK_STATE_REGISTRY,
(org.spongepowered.api.registry.Registry<BlockType>) ((LevelAccessor) (Object) this).registryAccess().registryOrThrow(Registries.BLOCK)
);
}

// MutableBiomeVolume

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,44 @@

import net.minecraft.core.BlockPos;
import net.minecraft.core.Holder;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.CollisionGetter;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.chunk.status.ChunkStatus;
import net.minecraft.world.level.levelgen.Heightmap;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.block.BlockState;
import org.spongepowered.api.block.BlockType;
import org.spongepowered.api.block.entity.BlockEntity;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.registry.Registry;
import org.spongepowered.api.util.AABB;
import org.spongepowered.api.world.HeightType;
import org.spongepowered.api.world.WorldType;
import org.spongepowered.api.world.biome.Biome;
import org.spongepowered.api.world.border.WorldBorder;
import org.spongepowered.api.world.chunk.Chunk;
import org.spongepowered.api.world.schematic.Palette;
import org.spongepowered.api.world.schematic.PaletteTypes;
import org.spongepowered.api.world.volume.game.Region;
import org.spongepowered.api.world.volume.stream.StreamOptions;
import org.spongepowered.api.world.volume.stream.VolumeStream;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.common.bridge.world.level.border.WorldBorderBridge;
import org.spongepowered.common.world.schematic.PaletteWrapper;
import org.spongepowered.common.world.volume.VolumeStreamUtils;
import org.spongepowered.math.vector.Vector3d;
import org.spongepowered.math.vector.Vector3i;

import java.util.Objects;

@SuppressWarnings({"RedundantTypeArguments", "unchecked", "RedundantCast"})
@SuppressWarnings({"RedundantTypeArguments", "RedundantCast"})
@Mixin(LevelReader.class)
public interface LevelReaderMixin_API<R extends Region<R>> extends Region<R> {

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

// BlockVolume

@SuppressWarnings("unchecked")
@Override
default Palette<BlockState, BlockType> blockPalette() {
return PaletteWrapper.of(
PaletteTypes.BLOCK_STATE_PALETTE.get(),
Block.BLOCK_STATE_REGISTRY,
(Registry<BlockType>) this.shadow$registryAccess().registryOrThrow(Registries.BLOCK)
);
}

// Region

@Override
Expand Down
Loading

0 comments on commit 847c811

Please sign in to comment.