|
| 1 | +package com.simibubi.create.content.contraptions; |
| 2 | + |
| 3 | +import javax.annotation.Nullable; |
| 4 | + |
| 5 | +import net.createmod.catnip.levelWrappers.WrappedServerLevel; |
| 6 | +import net.minecraft.core.BlockPos; |
| 7 | +import net.minecraft.server.level.ServerLevel; |
| 8 | +import net.minecraft.world.entity.Entity; |
| 9 | +import net.minecraft.world.item.ItemStack; |
| 10 | +import net.minecraft.world.level.block.Block; |
| 11 | +import net.minecraft.world.level.block.Blocks; |
| 12 | +import net.minecraft.world.level.block.EntityBlock; |
| 13 | +import net.minecraft.world.level.block.entity.BlockEntity; |
| 14 | +import net.minecraft.world.level.block.state.BlockState; |
| 15 | + |
| 16 | +public class SingleBlockServerLevel extends WrappedServerLevel { |
| 17 | + |
| 18 | + public BlockState blockState; |
| 19 | + public @Nullable BlockEntity blockEntity; |
| 20 | + |
| 21 | + public SingleBlockServerLevel(ServerLevel level) { |
| 22 | + super(level); |
| 23 | + blockState = Blocks.AIR.defaultBlockState(); |
| 24 | + blockEntity = null; |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public boolean setBlock(BlockPos pos, BlockState newState, int flags) { |
| 29 | + blockState.onRemove(this, pos, newState, (flags & 64) != 0); |
| 30 | + blockState = newState; |
| 31 | + blockEntity = null; |
| 32 | + return true; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public BlockState getBlockState(BlockPos pos) { |
| 37 | + return blockState; |
| 38 | + } |
| 39 | + |
| 40 | + @Nullable |
| 41 | + public BlockEntity getBlockEntity(BlockPos pos) { |
| 42 | + if (blockState.hasBlockEntity() && blockEntity == null) |
| 43 | + blockEntity = ((EntityBlock) blockState.getBlock()).newBlockEntity(pos, blockState); |
| 44 | + return blockEntity; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public boolean destroyBlock(BlockPos pos, boolean dropBlock, @Nullable Entity entity, int recursionLeft){ |
| 49 | + if (blockState.isAir()) |
| 50 | + return false; |
| 51 | + if (dropBlock) |
| 52 | + Block.dropResources(blockState, this, pos, blockEntity, entity, ItemStack.EMPTY); |
| 53 | + setBlock(pos, Blocks.AIR.defaultBlockState(), Block.UPDATE_NONE); |
| 54 | + return true; |
| 55 | + } |
| 56 | +} |
0 commit comments