Support for SNBT in structures

This commit is contained in:
Zoë Gidiere
2025-10-05 16:58:08 -06:00
parent 42f3c56b71
commit 089850d633
10 changed files with 272 additions and 25 deletions

View File

@@ -17,9 +17,17 @@
package com.dfsek.terra.mod.generation;
import com.dfsek.seismic.math.coord.CoordFunctions;
import com.dfsek.terra.api.block.state.BlockStateExtended;
import com.mojang.serialization.MapCodec;
import net.minecraft.block.BlockEntityProvider;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.command.argument.BlockStateArgument;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.util.Util;
import net.minecraft.util.math.BlockPos;
@@ -183,9 +191,11 @@ public class MinecraftChunkGeneratorWrapper extends net.minecraft.world.gen.chun
BiomeProvider biomeProvider = pack.getBiomeProvider();
int min = height.getBottomY();
for(int y = height.getTopYInclusive() - 1; y >= min; y--) {
com.dfsek.terra.api.block.state.BlockState terraBlockState = delegate.getBlock(properties, x, y, z, biomeProvider);
BlockState blockState = (BlockState) (terraBlockState.isExtended() ? ((BlockStateExtended) terraBlockState).getState() : terraBlockState);
if(heightmap
.getBlockPredicate()
.test((BlockState) delegate.getBlock(properties, x, y, z, biomeProvider))) return y + 1;
.test(blockState)) return y + 1;
}
return min;
}
@@ -196,7 +206,9 @@ public class MinecraftChunkGeneratorWrapper extends net.minecraft.world.gen.chun
WorldProperties properties = MinecraftAdapter.adapt(height, SeedHack.getSeed(noiseConfig.getMultiNoiseSampler()));
BiomeProvider biomeProvider = pack.getBiomeProvider();
for(int y = height.getTopYInclusive() - 1; y >= height.getBottomY(); y--) {
array[y - height.getBottomY()] = (BlockState) delegate.getBlock(properties, x, y, z, biomeProvider);
com.dfsek.terra.api.block.state.BlockState terraBlockState = delegate.getBlock(properties, x, y, z, biomeProvider);
BlockState blockState = (BlockState) (terraBlockState.isExtended() ? ((BlockStateExtended) terraBlockState).getState() : terraBlockState);
array[y - height.getBottomY()] = blockState;
}
return new VerticalBlockSample(height.getBottomY(), array);
}

View File

@@ -17,9 +17,14 @@
package com.dfsek.terra.mod.handle;
import com.dfsek.terra.api.block.state.BlockStateExtended;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.block.Blocks;
import net.minecraft.command.argument.BlockArgumentParser;
import net.minecraft.command.argument.BlockArgumentParser.BlockResult;
import net.minecraft.command.argument.BlockStateArgument;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.registry.Registries;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.NotNull;
@@ -41,10 +46,18 @@ public class MinecraftWorldHandle implements WorldHandle {
@Override
public @NotNull BlockState createBlockState(@NotNull String data) {
try {
net.minecraft.block.BlockState state = BlockArgumentParser.block(Registries.BLOCK, data, true)
.blockState();
if(state == null) throw new IllegalArgumentException("Invalid data: " + data);
return (BlockState) state;
BlockResult blockResult = BlockArgumentParser.block(Registries.BLOCK, data, true);
BlockState blockState;
if (blockResult.nbt() != null) {
net.minecraft.block.BlockState state = blockResult.blockState();
NbtCompound nbtCompound = blockResult.nbt();
blockState = (BlockStateExtended) new BlockStateArgument(state, blockResult.properties().keySet(), nbtCompound);
} else {
blockState = (BlockState) blockResult.blockState();
}
if(blockState == null) throw new IllegalArgumentException("Invalid data: " + data);
return blockState;
} catch(CommandSyntaxException e) {
throw new IllegalArgumentException(e);
}

View File

@@ -0,0 +1,96 @@
package com.dfsek.terra.mod.mixin.implementations.terra.block.state;
import com.dfsek.terra.api.block.BlockData;
import com.dfsek.terra.api.block.BlockType;
import com.dfsek.terra.api.block.state.BlockStateExtended;
import com.dfsek.terra.api.block.state.properties.Property;
import com.dfsek.terra.mod.mixin.access.StateAccessor;
import net.minecraft.block.AbstractBlock.AbstractBlockState;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.pattern.CachedBlockPosition;
import net.minecraft.command.argument.BlockStateArgument;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.registry.Registries;
import net.minecraft.state.State;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Implements;
import org.spongepowered.asm.mixin.Interface;
import org.spongepowered.asm.mixin.Intrinsic;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
@Mixin(BlockStateArgument.class)
@Implements(@Interface(iface = BlockStateExtended.class, prefix = "terra$"))
public abstract class BlockStateArgumentMixin implements Predicate<CachedBlockPosition> {
@Shadow
public abstract BlockState getBlockState();
@Shadow
public abstract Set<net.minecraft.state.property.Property<?>> getProperties();
@Shadow
@Nullable
@Final
private NbtCompound data;
public boolean terra$matches(com.dfsek.terra.api.block.state.BlockState other) {
return ((com.dfsek.terra.api.block.state.BlockState) getBlockState()).matches(other);
}
@Intrinsic
public <T extends Comparable<T>> boolean terra$has(com.dfsek.terra.api.block.state.properties.Property<T> property) {
return ((com.dfsek.terra.api.block.state.BlockState) getBlockState()).has(property);
}
@Intrinsic
public <T extends Comparable<T>> T terra$get(com.dfsek.terra.api.block.state.properties.Property<T> property) {
return ((com.dfsek.terra.api.block.state.BlockState) getBlockState()).get(property);
}
@Intrinsic
public <T extends Comparable<T>> com.dfsek.terra.api.block.state.BlockState terra$set(Property<T> property, T value) {
return ((com.dfsek.terra.api.block.state.BlockState) getBlockState()).set(property, value);
}
@Intrinsic
public BlockType terra$getBlockType() {
return ((com.dfsek.terra.api.block.state.BlockState) getBlockState()).getBlockType();
}
@Intrinsic
public String terra$getAsString(boolean properties) {
return ((com.dfsek.terra.api.block.state.BlockState) getBlockState()).getAsString(properties);
}
@Intrinsic
public boolean terra$isAir() {
return ((com.dfsek.terra.api.block.state.BlockState) getBlockState()).isAir();
}
@SuppressWarnings({ "ConstantValue", "DataFlowIssue", "EqualsBetweenInconvertibleTypes" })
@Intrinsic
public BlockStateExtended terra$setData(BlockData data) {
return (BlockStateExtended) new BlockStateArgument(getBlockState(), getProperties(), data.getClass().equals(NbtCompound.class) ? ((NbtCompound) ((Object) data)) : null);
}
@SuppressWarnings("DataFlowIssue")
@Intrinsic
public BlockData terra$getData() {
return ((BlockData) ((Object) data));
}
@Intrinsic
public com.dfsek.terra.api.block.state.BlockState terra$getState() {
return (com.dfsek.terra.api.block.state.BlockState) getBlockState();
}
}

View File

@@ -17,8 +17,20 @@
package com.dfsek.terra.mod.mixin.implementations.terra.chunk;
import com.dfsek.seismic.math.coord.CoordFunctions;
import net.minecraft.block.Block;
import net.minecraft.command.argument.BlockStateArgument;
import net.minecraft.fluid.Fluid;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.ChunkRegion;
import net.minecraft.world.WorldProperties;
import net.minecraft.world.biome.source.BiomeAccess;
import net.minecraft.world.dimension.DimensionType;
import net.minecraft.world.tick.MultiTickScheduler;
import net.minecraft.world.tick.OrderedTick;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Implements;
@@ -38,9 +50,36 @@ public abstract class ChunkRegionMixin {
@Final
private net.minecraft.world.chunk.Chunk centerPos;
public void terraChunk$setBlock(int x, int y, int z, @NotNull BlockState blockState, boolean physics) {
((ChunkRegion) (Object) this).setBlockState(new BlockPos(x + (centerPos.getPos().x << 4), y, z + (centerPos.getPos().z << 4)),
(net.minecraft.block.BlockState) blockState, 0);
@Shadow
@Final
private ServerWorld world;
@Shadow
@Final
private MultiTickScheduler<Block> blockTickScheduler;
@Shadow
@Final
private MultiTickScheduler<Fluid> fluidTickScheduler;
public void terraChunk$setBlock(int x, int y, int z, @NotNull BlockState data, boolean physics) {
ChunkPos pos = centerPos.getPos();
BlockPos blockPos = new BlockPos(CoordFunctions.chunkAndRelativeToAbsolute(pos.x, x), y, CoordFunctions.chunkAndRelativeToAbsolute(pos.z, z));
boolean isExtended = data.isExtended() && data.getClass().equals(BlockStateArgument.class);
if (isExtended) {
((BlockStateArgument) data).setBlockState(world, blockPos, 0);
} else {
((ChunkRegion) (Object) this).setBlockState(blockPos, (net.minecraft.block.BlockState) data, 0);
}
if(physics) {
net.minecraft.block.BlockState state = isExtended ? ((BlockStateArgument) data).getBlockState() : ((net.minecraft.block.BlockState) data);
if(state.isLiquid()) {
fluidTickScheduler.scheduleTick(OrderedTick.create(state.getFluidState().getFluid(), blockPos));
} else {
blockTickScheduler.scheduleTick(OrderedTick.create(state.getBlock(), blockPos));
}
}
}
public @NotNull BlockState terraChunk$getBlock(int x, int y, int z) {

View File

@@ -17,8 +17,12 @@
package com.dfsek.terra.mod.mixin.implementations.terra.chunk;
import net.minecraft.command.argument.BlockStateArgument;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.server.command.SetBlockCommand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.chunk.WorldChunk;
import net.minecraft.world.gen.feature.EndGatewayFeature;
import net.minecraft.world.tick.OrderedTick;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -48,23 +52,29 @@ public abstract class WorldChunkMixin {
@Nullable
public abstract net.minecraft.block.BlockState setBlockState(BlockPos pos, net.minecraft.block.BlockState state, int flags);
@SuppressWarnings("ConstantValue")
public void terra$setBlock(int x, int y, int z, BlockState data, boolean physics) {
BlockPos blockPos = new BlockPos(x, y, z);
setBlockState(blockPos, (net.minecraft.block.BlockState) data, 0);
boolean isExtended = data.isExtended() && data.getClass().equals(BlockStateArgument.class);
if (isExtended) {
((BlockStateArgument) data).setBlockState((net.minecraft.server.world.ServerWorld) world, blockPos, 0);
} else {
setBlockState(blockPos, (net.minecraft.block.BlockState) data, 0);
}
if(physics) {
net.minecraft.block.BlockState state = ((net.minecraft.block.BlockState) data);
net.minecraft.block.BlockState state = isExtended ? ((BlockStateArgument) data).getBlockState() : ((net.minecraft.block.BlockState) data);
if(state.isLiquid()) {
world.getFluidTickScheduler().scheduleTick(OrderedTick.create(state.getFluidState().getFluid(), blockPos));
} else {
world.getBlockTickScheduler().scheduleTick(OrderedTick.create(state.getBlock(), blockPos));
}
}
}
public void terra$setBlock(int x, int y, int z, @NotNull BlockState blockState) {
((net.minecraft.world.chunk.Chunk) (Object) this).setBlockState(new BlockPos(x, y, z), (net.minecraft.block.BlockState) blockState,
0);
@SuppressWarnings("ConstantValue")
public void terra$setBlock(int x, int y, int z, @NotNull BlockState data) {
terra$setBlock(x, y, z, data, false);
}
@Intrinsic

View File

@@ -0,0 +1,25 @@
package com.dfsek.terra.mod.mixin.implementations.terra.nbt;
import com.dfsek.terra.api.block.BlockData;
import com.dfsek.terra.api.block.state.BlockStateExtended;
import net.minecraft.block.pattern.CachedBlockPosition;
import net.minecraft.command.argument.BlockStateArgument;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import org.spongepowered.asm.mixin.Implements;
import org.spongepowered.asm.mixin.Interface;
import org.spongepowered.asm.mixin.Intrinsic;
import org.spongepowered.asm.mixin.Mixin;
import java.util.function.Predicate;
@Mixin(NbtCompound.class)
@Implements(@Interface(iface = BlockData.class, prefix = "terra$"))
public abstract class NbtCompoundMixin implements NbtElement {
@Intrinsic
public String terra$toString() {
return this.toString();
}
}

View File

@@ -17,7 +17,9 @@
package com.dfsek.terra.mod.mixin.implementations.terra.world;
import net.minecraft.block.Block;
import net.minecraft.block.FluidBlock;
import net.minecraft.command.argument.BlockStateArgument;
import net.minecraft.entity.SpawnReason;
import net.minecraft.fluid.Fluid;
import net.minecraft.util.collection.BoundedRegionArray;
@@ -73,6 +75,10 @@ public abstract class ChunkRegionMixin {
@Final
private MultiTickScheduler<Fluid> fluidTickScheduler;
@Shadow
@Final
private MultiTickScheduler<Block> blockTickScheduler;
@Inject(at = @At("RETURN"),
method = "<init>(Lnet/minecraft/server/world/ServerWorld;Lnet/minecraft/util/collection/BoundedRegionArray;" +
@@ -85,12 +91,22 @@ public abstract class ChunkRegionMixin {
@Intrinsic(displace = true)
public void terraWorld$setBlockState(int x, int y, int z, BlockState data, boolean physics) {
BlockPos pos = new BlockPos(x, y, z);
((ChunkRegion) (Object) this).setBlockState(pos, (net.minecraft.block.BlockState) data, physics ? 3 : 1042);
if(physics && ((net.minecraft.block.BlockState) data).getBlock() instanceof FluidBlock) {
fluidTickScheduler.scheduleTick(
OrderedTick.create((((FluidBlockInvoker) ((net.minecraft.block.BlockState) data).getBlock())).invokeGetFluidState(
(net.minecraft.block.BlockState) data).getFluid(), pos));
BlockPos blockPos = new BlockPos(x, y, z);
int flags = physics ? 3 : 1042;
boolean isExtended = data.isExtended() && data.getClass().equals(BlockStateArgument.class);
if (isExtended) {
((BlockStateArgument) data).setBlockState(world, blockPos, flags);
} else {
((ChunkRegion) (Object) this).setBlockState(blockPos, (net.minecraft.block.BlockState) data, flags);
}
if(physics) {
net.minecraft.block.BlockState state = isExtended ? ((BlockStateArgument) data).getBlockState() : ((net.minecraft.block.BlockState) data);
if(state.isLiquid()) {
fluidTickScheduler.scheduleTick(OrderedTick.create(state.getFluidState().getFluid(), blockPos));
} else {
blockTickScheduler.scheduleTick(OrderedTick.create(state.getBlock(), blockPos));
}
}
}

View File

@@ -17,9 +17,20 @@
package com.dfsek.terra.mod.mixin.implementations.terra.world;
import com.dfsek.terra.mod.mixin.invoke.FluidBlockInvoker;
import net.minecraft.block.Block;
import net.minecraft.block.FluidBlock;
import net.minecraft.command.argument.BlockStateArgument;
import net.minecraft.entity.SpawnReason;
import net.minecraft.fluid.Fluid;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.ChunkRegion;
import net.minecraft.world.WorldAccess;
import net.minecraft.world.tick.MultiTickScheduler;
import net.minecraft.world.tick.OrderedTick;
import net.minecraft.world.tick.WorldTickScheduler;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Implements;
import org.spongepowered.asm.mixin.Interface;
import org.spongepowered.asm.mixin.Intrinsic;
@@ -38,10 +49,18 @@ import com.dfsek.terra.mod.generation.MinecraftChunkGeneratorWrapper;
import com.dfsek.terra.mod.generation.TerraBiomeSource;
import com.dfsek.terra.mod.util.MinecraftUtil;
import org.spongepowered.asm.mixin.Shadow;
@Mixin(net.minecraft.server.world.ServerWorld.class)
@Implements(@Interface(iface = ServerWorld.class, prefix = "terra$"))
public abstract class ServerWorldMixin {
@Shadow
public abstract WorldTickScheduler<Block> getBlockTickScheduler();
@Shadow
public abstract WorldTickScheduler<Fluid> getFluidTickScheduler();
public Entity terra$spawnEntity(double x, double y, double z, EntityType entityType) {
net.minecraft.entity.Entity entity = ((net.minecraft.entity.EntityType<?>) entityType).create(null, SpawnReason.CHUNK_GENERATION);
entity.setPos(x, y, z);
@@ -50,9 +69,22 @@ public abstract class ServerWorldMixin {
}
public void terra$setBlockState(int x, int y, int z, BlockState data, boolean physics) {
BlockPos pos = new BlockPos(x, y, z);
((net.minecraft.server.world.ServerWorld) (Object) this).setBlockState(pos, (net.minecraft.block.BlockState) data,
physics ? 3 : 1042);
BlockPos blockPos = new BlockPos(x, y, z);
int flags = physics ? 3 : 1042;
boolean isExtended = data.isExtended() && data.getClass().equals(BlockStateArgument.class);
if (isExtended) {
((BlockStateArgument) data).setBlockState(((net.minecraft.server.world.ServerWorld) (Object) this), blockPos, flags);
} else {
((net.minecraft.server.world.ServerWorld) (Object) this).setBlockState(blockPos, (net.minecraft.block.BlockState) data, flags);
}
if(physics) {
net.minecraft.block.BlockState state = isExtended ? ((BlockStateArgument) data).getBlockState() : ((net.minecraft.block.BlockState) data);
if(state.isLiquid()) {
getFluidTickScheduler().scheduleTick(OrderedTick.create(state.getFluidState().getFluid(), blockPos));
} else {
getBlockTickScheduler().scheduleTick(OrderedTick.create(state.getBlock(), blockPos));
}
}
}
@Intrinsic

View File

@@ -3,3 +3,5 @@ accessible class net/minecraft/world/biome/Biome$Weather
accessible class net/minecraft/world/gen/WorldPresets$Registrar
accessible field net/minecraft/world/dimension/DimensionOptionsRegistryHolder VANILLA_KEYS Ljava/util/Set;
accessible class net/minecraft/registry/RegistryLoader$Loader
extendable class net/minecraft/nbt/NbtElement
accessible field net/minecraft/block/entity/BlockEntity TYPE_CODEC Lcom/mojang/serialization/Codec;

View File

@@ -18,6 +18,7 @@
"implementations.terra.block.entity.LootableContainerBlockEntityMixin",
"implementations.terra.block.entity.MobSpawnerBlockEntityMixin",
"implementations.terra.block.entity.SignBlockEntityMixin",
"implementations.terra.block.state.BlockStateArgumentMixin",
"implementations.terra.block.state.BlockStateMixin",
"implementations.terra.block.state.PropertyMixin",
"implementations.terra.chunk.ChunkRegionMixin",
@@ -33,6 +34,7 @@
"implementations.terra.inventory.meta.EnchantmentMixin",
"implementations.terra.inventory.meta.ItemStackDamageableMixin",
"implementations.terra.inventory.meta.ItemStackMetaMixin",
"implementations.terra.nbt.NbtCompoundMixin",
"implementations.terra.world.ChunkRegionMixin",
"implementations.terra.world.ServerWorldMixin",
"invoke.BiomeInvoker",