Skip to content

Commit a784072

Browse files
authored
Improve LivingSetAttackEvent (#29)
1 parent fd336ce commit a784072

File tree

3 files changed

+59
-18
lines changed

3 files changed

+59
-18
lines changed

patches/minecraft/net/minecraft/entity/EntityLiving.java.patch

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
--- before/net/minecraft/entity/EntityLiving.java
22
+++ after/net/minecraft/entity/EntityLiving.java
3-
@@ -171,6 +171,7 @@
3+
@@ -171,6 +171,6 @@
44
public void setAttackTarget(@Nullable EntityLivingBase entitylivingbaseIn)
55
{
6-
this.attackTarget = entitylivingbaseIn;
7-
+ net.minecraftforge.common.ForgeHooks.onLivingSetAttackTarget(this, entitylivingbaseIn);
6+
- this.attackTarget = entitylivingbaseIn;
7+
+ this.attackTarget = net.minecraftforge.common.ForgeHooks.onLivingSetAttackTarget(this, entitylivingbaseIn);
88
}
99

1010
public boolean canAttackClass(Class <? extends EntityLivingBase > cls)
11-
@@ -601,7 +602,7 @@
11+
@@ -601,7 +601,7 @@
1212
super.onLivingUpdate();
1313
this.world.profiler.startSection("looting");
1414

@@ -17,7 +17,7 @@
1717
{
1818
for (EntityItem entityitem : this.world.getEntitiesWithinAABB(EntityItem.class, this.getEntityBoundingBox().grow(1.0, 0.0, 1.0)))
1919
{
20-
@@ -729,10 +730,22 @@
20+
@@ -729,10 +729,22 @@
2121

2222
protected void despawnEntity()
2323
{
@@ -40,15 +40,15 @@
4040
else
4141
{
4242
Entity entity = this.world.getClosestPlayerToEntity(this, -1.0);
43-
@@ -870,7 +883,6 @@
43+
@@ -870,7 +882,6 @@
4444
&& this.world.checkNoEntityCollision(this.getEntityBoundingBox(), this);
4545
}
4646

4747
- @SideOnly(Side.CLIENT)
4848
public float getRenderSizeModifier()
4949
{
5050
return 1.0F;
51-
@@ -1033,6 +1045,8 @@
51+
@@ -1033,6 +1044,8 @@
5252

5353
public static EntityEquipmentSlot getSlotForItemStack(ItemStack stack)
5454
{
@@ -57,7 +57,7 @@
5757
if (stack.getItem() == Item.getItemFromBlock(Blocks.PUMPKIN) || stack.getItem() == Items.SKULL)
5858
{
5959
return EntityEquipmentSlot.HEAD;
60-
@@ -1047,7 +1061,7 @@
60+
@@ -1047,7 +1060,7 @@
6161
}
6262
else
6363
{
@@ -66,7 +66,7 @@
6666
}
6767
}
6868

69-
@@ -1490,5 +1504,19 @@
69+
@@ -1490,5 +1503,19 @@
7070
ON_GROUND,
7171
IN_AIR,
7272
IN_WATER;

src/main/java/net/minecraftforge/common/ForgeHooks.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import net.minecraft.enchantment.Enchantment;
5757
import net.minecraft.enchantment.EnchantmentHelper;
5858
import net.minecraft.entity.Entity;
59+
import net.minecraft.entity.EntityLiving;
5960
import net.minecraft.entity.EntityLivingBase;
6061
import net.minecraft.entity.item.EntityItem;
6162
import net.minecraft.entity.item.EntityMinecartContainer;
@@ -576,11 +577,20 @@ public static void onDifficultyChange(EnumDifficulty difficulty, EnumDifficulty
576577
//Optifine Helper Functions u.u, these are here specifically for Optifine
577578
//Note: When using Optifine, these methods are invoked using reflection, which
578579
//incurs a major performance penalty.
580+
@Deprecated(since = "15.24.0.3026")
579581
public static void onLivingSetAttackTarget(EntityLivingBase entity, EntityLivingBase target)
580582
{
581583
MinecraftForge.EVENT_BUS.post(new LivingSetAttackTargetEvent(entity, target));
582584
}
583585

586+
public static EntityLivingBase onLivingSetAttackTarget(EntityLiving living, EntityLivingBase target)
587+
{
588+
LivingSetAttackTargetEvent event = new LivingSetAttackTargetEvent(living, target);
589+
if (MinecraftForge.EVENT_BUS.post(event)){
590+
return living.getAttackTarget();
591+
}else return event.getTarget();
592+
}
593+
584594
public static boolean onLivingUpdate(EntityLivingBase entity)
585595
{
586596
return MinecraftForge.EVENT_BUS.post(new LivingUpdateEvent(entity));

src/main/java/net/minecraftforge/event/entity/living/LivingSetAttackTargetEvent.java

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,59 @@
3030
* This event is fired whenever an Entity sets a target to attack in
3131
* {@link EntityLiving#setAttackTarget(EntityLivingBase)}.<br>
3232
* <br>
33-
* This event is fired via the {@link ForgeHooks#onLivingSetAttackTarget(EntityLivingBase, EntityLivingBase)}.<br>
33+
* This event is fired via the {@link ForgeHooks#onLivingSetAttackTarget(EntityLiving, EntityLivingBase)}.<br>
3434
* <br>
35-
* {@link #target} contains the newly targeted Entity.<br>
35+
* {@link #originalTarget} contains the newly targeted Entity.<br>
3636
* <br>
37-
* This event is not {@link Cancelable}.<br>
37+
* {@link #newTarget} contains the redirected Targeted Entity.<br>
38+
* <br>
39+
* This event is {@link Cancelable}.<br>
3840
* <br>
3941
* This event does not have a result. {@link HasResult}<br>
4042
* <br>
4143
* This event is fired on the {@link MinecraftForge#EVENT_BUS}.
4244
**/
43-
public class LivingSetAttackTargetEvent extends LivingEvent
44-
{
45-
46-
private final EntityLivingBase target;
45+
@Cancelable
46+
public class LivingSetAttackTargetEvent extends LivingEvent{
47+
private final EntityLivingBase originalTarget;
48+
private EntityLivingBase newTarget;
49+
private boolean isModified;
50+
4751
public LivingSetAttackTargetEvent(EntityLivingBase entity, EntityLivingBase target)
4852
{
4953
super(entity);
50-
this.target = target;
54+
this.originalTarget = target;
55+
this.newTarget = null;
56+
this.isModified = false;
5157
}
5258

59+
/**
60+
* Get the target that will be actually applied
61+
**/
5362
public EntityLivingBase getTarget()
5463
{
55-
return target;
64+
return isModified ? newTarget : originalTarget;
65+
}
66+
67+
/**
68+
* return the original attack target
69+
**/
70+
public EntityLivingBase getOriginalTarget(){
71+
return originalTarget;
72+
}
73+
74+
/**
75+
* Set the attack target of the living's, null if remove it
76+
**/
77+
public void setNewTarget(EntityLivingBase living){
78+
this.newTarget = living;
79+
this.isModified = true;
80+
}
81+
82+
/**
83+
* Is the attack target is modified
84+
**/
85+
public boolean isModified(){
86+
return this.isModified;
5687
}
5788
}

0 commit comments

Comments
 (0)