Skip to content

Commit a5da068

Browse files
committed
Re-add structure array placement default implementation
Signed-off-by: HellFirePvP <7419378+HellFirePvP@users.noreply.github.com>
1 parent d671f2f commit a5da068

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

src/main/java/hellfirepvp/observerlib/api/util/StructureBlockArray.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
package hellfirepvp.observerlib.api.util;
22

33
import hellfirepvp.observerlib.api.structure.MatchableStructure;
4+
import hellfirepvp.observerlib.api.tile.MatchableTile;
5+
import net.minecraft.core.BlockPos;
6+
import net.minecraft.tags.FluidTags;
7+
import net.minecraft.world.level.Level;
8+
import net.minecraft.world.level.LevelReader;
9+
import net.minecraft.world.level.LevelWriter;
10+
import net.minecraft.world.level.block.Block;
11+
import net.minecraft.world.level.block.entity.BlockEntity;
12+
import net.minecraft.world.level.block.state.BlockState;
13+
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
14+
import net.minecraft.world.level.material.Fluid;
15+
import net.minecraft.world.ticks.TickPriority;
16+
17+
import java.util.HashMap;
18+
import java.util.Map;
19+
import java.util.function.Predicate;
420

521
/**
622
* This class is an exemplary simple implementation of the matchable structure interface.
@@ -13,4 +29,45 @@
1329
* Date: 11.08.2019 / 09:12
1430
*/
1531
public class StructureBlockArray extends BlockArray implements MatchableStructure {
32+
33+
public <T extends LevelWriter & LevelReader> Map<BlockPos, BlockState> place(T levelAccess, BlockPos center) {
34+
return place(levelAccess, center, pos -> true);
35+
}
36+
37+
public <T extends LevelWriter & LevelReader> Map<BlockPos, BlockState> place(T levelAccess, BlockPos center, Predicate<BlockPos> posFilter) {
38+
Map<BlockPos, BlockState> placedResult = new HashMap<>();
39+
this.getContents().forEach((offset, matchableState) -> {
40+
BlockPos at = center.offset(offset);
41+
if (!posFilter.test(at)) return;
42+
43+
BlockState state = matchableState.getDescriptiveState(0);
44+
BlockState existing = levelAccess.getBlockState(at);
45+
46+
//Copy waterlogged state
47+
if (!existing.getFluidState().isEmpty() &&
48+
existing.getFluidState().is(FluidTags.WATER) &&
49+
state.hasProperty(BlockStateProperties.WATERLOGGED)) {
50+
state = state.setValue(BlockStateProperties.WATERLOGGED, true);
51+
}
52+
53+
if (!levelAccess.setBlock(at, state, Block.UPDATE_ALL)) return;
54+
placedResult.put(at, state);
55+
56+
if (levelAccess instanceof Level level) {
57+
if (!state.getFluidState().isEmpty()) {
58+
Fluid fluid = state.getFluidState().getType();
59+
level.scheduleTick(at, fluid, fluid.getTickDelay(level), TickPriority.HIGH);
60+
}
61+
}
62+
63+
BlockEntity placedTile = levelAccess.getBlockEntity(at);
64+
if (placedTile != null && hasTileAt(offset)) {
65+
MatchableTile matchTile = getTileEntityAt(offset);
66+
if (matchTile != null) {
67+
matchTile.postPlacement(placedTile, levelAccess, offset);
68+
}
69+
}
70+
});
71+
return placedResult;
72+
}
1673
}

0 commit comments

Comments
 (0)