mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-19 03:49:14 +00:00
Allow result of PlayerInteractEvent to be changed when it's cancelled (#3744)
This commit is contained in:
1 parent
5ffbbd3b9f
commit
72dd06ca85
7 files changed
+170
-67
No files matched your search
@@ -1035,25 +1035,56 @@ public class ForgeHooks
|
||||
return git == null ? null : git.hitVec;
|
||||
}
|
||||
|
||||
// TODO 1.12 remove these three
|
||||
@Deprecated
|
||||
public static boolean onInteractEntityAt(EntityPlayer player, Entity entity, RayTraceResult ray, EnumHand hand)
|
||||
{
|
||||
Vec3d vec3d = new Vec3d(ray.hitVec.xCoord - entity.posX, ray.hitVec.yCoord - entity.posY, ray.hitVec.zCoord - entity.posZ);
|
||||
return onInteractEntityAt(player, entity, vec3d, hand);
|
||||
return onInteractEntityAtAction(player, entity, ray, hand) != null;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static boolean onInteractEntityAt(EntityPlayer player, Entity entity, Vec3d vec3d, EnumHand hand)
|
||||
{
|
||||
return MinecraftForge.EVENT_BUS.post(new PlayerInteractEvent.EntityInteractSpecific(player, hand, entity, vec3d));
|
||||
return onInteractEntityAtAction(player, entity, vec3d, hand) != null;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static boolean onInteractEntity(EntityPlayer player, Entity entity, EnumHand hand)
|
||||
{
|
||||
return MinecraftForge.EVENT_BUS.post(new PlayerInteractEvent.EntityInteract(player, hand, entity));
|
||||
return onInteractEntityAction(player, entity, hand) != null;
|
||||
}
|
||||
|
||||
public static EnumActionResult onInteractEntityAtAction(EntityPlayer player, Entity entity, RayTraceResult ray, EnumHand hand)
|
||||
{
|
||||
Vec3d vec3d = new Vec3d(ray.hitVec.xCoord - entity.posX, ray.hitVec.yCoord - entity.posY, ray.hitVec.zCoord - entity.posZ);
|
||||
return onInteractEntityAtAction(player, entity, vec3d, hand);
|
||||
}
|
||||
|
||||
public static EnumActionResult onInteractEntityAtAction(EntityPlayer player, Entity entity, Vec3d vec3d, EnumHand hand)
|
||||
{
|
||||
PlayerInteractEvent.EntityInteractSpecific evt = new PlayerInteractEvent.EntityInteractSpecific(player, hand, entity, vec3d);
|
||||
MinecraftForge.EVENT_BUS.post(evt);
|
||||
return evt.isCanceled() ? evt.getCancellationResult() : null;
|
||||
}
|
||||
|
||||
public static EnumActionResult onInteractEntityAction(EntityPlayer player, Entity entity, EnumHand hand)
|
||||
{
|
||||
PlayerInteractEvent.EntityInteract evt = new PlayerInteractEvent.EntityInteract(player, hand, entity);
|
||||
MinecraftForge.EVENT_BUS.post(evt);
|
||||
return evt.isCanceled() ? evt.getCancellationResult() : null;
|
||||
}
|
||||
|
||||
@Deprecated // TODO 1.12 remove
|
||||
public static boolean onItemRightClick(EntityPlayer player, EnumHand hand)
|
||||
{
|
||||
return MinecraftForge.EVENT_BUS.post(new PlayerInteractEvent.RightClickItem(player, hand));
|
||||
return onItemRightClickAction(player, hand) != null;
|
||||
}
|
||||
|
||||
public static EnumActionResult onItemRightClickAction(EntityPlayer player, EnumHand hand)
|
||||
{
|
||||
PlayerInteractEvent.RightClickItem evt = new PlayerInteractEvent.RightClickItem(player, hand);
|
||||
MinecraftForge.EVENT_BUS.post(evt);
|
||||
return evt.isCanceled() ? evt.getCancellationResult() : null;
|
||||
}
|
||||
|
||||
public static PlayerInteractEvent.LeftClickBlock onLeftClickBlock(EntityPlayer player, BlockPos pos, EnumFacing face, Vec3d hitVec)
|
||||
|
||||
@@ -23,6 +23,7 @@ import com.google.common.base.Preconditions;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.EnumActionResult;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.EnumHand;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
@@ -49,6 +50,7 @@ public class PlayerInteractEvent extends PlayerEvent
|
||||
private final BlockPos pos;
|
||||
@Nullable
|
||||
private final EnumFacing face;
|
||||
private EnumActionResult cancellationResult = EnumActionResult.PASS;
|
||||
|
||||
private PlayerInteractEvent(EntityPlayer player, EnumHand hand, BlockPos pos, @Nullable EnumFacing face)
|
||||
{
|
||||
@@ -63,11 +65,9 @@ public class PlayerInteractEvent extends PlayerEvent
|
||||
*
|
||||
* "Interact at" is an interact where the local vector (which part of the entity you clicked) is known.
|
||||
* The state of this event affects whether {@link Entity#applyPlayerInteraction} is called.
|
||||
* If {@link Entity#applyPlayerInteraction} returns {@link net.minecraft.util.EnumActionResult#SUCCESS}, then processing ends.
|
||||
* Otherwise processing will continue to {@link EntityInteract}
|
||||
*
|
||||
* Canceling the event clientside will cause processing to continue to {@link EntityInteract},
|
||||
* while canceling serverside will simply do no further processing.
|
||||
* Let result be the return value of {@link Entity#applyPlayerInteraction}, or {@link #cancellationResult} if the event is cancelled.
|
||||
* If we are on the client and result is not {@link EnumActionResult#SUCCESS}, the client will then try {@link EntityInteract}.
|
||||
*/
|
||||
@Cancelable
|
||||
public static class EntityInteractSpecific extends PlayerInteractEvent
|
||||
@@ -103,14 +103,12 @@ public class PlayerInteractEvent extends PlayerEvent
|
||||
* This event is fired on both sides when the player right clicks an entity.
|
||||
* It is responsible for all general entity interactions.
|
||||
*
|
||||
* This event is fired completely independently of the above {@link EntityInteractSpecific}, except for the case
|
||||
* where the above call to {@link Entity#applyPlayerInteraction} returns {@link net.minecraft.util.EnumActionResult#SUCCESS}.
|
||||
* In that case, general entity interactions, and hence this event, will not be called. See the above javadoc for more details.
|
||||
*
|
||||
* This event is fired only if the result of the above {@link EntityInteractSpecific} is not {@link EnumActionResult#SUCCESS}.
|
||||
* This event's state affects whether {@link Entity#processInitialInteract} and {@link net.minecraft.item.Item#itemInteractionForEntity} are called.
|
||||
*
|
||||
* Canceling the event clientside will cause processing to continue to {@link RightClickItem},
|
||||
* while canceling serverside will simply do no further processing.
|
||||
* Let result be {@link EnumActionResult#SUCCESS} if {@link Entity#processInitialInteract} or {@link net.minecraft.item.Item#itemInteractionForEntity} return true,
|
||||
* or {@link #cancellationResult} if the event is cancelled.
|
||||
* If we are on the client and result is not {@link EnumActionResult#SUCCESS}, the client will then try {@link RightClickItem}.
|
||||
*/
|
||||
@Cancelable
|
||||
public static class EntityInteract extends PlayerInteractEvent
|
||||
@@ -133,7 +131,11 @@ public class PlayerInteractEvent extends PlayerEvent
|
||||
* This event is fired on both sides whenever the player right clicks while targeting a block.
|
||||
* This event controls which of {@link net.minecraft.block.Block#onBlockActivated} and/or {@link net.minecraft.item.Item#onItemUse}
|
||||
* will be called after {@link net.minecraft.item.Item#onItemUseFirst} is called.
|
||||
* Canceling the event will cause none of the above three to be called.
|
||||
* Canceling the event will cause none of the above three to be called
|
||||
*
|
||||
* Let result be a return value of the above three methods, or {@link #cancellationResult} if the event is cancelled.
|
||||
* If we are on the client and result is not {@link EnumActionResult#SUCCESS}, the client will then try {@link RightClickItem}.
|
||||
*
|
||||
* There are various results to this event, see the getters below.
|
||||
* Note that handling things differently on the client vs server may cause desynchronizations!
|
||||
*/
|
||||
@@ -207,9 +209,10 @@ public class PlayerInteractEvent extends PlayerEvent
|
||||
|
||||
/**
|
||||
* This event is fired on both sides before the player triggers {@link net.minecraft.item.Item#onItemRightClick}.
|
||||
* Note that this is NOT fired if the player is targeting a block. For that case, see {@link RightClickBlock}.
|
||||
* Canceling the event clientside causes processing to continue to the other hands,
|
||||
* while canceling serverside will simply do no further processing.
|
||||
* Note that this is NOT fired if the player is targeting a block {@link RightClickBlock} or entity {@link EntityInteract} {@link EntityInteractSpecific}.
|
||||
*
|
||||
* Let result be the return value of {@link net.minecraft.item.Item#onItemRightClick}, or {@link #cancellationResult} if the event is cancelled.
|
||||
* If we are on the client and result is not {@link EnumActionResult#SUCCESS}, the client will then continue to other hands.
|
||||
*/
|
||||
@Cancelable
|
||||
public static class RightClickItem extends PlayerInteractEvent
|
||||
@@ -381,4 +384,24 @@ public class PlayerInteractEvent extends PlayerEvent
|
||||
return getWorld().isRemote ? Side.CLIENT : Side.SERVER;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The EnumActionResult that will be returned to vanilla if the event is cancelled, instead of calling the relevant
|
||||
* method of the event. By default, this is {@link EnumActionResult#PASS}, meaning cancelled events will cause
|
||||
* the client to keep trying more interactions until something works.
|
||||
*/
|
||||
public EnumActionResult getCancellationResult()
|
||||
{
|
||||
return cancellationResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the EnumActionResult that will be returned to vanilla if the event is cancelled, instead of calling the relevant
|
||||
* method of the event.
|
||||
* Note that this only has an effect on {@link RightClickBlock}, {@link RightClickItem}, {@link EntityInteract}, and {@link EntityInteractSpecific}.
|
||||
*/
|
||||
public void setCancellationResult(EnumActionResult result)
|
||||
{
|
||||
this.cancellationResult = result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,9 +5,11 @@ import net.minecraft.entity.monster.EntityCreeper;
|
||||
import net.minecraft.entity.monster.EntitySkeleton;
|
||||
import net.minecraft.entity.passive.EntityHorse;
|
||||
import net.minecraft.init.Items;
|
||||
import net.minecraft.item.ItemBlock;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.tileentity.TileEntityChest;
|
||||
import net.minecraft.tileentity.TileEntityDropper;
|
||||
import net.minecraft.util.EnumActionResult;
|
||||
import net.minecraft.util.EnumHand;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
|
||||
@@ -49,7 +51,7 @@ public class PlayerInteractEventTest
|
||||
if (!ENABLE) return;
|
||||
logger.info("HIT VEC: {}", evt.getHitVec());
|
||||
|
||||
if (evt.getItemStack() != null)
|
||||
if (!evt.getItemStack().isEmpty())
|
||||
{
|
||||
if (evt.getItemStack().getItem() == Items.GOLDEN_PICKAXE)
|
||||
evt.setCanceled(true); // Redstone should not activate and pick should not be able to dig anything
|
||||
@@ -89,22 +91,34 @@ public class PlayerInteractEventTest
|
||||
// Case: Flint and steel in main hand on top of a TE will light a fire, not open the TE.
|
||||
// Note that if you do this on a chest, the f+s will fail, but then your off hand will open the chest
|
||||
// If you dual wield flints and steels and right click a chest nothing should happen
|
||||
if (evt.getItemStack() != null && evt.getItemStack().getItem() == Items.FLINT_AND_STEEL)
|
||||
if (!evt.getItemStack().isEmpty() && evt.getItemStack().getItem() == Items.FLINT_AND_STEEL)
|
||||
evt.setUseBlock(Event.Result.DENY);
|
||||
|
||||
// Case: Painting in main hand
|
||||
// Opening a TE will also place a painting on the TE if possible
|
||||
if (evt.getHand() == EnumHand.MAIN_HAND && evt.getItemStack() != null && evt.getItemStack().getItem() == Items.PAINTING) {
|
||||
if (evt.getHand() == EnumHand.MAIN_HAND && !evt.getItemStack().isEmpty() && evt.getItemStack().getItem() == Items.PAINTING) {
|
||||
evt.setUseItem(Event.Result.ALLOW);
|
||||
}
|
||||
|
||||
// Spawn egg in main hand, block in offhand -> block should be placed
|
||||
// Sword in main hand, spawn egg in offhand -> nothing should happen
|
||||
if (evt.getItemStack() != null && evt.getItemStack().getItem() == Items.SPAWN_EGG) {
|
||||
if (!evt.getItemStack().isEmpty()
|
||||
&& evt.getItemStack().getItem() == Items.SPAWN_EGG
|
||||
&& evt.getHand() == EnumHand.MAIN_HAND
|
||||
&& !evt.getEntityPlayer().getHeldItemOffhand().isEmpty()
|
||||
&& evt.getEntityPlayer().getHeldItemOffhand().getItem() instanceof ItemBlock) {
|
||||
evt.setCanceled(true);
|
||||
}
|
||||
|
||||
|
||||
// Spawn egg in main hand, potion in offhand -> potion should NOT be thrown
|
||||
if (!evt.getItemStack().isEmpty()
|
||||
&& evt.getItemStack().getItem() == Items.SPAWN_EGG
|
||||
&& evt.getHand() == EnumHand.MAIN_HAND
|
||||
&& !evt.getEntityPlayer().getHeldItemOffhand().isEmpty()
|
||||
&& evt.getEntityPlayer().getHeldItemOffhand().getItem() == Items.SPLASH_POTION) {
|
||||
evt.setCanceled(true);
|
||||
// Fake spawn egg success so splash potion does not trigger
|
||||
evt.setCancellationResult(EnumActionResult.SUCCESS);
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
@@ -112,12 +126,24 @@ public class PlayerInteractEventTest
|
||||
{
|
||||
if (!ENABLE) return;
|
||||
|
||||
// Use survival mode
|
||||
// Case: Ender pearl in main hand, block in offhand -> Block is NOT placed
|
||||
if (!evt.getItemStack().isEmpty()
|
||||
&& evt.getItemStack().getItem() == Items.ENDER_PEARL
|
||||
&& evt.getHand() == EnumHand.MAIN_HAND
|
||||
&& !evt.getEntityPlayer().getHeldItemOffhand().isEmpty()
|
||||
&& evt.getEntityPlayer().getHeldItemOffhand().getItem() instanceof ItemBlock)
|
||||
{
|
||||
evt.setCanceled(true);
|
||||
evt.setCancellationResult(EnumActionResult.SUCCESS); // We fake success on the ender pearl so block is not placed
|
||||
return;
|
||||
}
|
||||
|
||||
// Case: Ender pearl in main hand, bow in offhand with arrows in inv -> Bow should trigger
|
||||
// Case: Sword in main hand, ender pearl in offhand -> Nothing should happen
|
||||
|
||||
if (evt.getItemStack() != null && evt.getItemStack().getItem() == Items.ENDER_PEARL)
|
||||
if (!evt.getItemStack().isEmpty() && evt.getItemStack().getItem() == Items.ENDER_PEARL)
|
||||
{
|
||||
evt.setCanceled(true);
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
@@ -126,17 +152,27 @@ public class PlayerInteractEventTest
|
||||
if (!ENABLE) return;
|
||||
logger.info("LOCAL POS: {}", evt.getLocalPos());
|
||||
|
||||
if (evt.getItemStack() != null
|
||||
if (!evt.getItemStack().isEmpty()
|
||||
&& evt.getTarget() instanceof EntityArmorStand
|
||||
&& evt.getItemStack().getItem() == Items.IRON_HELMET)
|
||||
evt.setCanceled(true); // Should not be able to place iron helmet onto armor stand (you will put it on instead)
|
||||
|
||||
if (evt.getWorld().isRemote
|
||||
if (!evt.getItemStack().isEmpty()
|
||||
&& evt.getTarget() instanceof EntityArmorStand
|
||||
&& evt.getItemStack().getItem() == Items.GOLDEN_HELMET)
|
||||
{
|
||||
evt.setCanceled(true);
|
||||
evt.setCancellationResult(EnumActionResult.SUCCESS);
|
||||
// Should not be able to place golden helmet onto armor stand
|
||||
// However you will NOT put it on because we fake success on the armorstand.
|
||||
}
|
||||
|
||||
if (!evt.getWorld().isRemote
|
||||
&& evt.getTarget() instanceof EntitySkeleton
|
||||
&& evt.getLocalPos().yCoord > evt.getTarget().height / 2.0)
|
||||
{
|
||||
// If we right click the upper half of a skeleton it dies.
|
||||
((EntitySkeleton) evt.getTarget()).setDead();
|
||||
evt.getTarget().setDead();
|
||||
evt.setCanceled(true);
|
||||
}
|
||||
}
|
||||
@@ -146,10 +182,18 @@ public class PlayerInteractEventTest
|
||||
{
|
||||
if (!ENABLE) return;
|
||||
|
||||
if (evt.getItemStack() != null && (evt.getTarget() instanceof EntityHorse || evt.getTarget() instanceof EntityCreeper))
|
||||
if (!evt.getItemStack().isEmpty() && evt.getTarget() instanceof EntityHorse) {
|
||||
// Should not be able to feed wild horses with golden apple (you will start eating it in survival)
|
||||
// Should not be able to ignite creeper with F+S
|
||||
// Applies to both hands
|
||||
evt.setCanceled(true);
|
||||
if (evt.getItemStack().getItem() == Items.GOLDEN_APPLE
|
||||
&& evt.getItemStack().getItemDamage() == 0)
|
||||
evt.setCanceled(true);
|
||||
// Should not be able to feed wild horses with notch apple but you will NOT eat it
|
||||
if (evt.getItemStack().getItem() == Items.GOLDEN_APPLE
|
||||
&& evt.getItemStack().getItemDamage() == 1)
|
||||
{
|
||||
evt.setCanceled(true);
|
||||
evt.setCancellationResult(EnumActionResult.SUCCESS);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user