Skip to content

Commit

Permalink
feat(api/entity): add EntityEvent.Push when an entity is pushed
Browse files Browse the repository at this point in the history
  • Loading branch information
danorris709 committed Jul 21, 2023
1 parent 93a615e commit f183afc
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 0 deletions.
10 changes: 10 additions & 0 deletions patches/minecraft/net/minecraft/world/entity/Entity.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,16 @@
}

public void m_19920_(float p_19921_, Vec3 p_19922_) {
@@ -1440,7 +_,8 @@
}

public void m_5997_(double p_20286_, double p_20287_, double p_20288_) {
- this.m_20256_(this.m_20184_().m_82520_(p_20286_, p_20287_, p_20288_));
+ var motion = net.minecraftforge.common.ForgeHooks.onPush(this, p_20286_, p_20287_, p_20288_);
+ this.m_20256_(this.m_20184_().m_82520_(motion.m_7096_(), motion.m_7098_(), motion.m_7094_()));
this.f_19812_ = true;
}

@@ -1620,6 +_,8 @@
p_20241_.m_128379_("HasVisualFire", this.f_146813_);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,14 @@
if (!blockstate.m_280296_() && !DiodeBlock.m_52586_(blockstate)) {
return false;
}
@@ -170,6 +_,10 @@
}

public void m_5997_(double p_31744_, double p_31745_, double p_31746_) {
+ var motion = net.minecraftforge.common.ForgeHooks.onPush(this, p_31744_, p_31745_, p_31746_);
+ p_31744_ = motion.m_7096_();
+ p_31745_ = motion.m_7098_();
+ p_31746_ = motion.m_7094_();
if (!this.m_9236_().f_46443_ && !this.m_213877_() && p_31744_ * p_31744_ + p_31745_ * p_31745_ + p_31746_ * p_31746_ > 0.0D) {
this.m_6074_();
this.m_5553_((Entity)null);
7 changes: 7 additions & 0 deletions src/main/java/net/minecraftforge/common/ForgeHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,13 @@ public static void onLivingJump(LivingEntity entity)
MinecraftForge.EVENT_BUS.post(new LivingJumpEvent(entity));
}

public static Vec3 onPush(Entity entity, double motionX, double motionY, double motionZ)
{
var event = new EntityEvent.Push(entity, motionX, motionY, motionZ);
MinecraftForge.EVENT_BUS.post(event);
return event.getMotion();
}

@Nullable
public static ItemEntity onPlayerTossEvent(@NotNull Player player, @NotNull ItemStack item, boolean includeName)
{
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/net/minecraftforge/event/entity/EntityEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityDimensions;
import net.minecraft.world.entity.Pose;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.extensions.IForgeEntity;
import net.minecraftforge.eventbus.api.Cancelable;
Expand Down Expand Up @@ -226,4 +227,36 @@ public EyeHeight(Entity entity, Pose pose, EntityDimensions size, float eyeHeigh
public float getNewEyeHeight() { return newEyeHeight; }
public void setNewEyeHeight(float newEyeHeight) { this.newEyeHeight = newEyeHeight; }
}

/**
* This event is fired whenever {@link Entity#push(double, double, double)} gets called.<br>
* You can use this to modify the push motion. <br>
* Setting the motion on all axies to 0 is equivalent to cancelling the push altogether
* <br>
* This event is not {@link Cancelable}.<br>
* <br>
* This event does not have a result. {@link HasResult}
* <br>
* This event is fired on the {@link MinecraftForge#EVENT_BUS}.<br>
**/
public static class Push extends EntityEvent
{
private Vec3 motion;

public Push(Entity entity, double motionX, double motionY, double motionZ)
{
super(entity);
this.motion = new Vec3(motionX, motionY, motionZ);
}

public Vec3 getMotion()
{
return this.motion;
}

public void setMotion(Vec3 motion)
{
this.motion = motion;
}
}
}
46 changes: 46 additions & 0 deletions src/test/java/net/minecraftforge/debug/entity/PushEventTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.minecraftforge.debug.entity;

import net.minecraft.world.entity.animal.Cow;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.EntityEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Mod(PushEventTest.MODID)
public class PushEventTest
{
public static final boolean ENABLE = true;
public static final String MODID = "push_event_test";
private static Logger logger = LogManager.getLogger(MODID);

public PushEventTest()
{
if (ENABLE)
{
MinecraftForge.EVENT_BUS.register(this);
}
}

@SubscribeEvent
public void pushEntity(EntityEvent.Push event)
{
if (event.getEntity() instanceof Player)
{
event.setMotion(event.getMotion().scale(100));
}

if (event.getEntity() instanceof Cow)
{
event.setMotion(Vec3.ZERO);
}
}
}
2 changes: 2 additions & 0 deletions src/test/resources/META-INF/mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -305,5 +305,7 @@ modId="custom_fluid_container_test"
modId="alter_ground_event_test"
[[mods]]
modId="keymapping_test"
[[mods]]
modId="push_event_test"

# ADD ABOVE THIS LINE

0 comments on commit f183afc

Please sign in to comment.