下面列出了怎么用net.minecraft.util.Hand的API类实例代码及写法,或者点击链接到github查看源代码。
@Redirect(method = "consumeItem", at = @At(
value = "INVOKE",
target = "Lnet/minecraft/entity/player/PlayerEntity;consumeItem()V"
))
private void finishedUsingItem(PlayerEntity playerEntity)
{
if (PLAYER_FINISHED_USING_ITEM.isNeeded())
{
Hand hand = getActiveHand();
ItemStack stack = getActiveItem().copy();
// do vanilla
super.consumeItem();
PLAYER_FINISHED_USING_ITEM.onItemAction((ServerPlayerEntity) (Object)this, hand, stack);
}
else
{
// do vanilla
super.consumeItem();
}
}
@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;
}
@Override
public void onUpdate()
{
ClientPlayerEntity player = MC.player;
if(player.getAttackCooldownProgress(0) < 1)
return;
if(MC.crosshairTarget == null
|| !(MC.crosshairTarget instanceof EntityHitResult))
return;
Entity target = ((EntityHitResult)MC.crosshairTarget).getEntity();
if(!isCorrectEntity(target))
return;
WURST.getHax().autoSwordHack.setSlot();
MC.interactionManager.attackEntity(player, target);
player.swingHand(Hand.MAIN_HAND);
}
private boolean tryToPlaceBlock(boolean legitMode, BlockPos pos)
{
if(!BlockUtils.getState(pos).getMaterial().isReplaceable())
return false;
if(legitMode)
{
if(!placeBlockLegit(pos))
return false;
IMC.setItemUseCooldown(4);
}else
{
if(!placeBlockSimple_old(pos))
return false;
MC.player.swingHand(Hand.MAIN_HAND);
IMC.setItemUseCooldown(4);
}
lastPos = pos;
return true;
}
@Override
public boolean interactMob(PlayerEntity playerEntity_1, Hand hand_1)
{
ItemStack stack = playerEntity_1.getStackInHand(hand_1);
if (CarpetExtraSettings.chickenShearing && stack.getItem() == Items.SHEARS && !this.isBaby())
{
if (!this.world.isClient)
{
this.damage(DamageSource.GENERIC, 1);
this.dropItem(Items.FEATHER, 1);
stack.damage(1, (LivingEntity)playerEntity_1, ((playerEntity_1x) -> {
playerEntity_1x.sendToolBreakStatus(hand_1);
}));
}
}
return super.interactMob(playerEntity_1, hand_1);
}
@Subscribe
public void onTick(EventTick event) {
delay++;
int reqDelay = (int) Math.round(20/getSettings().get(3).toSlider().getValue());
for (Entity e: mc.world.getEntities()) {
if (e instanceof EnderCrystalEntity && mc.player.distanceTo(e) < getSettings().get(2).toSlider().getValue()) {
if (!mc.player.canSee(e) && !getSettings().get(1).toToggle().state) continue;
if (getSettings().get(0).toToggle().state) EntityUtils.facePos(e.x, e.y + e.getHeight()/2, e.z);
if (delay > reqDelay || reqDelay == 0) {
mc.player.networkHandler.sendPacket(new PlayerInteractEntityC2SPacket(e));
mc.player.attack(e);
mc.player.swingHand(Hand.MAIN_HAND);
delay=0;
}
}
}
}
public void placeBlockAuto(BlockPos block) {
if (lastPlaced.containsKey(block)) return;
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.connection.sendPacket(new CEntityActionPacket(mc.player, Action.START_SNEAKING));}
mc.player.connection.sendPacket(new CPlayerTryUseItemOnBlockPacket(Hand.MAIN_HAND,
new BlockRayTraceResult(new Vec3d(block), d.getOpposite(), block.offset(d), false)));
mc.player.swingArm(Hand.MAIN_HAND);
mc.world.playSound(block, SoundEvents.BLOCK_NOTE_BLOCK_HAT, SoundCategory.BLOCKS, 1f, 1f, false);
if (WorldUtils.RIGHTCLICKABLE_BLOCKS.contains(mc.world.getBlockState(block.offset(d)).getBlock())) {
mc.player.connection.sendPacket(new CEntityActionPacket(mc.player, Action.STOP_SNEAKING));}
lastPlaced.put(block, 5);
return;
}
}
}
@Subscribe
public void onTick(EventTick event) {
delay++;
int reqDelay = (int) Math.round(20/getSettings().get(3).toSlider().getValue());
for (Entity e: mc.world.getEntities()) {
if (e instanceof EndCrystalEntity && mc.player.distanceTo(e) < getSettings().get(2).toSlider().getValue()) {
if (!mc.player.canSee(e) && !getSettings().get(1).toToggle().state) continue;
if (getSettings().get(0).toToggle().state) EntityUtils.facePos(e.getX(), e.getY() + e.getHeight()/2, e.getZ());
if (delay > reqDelay || reqDelay == 0) {
mc.player.networkHandler.sendPacket(new PlayerInteractEntityC2SPacket(e, mc.player.isSneaking()));
mc.player.attack(e);
mc.player.swingHand(Hand.MAIN_HAND);
delay=0;
}
}
}
}
@Subscribe
public void onTick(EventTick event) {
delay++;
int reqDelay = (int) Math.round(20/getSettings().get(3).toSlider().getValue());
for (Entity e: mc.world.getEntities()) {
if (e instanceof EnderCrystalEntity && mc.player.distanceTo(e) < getSettings().get(2).toSlider().getValue()) {
if (!mc.player.canSee(e) && !getSettings().get(1).toToggle().state) continue;
if (getSettings().get(0).toToggle().state) EntityUtils.facePos(e.getX(), e.getY() + e.getHeight()/2, e.getZ());
if (delay > reqDelay || reqDelay == 0) {
mc.player.networkHandler.sendPacket(new PlayerInteractEntityC2SPacket(e));
mc.player.attack(e);
mc.player.swingHand(Hand.MAIN_HAND);
delay=0;
}
}
}
}
@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);
}
}
@SubscribeEvent
public void onAttackEntity(AttackEntityEvent ev)
{
if (!ConfigManager.SERVER.enableTorchFire.get())
return;
if (!ev.getTarget().func_230279_az_() && !ev.getTarget().world.isRemote)
{
PlayerEntity player = ev.getPlayer();
ItemStack stack = player.getHeldItem(Hand.MAIN_HAND);
if (stack.getCount() > 0 && stack.getItem() instanceof BlockItem)
{
BlockItem b = (BlockItem) stack.getItem();
Block bl = b.getBlock();
if (bl == Blocks.TORCH)
{
ev.getTarget().setFire(2);
if (!ev.getPlayer().isCreative() && rnd.nextFloat() > 0.25)
{
stack.grow(-1);
if (stack.getCount() <= 0)
{
player.inventory.setInventorySlotContents(player.inventory.currentItem, ItemStack.EMPTY);
}
}
}
}
}
}
@Override //should sync with server
public TypedActionResult<ItemStack> use(World world_1, PlayerEntity playerEntity_1, Hand hand_1) {
SimpleInventoryComponent inv = ((GCPlayerAccessor) playerEntity_1).getGearInventory();
ItemStack thermalPiece = inv.getStack(getSlotIdForType(getSlotType()));
if (thermalPiece.isEmpty()) {
inv.setStack(getSlotIdForType(getSlotType()), playerEntity_1.getStackInHand(hand_1));
return new TypedActionResult<>(ActionResult.SUCCESS, ItemStack.EMPTY);
}
return super.use(world_1, playerEntity_1, hand_1);
}
@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
if (((GCPlayerAccessor) user).getGearInventory().getStack(5).isEmpty()) {
((GCPlayerAccessor) user).getGearInventory().setStack(5, user.getStackInHand(hand));
return new TypedActionResult<>(ActionResult.SUCCESS, ItemStack.EMPTY);
}
return super.use(world, user, hand);
}
@Override
public ActionResultType onBlockActivated(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult result) {
if (!world.isRemote) {
TileEntity tileEntity = world.getTileEntity(pos);
if (tileEntity instanceof INamedContainerProvider) {
NetworkHooks.openGui((ServerPlayerEntity) player, (INamedContainerProvider) tileEntity, tileEntity.getPos());
} else {
throw new IllegalStateException("Our named container provider is missing!");
}
return ActionResultType.SUCCESS;
}
return ActionResultType.SUCCESS;
}
@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
if (((GCPlayerAccessor) user).getGearInventory().getStack(4).isEmpty()) {
((GCPlayerAccessor) user).getGearInventory().setStack(4, user.getStackInHand(hand));
return new TypedActionResult<>(ActionResult.SUCCESS, ItemStack.EMPTY);
}
return super.use(world, user, hand);
}
@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
ItemStack stack = player.getStackInHand(hand);
float soundPitch = (((player.pitch - 90) * 1) / -90);
if (player.isSneaking()) {
player.playSound(HallowedSounds.MEGALADOOT, 0.8f, 1.0f);
} else {
player.playSound(HallowedSounds.DOOT, 0.8f, soundPitch);
}
return new TypedActionResult<>(ActionResult.SUCCESS, stack);
}
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;
}
@Inject(method = "interactBlock", at = @At(
value = "RETURN",
ordinal = 2
))
private void onBlockActivated(PlayerEntity playerArg, World world, ItemStack stack, Hand hand, BlockHitResult blockHitResult, CallbackInfoReturnable<ActionResult> cir)
{
PLAYER_INTERACTS_WITH_BLOCK.onBlockHit(player, hand, blockHitResult);
}
@Inject(method = "onPlayerInteractItem", at = @At(
value = "INVOKE",
target = "Lnet/minecraft/server/network/ServerPlayerEntity;updateLastActionTime()V"
))
private void onItemClicked(PlayerInteractItemC2SPacket playerInteractItemC2SPacket_1, CallbackInfo ci)
{
if (PLAYER_USES_ITEM.isNeeded())
{
Hand hand = playerInteractItemC2SPacket_1.getHand();
PLAYER_USES_ITEM.onItemAction(player, hand, player.getStackInHand(hand).copy());
}
}
@Inject(method = "onPlayerInteractBlock", at = @At(
value = "INVOKE",
target = "Lnet/minecraft/server/network/ServerPlayerInteractionManager;interactBlock(Lnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/world/World;Lnet/minecraft/item/ItemStack;Lnet/minecraft/util/Hand;Lnet/minecraft/util/hit/BlockHitResult;)Lnet/minecraft/util/ActionResult;"
))
private void onBlockInteracted(PlayerInteractBlockC2SPacket playerInteractBlockC2SPacket_1, CallbackInfo ci)
{
if (PLAYER_RIGHT_CLICKS_BLOCK.isNeeded())
{
Hand hand = playerInteractBlockC2SPacket_1.getHand();
BlockHitResult hitRes = playerInteractBlockC2SPacket_1.getHitY();
PLAYER_RIGHT_CLICKS_BLOCK.onBlockHit(player, hand, hitRes);
}
}
@Override
public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity player, Hand hand) {
ItemStack stack = player.getHeldItem(hand);
if (player.isShiftKeyDown()) {
return new ActionResult<>(ActionResultType.PASS, stack);
}
if (!world.isRemote) {
Frequency frequency = Frequency.readFromStack(stack);
EnderStorageManager.instance(world.isRemote).getStorage(frequency, EnderItemStorage.TYPE).openContainer((ServerPlayerEntity) player, new TranslationTextComponent(stack.getTranslationKey()));
}
return new ActionResult<>(ActionResultType.SUCCESS, stack);
}
@Override
public void onItemAction(ServerPlayerEntity player, Hand enumhand, ItemStack itemstack)
{
// this.getStackInHand(this.getActiveHand()), this.activeItemStack)
handler.call( () ->
{
return Arrays.asList(
((c, t) -> new EntityValue(player)),
((c, t) -> ListValue.fromItemStack(itemstack)),
((c, t) -> new StringValue(enumhand == Hand.MAIN_HAND ? "mainhand" : "offhand"))
);
}, player::getCommandSource);
}
@Deprecated
@Override
public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult blockRayTraceResult)
{
if (worldIn.isRemote)
return ActionResultType.SUCCESS;
TileEntity tileEntity = worldIn.getTileEntity(pos);
if (!(tileEntity instanceof INamedContainerProvider))
return ActionResultType.FAIL;
NetworkHooks.openGui((ServerPlayerEntity) player, (INamedContainerProvider) tileEntity);
return ActionResultType.SUCCESS;
}
@Override
public void onPostMotion()
{
if(target == null)
return;
ClientPlayerEntity player = MC.player;
MC.interactionManager.attackEntity(player, target);
player.swingHand(Hand.MAIN_HAND);
target = null;
}