Skip to content

Commit a25fa94

Browse files
authored
Equality rework Port (#823)
1 parent 1995a0c commit a25fa94

15 files changed

+831
-297
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
import com.ldtteam.structurize.client.gui.util.InputFilters;
1313
import com.ldtteam.structurize.client.gui.util.ItemPositionsStorage;
1414
import com.ldtteam.structurize.network.messages.*;
15+
import com.ldtteam.structurize.placement.SimplePlacementContext;
1516
import com.ldtteam.structurize.placement.handlers.placement.IPlacementHandler;
1617
import com.ldtteam.structurize.placement.handlers.placement.PlacementHandlers;
1718
import com.ldtteam.structurize.storage.rendering.RenderingCache;
1819
import com.ldtteam.structurize.storage.rendering.types.BoxPreviewData;
20+
import com.ldtteam.structurize.util.PlacementSettings;
1921
import com.ldtteam.structurize.util.ScanToolData;
2022
import it.unimi.dsi.fastutil.objects.Object2IntMap;
2123
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
@@ -514,7 +516,7 @@ private void updateResources()
514516
else
515517
{
516518
final IPlacementHandler handler = PlacementHandlers.getHandler(world, BlockPos.ZERO, blockState);
517-
final List<ItemStack> itemList = handler.getRequiredItems(world, here, blockState, tileEntity == null ? null : tileEntity.saveWithFullMetadata(), true);
519+
final List<ItemStack> itemList = handler.getRequiredItems(world, here, blockState, tileEntity == null ? null : tileEntity.saveWithFullMetadata(), new SimplePlacementContext(false, new PlacementSettings()));
518520
for (final ItemStack stack : itemList)
519521
{
520522
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.minecraftforge.common.util.TriPredicate;
@@ -106,9 +106,7 @@ private Result iterateWithCondition(final TriPredicate<BlueprintPositionInfo, Bl
106106
{
107107
continue;
108108
}
109-
else if (!isRemoving() && BlockUtils.areBlockStatesEqual(info.getBlockInfo().getState(), structureHandler.getWorld().getBlockState(worldPos), structureHandler::replaceWithSolidBlock, structureHandler.fancyPlacement(), structureHandler::shouldBlocksBeConsideredEqual,
110-
info.getBlockInfo().getTileEntityData(),
111-
info.getBlockInfo().getTileEntityData() == null ? null : structureHandler.getWorld().getBlockEntity(worldPos)) && info.getEntities().length == 0)
109+
else if (!isRemoving() && IPlacementHandler.doesWorldStateMatchBlueprintState(info.getBlockInfo(), worldPos, structureHandler) && info.getEntities().length == 0)
112110
{
113111
structureHandler.triggerSuccess(progressPos, Collections.emptyList(), false);
114112
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.blueprints.v1.Blueprint;
4+
import com.ldtteam.structurize.util.PlacementSettings;
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+
PlacementSettings 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.blueprints.v1.Blueprint;
4+
import com.ldtteam.structurize.util.PlacementSettings;
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 PlacementSettings rotationMirror;
26+
27+
public SimplePlacementContext(final boolean fancyPlacement, final PlacementSettings rotationMirror)
28+
{
29+
this.fancyPlacement = fancyPlacement;
30+
this.rotationMirror = rotationMirror;
31+
}
32+
33+
@Override
34+
public PlacementSettings 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: 20 additions & 34 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;
@@ -169,7 +170,7 @@ public StructurePhasePlacementResult executeStructureStep(
169170
result = handleEntitySpawn(world, worldPos, localPos, storage);
170171
break;
171172
default:
172-
result = handleBlockPlacement(world, worldPos, localPos, storage, localState, handler.getBluePrint().getTileEntityData(worldPos, localPos));
173+
result = handleBlockPlacement(world, worldPos, storage, new BlockInfo(localPos, localState, handler.getBluePrint().getTileEntityData(worldPos, localPos)));
173174
}
174175
count++;
175176

@@ -208,19 +209,19 @@ public StructurePhasePlacementResult executeStructureStep(
208209
* When we extract this into another mod, we have to override the method.
209210
* @param world the world.
210211
* @param worldPos the world position.
211-
* @param localPos the local pos
212212
* @param storage the change storage.
213-
* @param localState the local state.
214-
* @param tileEntityData the tileEntity.
213+
* @param blockInfo the tileEntity.
215214
*/
216215
public BlockPlacementResult handleBlockPlacement(
217216
final Level world,
218217
final BlockPos worldPos,
219-
final BlockPos localPos,
220218
final ChangeStorage storage,
221-
BlockState localState,
222-
CompoundTag tileEntityData)
219+
final BlockInfo blockInfo)
223220
{
221+
BlockState localState = blockInfo.getState();
222+
CompoundTag tileEntityData = blockInfo.getTileEntityData();
223+
final BlockPos localPos = blockInfo.getPos();
224+
224225
final BlockState worldState = world.getBlockState(worldPos);
225226
boolean sameBlockInWorld = false;
226227
if (worldState.getBlock() == localState.getBlock() && tileEntityData == null)
@@ -243,7 +244,7 @@ public BlockPlacementResult handleBlockPlacement(
243244
{
244245
try
245246
{
246-
final BlockPos pos = this.handler.getWorldPos().subtract(handler.getBluePrint().getPrimaryBlockOffset());
247+
final BlockPos pos = this.handler.getCenterPos().subtract(handler.getBluePrint().getPrimaryBlockOffset());
247248

248249
final Optional<EntityType<?>> type = EntityType.by(compound);
249250
if (type.isPresent())
@@ -314,16 +315,6 @@ else if (requiredItems == null)
314315
}
315316
}
316317

317-
BlockEntity worldEntity = null;
318-
if (tileEntityData != null)
319-
{
320-
worldEntity = world.getBlockEntity(worldPos);
321-
}
322-
323-
if (localState.getBlock() == ModBlocks.blockSolidSubstitution.get() && handler.fancyPlacement())
324-
{
325-
localState = this.handler.getSolidBlockForPos(worldPos, handler.getBluePrint().getRawBlockStateFunction().compose(handler::getStructurePosFromWorld));
326-
}
327318
if (localState.getBlock() == ModBlocks.blockTagSubstitution.get() && handler.fancyPlacement())
328319
{
329320
if (tileEntityData != null && BlockEntity.loadStatic(localPos, localState, tileEntityData) instanceof BlockEntityTagSubstitution tagEntity)
@@ -337,7 +328,7 @@ else if (requiredItems == null)
337328
}
338329
}
339330

340-
if (BlockUtils.areBlockStatesEqual(localState, worldState, handler::replaceWithSolidBlock, handler.fancyPlacement(), handler::shouldBlocksBeConsideredEqual, tileEntityData, worldEntity))
331+
if (IPlacementHandler.doesWorldStateMatchBlueprintState(blockInfo, worldPos, this.handler))
341332
{
342333
return new BlockPlacementResult(worldPos, BlockPlacementResult.Result.SUCCESS);
343334
}
@@ -349,7 +340,7 @@ else if (requiredItems == null)
349340

350341
if (!sameBlockInWorld && !this.handler.isCreative())
351342
{
352-
for (final ItemStack stack : placementHandler.getRequiredItems(world, worldPos, localState, tileEntityData, false))
343+
for (final ItemStack stack : placementHandler.getRequiredItems(world, worldPos, localState, tileEntityData, handler))
353344
{
354345
if (!stack.isEmpty() && !this.handler.isStackFree(stack))
355346
{
@@ -375,7 +366,7 @@ else if (requiredItems == null)
375366

376367
this.handler.prePlacementLogic(worldPos, localState, requiredItems);
377368

378-
final IPlacementHandler.ActionProcessingResult result = placementHandler.handle(getHandler().getBluePrint(), world, worldPos, localState, tileEntityData, !this.handler.fancyPlacement(), this.handler.getWorldPos(), this.handler.getSettings());
369+
final IPlacementHandler.ActionProcessingResult result = placementHandler.handle(world, worldPos, localState, tileEntityData, this.handler);
379370
if (result == IPlacementHandler.ActionProcessingResult.DENY)
380371
{
381372
placementHandler.handleRemoval(handler, world, worldPos, tileEntityData);
@@ -417,7 +408,7 @@ public BlockPlacementResult handleEntitySpawn(
417408
{
418409
try
419410
{
420-
final BlockPos pos = this.handler.getWorldPos().subtract(handler.getBluePrint().getPrimaryBlockOffset());
411+
final BlockPos pos = this.handler.getCenterPos().subtract(handler.getBluePrint().getPrimaryBlockOffset());
421412

422413
final Optional<EntityType<?>> type = EntityType.by(compound);
423414
if (type.isPresent())
@@ -584,7 +575,7 @@ public BlockPlacementResult getResourceRequirements(
584575
{
585576
try
586577
{
587-
final BlockPos pos = this.handler.getWorldPos().subtract(handler.getBluePrint().getPrimaryBlockOffset());
578+
final BlockPos pos = this.handler.getCenterPos().subtract(handler.getBluePrint().getPrimaryBlockOffset());
588579

589580
final Optional<EntityType<?>> type = EntityType.by(compound);
590581
if (type.isPresent())
@@ -622,15 +613,6 @@ public BlockPlacementResult getResourceRequirements(
622613
}
623614
}
624615

625-
BlockEntity worldEntity = null;
626-
if (tileEntityData != null)
627-
{
628-
worldEntity = world.getBlockEntity(worldPos);
629-
}
630-
if (localState.getBlock() == ModBlocks.blockSolidSubstitution.get() && handler.fancyPlacement())
631-
{
632-
localState = this.handler.getSolidBlockForPos(worldPos, handler.getBluePrint().getRawBlockStateFunction().compose(handler::getStructurePosFromWorld));
633-
}
634616
if (localState.getBlock() == ModBlocks.blockTagSubstitution.get() && handler.fancyPlacement())
635617
{
636618
if (tileEntityData != null && BlockEntity.loadStatic(localPos, localState, tileEntityData) instanceof BlockEntityTagSubstitution tagEntity)
@@ -644,15 +626,19 @@ public BlockPlacementResult getResourceRequirements(
644626
}
645627
}
646628

647-
if (BlockUtils.areBlockStatesEqual(localState, worldState, handler::replaceWithSolidBlock, handler.fancyPlacement(), handler::shouldBlocksBeConsideredEqual, tileEntityData, worldEntity))
629+
if (IPlacementHandler.doesWorldStateMatchBlueprintState(new BlockInfo(localPos, localState, handler.getBluePrint().getTileEntityData(worldPos, localPos)), worldPos, this.handler))
648630
{
631+
if (requiredItems.isEmpty())
632+
{
633+
return new BlockPlacementResult(worldPos, BlockPlacementResult.Result.SUCCESS);
634+
}
649635
return new BlockPlacementResult(worldPos, BlockPlacementResult.Result.MISSING_ITEMS, requiredItems);
650636
}
651637

652638
final IPlacementHandler placementHandler = PlacementHandlers.getHandler(world, worldPos, localState);
653639
if (!sameBlockInWorld)
654640
{
655-
for (final ItemStack stack : placementHandler.getRequiredItems(world, worldPos, localState, tileEntityData, false))
641+
for (final ItemStack stack : placementHandler.getRequiredItems(world, worldPos, localState, tileEntityData, handler))
656642
{
657643
if (!stack.isEmpty() && !this.handler.isStackFree(stack))
658644
{

0 commit comments

Comments
 (0)