Skip to content

Commit 6d0d4f2

Browse files
authored
Tab substitution placement handler (#834)
1 parent b52ed07 commit 6d0d4f2

4 files changed

Lines changed: 177 additions & 27 deletions

File tree

src/main/java/com/ldtteam/structurize/placement/StructurePlacer.java

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -316,19 +316,6 @@ else if (requiredItems == null)
316316
}
317317
}
318318

319-
if (localState.getBlock() == ModBlocks.blockTagSubstitution.get() && handler.fancyPlacement())
320-
{
321-
if (tileEntityData != null && BlockEntity.loadStatic(localPos, localState, tileEntityData) instanceof BlockEntityTagSubstitution tagEntity)
322-
{
323-
localState = tagEntity.getReplacement().getBlockState();
324-
tileEntityData = tagEntity.getReplacement().getBlockEntityTag();
325-
}
326-
else
327-
{
328-
localState = Blocks.AIR.defaultBlockState();
329-
}
330-
}
331-
332319
if (IPlacementHandler.doesWorldStateMatchBlueprintState(blockInfo, worldPos, this.handler))
333320
{
334321
return new BlockPlacementResult(worldPos, BlockPlacementResult.Result.SUCCESS);
@@ -614,19 +601,6 @@ public BlockPlacementResult getResourceRequirements(
614601
}
615602
}
616603

617-
if (localState.getBlock() == ModBlocks.blockTagSubstitution.get() && handler.fancyPlacement())
618-
{
619-
if (tileEntityData != null && BlockEntity.loadStatic(localPos, localState, tileEntityData) instanceof BlockEntityTagSubstitution tagEntity)
620-
{
621-
localState = tagEntity.getReplacement().getBlockState();
622-
tileEntityData = tagEntity.getReplacement().getBlockEntityTag();
623-
}
624-
else
625-
{
626-
localState = Blocks.AIR.defaultBlockState();
627-
}
628-
}
629-
630604
if (IPlacementHandler.doesWorldStateMatchBlueprintState(new BlockInfo(localPos, localState, handler.getBluePrint().getTileEntityData(worldPos, localPos)), worldPos, this.handler))
631605
{
632606
if (requiredItems.isEmpty())
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
package com.ldtteam.structurize.placement.handlers.placement;
2+
3+
import com.ldtteam.structurize.api.util.ItemStackUtils;
4+
import com.ldtteam.structurize.api.util.Log;
5+
import com.ldtteam.structurize.blockentities.BlockEntityTagSubstitution;
6+
import com.ldtteam.structurize.blocks.ModBlocks;
7+
import com.ldtteam.structurize.placement.IPlacementContext;
8+
import com.ldtteam.structurize.util.BlockUtils;
9+
import net.minecraft.core.BlockPos;
10+
import net.minecraft.nbt.CompoundTag;
11+
import net.minecraft.util.Tuple;
12+
import net.minecraft.world.item.ItemStack;
13+
import net.minecraft.world.level.Level;
14+
import net.minecraft.world.level.block.entity.BlockEntity;
15+
import net.minecraft.world.level.block.state.BlockState;
16+
import org.jetbrains.annotations.NotNull;
17+
import org.jetbrains.annotations.Nullable;
18+
19+
import java.util.ArrayList;
20+
import java.util.Collections;
21+
import java.util.List;
22+
23+
import static com.ldtteam.structurize.api.util.constant.Constants.UPDATE_FLAG;
24+
import static com.ldtteam.structurize.placement.handlers.placement.PlacementHandlers.getItemsFromTileEntity;
25+
import static com.ldtteam.structurize.placement.handlers.placement.PlacementHandlers.handleTileEntityPlacement;
26+
27+
/**
28+
* Handle the substitution handler.
29+
*/
30+
public class BlockTagSubstitutionPlacementHandler implements IPlacementHandler
31+
{
32+
@Override
33+
public boolean canHandle(@NotNull final Level world, @NotNull final BlockPos pos, @NotNull final BlockState blockState)
34+
{
35+
return blockState.getBlock() == ModBlocks.blockTagSubstitution.get();
36+
}
37+
38+
@Override
39+
public ActionProcessingResult handle(
40+
@NotNull final Level world,
41+
@NotNull final BlockPos pos,
42+
@NotNull final BlockState blockState,
43+
@Nullable final CompoundTag tileEntityData,
44+
@NotNull final IPlacementContext placementContext)
45+
{
46+
if (placementContext.fancyPlacement())
47+
{
48+
if (tileEntityData != null && BlockEntity.loadStatic(pos, blockState, tileEntityData) instanceof BlockEntityTagSubstitution tagEntity)
49+
{
50+
final IPlacementHandler placementHandler = PlacementHandlers.getHandler(world, pos, tagEntity.getReplacement().getBlockState());
51+
if (placementHandler != this)
52+
{
53+
return placementHandler.handle(world, pos, tagEntity.getReplacement().getBlockState(), tagEntity.getReplacement().getBlockEntityTag(), placementContext);
54+
}
55+
else
56+
{
57+
Log.getLogger().warn("Blueprint contains faulty tag substitution block with recursive data.");
58+
return ActionProcessingResult.PASS;
59+
}
60+
}
61+
else
62+
{
63+
Log.getLogger().warn("Blueprint contains faulty tag substitution block without data.");
64+
return ActionProcessingResult.PASS;
65+
}
66+
}
67+
else
68+
{
69+
// Normal placement with full handler.
70+
if (world.getBlockState(pos).equals(blockState))
71+
{
72+
world.removeBlock(pos, false);
73+
world.setBlock(pos, blockState, UPDATE_FLAG);
74+
if (tileEntityData != null)
75+
{
76+
handleTileEntityPlacement(tileEntityData, world, pos, placementContext.getRotationMirror());
77+
}
78+
return ActionProcessingResult.PASS;
79+
}
80+
81+
if (!world.setBlock(pos, blockState, UPDATE_FLAG))
82+
{
83+
return ActionProcessingResult.DENY;
84+
}
85+
86+
if (tileEntityData != null)
87+
{
88+
handleTileEntityPlacement(tileEntityData, world, pos, placementContext.getRotationMirror());
89+
}
90+
}
91+
92+
return ActionProcessingResult.SUCCESS;
93+
}
94+
95+
@Override
96+
public boolean doesWorldStateMatchBlueprintState(final BlockState worldState, final BlockState blueprintState, final Tuple<BlockEntity, CompoundTag> blockEntityData, final @NotNull IPlacementContext placementContext)
97+
{
98+
if (placementContext.fancyPlacement())
99+
{
100+
if (blockEntityData != null && BlockEntity.loadStatic(BlockPos.ZERO, blueprintState, blockEntityData.getB()) instanceof BlockEntityTagSubstitution tagEntity)
101+
{
102+
try
103+
{
104+
final IPlacementHandler placementHandler = PlacementHandlers.getHandler(null, BlockPos.ZERO, tagEntity.getReplacement().getBlockState());
105+
if (placementHandler != this)
106+
{
107+
final CompoundTag tileEntityData = tagEntity.getReplacement().getBlockEntityTag();
108+
Tuple<BlockEntity, CompoundTag> updatedTETuple = null;
109+
if (!tileEntityData.isEmpty())
110+
{
111+
updatedTETuple = new Tuple<>(blockEntityData.getA(), tileEntityData);
112+
}
113+
114+
return placementHandler.doesWorldStateMatchBlueprintState(worldState, tagEntity.getReplacement().getBlockState(), updatedTETuple, placementContext);
115+
}
116+
else
117+
{
118+
Log.getLogger().warn("Blueprint contains faulty tag substitution block with recursive data.");
119+
return true;
120+
}
121+
}
122+
catch (final Exception ex)
123+
{
124+
Log.getLogger().warn("Blueprint contains tag substitution handler with block that needs world in match. This is deprecated. We will be moving to state only matching in the future.");
125+
return true;
126+
}
127+
}
128+
else
129+
{
130+
Log.getLogger().warn("Blueprint contains faulty tag substitution block without data.");
131+
return true;
132+
}
133+
}
134+
135+
// Always paste over to repair data if necessary.
136+
return false;
137+
}
138+
139+
@Override
140+
public List<ItemStack> getRequiredItems(
141+
@NotNull final Level world,
142+
@NotNull final BlockPos pos,
143+
@NotNull final BlockState blockState,
144+
@Nullable final CompoundTag tileEntityData,
145+
@NotNull final IPlacementContext placementContext)
146+
{
147+
if (placementContext.fancyPlacement())
148+
{
149+
if (tileEntityData != null && BlockEntity.loadStatic(pos, blockState, tileEntityData) instanceof BlockEntityTagSubstitution tagEntity)
150+
{
151+
final IPlacementHandler placementHandler = PlacementHandlers.getHandler(world, pos, tagEntity.getReplacement().getBlockState());
152+
if (placementHandler != this)
153+
{
154+
return placementHandler.getRequiredItems(world, pos, tagEntity.getReplacement().getBlockState(), tagEntity.getReplacement().getBlockEntityTag(), placementContext);
155+
}
156+
else
157+
{
158+
Log.getLogger().warn("Blueprint contains faulty tag substitution block with recursive data.");
159+
return Collections.emptyList();
160+
}
161+
}
162+
else
163+
{
164+
Log.getLogger().warn("Blueprint contains faulty tag substitution block without data.");
165+
return Collections.emptyList();
166+
}
167+
}
168+
169+
final List<ItemStack> itemList = new ArrayList<>(getItemsFromTileEntity(tileEntityData, blockState));
170+
itemList.add(BlockUtils.getItemStackFromBlockState(blockState));
171+
itemList.removeIf(ItemStackUtils::isEmpty);
172+
return itemList;
173+
}
174+
}

src/main/java/com/ldtteam/structurize/placement/handlers/placement/IPlacementHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.ldtteam.structurize.placement.handlers.placement;
22

3+
import com.ldtteam.structurize.blocks.ModBlocks;
34
import com.ldtteam.structurize.placement.IPlacementContext;
45
import com.ldtteam.structurize.placement.structure.IStructureHandler;
56
import com.ldtteam.structurize.util.BlockInfo;
@@ -30,7 +31,7 @@ static boolean doesWorldStateMatchBlueprintState(@NotNull BlockInfo blockInfo, @
3031
if (blockInfo.hasTileEntityData())
3132
{
3233
final BlockEntity blockEntity = structureHandler.getWorld().getBlockEntity(worldPos);
33-
if (blockEntity == null)
34+
if (blockEntity == null && blockInfo.getState().getBlock() != ModBlocks.blockTagSubstitution.get())
3435
{
3536
return false;
3637
}

src/main/java/com/ldtteam/structurize/placement/handlers/placement/PlacementHandlers.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public enum AddType {
7272
handlers.add(new DoBlockPlacementHandler());
7373
handlers.add(new DoDoorBlockPlacementHandler());
7474
handlers.add(new ContainerPlacementHandler());
75+
handlers.add(new BlockTagSubstitutionPlacementHandler());
7576
handlers.add(new GeneralBlockPlacementHandler());
7677
}
7778

0 commit comments

Comments
 (0)