From d6cd51191e24f2e5acf8d3c6d9d2edf3912041f4 Mon Sep 17 00:00:00 2001 From: Andrii Horpenko Date: Mon, 29 May 2023 22:17:26 +0300 Subject: [PATCH] [1.18.x] Fixed PlayerInteractEvent.EntityInteractSpecific being called too late (#9494) --- .../ServerGamePacketListenerImpl.java.patch | 15 ++++++--- .../EntityInteractSpecificEventTest.java | 32 +++++++++++++++++++ src/test/resources/META-INF/mods.toml | 2 ++ 3 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 src/test/java/net/minecraftforge/debug/entity/player/EntityInteractSpecificEventTest.java diff --git a/patches/minecraft/net/minecraft/server/network/ServerGamePacketListenerImpl.java.patch b/patches/minecraft/net/minecraft/server/network/ServerGamePacketListenerImpl.java.patch index 741f125f0f..ac82d41e3b 100644 --- a/patches/minecraft/net/minecraft/server/network/ServerGamePacketListenerImpl.java.patch +++ b/patches/minecraft/net/minecraft/server/network/ServerGamePacketListenerImpl.java.patch @@ -52,7 +52,7 @@ }, ChatType.CHAT, this.f_9743_.m_142081_()); } -@@ -1216,12 +_,13 @@ +@@ -1216,10 +_,10 @@ return; } @@ -64,11 +64,16 @@ + if(!ServerGamePacketListenerImpl.this.f_9743_.canInteractWith(entity, 1.5D)) return; //Forge: If the entity cannot be reached, do nothing. Original check was dist < 6, range is 4.5, so vanilla used padding=1.5 ItemStack itemstack = ServerGamePacketListenerImpl.this.f_9743_.m_21120_(p_143679_).m_41777_(); InteractionResult interactionresult = p_143680_.m_143694_(ServerGamePacketListenerImpl.this.f_9743_, entity, p_143679_); -+ if (net.minecraftforge.common.ForgeHooks.onInteractEntityAt(f_9743_, entity, entity.m_20182_(), p_143679_) != null) return; if (interactionresult.m_19077_()) { - CriteriaTriggers.f_10565_.m_61494_(ServerGamePacketListenerImpl.this.f_9743_, itemstack, entity); - if (interactionresult.m_19080_()) { -@@ -1243,7 +_,7 @@ +@@ -1237,13 +_,15 @@ + + public void m_142143_(InteractionHand p_143682_, Vec3 p_143683_) { + this.m_143678_(p_143682_, (p_143686_, p_143687_, p_143688_) -> { ++ InteractionResult onInteractEntityAtResult = net.minecraftforge.common.ForgeHooks.onInteractEntityAt(f_9743_, entity, p_143683_, p_143682_); ++ if (onInteractEntityAtResult != null) return onInteractEntityAtResult; + return p_143687_.m_7111_(p_143686_, p_143683_, p_143688_); + }); + } public void m_141994_() { if (!(entity instanceof ItemEntity) && !(entity instanceof ExperienceOrb) && !(entity instanceof AbstractArrow) && entity != ServerGamePacketListenerImpl.this.f_9743_) { diff --git a/src/test/java/net/minecraftforge/debug/entity/player/EntityInteractSpecificEventTest.java b/src/test/java/net/minecraftforge/debug/entity/player/EntityInteractSpecificEventTest.java new file mode 100644 index 0000000000..19abb67121 --- /dev/null +++ b/src/test/java/net/minecraftforge/debug/entity/player/EntityInteractSpecificEventTest.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) Forge Development LLC and contributors + * SPDX-License-Identifier: LGPL-2.1-only + */ + +package net.minecraftforge.debug.entity.player; + +import net.minecraft.world.entity.decoration.ArmorStand; +import net.minecraftforge.event.entity.player.PlayerInteractEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.common.Mod; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +@Mod("entity_interact_specific_event_test") +@Mod.EventBusSubscriber +public class EntityInteractSpecificEventTest +{ + private static final boolean ENABLE = false; + private static final Logger LOGGER = LogManager.getLogger(); + + @SubscribeEvent + public static void onEntityInteractSpecific(PlayerInteractEvent.EntityInteractSpecific event) + { + if (!ENABLE) return; + if (event.getTarget() instanceof ArmorStand) + { + LOGGER.info("EntityInteractSpecific event has been canceled for armor stand"); + event.setCanceled(true); + } + } +} diff --git a/src/test/resources/META-INF/mods.toml b/src/test/resources/META-INF/mods.toml index be8a7c7be6..91163d355a 100644 --- a/src/test/resources/META-INF/mods.toml +++ b/src/test/resources/META-INF/mods.toml @@ -242,5 +242,7 @@ license="LGPL v2.1" modId="block_grow_feature_test" [[mods]] modId="grindstone_event_test" +[[mods]] + modId="entity_interact_specific_event_test" # ADD ABOVE THIS LINE