Skip to content

Commit

Permalink
Allow Structure-Block-Registry Processor to be used in datapack conte…
Browse files Browse the repository at this point in the history
…xts (#626)

The `StructureBlockRegistry` is now obtained by the processor from within the process function, allowing the processor to be serialized/deserialized. It should now be possible to use this processor in jigsaw structures.
  • Loading branch information
Dweblenod authored Jun 30, 2024
1 parent bf61116 commit 78c93eb
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 30 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
- Block Teleporter
- Built in compatability for Better Combat mod

### Changed
- Structure Block Registry Processor can now be used for data generated structures through a processor_list

### Contributors for this release

- Dweblenod, sipherNil, gtzc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public class MSStructureProcessorTypes
{
public static final DeferredRegister<StructureProcessorType<?>> REGISTER = DeferredRegister.create(Registries.STRUCTURE_PROCESSOR, Minestuck.MOD_ID);

public static final Supplier<StructureProcessorType<StructureBlockRegistryProcessor>> BLOCK_REGISTRY = REGISTER.register("block_entity", () -> () -> StructureBlockRegistryProcessor.CODEC);
public static final Supplier<StructureProcessorType<StructureBlockRegistryProcessor>> BLOCK_REGISTRY = REGISTER.register("block_registry", () -> () -> StructureBlockRegistryProcessor.CODEC);

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public boolean place(FeaturePlaceContext<Config> context)

StructurePlaceSettings settings = new StructurePlaceSettings();
if(config.useLandBlocks)
settings.addProcessor(StructureBlockRegistryProcessor.from(context));
settings.addProcessor(StructureBlockRegistryProcessor.INSTANCE);

if(config.heightPlacement.isPresent())
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,49 @@
package com.mraof.minestuck.world.gen.feature;

import com.mojang.serialization.Codec;
import com.mojang.serialization.Decoder;
import com.mojang.serialization.Encoder;
import com.mraof.minestuck.world.gen.structure.blocks.StructureBlockRegistry;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.LevelReader;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.ServerLevelAccessor;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.levelgen.feature.FeaturePlaceContext;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructurePlaceSettings;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureProcessor;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureProcessorType;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
import net.minecraft.world.level.chunk.ChunkGenerator;
import net.minecraft.world.level.levelgen.structure.templatesystem.*;

import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.List;

@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
public class StructureBlockRegistryProcessor extends StructureProcessor
public final class StructureBlockRegistryProcessor extends StructureProcessor
{
public static final Codec<StructureBlockRegistryProcessor> CODEC = Codec.of(Encoder.error("StructureBlockRegistryProcessor is not serializable."), Decoder.error("StructureBlockRegistryProcessor is not serializable."));
private final StructureBlockRegistry blocks;
public static final Codec<StructureBlockRegistryProcessor> CODEC = Codec.unit(() -> StructureBlockRegistryProcessor.INSTANCE);
public static final StructureBlockRegistryProcessor INSTANCE = new StructureBlockRegistryProcessor();

public StructureBlockRegistryProcessor(StructureBlockRegistry blocks)
private StructureBlockRegistryProcessor()
{
this.blocks = blocks;
}

public static StructureBlockRegistryProcessor from(FeaturePlaceContext<?> context)
{
return new StructureBlockRegistryProcessor(StructureBlockRegistry.getOrDefault(context.chunkGenerator()));
}

@Nullable
@Override //TODO figure out blockpos difference
public StructureTemplate.StructureBlockInfo process(LevelReader level, BlockPos blockPos, BlockPos blockPos2, StructureTemplate.StructureBlockInfo original, StructureTemplate.StructureBlockInfo current, StructurePlaceSettings placementSettings, @Nullable StructureTemplate template)
@Override
public List<StructureTemplate.StructureBlockInfo> finalizeProcessing(ServerLevelAccessor levelAccessor, BlockPos pOffset, BlockPos pPos, List<StructureTemplate.StructureBlockInfo> originals, List<StructureTemplate.StructureBlockInfo> currents, StructurePlaceSettings placementSettings)
{
if(original.state() != current.state())
return current;
BlockState newState = blocks.getTemplateState(current.state());
return new StructureTemplate.StructureBlockInfo(current.pos(), newState, current.nbt());
ServerLevel serverLevel = levelAccessor.getLevel();
ChunkGenerator chunkGenerator = serverLevel.getChunkSource().getGenerator();
StructureBlockRegistry blockRegistry = StructureBlockRegistry.getOrDefault(chunkGenerator);

for(int i = 0; i < originals.size(); i++)
{
StructureTemplate.StructureBlockInfo original = originals.get(i);
StructureTemplate.StructureBlockInfo current = currents.get(i);

if(original.state() != current.state())
continue;

BlockState newState = blockRegistry.getTemplateState(current.state());
currents.set(i, new StructureTemplate.StructureBlockInfo(current.pos(), newState, current.nbt()));
}

return currents;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public void placeWithStructureBlockRegistry(FeaturePlaceContext<?> context)

public void placeWithStructureBlockRegistry(FeaturePlaceContext<?> context, StructurePlaceSettings settings)
{
this.place(context, settings.addProcessor(StructureBlockRegistryProcessor.from(context)));
this.place(context, settings.addProcessor(StructureBlockRegistryProcessor.INSTANCE));
}

public void place(FeaturePlaceContext<?> context)
Expand All @@ -173,7 +173,7 @@ public void placeWithStructureBlockRegistryAt(int y, FeaturePlaceContext<?> cont

public void placeWithStructureBlockRegistryAt(int y, FeaturePlaceContext<?> context, StructurePlaceSettings settings)
{
this.placeAt(y, context, settings.addProcessor(StructureBlockRegistryProcessor.from(context)));
this.placeAt(y, context, settings.addProcessor(StructureBlockRegistryProcessor.INSTANCE));
}

public void placeAt(int y, FeaturePlaceContext<?> context)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"processors": [
{
"processor_type": "minestuck:block_registry"
}
]
}

0 comments on commit 78c93eb

Please sign in to comment.