mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-17 11:05:40 +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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user