Search in sources :

Example 1 with DispenserBlock

use of net.minecraft.world.level.block.DispenserBlock in project fabric-carpet by gnembon.

the class BlockRotator method dispenserRotate.

public static ItemStack dispenserRotate(BlockSource source, ItemStack stack) {
    Direction sourceFace = source.getBlockState().getValue(DispenserBlock.FACING);
    Level world = source.getLevel();
    // offset
    BlockPos blockpos = source.getPos().relative(sourceFace);
    BlockState iblockstate = world.getBlockState(blockpos);
    Block block = iblockstate.getBlock();
    // Block rotation for blocks that can be placed in all 6 or 4 rotations.
    if (block instanceof DirectionalBlock || block instanceof DispenserBlock) {
        Direction face = iblockstate.getValue(DirectionalBlock.FACING);
        if (block instanceof PistonBaseBlock && (iblockstate.getValue(PistonBaseBlock.EXTENDED) || (((PistonBlockInterface) block).publicShouldExtend(world, blockpos, face) && (new PistonStructureResolver(world, blockpos, face, true)).resolve())))
            return stack;
        Direction rotated_face = rotateClockwise(face, sourceFace.getAxis());
        if (sourceFace.get3DDataValue() % 2 == 0 || rotated_face == face) {
            // Flip to make blocks always rotate clockwise relative to the dispenser
            // when index is equal to zero. when index is equal to zero the dispenser is in the opposite direction.
            rotated_face = rotated_face.getOpposite();
        }
        world.setBlock(blockpos, iblockstate.setValue(DirectionalBlock.FACING, rotated_face), 3);
    } else if (// Block rotation for blocks that can be placed in only 4 horizontal rotations.
    block instanceof HorizontalDirectionalBlock) {
        if (block instanceof BedBlock)
            return stack;
        Direction face = iblockstate.getValue(HorizontalDirectionalBlock.FACING);
        face = rotateClockwise(face, Direction.Axis.Y);
        if (sourceFace == Direction.DOWN) {
            // same as above.
            face = face.getOpposite();
        }
        world.setBlock(blockpos, iblockstate.setValue(HorizontalDirectionalBlock.FACING, face), 3);
    } else if (block == Blocks.HOPPER) {
        Direction face = iblockstate.getValue(HopperBlock.FACING);
        if (face != Direction.DOWN) {
            face = rotateClockwise(face, Direction.Axis.Y);
            world.setBlock(blockpos, iblockstate.setValue(HopperBlock.FACING, face), 3);
        }
    }
    // Send block update to the block that just have been rotated.
    world.neighborChanged(blockpos, block, source.getPos());
    return stack;
}
Also used : BedBlock(net.minecraft.world.level.block.BedBlock) HorizontalDirectionalBlock(net.minecraft.world.level.block.HorizontalDirectionalBlock) BlockState(net.minecraft.world.level.block.state.BlockState) HorizontalDirectionalBlock(net.minecraft.world.level.block.HorizontalDirectionalBlock) DirectionalBlock(net.minecraft.world.level.block.DirectionalBlock) DispenserBlock(net.minecraft.world.level.block.DispenserBlock) PistonStructureResolver(net.minecraft.world.level.block.piston.PistonStructureResolver) BaseRailBlock(net.minecraft.world.level.block.BaseRailBlock) FenceGateBlock(net.minecraft.world.level.block.FenceGateBlock) TrapDoorBlock(net.minecraft.world.level.block.TrapDoorBlock) LeverBlock(net.minecraft.world.level.block.LeverBlock) ObserverBlock(net.minecraft.world.level.block.ObserverBlock) SlabBlock(net.minecraft.world.level.block.SlabBlock) BedBlock(net.minecraft.world.level.block.BedBlock) EndRodBlock(net.minecraft.world.level.block.EndRodBlock) RepeaterBlock(net.minecraft.world.level.block.RepeaterBlock) GlazedTerracottaBlock(net.minecraft.world.level.block.GlazedTerracottaBlock) DiodeBlock(net.minecraft.world.level.block.DiodeBlock) DispenserBlock(net.minecraft.world.level.block.DispenserBlock) ComparatorBlock(net.minecraft.world.level.block.ComparatorBlock) HopperBlock(net.minecraft.world.level.block.HopperBlock) PistonBaseBlock(net.minecraft.world.level.block.piston.PistonBaseBlock) Block(net.minecraft.world.level.block.Block) RotatedPillarBlock(net.minecraft.world.level.block.RotatedPillarBlock) HorizontalDirectionalBlock(net.minecraft.world.level.block.HorizontalDirectionalBlock) StairBlock(net.minecraft.world.level.block.StairBlock) DirectionalBlock(net.minecraft.world.level.block.DirectionalBlock) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) Direction(net.minecraft.core.Direction) PistonBaseBlock(net.minecraft.world.level.block.piston.PistonBaseBlock)

Example 2 with DispenserBlock

use of net.minecraft.world.level.block.DispenserBlock in project fabric-carpet by gnembon.

the class BlockRotator method alternativeBlockPlacement.

public static // World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
BlockState alternativeBlockPlacement(// World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
Block block, // World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
BlockPlaceContext context) {
    // 
    if (true)
        throw new UnsupportedOperationException("Alternative Block Placement / client controlled / is not implemnted");
    Direction facing;
    Vec3 vec3d = context.getClickLocation();
    float hitX = (float) vec3d.x;
    if (// vanilla
    hitX < 2)
        return null;
    int code = (int) (hitX - 2) / 2;
    // 
    // now it would be great if hitX was adjusted in context to original range from 0.0 to 1.0
    // since its actually using it. Its private - maybe with Reflections?
    // 
    Player placer = context.getPlayer();
    BlockPos pos = context.getClickedPos();
    Level world = context.getLevel();
    if (block instanceof GlazedTerracottaBlock) {
        facing = Direction.from3DDataValue(code);
        if (facing == Direction.UP || facing == Direction.DOWN) {
            facing = placer.getDirection().getOpposite();
        }
        return block.defaultBlockState().setValue(HorizontalDirectionalBlock.FACING, facing);
    } else if (block instanceof ObserverBlock) {
        return block.defaultBlockState().setValue(DirectionalBlock.FACING, Direction.from3DDataValue(code)).setValue(ObserverBlock.POWERED, true);
    } else if (block instanceof RepeaterBlock) {
        facing = Direction.from3DDataValue(code % 16);
        if (facing == Direction.UP || facing == Direction.DOWN) {
            facing = placer.getDirection().getOpposite();
        }
        return block.defaultBlockState().setValue(HorizontalDirectionalBlock.FACING, facing).setValue(RepeaterBlock.DELAY, Mth.clamp(code / 16, 1, 4)).setValue(RepeaterBlock.LOCKED, Boolean.FALSE);
    } else if (block instanceof TrapDoorBlock) {
        return block.defaultBlockState().setValue(TrapDoorBlock.FACING, Direction.from3DDataValue(code % 16)).setValue(TrapDoorBlock.OPEN, Boolean.FALSE).setValue(TrapDoorBlock.HALF, (code >= 16) ? Half.TOP : Half.BOTTOM).setValue(TrapDoorBlock.OPEN, world.hasNeighborSignal(pos));
    } else if (block instanceof ComparatorBlock) {
        facing = Direction.from3DDataValue(code % 16);
        if ((facing == Direction.UP) || (facing == Direction.DOWN)) {
            facing = placer.getDirection().getOpposite();
        }
        ComparatorMode m = (hitX >= 16) ? ComparatorMode.SUBTRACT : ComparatorMode.COMPARE;
        return block.defaultBlockState().setValue(HorizontalDirectionalBlock.FACING, facing).setValue(ComparatorBlock.POWERED, Boolean.FALSE).setValue(ComparatorBlock.MODE, m);
    } else if (block instanceof DispenserBlock) {
        return block.defaultBlockState().setValue(DispenserBlock.FACING, Direction.from3DDataValue(code)).setValue(DispenserBlock.TRIGGERED, Boolean.FALSE);
    } else if (block instanceof PistonBaseBlock) {
        return block.defaultBlockState().setValue(DirectionalBlock.FACING, Direction.from3DDataValue(code)).setValue(PistonBaseBlock.EXTENDED, Boolean.FALSE);
    } else if (block instanceof StairBlock) {
        return // worldIn, pos, facing, hitX, hitY, hitZ, meta, placer)
        block.getStateForPlacement(context).setValue(StairBlock.FACING, Direction.from3DDataValue(code % 16)).setValue(StairBlock.HALF, (hitX >= 16) ? Half.TOP : Half.BOTTOM);
    }
    return null;
}
Also used : GlazedTerracottaBlock(net.minecraft.world.level.block.GlazedTerracottaBlock) Player(net.minecraft.world.entity.player.Player) DispenserBlock(net.minecraft.world.level.block.DispenserBlock) ObserverBlock(net.minecraft.world.level.block.ObserverBlock) ComparatorBlock(net.minecraft.world.level.block.ComparatorBlock) StairBlock(net.minecraft.world.level.block.StairBlock) Direction(net.minecraft.core.Direction) ComparatorMode(net.minecraft.world.level.block.state.properties.ComparatorMode) PistonBaseBlock(net.minecraft.world.level.block.piston.PistonBaseBlock) TrapDoorBlock(net.minecraft.world.level.block.TrapDoorBlock) Vec3(net.minecraft.world.phys.Vec3) BlockPos(net.minecraft.core.BlockPos) Level(net.minecraft.world.level.Level) RepeaterBlock(net.minecraft.world.level.block.RepeaterBlock)

Example 3 with DispenserBlock

use of net.minecraft.world.level.block.DispenserBlock in project fabric-carpet by gnembon.

the class BlockRotator method flip_block.

public static boolean flip_block(BlockState state, Level world, Player player, InteractionHand hand, BlockHitResult hit) {
    Block block = state.getBlock();
    BlockPos pos = hit.getBlockPos();
    Vec3 hitVec = hit.getLocation().subtract(pos.getX(), pos.getY(), pos.getZ());
    Direction facing = hit.getDirection();
    BlockState newState = null;
    if ((block instanceof GlazedTerracottaBlock) || (block instanceof DiodeBlock) || (block instanceof BaseRailBlock) || (block instanceof TrapDoorBlock) || (block instanceof LeverBlock) || (block instanceof FenceGateBlock)) {
        newState = state.rotate(Rotation.CLOCKWISE_90);
    } else if ((block instanceof ObserverBlock) || (block instanceof EndRodBlock)) {
        newState = state.setValue(DirectionalBlock.FACING, state.getValue(DirectionalBlock.FACING).getOpposite());
    } else if (block instanceof DispenserBlock) {
        newState = state.setValue(DispenserBlock.FACING, state.getValue(DispenserBlock.FACING).getOpposite());
    } else if (block instanceof PistonBaseBlock) {
        if (!(state.getValue(PistonBaseBlock.EXTENDED)))
            newState = state.setValue(DirectionalBlock.FACING, state.getValue(DirectionalBlock.FACING).getOpposite());
    } else if (block instanceof SlabBlock) {
        if (((SlabBlock) block).useShapeForLightOcclusion(state)) {
            newState = state.setValue(SlabBlock.TYPE, state.getValue(SlabBlock.TYPE) == SlabType.TOP ? SlabType.BOTTOM : SlabType.TOP);
        }
    } else if (block instanceof HopperBlock) {
        if (state.getValue(HopperBlock.FACING) != Direction.DOWN) {
            newState = state.setValue(HopperBlock.FACING, state.getValue(HopperBlock.FACING).getClockWise());
        }
    } else if (block instanceof StairBlock) {
        // LOG.error(String.format("hit with facing: %s, at side %.1fX, X %.1fY, Y %.1fZ",facing, hitX, hitY, hitZ));
        if ((facing == Direction.UP && hitVec.y == 1.0f) || (facing == Direction.DOWN && hitVec.y == 0.0f)) {
            newState = state.setValue(StairBlock.HALF, state.getValue(StairBlock.HALF) == Half.TOP ? Half.BOTTOM : Half.TOP);
        } else {
            boolean turn_right;
            if (facing == Direction.NORTH) {
                turn_right = (hitVec.x <= 0.5);
            } else if (facing == Direction.SOUTH) {
                turn_right = !(hitVec.x <= 0.5);
            } else if (facing == Direction.EAST) {
                turn_right = (hitVec.z <= 0.5);
            } else if (facing == Direction.WEST) {
                turn_right = !(hitVec.z <= 0.5);
            } else {
                return false;
            }
            if (turn_right) {
                newState = state.rotate(Rotation.COUNTERCLOCKWISE_90);
            } else {
                newState = state.rotate(Rotation.CLOCKWISE_90);
            }
        }
    } else if (block instanceof RotatedPillarBlock) {
        switch((Direction.Axis) state.getValue(RotatedPillarBlock.AXIS)) {
            case X:
                newState = (BlockState) state.setValue(RotatedPillarBlock.AXIS, Direction.Axis.Z);
                break;
            case Z:
                newState = (BlockState) state.setValue(RotatedPillarBlock.AXIS, Direction.Axis.Y);
                break;
            case Y:
                newState = (BlockState) state.setValue(RotatedPillarBlock.AXIS, Direction.Axis.X);
                break;
        }
    } else {
        return false;
    }
    if (newState != null) {
        world.setBlock(pos, newState, 2 | 1024);
        world.setBlocksDirty(pos, state, newState);
        return true;
    }
    return false;
}
Also used : GlazedTerracottaBlock(net.minecraft.world.level.block.GlazedTerracottaBlock) DispenserBlock(net.minecraft.world.level.block.DispenserBlock) SlabBlock(net.minecraft.world.level.block.SlabBlock) HopperBlock(net.minecraft.world.level.block.HopperBlock) LeverBlock(net.minecraft.world.level.block.LeverBlock) ObserverBlock(net.minecraft.world.level.block.ObserverBlock) StairBlock(net.minecraft.world.level.block.StairBlock) Direction(net.minecraft.core.Direction) PistonBaseBlock(net.minecraft.world.level.block.piston.PistonBaseBlock) DiodeBlock(net.minecraft.world.level.block.DiodeBlock) TrapDoorBlock(net.minecraft.world.level.block.TrapDoorBlock) BlockState(net.minecraft.world.level.block.state.BlockState) BaseRailBlock(net.minecraft.world.level.block.BaseRailBlock) FenceGateBlock(net.minecraft.world.level.block.FenceGateBlock) Vec3(net.minecraft.world.phys.Vec3) BaseRailBlock(net.minecraft.world.level.block.BaseRailBlock) FenceGateBlock(net.minecraft.world.level.block.FenceGateBlock) TrapDoorBlock(net.minecraft.world.level.block.TrapDoorBlock) LeverBlock(net.minecraft.world.level.block.LeverBlock) ObserverBlock(net.minecraft.world.level.block.ObserverBlock) SlabBlock(net.minecraft.world.level.block.SlabBlock) BedBlock(net.minecraft.world.level.block.BedBlock) EndRodBlock(net.minecraft.world.level.block.EndRodBlock) RepeaterBlock(net.minecraft.world.level.block.RepeaterBlock) GlazedTerracottaBlock(net.minecraft.world.level.block.GlazedTerracottaBlock) DiodeBlock(net.minecraft.world.level.block.DiodeBlock) DispenserBlock(net.minecraft.world.level.block.DispenserBlock) ComparatorBlock(net.minecraft.world.level.block.ComparatorBlock) HopperBlock(net.minecraft.world.level.block.HopperBlock) PistonBaseBlock(net.minecraft.world.level.block.piston.PistonBaseBlock) Block(net.minecraft.world.level.block.Block) RotatedPillarBlock(net.minecraft.world.level.block.RotatedPillarBlock) HorizontalDirectionalBlock(net.minecraft.world.level.block.HorizontalDirectionalBlock) StairBlock(net.minecraft.world.level.block.StairBlock) DirectionalBlock(net.minecraft.world.level.block.DirectionalBlock) BlockPos(net.minecraft.core.BlockPos) EndRodBlock(net.minecraft.world.level.block.EndRodBlock) RotatedPillarBlock(net.minecraft.world.level.block.RotatedPillarBlock)

Example 4 with DispenserBlock

use of net.minecraft.world.level.block.DispenserBlock in project Mohist by MohistMC.

the class CraftDispenser method dispense.

@Override
public boolean dispense() {
    ensureNoWorldGeneration();
    Block block = getBlock();
    if (block.getType() == Material.DISPENSER) {
        CraftWorld world = (CraftWorld) this.getWorld();
        DispenserBlock dispense = (DispenserBlock) Blocks.DISPENSER;
        dispense.dispenseFrom(world.getHandle(), this.getPosition());
        return true;
    } else {
        return false;
    }
}
Also used : DispenserBlock(net.minecraft.world.level.block.DispenserBlock) DispenserBlock(net.minecraft.world.level.block.DispenserBlock) Block(org.bukkit.block.Block) CraftWorld(org.bukkit.craftbukkit.v1_18_R2.CraftWorld)

Aggregations

DispenserBlock (net.minecraft.world.level.block.DispenserBlock)4 BlockPos (net.minecraft.core.BlockPos)3 Direction (net.minecraft.core.Direction)3 ComparatorBlock (net.minecraft.world.level.block.ComparatorBlock)3 GlazedTerracottaBlock (net.minecraft.world.level.block.GlazedTerracottaBlock)3 ObserverBlock (net.minecraft.world.level.block.ObserverBlock)3 RepeaterBlock (net.minecraft.world.level.block.RepeaterBlock)3 StairBlock (net.minecraft.world.level.block.StairBlock)3 TrapDoorBlock (net.minecraft.world.level.block.TrapDoorBlock)3 PistonBaseBlock (net.minecraft.world.level.block.piston.PistonBaseBlock)3 Level (net.minecraft.world.level.Level)2 BaseRailBlock (net.minecraft.world.level.block.BaseRailBlock)2 BedBlock (net.minecraft.world.level.block.BedBlock)2 Block (net.minecraft.world.level.block.Block)2 DiodeBlock (net.minecraft.world.level.block.DiodeBlock)2 DirectionalBlock (net.minecraft.world.level.block.DirectionalBlock)2 EndRodBlock (net.minecraft.world.level.block.EndRodBlock)2 FenceGateBlock (net.minecraft.world.level.block.FenceGateBlock)2 HopperBlock (net.minecraft.world.level.block.HopperBlock)2 HorizontalDirectionalBlock (net.minecraft.world.level.block.HorizontalDirectionalBlock)2