1- //package dev.compactmods.crafting.client.fakeworld;
2- //
3- //import dev.compactmods.crafting.api.components.IRecipeBlockComponent;
4- //import dev.compactmods.crafting.api.recipe.layers.IRecipeLayer;
5- //import dev.compactmods.crafting.recipes.MiniaturizationRecipe;
6- //import dev.compactmods.crafting.util.BlockSpaceUtil;
7- //import net.minecraft.core.BlockPos;
8- //import net.minecraft.core.registries.Registries;
9- //import net.minecraft.world.level.ChunkPos;
10- //import net.minecraft.world.level.biome.Biomes;
11- //import net.minecraft.world.level.block.Blocks;
12- //import net.minecraft.world.level.block.EntityBlock;
13- //import net.minecraft.world.level.block.entity.BlockEntity;
14- //import net.minecraft.world.level.block.state.BlockState;
15- //import net.minecraft.world.level.chunk.EmptyLevelChunk;
16- //import net.minecraft.world.level.material.FluidState;
17- //
18- //import org.jetbrains.annotations.Nullable;
19- //import java.util.HashMap;
20- //import java.util.Map;
21- //import java.util.Optional;
22- //
23- //public class RecipeChunk extends EmptyLevelChunk {
24- // private final MiniaturizationRecipe recipe;
25- // private final Map<BlockPos, BlockState> blockCache;
26- // private final Map<BlockPos, BlockEntity> tileCache;
27- //
28- // public RecipeChunk(RenderingWorld renderingLevel, ChunkPos chunkPos, MiniaturizationRecipe recipe) {
29- // super(renderingLevel, chunkPos, renderingLevel.registryAccess().registryOrThrow(Registries.BIOME).getHolderOrThrow(Biomes.THE_VOID));
30- // this.recipe = recipe;
31- //
32- // this.blockCache = new HashMap<>();
33- // this.tileCache = new HashMap<>();
34- //
35- // BlockSpaceUtil.getBlocksIn(recipe.getDimensions()).forEach(pos -> {
36- // int y = pos.getY();
37- // Optional<IRecipeLayer> layer = recipe.getLayer(y);
38- //
39- // if(!layer.isPresent())
40- // return;
41- //
42- // IRecipeLayer rLayer = layer.get();
43- // Optional<String> componentForPosition = rLayer.getComponentForPosition(pos.below(y));
44- //
45- // BlockState posState = componentForPosition
46- // .flatMap(recipe.getComponents()::getBlock)
47- // .map(IRecipeBlockComponent::getRenderState)
48- // .orElse(Blocks.VOID_AIR.defaultBlockState());
49- //
50- // blockCache.put(pos, posState);
51- //
52- // if(posState.getBlock() instanceof EntityBlock eb) {
53- // BlockEntity tile = eb.newBlockEntity(pos.immutable(), posState);
54- // if(tile != null) {
55- // tile.setLevel(renderingLevel);
56- // tileCache.put(pos.immutable(), tile);
57- // }
58- // }
59- // });
60- // }
61- //
62- // @Override
63- // public BlockState getBlockState(BlockPos pos) {
64- // if(blockCache.containsKey(pos))
65- // return blockCache.get(pos);
66- //
67- // return Blocks.VOID_AIR.defaultBlockState();
68- // }
69- //
70- // @Override
71- // public FluidState getFluidState(BlockPos pos) {
72- // return getBlockState(pos).getFluidState();
73- // }
74- //
75- // @Nullable
76- // @Override
77- // public BlockEntity getBlockEntity(BlockPos pos) {
78- // return tileCache.get(pos);
79- // }
80- //
81- // @Nullable
82- // @Override
83- // public BlockEntity getBlockEntity(BlockPos pos, EntityCreationType createType) {
84- // return tileCache.get(pos);
85- // }
86- //}
1+ package dev .compactmods .crafting .fakeworld ;
2+
3+ import javax .annotation .Nullable ;
4+ import java .util .HashMap ;
5+ import java .util .Map ;
6+ import java .util .Optional ;
7+ import dev .compactmods .crafting .api .components .IRecipeBlockComponent ;
8+ import dev .compactmods .crafting .api .recipe .layers .IRecipeLayer ;
9+ import dev .compactmods .crafting .recipes .MiniaturizationRecipe ;
10+ import dev .compactmods .crafting .util .BlockSpaceUtil ;
11+ import net .minecraft .core .BlockPos ;
12+ import net .minecraft .world .level .ChunkPos ;
13+ import net .minecraft .world .level .biome .Biomes ;
14+ import net .minecraft .world .level .block .Blocks ;
15+ import net .minecraft .world .level .block .EntityBlock ;
16+ import net .minecraft .world .level .block .entity .BlockEntity ;
17+ import net .minecraft .world .level .block .state .BlockState ;
18+ import net .minecraft .world .level .chunk .EmptyLevelChunk ;
19+ import net .minecraft .world .level .material .FluidState ;
20+ import net .minecraft .core .registries .Registries ;
21+
22+ public class RecipeChunk extends EmptyLevelChunk {
23+ private final MiniaturizationRecipe recipe ;
24+ private final Map <BlockPos , BlockState > blockCache ;
25+ private final Map <BlockPos , BlockEntity > tileCache ;
26+
27+ public RecipeChunk (RenderingWorld renderingLevel , ChunkPos chunkPos , MiniaturizationRecipe recipe ) {
28+ super (renderingLevel , chunkPos , renderingLevel .registryAccess ().registryOrThrow (Registries .BIOME ).getHolderOrThrow (Biomes .THE_VOID ));
29+ this .recipe = recipe ;
30+
31+ this .blockCache = new HashMap <>();
32+ this .tileCache = new HashMap <>();
33+
34+ BlockSpaceUtil .getBlocksIn (recipe .getDimensions ()).forEach (pos -> {
35+ int y = pos .getY ();
36+ Optional <IRecipeLayer > layer = recipe .getLayer (y );
37+
38+ if (!layer .isPresent ())
39+ return ;
40+
41+ IRecipeLayer rLayer = layer .get ();
42+ Optional <String > componentForPosition = rLayer .getComponentForPosition (pos .below (y ));
43+
44+ BlockState posState = componentForPosition
45+ .flatMap (recipe .getComponents ()::getBlock )
46+ .map (IRecipeBlockComponent ::getRenderState )
47+ .orElse (Blocks .VOID_AIR .defaultBlockState ());
48+
49+ blockCache .put (pos , posState );
50+
51+ if (posState .getBlock () instanceof EntityBlock eb ) {
52+ BlockEntity tile = eb .newBlockEntity (pos .immutable (), posState );
53+ if (tile != null ) {
54+ tile .setLevel (renderingLevel );
55+ tileCache .put (pos .immutable (), tile );
56+ }
57+ }
58+ });
59+ }
60+
61+ @ Override
62+ public BlockState getBlockState (BlockPos pos ) {
63+ if (blockCache .containsKey (pos ))
64+ return blockCache .get (pos );
65+
66+ return Blocks .VOID_AIR .defaultBlockState ();
67+ }
68+
69+ @ Override
70+ public FluidState getFluidState (BlockPos pos ) {
71+ return getBlockState (pos ).getFluidState ();
72+ }
73+
74+ @ Nullable
75+ @ Override
76+ public BlockEntity getBlockEntity (BlockPos pos ) {
77+ return tileCache .get (pos );
78+ }
79+
80+ @ Nullable
81+ @ Override
82+ public BlockEntity getBlockEntity (BlockPos pos , EntityCreationType createType ) {
83+ return tileCache .get (pos );
84+ }
85+ }
0 commit comments