Fix up the setHandeled/setHandled for events, it is now a generalized setResult, the meaning of which is defined by each event.

This commit is contained in:
LexManos 2012-10-22 00:29:28 -07:00
parent ad51183584
commit 5ca845f069
11 changed files with 133 additions and 89 deletions

View File

@ -1,11 +1,21 @@
package net.minecraftforge.event;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
/**
* Base Event class that all other events are derived from
*/
public class Event
{
@Retention(value = RUNTIME)
@Target(value = TYPE)
public @interface HasResult{}
public enum Result
{
DENY,
@ -15,25 +25,31 @@ public class Event
private boolean isCanceled = false;
private final boolean isCancelable;
private Result result = Result.DEFAULT;
private final boolean hasResult;
private static ListenerList listeners = new ListenerList();
public Event()
{
setup();
isCancelable = hasAnnotation(Cancelable.class);
hasResult = hasAnnotation(HasResult.class);
}
private boolean hasAnnotation(Class annotation)
{
Class cls = this.getClass();
boolean found = false;
while (cls != Event.class)
{
if (cls.isAnnotationPresent(Cancelable.class))
{
found = true;
break;
return true;
}
cls = cls.getSuperclass();
}
isCancelable = found;
return false;
}
/**
* Determine if this function is cancelable at all.
* @return If access to setCanceled should be allowed
@ -42,7 +58,7 @@ public class Event
{
return isCancelable;
}
/**
* Determine if this event is canceled and should stop executing.
* @return The current canceled state
@ -51,7 +67,7 @@ public class Event
{
return isCanceled;
}
/**
* Sets the state of this event, not all events are cancelable, and any attempt to
* cancel a event that can't be will result in a IllegalArgumentException.
@ -68,7 +84,35 @@ public class Event
}
isCanceled = cancel;
}
/**
* Determines if this event expects a significant result value.
*/
public boolean hasResult()
{
return hasResult;
}
/**
* Returns the value set as the result of this event
*/
public Result getResult()
{
return result;
}
/**
* Sets the result value for this event, not all events can have a result set, and any attempt to
* set a result for a event that isn't expecting it will result in a IllegalArgumentException.
*
* The functionality of setting the result is defined on a per-event bases.
*
* @param value The new result
*/
public void setResult(Result value)
{
result = value;
}
/**
* Called by the base constructor, this is used by ASM generated
* event classes to setup various functionality such as the listener's list.

View File

@ -2,7 +2,9 @@ package net.minecraftforge.event.entity.living;
import net.minecraft.src.EntityLiving;
import net.minecraft.src.World;
import net.minecraftforge.event.Cancelable;
@Cancelable
public class LivingSpecialSpawnEvent extends LivingEvent
{
public final World world;
@ -19,14 +21,4 @@ public class LivingSpecialSpawnEvent extends LivingEvent
this.y = y;
this.z = z;
}
public void setHandeled()
{
handeled = true;
}
public boolean isHandeled()
{
return handeled;
}
}

View File

@ -3,16 +3,27 @@ package net.minecraftforge.event.entity.player;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.World;
import net.minecraftforge.event.Cancelable;
import net.minecraftforge.event.Event;
@Cancelable
@Event.HasResult
public class BonemealEvent extends PlayerEvent
{
/**
* This event is called when a player attempts to use Bonemeal on a block.
* It can be canceled to completely prevent any further processing.
*
* You can also set the result to ALLOW to mark the event as processed
* and use up a bonemeal from the stack but do no further processing.
*
* setResult(ALLOW) is the same as the old setHandeled()
*/
public final World world;
public final int ID;
public final int X;
public final int Y;
public final int Z;
private boolean handeled = false;
public BonemealEvent(EntityPlayer player, World world, int id, int x, int y, int z)
{
@ -23,14 +34,4 @@ public class BonemealEvent extends PlayerEvent
this.Y = y;
this.Z = z;
}
public void setHandeled()
{
handeled = true;
}
public boolean isHandeled()
{
return handeled;
}
}

View File

@ -4,26 +4,29 @@ import net.minecraft.src.Entity;
import net.minecraft.src.EntityItem;
import net.minecraft.src.EntityPlayer;
import net.minecraftforge.event.Cancelable;
import net.minecraftforge.event.Event;
@Cancelable
@Event.HasResult
public class EntityItemPickupEvent extends PlayerEvent
{
/**
* This event is called when a player collides with a EntityItem on the ground.
* The event can be canceled, and no further processing will be done.
*
* You can set the result of this event to ALLOW which will trigger the
* processing of achievements, FML's event, play the sound, and kill the
* entity if all the items are picked up.
*
* setResult(ALLOW) is the same as the old setHandled()
*/
public final EntityItem item;
private boolean handled = false;
public EntityItemPickupEvent(EntityPlayer player, EntityItem item)
{
super(player);
this.item = item;
}
public boolean isHandled()
{
return handled;
}
public void setHandled()
{
handled = true;
}
}

View File

@ -5,17 +5,28 @@ import net.minecraft.src.ItemStack;
import net.minecraft.src.MovingObjectPosition;
import net.minecraft.src.World;
import net.minecraftforge.event.Cancelable;
import net.minecraftforge.event.Event;
@Cancelable
@Event.HasResult
public class FillBucketEvent extends PlayerEvent
{
/**
* This event is fired when a player attempts to use a Empty bucket, it
* can be canceled to completely prevent any further processing.
*
* If you set the result to 'ALLOW', it means that you have processed
* the event and wants the basic functionality of adding the new
* ItemStack to your inventory and reducing the stack size to process.
* setResult(ALLOW) is the same as the old setHandeled();
*/
public final ItemStack current;
public final World world;
public final MovingObjectPosition target;
public ItemStack result;
private boolean handeled = false;
public FillBucketEvent(EntityPlayer player, ItemStack current, World world, MovingObjectPosition target)
{
super(player);
@ -23,14 +34,4 @@ public class FillBucketEvent extends PlayerEvent
this.world = world;
this.target = target;
}
public boolean isHandeled()
{
return handeled;
}
public void setHandeled()
{
handeled = true;
}
}

View File

@ -1,14 +1,24 @@
package net.minecraftforge.event.entity;
package net.minecraftforge.event.entity.player;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.ItemStack;
import net.minecraft.src.World;
import net.minecraftforge.event.Cancelable;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.event.Event;
@Cancelable
@Event.HasResult
public class UseHoeEvent extends PlayerEvent
{
/**
* This event is fired when a player attempts to use a Hoe on a block, it
* can be canceled to completely prevent any further processing.
*
* You can also set the result to ALLOW to mark the event as processed
* and damage the hoe.
*
* setResult(ALLOW) is the same as the old setHandeled();
*/
public final ItemStack current;
public final World world;
@ -27,14 +37,4 @@ public class UseHoeEvent extends PlayerEvent
this.y = y;
this.z = z;
}
public boolean isHandeled()
{
return handeled;
}
public void setHandeled()
{
handeled = true;
}
}

View File

@ -1,17 +1,18 @@
--- ../src_base/common/net/minecraft/src/EntityItem.java
+++ ../src_work/common/net/minecraft/src/EntityItem.java
@@ -1,6 +1,10 @@
@@ -1,6 +1,11 @@
package net.minecraft.src;
import java.util.Iterator;
+
+import net.minecraftforge.common.MinecraftForge;
+import net.minecraftforge.event.Event.Result;
+import net.minecraftforge.event.entity.item.ItemExpireEvent;
+import net.minecraftforge.event.entity.player.EntityItemPickupEvent;
import cpw.mods.fml.common.registry.GameRegistry;
@@ -20,6 +24,11 @@
@@ -20,6 +25,11 @@
/** The EntityItem's random initial float height. */
public float hoverStart = (float)(Math.random() * Math.PI * 2.0D);
@ -23,7 +24,7 @@
public EntityItem(World par1World, double par2, double par4, double par6, ItemStack par8ItemStack)
{
@@ -32,6 +41,7 @@
@@ -32,6 +42,7 @@
this.motionX = (double)((float)(Math.random() * 0.20000000298023224D - 0.10000000149011612D));
this.motionY = 0.20000000298023224D;
this.motionZ = (double)((float)(Math.random() * 0.20000000298023224D - 0.10000000149011612D));
@ -31,7 +32,7 @@
}
/**
@@ -118,7 +128,20 @@
@@ -118,7 +129,20 @@
++this.age;
@ -53,7 +54,7 @@
{
this.setDead();
}
@@ -215,6 +238,7 @@
@@ -215,6 +239,7 @@
{
par1NBTTagCompound.setShort("Health", (short)((byte)this.health));
par1NBTTagCompound.setShort("Age", (short)this.age);
@ -61,7 +62,7 @@
if (this.item != null)
{
@@ -232,10 +256,15 @@
@@ -232,10 +257,15 @@
NBTTagCompound var2 = par1NBTTagCompound.getCompoundTag("Item");
this.item = ItemStack.loadItemStackFromNBT(var2);
@ -78,7 +79,7 @@
}
/**
@@ -245,9 +274,21 @@
@@ -245,9 +275,21 @@
{
if (!this.worldObj.isRemote)
{
@ -97,7 +98,7 @@
int var2 = this.item.stackSize;
- if (this.delayBeforeCanPickup == 0 && par1EntityPlayer.inventory.addItemStackToInventory(this.item))
+ if (this.delayBeforeCanPickup <= 0 && (event.isHandled() || var2 <= 0 || par1EntityPlayer.inventory.addItemStackToInventory(this.item)))
+ if (this.delayBeforeCanPickup <= 0 && (event.getResult() == Result.ALLOW || var2 <= 0 || par1EntityPlayer.inventory.addItemStackToInventory(this.item)))
{
if (this.item.itemID == Block.wood.blockID)
{

View File

@ -1,14 +1,15 @@
--- ../src_base/common/net/minecraft/src/ItemBucket.java
+++ ../src_work/common/net/minecraft/src/ItemBucket.java
@@ -1,4 +1,7 @@
@@ -1,4 +1,8 @@
package net.minecraft.src;
+
+import net.minecraftforge.common.MinecraftForge;
+import net.minecraftforge.event.Event;
+import net.minecraftforge.event.entity.player.FillBucketEvent;
public class ItemBucket extends Item
{
@@ -31,6 +34,32 @@
@@ -31,6 +35,32 @@
}
else
{
@ -18,7 +19,7 @@
+ return par1ItemStack;
+ }
+
+ if (event.isHandeled())
+ if (event.getResult() == Event.Result.ALLOW)
+ {
+ if (par3EntityPlayer.capabilities.isCreativeMode)
+ {

View File

@ -1,17 +1,18 @@
--- ../src_base/common/net/minecraft/src/ItemDye.java
+++ ../src_work/common/net/minecraft/src/ItemDye.java
@@ -3,6 +3,10 @@
@@ -3,6 +3,11 @@
import cpw.mods.fml.common.Side;
import cpw.mods.fml.common.asm.SideOnly;
import java.util.List;
+
+import net.minecraftforge.common.ForgeHooks;
+import net.minecraftforge.common.MinecraftForge;
+import net.minecraftforge.event.Event.Result;
+import net.minecraftforge.event.entity.player.BonemealEvent;
public class ItemDye extends Item
{
@@ -53,6 +57,21 @@
@@ -53,6 +58,21 @@
if (par1ItemStack.getItemDamage() == 15)
{
var11 = par3World.getBlockId(par4, par5, par6);
@ -22,9 +23,9 @@
+ return false;
+ }
+
+ if (event.isHandeled())
+ if (event.getResult() == Result.ALLOW)
+ {
+ if (!par3World.isRemote)
+ if (!par3World.isRemote)
+ {
+ par1ItemStack.stackSize--;
+ }
@ -33,7 +34,7 @@
if (var11 == Block.sapling.blockID)
{
@@ -152,16 +171,9 @@
@@ -152,16 +172,9 @@
par3World.setBlockAndMetadataWithNotify(var13, var14, var15, Block.tallGrass.blockID, 1);
}
}

View File

@ -1,14 +1,15 @@
--- ../src_base/common/net/minecraft/src/ItemHoe.java
+++ ../src_work/common/net/minecraft/src/ItemHoe.java
@@ -1,5 +1,7 @@
@@ -1,5 +1,8 @@
package net.minecraft.src;
+import net.minecraftforge.common.MinecraftForge;
+import net.minecraftforge.event.entity.UseHoeEvent;
+import net.minecraftforge.event.Event.Result;
+import net.minecraftforge.event.entity.player.UseHoeEvent;
import cpw.mods.fml.common.Side;
import cpw.mods.fml.common.asm.SideOnly;
@@ -28,6 +30,17 @@
@@ -28,6 +31,18 @@
}
else
{
@ -17,7 +18,8 @@
+ {
+ return false;
+ }
+ if (event.isHandeled())
+
+ if (event.getResult() == Result.ALLOW)
+ {
+ par1ItemStack.damageItem(1, par2EntityPlayer);
+ return true;

View File

@ -35,13 +35,11 @@
}
}
@@ -216,6 +225,13 @@
@@ -216,6 +225,11 @@
*/
private static void creatureSpecificInit(EntityLiving par0EntityLiving, World par1World, float par2, float par3, float par4)
{
+ LivingSpecialSpawnEvent event = new LivingSpecialSpawnEvent(par0EntityLiving, par1World, par2, par3, par4);
+ MinecraftForge.EVENT_BUS.post(event);
+ if (event.isHandeled())
+ if (MinecraftForge.EVENT_BUS.post(new LivingSpecialSpawnEvent(par0EntityLiving, par1World, par2, par3, par4)))
+ {
+ return;
+ }