From e6bb7eecbde4370ea772ea9866b1a4343fd060ca Mon Sep 17 00:00:00 2001 From: LegoMaster3650 <37251737+LegoMaster3650@users.noreply.github.com> Date: Mon, 17 Jul 2023 10:56:39 -0700 Subject: [PATCH] Added Action value to PlayerInteractEvent.LeftClickEvent to expose what action fired the event. #9175 --- .../MultiPlayerGameMode.java.patch | 8 +-- .../level/ServerPlayerGameMode.java.patch | 2 +- .../net/minecraftforge/common/ForgeHooks.java | 19 ++++++- .../entity/player/PlayerInteractEvent.java | 52 +++++++++++++++++++ 4 files changed, 75 insertions(+), 6 deletions(-) diff --git a/patches/minecraft/net/minecraft/client/multiplayer/MultiPlayerGameMode.java.patch b/patches/minecraft/net/minecraft/client/multiplayer/MultiPlayerGameMode.java.patch index 396188f917..8699d5d616 100644 --- a/patches/minecraft/net/minecraft/client/multiplayer/MultiPlayerGameMode.java.patch +++ b/patches/minecraft/net/minecraft/client/multiplayer/MultiPlayerGameMode.java.patch @@ -23,7 +23,7 @@ BlockState blockstate = this.f_105189_.f_91073_.m_8055_(p_105270_); this.f_105189_.m_91301_().m_120581_(this.f_105189_.f_91073_, p_105270_, blockstate, 1.0F); this.m_233729_(this.f_105189_.f_91073_, (p_233757_) -> { -+ if (!net.minecraftforge.common.ForgeHooks.onLeftClickBlock(this.f_105189_.f_91074_, p_105270_, p_105271_).isCanceled()) ++ if (!net.minecraftforge.common.ForgeHooks.onLeftClickBlock(this.f_105189_.f_91074_, p_105270_, p_105271_, ServerboundPlayerActionPacket.Action.START_DESTROY_BLOCK).isCanceled()) this.m_105267_(p_105270_); return new ServerboundPlayerActionPacket(ServerboundPlayerActionPacket.Action.START_DESTROY_BLOCK, p_105270_, p_105271_, p_233757_); }); @@ -31,7 +31,7 @@ if (this.f_105196_) { this.f_105190_.m_104955_(new ServerboundPlayerActionPacket(ServerboundPlayerActionPacket.Action.ABORT_DESTROY_BLOCK, this.f_105191_, p_105271_)); } -+ net.minecraftforge.event.entity.player.PlayerInteractEvent.LeftClickBlock event = net.minecraftforge.common.ForgeHooks.onLeftClickBlock(this.f_105189_.f_91074_, p_105270_, p_105271_); ++ net.minecraftforge.event.entity.player.PlayerInteractEvent.LeftClickBlock event = net.minecraftforge.common.ForgeHooks.onLeftClickBlock(this.f_105189_.f_91074_, p_105270_, p_105271_, ServerboundPlayerActionPacket.Action.START_DESTROY_BLOCK); BlockState blockstate1 = this.f_105189_.f_91073_.m_8055_(p_105270_); this.f_105189_.m_91301_().m_120581_(this.f_105189_.f_91073_, p_105270_, blockstate1, 0.0F); @@ -60,7 +60,7 @@ BlockState blockstate1 = this.f_105189_.f_91073_.m_8055_(p_105284_); this.f_105189_.m_91301_().m_120581_(this.f_105189_.f_91073_, p_105284_, blockstate1, 1.0F); this.m_233729_(this.f_105189_.f_91073_, (p_233753_) -> { -+ if (!net.minecraftforge.common.ForgeHooks.onLeftClickBlock(this.f_105189_.f_91074_, p_105284_, p_105285_).isCanceled()) ++ if (!net.minecraftforge.common.ForgeHooks.onLeftClickBlock(this.f_105189_.f_91074_, p_105284_, p_105285_, ServerboundPlayerActionPacket.Action.START_DESTROY_BLOCK).isCanceled()) this.m_105267_(p_105284_); return new ServerboundPlayerActionPacket(ServerboundPlayerActionPacket.Action.START_DESTROY_BLOCK, p_105284_, p_105285_, p_233753_); }); @@ -75,7 +75,7 @@ ++this.f_105194_; this.f_105189_.m_91301_().m_120581_(this.f_105189_.f_91073_, p_105284_, blockstate, Mth.m_14036_(this.f_105193_, 0.0F, 1.0F)); -+ if (net.minecraftforge.common.ForgeHooks.onLeftClickBlock(this.f_105189_.f_91074_, p_105284_, p_105285_).getUseItem() == net.minecraftforge.eventbus.api.Event.Result.DENY) return true; ++ if (net.minecraftforge.common.ForgeHooks.onClientMineHold(this.f_105189_.f_91074_, p_105284_, p_105285_).getUseItem() == net.minecraftforge.eventbus.api.Event.Result.DENY) return true; if (this.f_105193_ >= 1.0F) { this.f_105196_ = false; this.m_233729_(this.f_105189_.f_91073_, (p_233739_) -> { diff --git a/patches/minecraft/net/minecraft/server/level/ServerPlayerGameMode.java.patch b/patches/minecraft/net/minecraft/server/level/ServerPlayerGameMode.java.patch index 1826e16ce2..1a56c7aaff 100644 --- a/patches/minecraft/net/minecraft/server/level/ServerPlayerGameMode.java.patch +++ b/patches/minecraft/net/minecraft/server/level/ServerPlayerGameMode.java.patch @@ -5,7 +5,7 @@ public void m_214168_(BlockPos p_215120_, ServerboundPlayerActionPacket.Action p_215121_, Direction p_215122_, int p_215123_, int p_215124_) { - if (this.f_9245_.m_146892_().m_82557_(Vec3.m_82512_(p_215120_)) > ServerGamePacketListenerImpl.f_215198_) { -+ net.minecraftforge.event.entity.player.PlayerInteractEvent.LeftClickBlock event = net.minecraftforge.common.ForgeHooks.onLeftClickBlock(f_9245_, p_215120_, p_215122_); ++ net.minecraftforge.event.entity.player.PlayerInteractEvent.LeftClickBlock event = net.minecraftforge.common.ForgeHooks.onLeftClickBlock(f_9245_, p_215120_, p_215122_, p_215121_); + if (event.isCanceled() || (!this.m_9295_() && event.getResult() == net.minecraftforge.eventbus.api.Event.Result.DENY)) { + return; + } diff --git a/src/main/java/net/minecraftforge/common/ForgeHooks.java b/src/main/java/net/minecraftforge/common/ForgeHooks.java index 0c8d7c5fdb..bf2e8a6c56 100644 --- a/src/main/java/net/minecraftforge/common/ForgeHooks.java +++ b/src/main/java/net/minecraftforge/common/ForgeHooks.java @@ -113,6 +113,7 @@ import net.minecraft.nbt.ListTag; import net.minecraft.network.protocol.Packet; import net.minecraft.network.syncher.EntityDataSerializer; import net.minecraft.network.protocol.game.ClientboundBlockUpdatePacket; +import net.minecraft.network.protocol.game.ServerboundPlayerActionPacket; import net.minecraft.world.item.alchemy.Potion; import net.minecraft.world.item.alchemy.PotionUtils; import net.minecraft.stats.Stats; @@ -769,9 +770,25 @@ public class ForgeHooks return evt.isCanceled() ? evt.getCancellationResult() : null; } + /** + * @deprecated Use {@link #onLeftClickBlock(Player, BlockPos, Direction, ServerboundPlayerActionPacket.Action)} instead + */ + @Deprecated(since = "1.20.1", forRemoval = true) public static PlayerInteractEvent.LeftClickBlock onLeftClickBlock(Player player, BlockPos pos, Direction face) { - PlayerInteractEvent.LeftClickBlock evt = new PlayerInteractEvent.LeftClickBlock(player, pos, face); + return onLeftClickBlock(player, pos, face, ServerboundPlayerActionPacket.Action.START_DESTROY_BLOCK); + } + + public static PlayerInteractEvent.LeftClickBlock onLeftClickBlock(Player player, BlockPos pos, Direction face, ServerboundPlayerActionPacket.Action action) + { + PlayerInteractEvent.LeftClickBlock evt = new PlayerInteractEvent.LeftClickBlock(player, pos, face, PlayerInteractEvent.LeftClickBlock.Action.convert(action)); + MinecraftForge.EVENT_BUS.post(evt); + return evt; + } + + public static PlayerInteractEvent.LeftClickBlock onClientMineHold(Player player, BlockPos pos, Direction face) + { + PlayerInteractEvent.LeftClickBlock evt = new PlayerInteractEvent.LeftClickBlock(player, pos, face, PlayerInteractEvent.LeftClickBlock.Action.CLIENT_HOLD); MinecraftForge.EVENT_BUS.post(evt); return evt; } diff --git a/src/main/java/net/minecraftforge/event/entity/player/PlayerInteractEvent.java b/src/main/java/net/minecraftforge/event/entity/player/PlayerInteractEvent.java index 6acc81f824..e828b7359d 100644 --- a/src/main/java/net/minecraftforge/event/entity/player/PlayerInteractEvent.java +++ b/src/main/java/net/minecraftforge/event/entity/player/PlayerInteractEvent.java @@ -13,6 +13,7 @@ import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.InteractionResult; import net.minecraft.core.Direction; +import net.minecraft.network.protocol.game.ServerboundPlayerActionPacket; import net.minecraft.world.InteractionHand; import net.minecraft.core.BlockPos; import net.minecraft.world.item.context.UseOnContext; @@ -29,6 +30,8 @@ import static net.minecraftforge.eventbus.api.Event.Result.DEFAULT; import static net.minecraftforge.eventbus.api.Event.Result.DENY; import net.minecraftforge.fml.LogicalSide; + +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -239,6 +242,9 @@ public class PlayerInteractEvent extends PlayerEvent * Canceling the event will cause none of the above noted methods to be called. * There are various results to this event, see the getters below. * + * This event is fired at various points during left clicking on blocks, at both the start and end on the server, and at the start and while held down on the client. + * Use {@link #getAction()} to check which type of action triggered this event. + * * Note that if the event is canceled and the player holds down left mouse, the event will continue to fire. * This is due to how vanilla calls the left click handler methods. * @@ -250,10 +256,19 @@ public class PlayerInteractEvent extends PlayerEvent { private Result useBlock = DEFAULT; private Result useItem = DEFAULT; + private final Action action; + @Deprecated(since = "1.20.1", forRemoval = true) public LeftClickBlock(Player player, BlockPos pos, Direction face) + { + this(player, pos, face, Action.START); + } + + @ApiStatus.Internal + public LeftClickBlock(Player player, BlockPos pos, Direction face, Action action) { super(player, InteractionHand.MAIN_HAND, pos, face); + this.action = action; } /** @@ -272,6 +287,14 @@ public class PlayerInteractEvent extends PlayerEvent return useItem; } + /** + * @return The action type for this interaction. Will never be null. + */ + @NotNull + public Action getAction() { + return this.action; + } + public void setUseBlock(Result triggerBlock) { this.useBlock = triggerBlock; @@ -292,6 +315,35 @@ public class PlayerInteractEvent extends PlayerEvent useItem = DENY; } } + + public static enum Action { + /** + * When the player first left clicks a block + */ + START, + /** + * When the player stops left clicking a block by completely breaking it + */ + STOP, + /** + * When the player stops left clicking a block by releasing the button, or no longer targeting the same block before it breaks. + */ + ABORT, + /** + * When the player is actively mining a block on the client side + * Warning: The event is fired every tick on the client + */ + CLIENT_HOLD; + + public static Action convert(ServerboundPlayerActionPacket.Action action) { + return switch (action) { + default -> START; + case START_DESTROY_BLOCK -> START; + case STOP_DESTROY_BLOCK -> STOP; + case ABORT_DESTROY_BLOCK -> ABORT; + }; + } + } } /**