下面列出了怎么用net.minecraft.util.hit.BlockHitResult的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
protected ItemStack dispenseSilently(BlockPointer source, ItemStack stack) {
if(!CarpetExtraSettings.dispensersToggleThings) {
return super.dispenseSilently(source, stack);
}
World world = source.getWorld();
Direction direction = (Direction) source.getBlockState().get(DispenserBlock.FACING);
BlockPos pos = source.getBlockPos().offset(direction);
BlockState state = world.getBlockState(pos);
if(toggleable.contains(state.getBlock())) {
ActionResult result = state.onUse(
world,
null,
Hand.MAIN_HAND,
new BlockHitResult(
new Vec3d(new Vec3i(pos.getX(), pos.getY(), pos.getZ())),
direction,
pos,
false
)
);
if(result.isAccepted()) return stack; // success or consume
}
return super.dispenseSilently(source, stack);
}
@SuppressWarnings("deprecation")
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
ItemStack stack = player.getStackInHand(hand);
if (stack.isEmpty() || !(stack.getItem() instanceof MiningToolItem)) {
return ActionResult.PASS;
}
MiningToolItem tool = (MiningToolItem) stack.getItem();
if (stripped != null && (tool.isEffectiveOn(state) || tool.getMiningSpeed(stack, state) > 1.0F)) {
world.playSound(player, pos, SoundEvents.ITEM_AXE_STRIP, SoundCategory.BLOCKS, 1.0F, 1.0F);
if (!world.isClient) {
BlockState target = stripped.getDefaultState().with(LogBlock.AXIS, state.get(LogBlock.AXIS));
world.setBlockState(pos, target);
stack.damage(1, player, consumedPlayer -> consumedPlayer.sendToolBreakStatus(hand));
}
return ActionResult.SUCCESS;
}
return ActionResult.PASS;
}
@SuppressWarnings("deprecation")
@Override
public ActionResult onUse(BlockState blockState, World world, BlockPos pos, PlayerEntity playerEntity, Hand hand, BlockHitResult blockHitResult) {
if (hand == Hand.MAIN_HAND) {
float x = pos.getX() + .49f;
float y = pos.getY();
float z = pos.getZ() + .51f;
HallowedTreasureChestEntity entity = new HallowedTreasureChestEntity(world, x, y, z, true, blockState.get(FACING).asRotation());
entity.updatePosition(x, y, z);
world.spawnEntity(entity);
return ActionResult.SUCCESS;
}
return ActionResult.PASS;
}
@Override
public ActionResult onUse(BlockState blockState, World world, BlockPos blockPos, PlayerEntity playerEntity, Hand hand, BlockHitResult blockHitResult) {
ItemStack stack = playerEntity.getStackInHand(hand);
if (stack.getItem() == Items.SHEARS) {
if (!world.isClient) {
Direction side = blockHitResult.getSide();
Direction facingTowardPlayer = side.getAxis() == Direction.Axis.Y ? playerEntity.getHorizontalFacing().getOpposite() : side;
world.playSound(null, blockPos, SoundEvents.BLOCK_PUMPKIN_CARVE, SoundCategory.BLOCKS, 1.0F, 1.0F);
world.setBlockState(blockPos, HallowedBlocks.CARVED_PUMPKIN_COLORS.get(this.color).getDefaultState().with(CarvedPumpkinBlock.FACING, facingTowardPlayer), 11);
ItemEntity itemEntity = new ItemEntity(world, blockPos.getX() + 0.5D + facingTowardPlayer.getOffsetX() * 0.65D, blockPos.getY() + 0.1D, blockPos.getZ() + 0.5D + facingTowardPlayer.getOffsetZ() * 0.65D, new ItemStack(Items.PUMPKIN_SEEDS, 4));
itemEntity.setVelocity(0.05D * (double) facingTowardPlayer.getOffsetX() + world.random.nextDouble() * 0.02D, 0.05D, 0.05D * (double) facingTowardPlayer.getOffsetZ() + world.random.nextDouble() * 0.02D);
world.spawnEntity(itemEntity);
stack.damage(1, playerEntity, (playerEntityVar) -> {
playerEntityVar.sendToolBreakStatus(hand);
});
}
return ActionResult.SUCCESS;
}
return super.onUse(blockState, world, blockPos, playerEntity, hand, blockHitResult);
}
@Override
public void onRightClick(RightClickEvent event)
{
if(IMC.getItemUseCooldown() > 0)
return;
if(!MC.options.keyUse.isPressed())
return;
for(int i = 0; i < amount.getValueI(); i++)
{
if(MC.crosshairTarget.getType() == HitResult.Type.BLOCK)
{
BlockHitResult hitResult = (BlockHitResult)MC.crosshairTarget;
IMC.getInteractionManager().rightClickBlock(
hitResult.getBlockPos(), hitResult.getSide(),
hitResult.getPos());
}
IMC.getInteractionManager().rightClickItem();
}
}
@Override
public void onLeftClick(LeftClickEvent event)
{
// check hitResult
if(MC.crosshairTarget == null
|| !(MC.crosshairTarget instanceof BlockHitResult))
return;
// check pos
BlockPos pos = ((BlockHitResult)MC.crosshairTarget).getBlockPos();
if(pos == null
|| BlockUtils.getState(pos).getMaterial() == Material.AIR)
return;
// check mode
if(mode.getSelected() != Mode.ID)
return;
// set id
WURST.getHax().nukerHack.setId(BlockUtils.getName(pos));
}
@Override
public void onRightClick(RightClickEvent event)
{
if(status != Status.IDLE)
return;
HitResult hitResult = MC.crosshairTarget;
if(hitResult == null || hitResult.getPos() == null
|| hitResult.getType() != HitResult.Type.BLOCK
|| !(hitResult instanceof BlockHitResult))
return;
BlockHitResult blockHitResult = (BlockHitResult)hitResult;
BlockPos hitResultPos = blockHitResult.getBlockPos();
if(!BlockUtils.canBeClicked(hitResultPos))
return;
BlockPos startPos = hitResultPos.offset(blockHitResult.getSide());
Direction direction = MC.player.getHorizontalFacing();
remainingBlocks = template.getPositions(startPos, direction);
if(instaBuild.isChecked() && template.size() <= 64)
buildInstantly();
else
status = Status.BUILDING;
}
@Override
public void onLeftClick(LeftClickEvent event)
{
// check hitResult
if(MC.crosshairTarget == null
|| !(MC.crosshairTarget instanceof BlockHitResult))
return;
// check pos
BlockPos pos = ((BlockHitResult)MC.crosshairTarget).getBlockPos();
if(pos == null
|| BlockUtils.getState(pos).getMaterial() == Material.AIR)
return;
// check mode
if(mode.getSelected() != Mode.ID)
return;
// set id
WURST.getHax().nukerHack.setId(BlockUtils.getName(pos));
}
private boolean isClickable(HitResult hitResult)
{
if(hitResult == null)
return false;
if(hitResult instanceof EntityHitResult)
{
Entity entity = ((EntityHitResult)hitResult).getEntity();
return entity instanceof VillagerEntity
|| entity instanceof TameableEntity;
}
if(hitResult instanceof BlockHitResult)
{
BlockPos pos = ((BlockHitResult)hitResult).getBlockPos();
if(pos == null)
return false;
Block block = MC.world.getBlockState(pos).getBlock();
return block instanceof BlockWithEntity
|| block instanceof CraftingTableBlock;
}
return false;
}
private boolean isClickable(HitResult hitResult)
{
if(hitResult == null)
return false;
if(hitResult instanceof EntityHitResult)
{
Entity entity = ((EntityHitResult)MC.crosshairTarget).getEntity();
return entity instanceof VillagerEntity
|| entity instanceof TameableEntity;
}
if(hitResult instanceof BlockHitResult)
{
BlockPos pos = ((BlockHitResult)MC.crosshairTarget).getBlockPos();
if(pos == null)
return false;
Block block = MC.world.getBlockState(pos).getBlock();
return block instanceof BlockWithEntity
|| block instanceof CraftingTableBlock;
}
return false;
}
@Inject(method = "onUse", at = @At("HEAD"))
private void onActivate(BlockState blockState_1, World world_1, BlockPos blockPos_1, PlayerEntity playerEntity_1, Hand hand_1, BlockHitResult blockHitResult_1, CallbackInfoReturnable<Boolean> cir)
{
if (CarpetExtraSettings.flowerPotChunkLoading && world_1.getServer() != null && !world_1.isClient)
{
ItemStack stack = playerEntity_1.getStackInHand(hand_1);
Item item = stack.getItem();
Block block = item instanceof BlockItem ? (Block) CONTENT_TO_POTTED.getOrDefault(((BlockItem) item).getBlock(), Blocks.AIR) : Blocks.AIR;
boolean boolean_1 = block == Blocks.AIR;
boolean boolean_2 = this.content == Blocks.AIR;
ServerWorld serverWorld = world_1.getServer().getWorld(world_1.getDimension().getType());
if (boolean_1 != boolean_2 && (block == Blocks.POTTED_WITHER_ROSE || this.content == Blocks.WITHER_ROSE))
{
// System.out.println("Chunk load status = " + boolean_2);
serverWorld.setChunkForced(blockPos_1.getX() >> 4, blockPos_1.getZ() >> 4, boolean_2);
}
}
}
public boolean placeBlockAuto(BlockPos block) {
if (lastPlaced.containsKey(block) || !WorldUtils.NONSOLID_BLOCKS.contains(mc.world.getBlockState(block).getBlock())) {
return false;
}
for (Direction d: Direction.values()) {
if (!WorldUtils.NONSOLID_BLOCKS.contains(mc.world.getBlockState(block.offset(d)).getBlock())) {
if (WorldUtils.RIGHTCLICKABLE_BLOCKS.contains(mc.world.getBlockState(block.offset(d)).getBlock())) {
mc.player.networkHandler.sendPacket(new ClientCommandC2SPacket(mc.player, Mode.START_SNEAKING));
}
mc.interactionManager.interactBlock(mc.player, mc.world, Hand.MAIN_HAND,
new BlockHitResult(new Vec3d(block), d.getOpposite(), block.offset(d), false));
mc.player.swingHand(Hand.MAIN_HAND);
if (WorldUtils.RIGHTCLICKABLE_BLOCKS.contains(mc.world.getBlockState(block.offset(d)).getBlock())) {
mc.player.networkHandler.sendPacket(new ClientCommandC2SPacket(mc.player, Mode.STOP_SNEAKING));
}
lastPlaced.put(block, 5);
return true;
}
}
return false;
}
public boolean placeBlockAuto(BlockPos block) {
if (lastPlaced.containsKey(block) || !WorldUtils.NONSOLID_BLOCKS.contains(mc.world.getBlockState(block).getBlock())) {
return false;
}
for (Direction d: Direction.values()) {
if (!WorldUtils.NONSOLID_BLOCKS.contains(mc.world.getBlockState(block.offset(d)).getBlock())) {
if (WorldUtils.RIGHTCLICKABLE_BLOCKS.contains(mc.world.getBlockState(block.offset(d)).getBlock())) {
mc.player.networkHandler.sendPacket(new ClientCommandC2SPacket(mc.player, Mode.PRESS_SHIFT_KEY));
}
mc.interactionManager.interactBlock(mc.player, mc.world, Hand.MAIN_HAND,
new BlockHitResult(Vec3d.of(block), d.getOpposite(), block.offset(d), false));
mc.player.swingHand(Hand.MAIN_HAND);
if (WorldUtils.RIGHTCLICKABLE_BLOCKS.contains(mc.world.getBlockState(block.offset(d)).getBlock())) {
mc.player.networkHandler.sendPacket(new ClientCommandC2SPacket(mc.player, Mode.RELEASE_SHIFT_KEY));
}
lastPlaced.put(block, 5);
return true;
}
}
return false;
}
public boolean placeBlockAuto(BlockPos block) {
if (lastPlaced.containsKey(block) || !WorldUtils.NONSOLID_BLOCKS.contains(mc.world.getBlockState(block).getBlock())) {
return false;
}
for (Direction d: Direction.values()) {
if (!WorldUtils.NONSOLID_BLOCKS.contains(mc.world.getBlockState(block.offset(d)).getBlock())) {
if (WorldUtils.RIGHTCLICKABLE_BLOCKS.contains(mc.world.getBlockState(block.offset(d)).getBlock())) {
mc.player.networkHandler.sendPacket(new ClientCommandC2SPacket(mc.player, Mode.PRESS_SHIFT_KEY));
}
mc.interactionManager.interactBlock(mc.player, mc.world, Hand.MAIN_HAND,
new BlockHitResult(new Vec3d(block), d.getOpposite(), block.offset(d), false));
mc.player.swingHand(Hand.MAIN_HAND);
if (WorldUtils.RIGHTCLICKABLE_BLOCKS.contains(mc.world.getBlockState(block.offset(d)).getBlock())) {
mc.player.networkHandler.sendPacket(new ClientCommandC2SPacket(mc.player, Mode.RELEASE_SHIFT_KEY));
}
lastPlaced.put(block, 5);
return true;
}
}
return false;
}
@Override
public void onBlockHit(ServerPlayerEntity player, Hand enumhand, BlockHitResult hitRes)//ItemStack itemstack, Hand enumhand, BlockPos blockpos, Direction enumfacing, Vec3d vec3d)
{
handler.call( () ->
{
ItemStack itemstack = player.getStackInHand(enumhand);
BlockPos blockpos = hitRes.getBlockPos();
Direction enumfacing = hitRes.getSide();
Vec3d vec3d = hitRes.getPos().subtract(blockpos.getX(), blockpos.getY(), blockpos.getZ());
return Arrays.asList(
((c, t) -> new EntityValue(player)),
((c, t) -> ListValue.fromItemStack(itemstack)),
((c, t) -> new StringValue(enumhand == Hand.MAIN_HAND ? "mainhand" : "offhand")),
((c, t) -> new BlockValue(null, player.getServerWorld(), blockpos)),
((c, t) -> new StringValue(enumfacing.getName())),
((c, t) -> ListValue.of(
new NumericValue(vec3d.x),
new NumericValue(vec3d.y),
new NumericValue(vec3d.z)
))
);
}, player::getCommandSource);
}
@Override
public void onBlockHit(ServerPlayerEntity player, Hand enumhand, BlockHitResult hitRes)
{
handler.call( () ->
{
BlockPos blockpos = hitRes.getBlockPos();
Direction enumfacing = hitRes.getSide();
Vec3d vec3d = hitRes.getPos().subtract(blockpos.getX(), blockpos.getY(), blockpos.getZ());
return Arrays.asList(
((c, t) -> new EntityValue(player)),
((c, t) -> new StringValue(enumhand == Hand.MAIN_HAND ? "mainhand" : "offhand")),
((c, t) -> new BlockValue(null, player.getServerWorld(), blockpos)),
((c, t) -> new StringValue(enumfacing.getName())),
((c, t) -> ListValue.of(
new NumericValue(vec3d.x),
new NumericValue(vec3d.y),
new NumericValue(vec3d.z)
))
);
}, player::getCommandSource);
}
@Override
@Deprecated
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
if (!world.isClient() && Galacticraft.configManager.get().isDebugLogEnabled()) {
Galacticraft.logger.info(((ServerWorldAccessor) world).getNetworkManager().getNetwork(pos));
}
return super.onUse(state, world, pos, player, hand, hit);
}
@Override
public ActionResult onUse(BlockState blockState, World world, BlockPos blockPos, PlayerEntity playerEntity, Hand hand, BlockHitResult blockHitResult) {
if (playerEntity.getStackInHand(hand).getItem() instanceof ShearsItem) {
world.setBlockState(blockPos, GalacticraftBlocks.CAVERNOUS_VINE.getDefaultState().with(VINES, blockState.get(VINES)));
world.playSound(blockPos.getX(), blockPos.getY(), blockPos.getZ(), SoundEvents.BLOCK_GRASS_BREAK, SoundCategory.BLOCKS, 1f, 1f, true);
return ActionResult.SUCCESS;
}
return ActionResult.SUCCESS;
}
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
if (player.getStackInHand(hand).getItem() instanceof FlintAndSteelItem) {
world.setBlockState(pos, Blocks.WALL_TORCH.getDefaultState().with(WallTorchBlock.FACING, state.get(WallTorchBlock.FACING)));
ItemStack stack = player.getStackInHand(hand).copy();
stack.damage(1, player, (playerEntity -> {
}));
player.setStackInHand(hand, stack);
}
return super.onUse(state, world, pos, player, hand, hit);
}
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
if (player.getStackInHand(hand).getItem() instanceof FlintAndSteelItem) {
world.setBlockState(pos, Blocks.TORCH.getDefaultState());
ItemStack stack = player.getStackInHand(hand).copy();
stack.damage(1, player, (playerEntity -> {
}));
player.setStackInHand(hand, stack);
}
return super.onUse(state, world, pos, player, hand, hit);
}
@Override
public ActionResult onUse(BlockState blockState, World world, BlockPos blockPos, PlayerEntity playerEntity, Hand hand, BlockHitResult blockHitResult) {
int age = blockState.get(AGE);
boolean mature = age == 3;
if (mature) {
int amount = 1 + world.random.nextInt(3);
dropStack(world, blockPos, new ItemStack(GalacticraftItems.MOON_BERRIES, amount));
world.playSound(null, blockPos, SoundEvents.ITEM_SWEET_BERRIES_PICK_FROM_BUSH, SoundCategory.BLOCKS, 1.0F, 0.8F + world.random.nextFloat() * 0.4F);
world.setBlockState(blockPos, blockState.with(AGE, 1), 2);
return ActionResult.SUCCESS;
} else {
return super.onUse(blockState, world, blockPos, playerEntity, hand, blockHitResult);
}
}
@Override
public ActionResult onUse(BlockState blockState_1, World world_1, BlockPos blockPos_1, PlayerEntity playerEntity_1, Hand hand_1, BlockHitResult blockHitResult_1) {
BlockEntity partEntity = world_1.getBlockEntity(blockPos_1);
if (world_1.isAir(blockPos_1) || !(partEntity instanceof BasicSolarPanelPartBlockEntity)) {
return ActionResult.SUCCESS;
}
if (world_1.isClient) return ActionResult.SUCCESS;
BlockPos basePos = ((BasicSolarPanelPartBlockEntity) partEntity).basePos;
BlockState base = world_1.getBlockState(basePos);
Block baseBlock = base.getBlock();
return ((BasicSolarPanelBlock) baseBlock).onUse(base, world_1, basePos, playerEntity_1, hand_1, blockHitResult_1);
}
@SuppressWarnings("deprecation")
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
BlockEntity be = world.getBlockEntity(pos);
if (be instanceof TinyPumpkinBlockEntity) {
return ((TinyPumpkinBlockEntity) be).use(player, hand, hit);
}
return ActionResult.PASS;
}
public ActionResult use(PlayerEntity player, Hand hand, BlockHitResult hit) {
Direction facing = getCachedState().get(HorizontalFacingBlock.FACING);
Direction hitSide = hit.getSide();
if (hitSide != facing.rotateYClockwise() && hitSide != facing.rotateYCounterclockwise()) {
return ActionResult.PASS;
}
if (!world.isClient) {
ItemStack handStack = player.getStackInHand(hand);
boolean isLeft = hitSide == facing.rotateYCounterclockwise();
ItemStack heldItem = isLeft ? leftItem : rightItem;
if (!heldItem.isEmpty()) {
ItemScatterer.spawn(world, pos, DefaultedList.copyOf(ItemStack.EMPTY, heldItem));
if (isLeft) {
leftItem = ItemStack.EMPTY;
} else {
rightItem = ItemStack.EMPTY;
}
sync();
markDirty();
} else if (!handStack.isEmpty()) {
if (isLeft) {
leftItem = handStack.copy();
leftItem.setCount(1);
} else {
rightItem = handStack.copy();
rightItem.setCount(1);
}
handStack.decrement(1);
sync();
markDirty();
}
}
return ActionResult.SUCCESS;
}
@SuppressWarnings("deprecation")
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hitResult) {
if (!world.isClient) {
return this.tryEat(world, pos, state, player);
}
ItemStack stack = player.getStackInHand(hand);
if(stack.isEmpty()) {
return ActionResult.PASS;
}
return this.tryEat(world, pos, state, player);
}
@SuppressWarnings("deprecation")
@Override
public ActionResult onUse(BlockState blockState, World world, BlockPos blockPos, PlayerEntity playerEntity, Hand hand, BlockHitResult blockHitResult) {
InfusionPillarBlockEntity pillarEntity = (InfusionPillarBlockEntity) world.getBlockEntity(blockPos);
if (pillarEntity != null) {
if (playerEntity.getStackInHand(hand).isEmpty()) {
playerEntity.inventory.insertStack(pillarEntity.takeStack());
} else {
playerEntity.setStackInHand(hand, pillarEntity.putStack(playerEntity.getStackInHand(hand)));
}
}
return ActionResult.SUCCESS;
}
@SuppressWarnings("deprecation")
@Override
public ActionResult onUse(BlockState blockState, World world, BlockPos blockPos, PlayerEntity playerEntity, Hand hand, BlockHitResult blockHitResult) {
InfusionAltarBlockEntity altarEntity = (InfusionAltarBlockEntity) world.getBlockEntity(blockPos);
if (playerEntity.isSneaking()) {
if (altarEntity != null) {
getLinkedPillars(altarEntity);
getCombinedInventory(altarEntity);
Optional<InfusionRecipe> recipe = world.getRecipeManager().getFirstMatch(InfusionRecipe.Type.INSTANCE, combinedInventory, world);
if (recipe.isPresent()) {
if (world.isClient()) {
createParticles(altarEntity);
createSound(altarEntity);
}
if (!altarEntity.storedStack.isEmpty()) {
createDrop(altarEntity, recipe.get().getOutput());
clearAllStacks(altarEntity);
} else {
altarEntity.storedStack = recipe.get().getOutput().copy();
clearPillarStacks(altarEntity);
}
}
}
} else {
if (altarEntity != null) {
if (playerEntity.getStackInHand(hand).isEmpty()) {
playerEntity.inventory.offerOrDrop(world, altarEntity.takeStack());
} else {
playerEntity.setStackInHand(hand, altarEntity.putStack(playerEntity.getStackInHand(hand)));
}
}
}
return ActionResult.SUCCESS;
}
private void handlePositionSelection()
{
// continue with next step
if(step.pos != null && InputUtil
.isKeyPressed(MC.getWindow().getHandle(), GLFW.GLFW_KEY_ENTER))
{
step = Step.values()[step.ordinal() + 1];
// delete posLookingAt
if(!step.selectPos)
posLookingAt = null;
return;
}
if(MC.crosshairTarget != null
&& MC.crosshairTarget instanceof BlockHitResult)
{
// set posLookingAt
posLookingAt = ((BlockHitResult)MC.crosshairTarget).getBlockPos();
// offset if sneaking
if(MC.options.keySneak.isPressed())
posLookingAt = posLookingAt
.offset(((BlockHitResult)MC.crosshairTarget).getSide());
}else
posLookingAt = null;
// set selected position
if(posLookingAt != null && MC.options.keyUse.isPressed())
step.pos = posLookingAt;
}
private void setCurrentBlockFromHitResult()
{
if(MC.crosshairTarget == null || MC.crosshairTarget.getPos() == null
|| MC.crosshairTarget.getType() != HitResult.Type.BLOCK
|| !(MC.crosshairTarget instanceof BlockHitResult))
{
stopMiningAndResetProgress();
return;
}
currentBlock = ((BlockHitResult)MC.crosshairTarget).getBlockPos();
}
@Override
public void onLeftClick(LeftClickEvent event)
{
if(mode.getSelected() != Mode.ID)
return;
if(MC.crosshairTarget == null
|| MC.crosshairTarget.getType() != HitResult.Type.BLOCK)
return;
BlockHitResult blockHitResult = (BlockHitResult)MC.crosshairTarget;
BlockPos pos = new BlockPos(blockHitResult.getBlockPos());
id = BlockUtils.getName(pos);
}