Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented LivingKnockBackEvent event and hook #4503

Merged
merged 10 commits into from
Dec 17, 2017
46 changes: 28 additions & 18 deletions patches/minecraft/net/minecraft/entity/EntityLivingBase.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,17 @@
}

this.field_70170_p.func_72960_a(this, (byte)3);
@@ -1253,15 +1273,7 @@
@@ -1195,6 +1215,9 @@

public void func_70653_a(Entity p_70653_1_, float p_70653_2_, double p_70653_3_, double p_70653_5_)
{
+ net.minecraftforge.event.entity.living.LivingKnockBackEvent event = net.minecraftforge.common.ForgeHooks.onLivingKnockBack(this, p_70653_1_, p_70653_2_, p_70653_3_, p_70653_5_);
+ if(event.isCanceled()) return;
+ p_70653_2_ = event.getStrength(); p_70653_3_ = event.getRatioX(); p_70653_5_ = event.getRatioZ();
if (this.field_70146_Z.nextDouble() >= this.func_110148_a(SharedMonsterAttributes.field_111266_c).func_111126_e())
{
this.field_70160_al = true;
@@ -1253,15 +1276,7 @@
BlockPos blockpos = new BlockPos(i, j, k);
IBlockState iblockstate = this.field_70170_p.func_180495_p(blockpos);
Block block = iblockstate.func_177230_c();
Expand All @@ -158,7 +168,7 @@
}
}

@@ -1287,6 +1299,9 @@
@@ -1287,6 +1302,9 @@

public void func_180430_e(float p_180430_1_, float p_180430_2_)
{
Expand All @@ -168,7 +178,7 @@
super.func_180430_e(p_180430_1_, p_180430_2_);
PotionEffect potioneffect = this.func_70660_b(MobEffects.field_76430_j);
float f = potioneffect == null ? 0.0F : (float)(potioneffect.func_76458_c() + 1);
@@ -1303,7 +1318,7 @@
@@ -1303,7 +1321,7 @@

if (iblockstate.func_185904_a() != Material.field_151579_a)
{
Expand All @@ -177,7 +187,7 @@
this.func_184185_a(soundtype.func_185842_g(), soundtype.func_185843_a() * 0.5F, soundtype.func_185847_b() * 0.75F);
}
}
@@ -1380,17 +1395,20 @@
@@ -1380,17 +1398,20 @@
{
if (!this.func_180431_b(p_70665_1_))
{
Expand All @@ -199,7 +209,7 @@
this.func_110149_m(this.func_110139_bj() - p_70665_2_);
}
}
@@ -1447,6 +1465,11 @@
@@ -1447,6 +1468,11 @@

public void func_184609_a(EnumHand p_184609_1_)
{
Expand All @@ -211,7 +221,7 @@
if (!this.field_82175_bq || this.field_110158_av >= this.func_82166_i() / 2 || this.field_110158_av < 0)
{
this.field_110158_av = -1;
@@ -1694,7 +1717,7 @@
@@ -1694,7 +1720,7 @@

if (!this.field_70170_p.func_184143_b(axisalignedbb1))
{
Expand All @@ -220,7 +230,7 @@
{
this.func_70634_a(d11, this.field_70163_u + 1.0D, d12);
return;
@@ -1702,14 +1725,14 @@
@@ -1702,14 +1728,14 @@

BlockPos blockpos = new BlockPos(d11, this.field_70163_u - 1.0D, d12);

Expand All @@ -237,15 +247,15 @@
{
d1 = d11;
d13 = this.field_70163_u + 2.0D;
@@ -1781,6 +1804,7 @@
@@ -1781,6 +1807,7 @@
}

this.field_70160_al = true;
+ net.minecraftforge.common.ForgeHooks.onLivingJump(this);
}

protected void func_70629_bd()
@@ -1874,7 +1898,8 @@
@@ -1874,7 +1901,8 @@

if (this.field_70122_E)
{
Expand All @@ -255,7 +265,7 @@
}

float f7 = 0.16277136F / (f6 * f6 * f6);
@@ -1894,7 +1919,8 @@
@@ -1894,7 +1922,8 @@

if (this.field_70122_E)
{
Expand All @@ -265,15 +275,15 @@
}

if (this.func_70617_f_())
@@ -2054,6 +2080,7 @@
@@ -2054,6 +2083,7 @@

public void func_70071_h_()
{
+ if (net.minecraftforge.common.ForgeHooks.onLivingUpdate(this)) return;
super.func_70071_h_();
this.func_184608_ct();

@@ -2096,7 +2123,9 @@
@@ -2096,7 +2126,9 @@

if (!ItemStack.func_77989_b(itemstack1, itemstack))
{
Expand All @@ -283,7 +293,7 @@

if (!itemstack.func_190926_b())
{
@@ -2575,6 +2604,40 @@
@@ -2575,6 +2607,40 @@
this.field_70752_e = true;
}

Expand Down Expand Up @@ -324,7 +334,7 @@
public abstract EnumHandSide func_184591_cq();

public boolean func_184587_cr()
@@ -2595,12 +2658,19 @@
@@ -2595,12 +2661,19 @@

if (itemstack == this.field_184627_bm)
{
Expand All @@ -345,7 +355,7 @@
{
this.func_71036_o();
}
@@ -2618,8 +2688,10 @@
@@ -2618,8 +2691,10 @@

if (!itemstack.func_190926_b() && !this.func_184587_cr())
{
Expand All @@ -357,7 +367,7 @@

if (!this.field_70170_p.field_72995_K)
{
@@ -2700,7 +2772,9 @@
@@ -2700,7 +2775,9 @@
if (!this.field_184627_bm.func_190926_b() && this.func_184587_cr())
{
this.func_184584_a(this.field_184627_bm, 16);
Expand All @@ -368,7 +378,7 @@
this.func_184602_cy();
}
}
@@ -2724,7 +2798,8 @@
@@ -2724,7 +2801,8 @@
{
if (!this.field_184627_bm.func_190926_b())
{
Expand All @@ -378,7 +388,7 @@
}

this.func_184602_cy();
@@ -2852,6 +2927,31 @@
@@ -2852,6 +2930,31 @@
return true;
}

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/net/minecraftforge/common/ForgeHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent;
import net.minecraftforge.event.entity.living.LivingFallEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.event.entity.living.LivingKnockBackEvent;
import net.minecraftforge.event.entity.living.LivingSetAttackTargetEvent;
import net.minecraftforge.event.entity.living.LootingLevelEvent;
import net.minecraftforge.event.entity.player.AnvilRepairEvent;
Expand Down Expand Up @@ -559,6 +560,13 @@ public static boolean onLivingAttack(EntityLivingBase entity, DamageSource src,
return !MinecraftForge.EVENT_BUS.post(new LivingAttackEvent(entity, src, amount));
}

public static LivingKnockBackEvent onLivingKnockBack(EntityLivingBase target, Entity attacker, float strength, double ratioX, double ratioZ)
{
LivingKnockBackEvent event = new LivingKnockBackEvent(target, attacker, strength, ratioX, ratioZ);
MinecraftForge.EVENT_BUS.post(event);
return event;
}

public static float onLivingHurt(EntityLivingBase entity, DamageSource src, float amount)
{
LivingHurtEvent event = new LivingHurtEvent(entity, src, amount);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Minecraft Forge
* Copyright (c) 2017.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

package net.minecraftforge.event.entity.living;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file needs a license header.


import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.monster.EntityMob;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.DamageSource;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.eventhandler.Cancelable;

/**
* LivingKnockBackEvent is fired when a living entity is about to be knocked back. <br>
* This event is fired whenever an Entity is knocked back in
* {@link EntityLivingBase#attackEntityFrom(DamageSource, float)},
* {@link EntityLivingBase#blockWithShield(EntityLivingBase)},
* {@link EntityMob#attackEntityAsMob(Entity)} and
* {@link EntityPlayer#attackTargetEntityWithCurrentItem(Entity)} <br>
* <br>
* This event is fired via {@link ForgeHooks#onLivingKnockBack(EntityLivingBase, Entity, float, double, double)}.<br>
* <br>
* {@link #attacker} contains the Entity that caused the knock back. <br>
* {@link #strength} contains the strength of the knock back. <br>
* {@link #ratioX} contains the x ratio of the knock back. <br>
* {@link #ratioZ} contains the z ratio of the knock back. <br>
* <br>
* This event is {@link Cancelable}.<br>
* If this event is canceled, the entity is not knocked back.<br>
* <br>
* This event does not have a result. {@link HasResult}<br>
*<br>
* This event is fired on the {@link MinecraftForge#EVENT_BUS}.
**/
@Cancelable
public class LivingKnockBackEvent extends LivingEvent
{
protected Entity attacker;
protected float strength;
protected double ratioX, ratioZ;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

protected final Entity attacker;
protected float strength;
protected double ratioX, ratioZ;
protected final float originalStrength;
protected final double originalRatioX, originalRatioZ;

public LivingKnockBackEvent(EntityLivingBase target, Entity attacker, float strength, double ratioX, double ratioZ)
{
    super(target);
    this.attacker = attacker;
    this.originalStrength = strength;
    this.originalRatioX = ratioX;
    this.originalRatioZ = ratioZ;
    this.strength = strength;
    this.ratioX = ratioX;
    this.ratioZ = ratioZ;
}

In case of multiple mods modifying the same event.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good suggestion from liach, please add the "original" values as final values, so mods using the events can have more information about modifications to the values.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed now.

protected final Entity originalAttacker;
protected final float originalStrength;
protected final double originalRatioX, originalRatioZ;

public LivingKnockBackEvent(EntityLivingBase target, Entity attacker, float strength, double ratioX, double ratioZ)
{
super(target);
this.attacker = this.originalAttacker = attacker;
this.strength = this.originalStrength = strength;
this.ratioX = this.originalRatioX = ratioX;
this.ratioZ = this.originalRatioZ = ratioZ;
}

public Entity getAttacker() {return this.attacker;}

public float getStrength() {return this.strength;}

public double getRatioX() {return this.ratioX;}

public double getRatioZ() {return this.ratioZ;}

public Entity getOriginalAttacker() {return this.originalAttacker;}

public float getOriginalStrength() {return this.originalStrength;}

public double getOriginalRatioX() {return this.originalRatioX;}

public double getOriginalRatioZ() {return this.originalRatioZ;}

public void setAttacker(Entity attacker) {this.attacker = attacker;}

public void setStrength(float strength) {this.strength = strength;}

public void setRatioX(double ratioX) {this.ratioX = ratioX;}

public void setRatioZ(double ratioZ) {this.ratioZ = ratioZ;}
}
30 changes: 30 additions & 0 deletions src/test/java/net/minecraftforge/debug/KnockBackHookTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package net.minecraftforge.debug;

import net.minecraft.entity.passive.EntityCow;
import net.minecraft.entity.passive.EntitySheep;
import net.minecraftforge.event.entity.living.LivingKnockBackEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

@Mod(modid = "kbhtest", name = "Knock Back Hook Test", version = "1.0")
@Mod.EventBusSubscriber
public class KnockBackHookTest
{
private static final boolean ENABLED = false;

@SubscribeEvent
public static void onKnockBack(LivingKnockBackEvent event)
{
if(ENABLED)
{
if(event.getEntityLiving() instanceof EntitySheep)
{
event.setStrength(0.2F);
}
else if(event.getEntityLiving() instanceof EntityCow)
{
event.setCanceled(true);
}
}
}
}