|
1 | | -@file:Suppress("OVERRIDE_DEPRECATION") |
| 1 | +@file:Suppress("OVERRIDE_DEPRECATION", "DEPRECATION") |
2 | 2 |
|
3 | 3 | package dev.hybridlabs.albob.block |
4 | 4 |
|
| 5 | +import dev.hybridlabs.albob.block.entity.WireBasketBlockEntity |
5 | 6 | import net.minecraft.core.BlockPos |
6 | 7 | import net.minecraft.core.Direction |
| 8 | +import net.minecraft.server.level.ServerLevel |
| 9 | +import net.minecraft.util.RandomSource |
| 10 | +import net.minecraft.world.Container |
| 11 | +import net.minecraft.world.Containers |
| 12 | +import net.minecraft.world.InteractionHand |
| 13 | +import net.minecraft.world.InteractionResult |
| 14 | +import net.minecraft.world.entity.LivingEntity |
| 15 | +import net.minecraft.world.entity.monster.piglin.PiglinAi |
| 16 | +import net.minecraft.world.entity.player.Player |
| 17 | +import net.minecraft.world.inventory.AbstractContainerMenu |
| 18 | +import net.minecraft.world.item.ItemStack |
7 | 19 | import net.minecraft.world.item.context.BlockPlaceContext |
8 | 20 | import net.minecraft.world.level.BlockGetter |
| 21 | +import net.minecraft.world.level.Level |
| 22 | +import net.minecraft.world.level.block.BaseEntityBlock |
9 | 23 | import net.minecraft.world.level.block.Block |
| 24 | +import net.minecraft.world.level.block.RenderShape |
10 | 25 | import net.minecraft.world.level.block.RotatedPillarBlock |
11 | 26 | import net.minecraft.world.level.block.Rotation |
| 27 | +import net.minecraft.world.level.block.entity.BlockEntity |
12 | 28 | import net.minecraft.world.level.block.state.BlockState |
13 | 29 | import net.minecraft.world.level.block.state.StateDefinition |
14 | 30 | import net.minecraft.world.level.block.state.properties.BlockStateProperties |
15 | 31 | import net.minecraft.world.level.block.state.properties.EnumProperty |
| 32 | +import net.minecraft.world.phys.BlockHitResult |
16 | 33 | import net.minecraft.world.phys.shapes.CollisionContext |
17 | 34 | import net.minecraft.world.phys.shapes.VoxelShape |
18 | 35 |
|
19 | | -class WireBasketBlock(properties: Properties) : Block(properties) { |
| 36 | +class WireBasketBlock(properties: Properties) : BaseEntityBlock(properties) { |
20 | 37 | init { |
21 | 38 | registerDefaultState( |
22 | 39 | stateDefinition.any() |
@@ -44,6 +61,65 @@ class WireBasketBlock(properties: Properties) : Block(properties) { |
44 | 61 | builder.add(HORIZONTAL_AXIS) |
45 | 62 | } |
46 | 63 |
|
| 64 | + override fun newBlockEntity(pos: BlockPos, state: BlockState): BlockEntity { |
| 65 | + return WireBasketBlockEntity(pos, state) |
| 66 | + } |
| 67 | + |
| 68 | + override fun use(state: BlockState, level: Level, pos: BlockPos, player: Player, hand: InteractionHand, hit: BlockHitResult): InteractionResult { |
| 69 | + if (level.isClientSide) { |
| 70 | + return InteractionResult.SUCCESS |
| 71 | + } else { |
| 72 | + val blockEntity = level.getBlockEntity(pos) |
| 73 | + if (blockEntity is WireBasketBlockEntity) { |
| 74 | + player.openMenu(blockEntity) |
| 75 | + // player.awardStat(Stats.OPEN_BARREL) // TODO |
| 76 | + PiglinAi.angerNearbyPiglins(player, true) |
| 77 | + } |
| 78 | + |
| 79 | + return InteractionResult.CONSUME |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + override fun onRemove(state: BlockState, level: Level, pos: BlockPos, otherState: BlockState, bl: Boolean) { |
| 84 | + if (!state.`is`(otherState.block)) { |
| 85 | + val blockEntity = level.getBlockEntity(pos) |
| 86 | + if (blockEntity is Container) { |
| 87 | + Containers.dropContents(level, pos, blockEntity as Container) |
| 88 | + level.updateNeighbourForOutputSignal(pos, this) |
| 89 | + } |
| 90 | + |
| 91 | + super.onRemove(state, level, pos, otherState, bl) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + override fun tick(state: BlockState, level: ServerLevel, pos: BlockPos, source: RandomSource) { |
| 96 | + val blockEntity = level.getBlockEntity(pos) |
| 97 | + if (blockEntity is WireBasketBlockEntity) { |
| 98 | + blockEntity.recheckOpen() |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + override fun getRenderShape(state: BlockState): RenderShape { |
| 103 | + return RenderShape.MODEL |
| 104 | + } |
| 105 | + |
| 106 | + override fun setPlacedBy(level: Level, pos: BlockPos, state: BlockState, entity: LivingEntity?, stack: ItemStack) { |
| 107 | + if (stack.hasCustomHoverName()) { |
| 108 | + val blockEntity = level.getBlockEntity(pos) |
| 109 | + if (blockEntity is WireBasketBlockEntity) { |
| 110 | + blockEntity.customName = stack.getHoverName() |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + override fun hasAnalogOutputSignal(state: BlockState): Boolean { |
| 116 | + return true |
| 117 | + } |
| 118 | + |
| 119 | + override fun getAnalogOutputSignal(state: BlockState, level: Level, pos: BlockPos): Int { |
| 120 | + return AbstractContainerMenu.getRedstoneSignalFromBlockEntity(level.getBlockEntity(pos)) |
| 121 | + } |
| 122 | + |
47 | 123 | companion object { |
48 | 124 | val Z_SHAPE: VoxelShape = box(3.0, 0.0, 0.0, 13.0, 10.0, 16.0) |
49 | 125 | val X_SHAPE: VoxelShape = box(0.0, 0.0, 3.0, 16.0, 10.0, 13.0) |
|
0 commit comments