|
| 1 | +package com.samsthenerd.hexgloop.blocks; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import javax.annotation.Nonnull; |
| 7 | +import javax.annotation.Nullable; |
| 8 | + |
| 9 | +import com.samsthenerd.hexgloop.blockentities.BlockEntityDial; |
| 10 | +import com.samsthenerd.hexgloop.items.HexGloopItems; |
| 11 | + |
| 12 | +import net.minecraft.block.Block; |
| 13 | +import net.minecraft.block.BlockRenderType; |
| 14 | +import net.minecraft.block.BlockState; |
| 15 | +import net.minecraft.block.BlockWithEntity; |
| 16 | +import net.minecraft.block.ShapeContext; |
| 17 | +import net.minecraft.block.entity.BlockEntity; |
| 18 | +import net.minecraft.entity.player.PlayerEntity; |
| 19 | +import net.minecraft.item.ItemPlacementContext; |
| 20 | +import net.minecraft.item.ItemStack; |
| 21 | +import net.minecraft.state.StateManager; |
| 22 | +import net.minecraft.state.property.DirectionProperty; |
| 23 | +import net.minecraft.state.property.IntProperty; |
| 24 | +import net.minecraft.state.property.Properties; |
| 25 | +import net.minecraft.util.ActionResult; |
| 26 | +import net.minecraft.util.Hand; |
| 27 | +import net.minecraft.util.Pair; |
| 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.math.Vec3d; |
| 32 | +import net.minecraft.util.shape.VoxelShape; |
| 33 | +import net.minecraft.util.shape.VoxelShapes; |
| 34 | +import net.minecraft.world.BlockView; |
| 35 | +import net.minecraft.world.World; |
| 36 | + |
| 37 | +public class BlockIoticDial extends BlockWithEntity { |
| 38 | + |
| 39 | + public static final DirectionProperty FACING = Properties.FACING; |
| 40 | + public static final IntProperty SELECTED = IntProperty.of("selected", 0, 6); |
| 41 | + |
| 42 | + public BlockIoticDial(Settings properties) { |
| 43 | + super(properties); |
| 44 | + setDefaultState(this.stateManager.getDefaultState() |
| 45 | + .with(FACING, Direction.NORTH) |
| 46 | + .with(SELECTED, 0) |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + public static final Map<Direction, VoxelShape> SHAPES = new HashMap<>(); |
| 51 | + |
| 52 | + static { |
| 53 | + SHAPES.put(Direction.NORTH, VoxelShapes.cuboid(0, 0, 11.0/16.0, 1, 1, 1)); |
| 54 | + SHAPES.put(Direction.SOUTH, VoxelShapes.cuboid(0, 0, 0, 1, 1, 5.0/16.0)); |
| 55 | + SHAPES.put(Direction.EAST, VoxelShapes.cuboid(0, 0, 0, 5.0/16.0, 1, 1)); |
| 56 | + SHAPES.put(Direction.WEST, VoxelShapes.cuboid(11.0/16.0, 0, 0, 1, 1, 1)); |
| 57 | + SHAPES.put(Direction.UP, VoxelShapes.cuboid(0, 0, 0, 1, 5.0/16.0, 1)); |
| 58 | + SHAPES.put(Direction.DOWN, VoxelShapes.cuboid(0, 11.0/16.0, 0, 1, 1, 1)); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + @Nullable |
| 63 | + public BlockEntity createBlockEntity(BlockPos pos, BlockState state){ |
| 64 | + return new BlockEntityDial(pos, state); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) { |
| 69 | + return SHAPES.get(state.get(FACING)); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public BlockState getPlacementState(ItemPlacementContext ctx) { |
| 74 | + return super.getPlacementState(ctx).with(FACING, ctx.getPlayerLookDirection().getOpposite()); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + protected void appendProperties(StateManager.Builder<Block, BlockState> builder) { |
| 79 | + super.appendProperties(builder); |
| 80 | + builder.add(FACING); |
| 81 | + builder.add(SELECTED); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public boolean hasComparatorOutput(BlockState state) { |
| 86 | + return true; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public int getComparatorOutput(BlockState state, World world, BlockPos pos) { |
| 91 | + return state.get(SELECTED); |
| 92 | + } |
| 93 | + |
| 94 | + @Nonnull |
| 95 | + @Override |
| 96 | + public BlockRenderType getRenderType( @Nonnull BlockState state ) |
| 97 | + { |
| 98 | + return BlockRenderType.MODEL; |
| 99 | + } |
| 100 | + |
| 101 | + private static Map<Direction.Axis, FaceCalcinator> faceCalcs = new HashMap<Direction.Axis, FaceCalcinator>(); |
| 102 | + |
| 103 | + static { |
| 104 | + faceCalcs.put(Direction.Axis.X, (mirrored, pos) -> mirrored ? new Pair<>(pos.z, pos.y) : new Pair<>(1-pos.z, pos.y)); |
| 105 | + faceCalcs.put(Direction.Axis.Y, (mirrored, pos) -> mirrored ? new Pair<>(pos.x, pos.z) : new Pair<>(1-pos.x, pos.z)); |
| 106 | + faceCalcs.put(Direction.Axis.Z, (mirrored, pos) -> mirrored ? new Pair<>(1-pos.x, pos.y) : new Pair<>(pos.x, pos.y)); |
| 107 | + }; |
| 108 | + |
| 109 | + @Override |
| 110 | + public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { |
| 111 | + if(!(world.getBlockEntity(pos) instanceof BlockEntityDial dialBE)) return ActionResult.FAIL; |
| 112 | + if(dialBE.getInnerMultiFocus().isEmpty() && player.getStackInHand(hand).getItem() == HexGloopItems.MULTI_FOCUS_ITEM.get()){ |
| 113 | + dialBE.setInnerMultiFocus(player.getStackInHand(hand).copy()); |
| 114 | + player.getStackInHand(hand).decrement(1); |
| 115 | + return ActionResult.SUCCESS; |
| 116 | + } |
| 117 | + if(!dialBE.getInnerMultiFocus().isEmpty() && player.isSneaking()){ |
| 118 | + player.getInventory().offerOrDrop(dialBE.getInnerMultiFocus()); |
| 119 | + dialBE.setInnerMultiFocus(ItemStack.EMPTY); |
| 120 | + return ActionResult.SUCCESS; |
| 121 | + } |
| 122 | + Direction face = hit.getSide(); |
| 123 | + Vec3d nPos = hit.getPos().subtract(pos.getX(), pos.getY(), pos.getZ()); |
| 124 | + Pair<Double, Double> coords = faceCalcs.get(face.getAxis()).calc(face.getDirection() == Direction.AxisDirection.NEGATIVE, nPos); |
| 125 | + double r = new Vec3d(coords.getLeft()-0.5, coords.getRight()-0.5, 0).length(); |
| 126 | + double angle = (Math.atan2(coords.getRight()-0.5, coords.getLeft()-0.5) + (2 * Math.PI)) % (2 * Math.PI); |
| 127 | + if(face == state.get(FACING)){ |
| 128 | + int sel = (1 + 6 - (int)Math.floor(6 * angle / (2 * Math.PI))) % 6 + 1; |
| 129 | + dialBE.setSelection(sel); |
| 130 | + return ActionResult.SUCCESS; |
| 131 | + } |
| 132 | + return ActionResult.PASS; |
| 133 | + // HexGloop.logPrint("(" + coords.getLeft() + ", " + coords.getRight() + ")"); |
| 134 | + } |
| 135 | + |
| 136 | + @FunctionalInterface |
| 137 | + public static interface FaceCalcinator{ |
| 138 | + public Pair<Double, Double> calc(boolean mirrored, Vec3d pos); |
| 139 | + } |
| 140 | +} |
0 commit comments