Skip to content

Commit e9284ba

Browse files
Raycomssomeaddons
andauthored
rework world equals checks (#818)
Co-authored-by: someaddons <someaddons@github.com>
1 parent df87d7b commit e9284ba

12 files changed

Lines changed: 765 additions & 295 deletions

src/main/java/com/ldtteam/structurize/client/gui/WindowScan.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
import com.ldtteam.blockui.views.ScrollingList;
77
import com.ldtteam.blockui.views.View;
88
import com.ldtteam.structurize.api.ItemStorage;
9+
import com.ldtteam.structurize.api.RotationMirror;
910
import com.ldtteam.structurize.api.constants.Constants;
1011
import com.ldtteam.structurize.blockentities.interfaces.IBlueprintDataProviderBE;
1112
import com.ldtteam.structurize.client.gui.util.InputFilters;
1213
import com.ldtteam.structurize.client.gui.util.ItemPositionsStorage;
1314
import com.ldtteam.structurize.network.messages.*;
15+
import com.ldtteam.structurize.placement.SimplePlacementContext;
1416
import com.ldtteam.structurize.placement.handlers.placement.IPlacementHandler;
1517
import com.ldtteam.structurize.placement.handlers.placement.PlacementHandlers;
1618
import com.ldtteam.structurize.storage.rendering.RenderingCache;
@@ -507,7 +509,8 @@ private void updateResources()
507509
{
508510
final IPlacementHandler handler = PlacementHandlers.getHandler(world, BlockPos.ZERO, blockState);
509511
final List<ItemStack> itemList =
510-
handler.getRequiredItems(world, here, blockState, tileEntity == null ? null : tileEntity.saveWithFullMetadata(world.registryAccess()), true);
512+
handler.getRequiredItems(world, here, blockState, tileEntity == null ? null : tileEntity.saveWithFullMetadata(world.registryAccess()),
513+
new SimplePlacementContext(false, RotationMirror.NONE));
511514
for (final ItemStack stack : itemList)
512515
{
513516
addNeededResource(stack, visible, here);

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.ldtteam.structurize.placement;
22

3+
import com.ldtteam.structurize.placement.handlers.placement.IPlacementHandler;
34
import com.ldtteam.structurize.placement.structure.IStructureHandler;
4-
import com.ldtteam.structurize.util.BlockUtils;
55
import com.ldtteam.structurize.util.BlueprintPositionInfo;
66
import net.minecraft.core.BlockPos;
77
import net.neoforged.neoforge.common.util.TriPredicate;
@@ -105,9 +105,7 @@ private Result iterateWithCondition(final TriPredicate<BlueprintPositionInfo, Bl
105105
{
106106
continue;
107107
}
108-
else if (!isRemoving() && BlockUtils.areBlockStatesEqual(info.getBlockInfo().getState(), structureHandler.getWorld().getBlockState(worldPos), structureHandler::replaceWithSolidBlock, structureHandler.fancyPlacement(), structureHandler::shouldBlocksBeConsideredEqual,
109-
info.getBlockInfo().getTileEntityData(),
110-
info.getBlockInfo().getTileEntityData() == null ? null : structureHandler.getWorld().getBlockEntity(worldPos)) && info.getEntities().length == 0)
108+
else if (!isRemoving() && IPlacementHandler.doesWorldStateMatchBlueprintState(info.getBlockInfo(), worldPos, structureHandler) && info.getEntities().length == 0)
111109
{
112110
structureHandler.triggerSuccess(progressPos, Collections.emptyList(), false);
113111
continue;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.ldtteam.structurize.placement;
2+
3+
import com.ldtteam.structurize.api.RotationMirror;
4+
import com.ldtteam.structurize.blueprints.v1.Blueprint;
5+
import net.minecraft.core.BlockPos;
6+
import net.minecraft.world.level.block.state.BlockState;
7+
import org.jetbrains.annotations.Nullable;
8+
9+
import java.util.function.Function;
10+
11+
public interface IPlacementContext
12+
{
13+
/**
14+
* Getter for the placement settings.
15+
* @return the settings object.
16+
*/
17+
RotationMirror getRotationMirror();
18+
19+
/**
20+
* If this is supposed to be fancy placement (player facing) or builder facing (complete).
21+
* @return true if fancy placement.
22+
*/
23+
boolean fancyPlacement();
24+
25+
/**
26+
* Get the solid worldgen block for given pos while using data from handler.
27+
*
28+
* @param worldPos the world pos.
29+
* @param virtualBlocks blueprint blocks, fnc may return null if virtual block is not available (then use level instead for getting surrounding block states), pos argument is using world coords
30+
* @return the solid worldgen block (classically biome dependent).
31+
*/
32+
BlockState getSolidBlockForPos(BlockPos worldPos, Function<BlockPos, @Nullable BlockState> virtualBlocks);
33+
34+
/**
35+
* Get the world position this is placed at.
36+
* @return the position.
37+
*/
38+
BlockPos getCenterPos();
39+
40+
/**
41+
* Get the bluerint from the handler.
42+
* @return the blueprint
43+
*/
44+
Blueprint getBluePrint();
45+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.ldtteam.structurize.placement;
2+
3+
import com.ldtteam.structurize.api.RotationMirror;
4+
import com.ldtteam.structurize.blueprints.v1.Blueprint;
5+
import net.minecraft.core.BlockPos;
6+
import net.minecraft.world.level.block.Blocks;
7+
import net.minecraft.world.level.block.state.BlockState;
8+
import org.jetbrains.annotations.Nullable;
9+
10+
import java.util.function.Function;
11+
12+
/**
13+
* Simple placement context for non blueprint handling.
14+
*/
15+
public class SimplePlacementContext implements IPlacementContext
16+
{
17+
/**
18+
* If placement should be fancy or complete.
19+
*/
20+
private final boolean fancyPlacement;
21+
22+
/**
23+
* Rotation mirror.
24+
*/
25+
private final RotationMirror rotationMirror;
26+
27+
public SimplePlacementContext(final boolean fancyPlacement, final RotationMirror rotationMirror)
28+
{
29+
this.fancyPlacement = fancyPlacement;
30+
this.rotationMirror = rotationMirror;
31+
}
32+
33+
@Override
34+
public RotationMirror getRotationMirror()
35+
{
36+
return rotationMirror;
37+
}
38+
39+
@Override
40+
public boolean fancyPlacement()
41+
{
42+
return fancyPlacement;
43+
}
44+
45+
@Override
46+
public BlockState getSolidBlockForPos(final BlockPos worldPos, final Function<BlockPos, @Nullable BlockState> virtualBlocks)
47+
{
48+
return Blocks.DIRT.defaultBlockState();
49+
}
50+
51+
@Override
52+
public BlockPos getCenterPos()
53+
{
54+
return BlockPos.ZERO;
55+
}
56+
57+
@Override
58+
public Blueprint getBluePrint()
59+
{
60+
return null;
61+
}
62+
}

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

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.ldtteam.structurize.placement.handlers.placement.IPlacementHandler;
99
import com.ldtteam.structurize.placement.handlers.placement.PlacementHandlers;
1010
import com.ldtteam.structurize.placement.structure.IStructureHandler;
11+
import com.ldtteam.structurize.util.BlockInfo;
1112
import com.ldtteam.structurize.util.BlockUtils;
1213
import com.ldtteam.structurize.util.ChangeStorage;
1314
import net.minecraft.core.BlockPos;
@@ -174,7 +175,7 @@ public StructurePhasePlacementResult executeStructureStep(
174175
result = handleEntitySpawn(world, worldPos, localPos, storage, false);
175176
break;
176177
default:
177-
result = handleBlockPlacement(world, worldPos, localPos, storage, localState, handler.getBluePrint().getTileEntityData(worldPos, localPos));
178+
result = handleBlockPlacement(world, worldPos, storage, new BlockInfo(localPos, localState, handler.getBluePrint().getTileEntityData(worldPos, localPos)));
178179
}
179180
count++;
180181

@@ -213,19 +214,19 @@ public StructurePhasePlacementResult executeStructureStep(
213214
* When we extract this into another mod, we have to override the method.
214215
* @param world the world.
215216
* @param worldPos the world position.
216-
* @param localPos the local pos
217217
* @param storage the change storage.
218-
* @param localState the local state.
219-
* @param tileEntityData the tileEntity.
218+
* @param blockInfo the tileEntity.
220219
*/
221220
public BlockPlacementResult handleBlockPlacement(
222221
final Level world,
223222
final BlockPos worldPos,
224-
final BlockPos localPos,
225223
final ChangeStorage storage,
226-
BlockState localState,
227-
CompoundTag tileEntityData)
224+
final BlockInfo blockInfo)
228225
{
226+
BlockState localState = blockInfo.getState();
227+
CompoundTag tileEntityData = blockInfo.getTileEntityData();
228+
final BlockPos localPos = blockInfo.getPos();
229+
229230
final BlockState worldState = world.getBlockState(worldPos);
230231
boolean sameBlockInWorld = false;
231232
if (worldState.getBlock() == localState.getBlock() && tileEntityData == null)
@@ -248,16 +249,6 @@ public BlockPlacementResult handleBlockPlacement(
248249
return entityResult;
249250
}
250251

251-
BlockEntity worldEntity = null;
252-
if (tileEntityData != null)
253-
{
254-
worldEntity = world.getBlockEntity(worldPos);
255-
}
256-
257-
if (localState.getBlock() == ModBlocks.blockSolidSubstitution.get() && handler.fancyPlacement())
258-
{
259-
localState = this.handler.getSolidBlockForPos(worldPos, handler.getBluePrint().getRawBlockStateFunction().compose(handler::getStructurePosFromWorld));
260-
}
261252
if (localState.getBlock() == ModBlocks.blockTagSubstitution.get() && handler.fancyPlacement())
262253
{
263254
if (tileEntityData != null && BlockEntity.loadStatic(localPos, localState, tileEntityData, world.registryAccess()) instanceof BlockEntityTagSubstitution tagEntity)
@@ -271,7 +262,7 @@ public BlockPlacementResult handleBlockPlacement(
271262
}
272263
}
273264

274-
if (BlockUtils.areBlockStatesEqual(localState, worldState, handler::replaceWithSolidBlock, handler.fancyPlacement(), handler::shouldBlocksBeConsideredEqual, tileEntityData, worldEntity))
265+
if (IPlacementHandler.doesWorldStateMatchBlueprintState(blockInfo, worldPos, this.handler))
275266
{
276267
return new BlockPlacementResult(worldPos, BlockPlacementResult.Result.SUCCESS);
277268
}
@@ -283,7 +274,7 @@ public BlockPlacementResult handleBlockPlacement(
283274

284275
if (!sameBlockInWorld && !this.handler.isCreative())
285276
{
286-
for (final ItemStack stack : placementHandler.getRequiredItems(world, worldPos, localState, tileEntityData, false))
277+
for (final ItemStack stack : placementHandler.getRequiredItems(world, worldPos, localState, tileEntityData, handler))
287278
{
288279
if (!stack.isEmpty() && !this.handler.isStackFree(stack))
289280
{
@@ -309,7 +300,7 @@ public BlockPlacementResult handleBlockPlacement(
309300

310301
this.handler.prePlacementLogic(worldPos, localState, requiredItems);
311302

312-
final IPlacementHandler.ActionProcessingResult result = placementHandler.handle(getHandler().getBluePrint(), world, worldPos, localState, tileEntityData, !this.handler.fancyPlacement(), this.handler.getWorldPos(), this.handler.getRotationMirror());
303+
final IPlacementHandler.ActionProcessingResult result = placementHandler.handle(world, worldPos, localState, tileEntityData, this.handler);
313304
if (result == IPlacementHandler.ActionProcessingResult.DENY)
314305
{
315306
placementHandler.handleRemoval(handler, world, worldPos, tileEntityData);
@@ -352,7 +343,7 @@ public BlockPlacementResult handleEntitySpawn(
352343
{
353344
try
354345
{
355-
final BlockPos pos = this.handler.getWorldPos().subtract(handler.getBluePrint().getPrimaryBlockOffset());
346+
final BlockPos pos = this.handler.getCenterPos().subtract(handler.getBluePrint().getPrimaryBlockOffset());
356347

357348
final Optional<EntityType<?>> type = EntityType.by(compound);
358349
if (type.isPresent())
@@ -526,10 +517,7 @@ public BlockPlacementResult getResourceRequirements(
526517
{
527518
worldEntity = world.getBlockEntity(worldPos);
528519
}
529-
if (localState.getBlock() == ModBlocks.blockSolidSubstitution.get() && handler.fancyPlacement())
530-
{
531-
localState = this.handler.getSolidBlockForPos(worldPos, handler.getBluePrint().getRawBlockStateFunction().compose(handler::getStructurePosFromWorld));
532-
}
520+
533521
if (localState.getBlock() == ModBlocks.blockTagSubstitution.get() && handler.fancyPlacement())
534522
{
535523
if (tileEntityData != null && BlockEntity.loadStatic(localPos, localState, tileEntityData, world.registryAccess()) instanceof BlockEntityTagSubstitution tagEntity)
@@ -551,7 +539,7 @@ public BlockPlacementResult getResourceRequirements(
551539
final IPlacementHandler placementHandler = PlacementHandlers.getHandler(world, worldPos, localState);
552540
if (!sameBlockInWorld)
553541
{
554-
for (final ItemStack stack : placementHandler.getRequiredItems(world, worldPos, localState, tileEntityData, false))
542+
for (final ItemStack stack : placementHandler.getRequiredItems(world, worldPos, localState, tileEntityData, handler))
555543
{
556544
if (!stack.isEmpty() && !this.handler.isStackFree(stack))
557545
{

0 commit comments

Comments
 (0)