InteractionEvent.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * This file is part of architectury.
  3. * Copyright (C) 2020, 2021 architectury
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 3 of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this program; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. package me.shedaniel.architectury.event.events;
  20. import me.shedaniel.architectury.event.Event;
  21. import me.shedaniel.architectury.event.EventFactory;
  22. import me.shedaniel.architectury.event.EventResult;
  23. import net.minecraft.core.BlockPos;
  24. import net.minecraft.core.Direction;
  25. import net.minecraft.world.InteractionHand;
  26. import net.minecraft.world.InteractionResult;
  27. import net.minecraft.world.InteractionResultHolder;
  28. import net.minecraft.world.entity.Entity;
  29. import net.minecraft.world.entity.player.Player;
  30. import net.minecraft.world.item.ItemStack;
  31. import net.minecraft.world.level.Level;
  32. import net.minecraft.world.level.block.state.BlockState;
  33. import org.jetbrains.annotations.ApiStatus;
  34. public interface InteractionEvent {
  35. /**
  36. * @see LeftClickBlock#click(Player, InteractionHand, BlockPos, Direction)
  37. */
  38. Event<LeftClickBlock> LEFT_CLICK_BLOCK = EventFactory.createInteractionResult();
  39. /**
  40. * @see RightClickBlock#click(Player, InteractionHand, BlockPos, Direction)
  41. */
  42. Event<RightClickBlock> RIGHT_CLICK_BLOCK = EventFactory.createInteractionResult();
  43. /**
  44. * @see RightClickItem#click(Player, InteractionHand)
  45. */
  46. Event<RightClickItem> RIGHT_CLICK_ITEM = EventFactory.createInteractionResultHolder();
  47. /**
  48. * @see ClientLeftClickAir#click(Player, InteractionHand)
  49. */
  50. Event<ClientLeftClickAir> CLIENT_LEFT_CLICK_AIR = EventFactory.createLoop();
  51. /**
  52. * @see ClientRightClickAir#click(Player, InteractionHand)
  53. */
  54. Event<ClientRightClickAir> CLIENT_RIGHT_CLICK_AIR = EventFactory.createLoop();
  55. /**
  56. * @see InteractEntity#interact(Player, Entity, InteractionHand)
  57. */
  58. Event<InteractEntity> INTERACT_ENTITY = EventFactory.createInteractionResult();
  59. /**
  60. * @see FarmlandTrample#trample(Level, BlockPos, BlockState, float, Entity)
  61. */
  62. Event<FarmlandTrample> FARMLAND_TRAMPLE = EventFactory.createEventResult();
  63. interface RightClickBlock {
  64. /**
  65. * Invoked whenever a player right clicks a block.
  66. * Equivalent to Forge's {@code PlayerInteractEvent.RightClickBlock} event and Fabric's {@code UseBlockCallback}.
  67. *
  68. * @param player The player right clicking the block.
  69. * @param hand The hand that is used.
  70. * @param pos The position of the block in the level.
  71. * @param face The face of the block clicked.
  72. * @return The event is canceled if anything else than {@link InteractionResult#PASS} is returned.
  73. */
  74. InteractionResult click(Player player, InteractionHand hand, BlockPos pos, Direction face);
  75. }
  76. interface LeftClickBlock {
  77. /**
  78. * Invoked whenever a player left clicks a block.
  79. * Equivalent to Forge's {@code PlayerInteractEvent.LeftClickBlock} event and Fabric's {@code AttackBlockCallback}.
  80. *
  81. * @param player The player left clicking the block.
  82. * @param hand The hand that is used.
  83. * @param pos The position of the block in the level. Use {@link Player#getCommandSenderWorld()} to get the level.
  84. * @param face The face of the block clicked.
  85. * @return The event is canceled if anything else than {@link InteractionResult#PASS} is returned.
  86. */
  87. InteractionResult click(Player player, InteractionHand hand, BlockPos pos, Direction face);
  88. }
  89. interface RightClickItem {
  90. /**
  91. * Invoked whenever a player uses an item on a block.
  92. * Equivalent to Forge's {@code PlayerInteractEvent.RightClickItem} event and Fabric's {@code UseItemCallback}.
  93. *
  94. * @param player The player right clicking the block.
  95. * @param hand The hand that is used.
  96. * @return Whenever the return is not {@link InteractionResult#PASS}, the result value is used and the event is canceled.
  97. */
  98. InteractionResultHolder<ItemStack> click(Player player, InteractionHand hand);
  99. }
  100. interface ClientRightClickAir {
  101. /**
  102. * Invoked whenever a player right clicks the air.
  103. * This only occurs on the client.
  104. * Equivalent to Forge's {@code PlayerInteractEvent.RightClickEmpty} event.
  105. *
  106. * @param player The player. Always {@link net.minecraft.client.player.LocalPlayer}
  107. * @param hand The hand used.
  108. */
  109. void click(Player player, InteractionHand hand);
  110. }
  111. interface ClientLeftClickAir {
  112. /**
  113. * Invoked whenever a player left clicks the air.
  114. * This only occurs on the client.
  115. * Equivalent to Forge's {@code PlayerInteractEvent.LeftClickEmpty} event.
  116. *
  117. * @param player The player. Always {@link net.minecraft.client.player.LocalPlayer}
  118. * @param hand The hand used.
  119. */
  120. void click(Player player, InteractionHand hand);
  121. }
  122. interface InteractEntity {
  123. /**
  124. * Invoked whenever a player right clicks an entity.
  125. * Equivalent to Forge's {@code PlayerInteractEvent.EntityInteract} event.
  126. *
  127. * @param player The player clicking the entity.
  128. * @param entity Then entity the player clicks.
  129. * @param hand The used hand.
  130. * @return If the return value is not {@link InteractionResult#PASS}, event is cancelled and the used result is passed as return value.
  131. */
  132. InteractionResult interact(Player player, Entity entity, InteractionHand hand);
  133. }
  134. /**
  135. * @deprecated use {@link BlockEvent#BREAK}
  136. */
  137. @ApiStatus.ScheduledForRemoval(inVersion = "2.0")
  138. @Deprecated
  139. interface BlockBreak {
  140. InteractionResult breakBlock(Player player, BlockPos pos, BlockState state);
  141. }
  142. interface FarmlandTrample {
  143. /**
  144. * Invoked when an entity attempts to trample farmland.
  145. * Equivalent to Forge's {@code BlockEvent.FarmlandTrampleEvent} event.
  146. *
  147. * @param world The level where the block and the player are located in.
  148. * @param pos The position of the block.
  149. * @param state The state of the block.
  150. * @param distance The distance of the player to the block.
  151. * @param entity The entity trampling.
  152. * @return The event callback result.
  153. */
  154. EventResult trample(Level world, BlockPos pos, BlockState state, float distance, Entity entity);
  155. }
  156. }