|
| 1 | +package io.github.dovecotmc.leadbeyond.common.block; |
| 2 | + |
| 3 | +import com.simibubi.create.content.contraptions.wrench.IWrenchable; |
| 4 | +import com.simibubi.create.content.contraptions.wrench.WrenchItem; |
| 5 | +import io.github.dovecotmc.leadbeyond.common.item.CardItem; |
| 6 | +import io.github.dovecotmc.leadbeyond.common.item.TicketItem; |
| 7 | +import io.github.dovecotmc.leadbeyond.common.reg.BlockEntityReg; |
| 8 | +import io.github.dovecotmc.leadbeyond.common.reg.SoundReg; |
| 9 | +import net.minecraft.block.*; |
| 10 | +import net.minecraft.block.entity.BlockEntity; |
| 11 | +import net.minecraft.block.entity.BlockEntityTicker; |
| 12 | +import net.minecraft.block.entity.BlockEntityType; |
| 13 | +import net.minecraft.entity.player.PlayerEntity; |
| 14 | +import net.minecraft.item.ItemPlacementContext; |
| 15 | +import net.minecraft.item.ItemStack; |
| 16 | +import net.minecraft.item.ItemUsageContext; |
| 17 | +import net.minecraft.nbt.NbtCompound; |
| 18 | +import net.minecraft.sound.SoundCategory; |
| 19 | +import net.minecraft.state.StateManager; |
| 20 | +import net.minecraft.state.property.BooleanProperty; |
| 21 | +import net.minecraft.state.property.DirectionProperty; |
| 22 | +import net.minecraft.state.property.Properties; |
| 23 | +import net.minecraft.text.TranslatableText; |
| 24 | +import net.minecraft.util.ActionResult; |
| 25 | +import net.minecraft.util.BlockMirror; |
| 26 | +import net.minecraft.util.BlockRotation; |
| 27 | +import net.minecraft.util.Hand; |
| 28 | +import net.minecraft.util.hit.BlockHitResult; |
| 29 | +import net.minecraft.util.math.BlockPos; |
| 30 | +import net.minecraft.util.math.Direction; |
| 31 | +import net.minecraft.util.shape.VoxelShape; |
| 32 | +import net.minecraft.util.shape.VoxelShapes; |
| 33 | +import net.minecraft.world.BlockView; |
| 34 | +import net.minecraft.world.World; |
| 35 | +import org.jetbrains.annotations.NotNull; |
| 36 | +import org.jetbrains.annotations.Nullable; |
| 37 | + |
| 38 | +public class TurnstileBlock extends BlockWithEntity |
| 39 | + implements IWrenchable { |
| 40 | + public static final DirectionProperty FACING = Properties.HORIZONTAL_FACING; |
| 41 | + public static final BooleanProperty OPEN = BooleanProperty.of("open"); |
| 42 | + |
| 43 | + public TurnstileBlock(Settings arg) { |
| 44 | + super(arg); |
| 45 | + setDefaultState(this.stateManager.getDefaultState() |
| 46 | + .with(FACING, Direction.NORTH) |
| 47 | + .with(OPEN, false)); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public BlockRenderType getRenderType(BlockState state) { |
| 52 | + return BlockRenderType.MODEL; |
| 53 | + } |
| 54 | + |
| 55 | + public BlockState rotate(BlockState state, BlockRotation rotation) { |
| 56 | + return state.with(FACING, rotation.rotate(state.get(FACING))); |
| 57 | + } |
| 58 | + |
| 59 | + public BlockState mirror(BlockState state, BlockMirror mirror) { |
| 60 | + return state.rotate(mirror.getRotation(state.get(FACING))); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + protected void appendProperties(StateManager.Builder<Block, BlockState> stateManager) { |
| 65 | + stateManager.add(FACING).add(OPEN); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public BlockState getPlacementState(ItemPlacementContext ctx) { |
| 70 | + return this.getDefaultState() |
| 71 | + .with(FACING, ctx.getPlayerFacing().getOpposite()) |
| 72 | + .with(OPEN, false); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { |
| 77 | + if (world.isClient()) return ActionResult.SUCCESS; |
| 78 | + ItemStack stack = player.getStackInHand(hand); |
| 79 | + BlockEntity be = world.getBlockEntity(pos); |
| 80 | + if (be instanceof TurnstileBlockEntity turnstile) { |
| 81 | + if (!turnstile.exit) { |
| 82 | + if (stack.getItem() instanceof TicketItem) { |
| 83 | + NbtCompound nbt = stack.getOrCreateSubNbt("ticketInfo"); |
| 84 | + if (!nbt.getBoolean("used")) { |
| 85 | + nbt.putBoolean("used", true); |
| 86 | + turnstile.setTimer(60); |
| 87 | + world.playSound(null, pos, SoundReg.BEEP_TURNSTILE.get(), SoundCategory.BLOCKS, 1f, 1f); |
| 88 | + return ActionResult.SUCCESS; |
| 89 | + } |
| 90 | + } else if (stack.getItem() instanceof CardItem) { |
| 91 | + NbtCompound nbt = stack.getOrCreateSubNbt("cardInfo"); |
| 92 | + if (nbt.getLong("money") >= 100) { |
| 93 | + nbt.putLong("money", nbt.getLong("money") - 100); |
| 94 | + turnstile.setTimer(60); |
| 95 | + world.playSound(null, pos, SoundReg.BEEP_TURNSTILE.get(), SoundCategory.BLOCKS, 1f, 1f); |
| 96 | + return ActionResult.SUCCESS; |
| 97 | + } |
| 98 | + } |
| 99 | + } else if (!(stack.getItem() instanceof WrenchItem)) { |
| 100 | + turnstile.setTimer(60); |
| 101 | + world.playSound(null, pos, SoundReg.BEEP_TURNSTILE.get(), SoundCategory.BLOCKS, 1f, 1f); |
| 102 | + return ActionResult.SUCCESS; |
| 103 | + } |
| 104 | + } |
| 105 | + return super.onUse(state, world, pos, player, hand, hit); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public ActionResult onWrenched(BlockState state, @NotNull ItemUsageContext context) { |
| 110 | + World world = context.getWorld(); |
| 111 | + PlayerEntity player = context.getPlayer(); |
| 112 | + if (player != null) { |
| 113 | + BlockEntity be = world.getBlockEntity(context.getBlockPos()); |
| 114 | + if (be instanceof TurnstileBlockEntity turnstile) { |
| 115 | + turnstile.exit = !turnstile.exit; |
| 116 | + player.sendMessage(new TranslatableText("message.lead_beyond.set_exit." + turnstile.exit), true); |
| 117 | + return ActionResult.SUCCESS; |
| 118 | + } |
| 119 | + } |
| 120 | + return ActionResult.PASS; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public VoxelShape getCollisionShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) { |
| 125 | + Direction dir = state.get(FACING); |
| 126 | + boolean open = state.get(OPEN); |
| 127 | + VoxelShape nsBarrier = Block.createCuboidShape(0, 0, 6, 16, 24, 10); |
| 128 | + VoxelShape ewBarrier = Block.createCuboidShape(6, 0, 0, 10, 24, 16); |
| 129 | + return switch (dir) { |
| 130 | + case SOUTH -> open ? VoxelShapes.union(Block.createCuboidShape(0, 0, 0, 3.5, 14, 16), |
| 131 | + Block.createCuboidShape(15, 0, 0, 16, 14, 16)) |
| 132 | + : VoxelShapes.union(Block.createCuboidShape(0, 0, 0, 3.5, 24, 16), |
| 133 | + Block.createCuboidShape(15, 0, 0, 16, 24, 16), |
| 134 | + nsBarrier); |
| 135 | + case NORTH -> open ? VoxelShapes.union(Block.createCuboidShape(0, 0, 0, 1, 14, 16), |
| 136 | + Block.createCuboidShape(12.5, 0, 0, 16, 14, 16)) |
| 137 | + : VoxelShapes.union(Block.createCuboidShape(0, 0, 0, 1, 24, 16), |
| 138 | + Block.createCuboidShape(12.5, 0, 0, 16, 24, 16), |
| 139 | + nsBarrier); |
| 140 | + case EAST -> open ? VoxelShapes.union(Block.createCuboidShape(0, 0, 0, 16, 14, 1), |
| 141 | + Block.createCuboidShape(0, 0, 12.5, 16, 14, 16)) |
| 142 | + : VoxelShapes.union(Block.createCuboidShape(0, 0, 0, 16, 24, 1), |
| 143 | + Block.createCuboidShape(0, 0, 12.5, 16, 24, 16), |
| 144 | + ewBarrier); |
| 145 | + case WEST -> open ? VoxelShapes.union(Block.createCuboidShape(0, 0, 0, 16, 14, 3.5), |
| 146 | + Block.createCuboidShape(0, 0, 15, 16, 14, 16)) |
| 147 | + : VoxelShapes.union(Block.createCuboidShape(0, 0, 0, 16, 24, 3.5), |
| 148 | + Block.createCuboidShape(0, 0, 15, 16, 24, 16), |
| 149 | + ewBarrier); |
| 150 | + default -> VoxelShapes.fullCube(); |
| 151 | + }; |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public VoxelShape getOutlineShape(BlockState state, BlockView view, BlockPos pos, ShapeContext ctx) { |
| 156 | + Direction dir = state.get(FACING); |
| 157 | + return switch (dir) { |
| 158 | + case SOUTH -> VoxelShapes.union(Block.createCuboidShape(0, 0, 0, 3.5, 14, 16), |
| 159 | + Block.createCuboidShape(15, 0, 0, 16, 14, 16)); |
| 160 | + case NORTH -> VoxelShapes.union(Block.createCuboidShape(0, 0, 0, 1, 14, 16), |
| 161 | + Block.createCuboidShape(12.5, 0, 0, 16, 14, 16)); |
| 162 | + case EAST -> VoxelShapes.union(Block.createCuboidShape(0, 0, 0, 16, 14, 1), |
| 163 | + Block.createCuboidShape(0, 0, 12.5, 16, 14, 16)); |
| 164 | + case WEST -> VoxelShapes.union(Block.createCuboidShape(0, 0, 0, 16, 14, 3.5), |
| 165 | + Block.createCuboidShape(0, 0, 15, 16, 14, 16)); |
| 166 | + default -> VoxelShapes.fullCube(); |
| 167 | + }; |
| 168 | + } |
| 169 | + |
| 170 | + @Nullable |
| 171 | + @Override |
| 172 | + public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { |
| 173 | + return new TurnstileBlockEntity(pos, state); |
| 174 | + } |
| 175 | + |
| 176 | + @Nullable |
| 177 | + @Override |
| 178 | + public <T extends BlockEntity> BlockEntityTicker<T> getTicker(World world, BlockState state, BlockEntityType<T> type) { |
| 179 | + return checkType(type, BlockEntityReg.TURNSTILE.get(), TurnstileBlockEntity::tick); |
| 180 | + } |
| 181 | +} |
0 commit comments