|
1 | 1 | package hellfirepvp.observerlib.api.util; |
2 | 2 |
|
3 | 3 | 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; |
4 | 20 |
|
5 | 21 | /** |
6 | 22 | * This class is an exemplary simple implementation of the matchable structure interface. |
|
13 | 29 | * Date: 11.08.2019 / 09:12 |
14 | 30 | */ |
15 | 31 | 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 | + } |
16 | 73 | } |
0 commit comments