Skip to content

Commit

Permalink
Add back getAffectedBlocks to explosion events
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Oct 30, 2024
1 parent 7205d37 commit f4d009d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
22 changes: 20 additions & 2 deletions patches/net/minecraft/world/level/ServerExplosion.java.patch
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
--- a/net/minecraft/world/level/ServerExplosion.java
+++ b/net/minecraft/world/level/ServerExplosion.java
@@ -165,7 +_,7 @@
return new ObjectArrayList<>(set);
}

- private void hurtEntities() {
+ private void hurtEntities(List<BlockPos> blocks) {
float f = this.radius * 2.0F;
int i = Mth.floor(this.center.x - (double)f - 1.0);
int j = Mth.floor(this.center.x + (double)f + 1.0);
@@ -174,7 +_,9 @@
int i1 = Mth.floor(this.center.z - (double)f - 1.0);
int j1 = Mth.floor(this.center.z + (double)f + 1.0);

- for (Entity entity : this.level.getEntities(this.source, new AABB((double)i, (double)k, (double)i1, (double)j, (double)l, (double)j1))) {
+ List<Entity> list = this.level.getEntities(this.source, new AABB((double)i, (double)k, (double)i1, (double)j, (double)l, (double)j1));
+ net.neoforged.neoforge.event.EventHooks.onExplosionDetonate(this.level, this, list, f);
+ net.neoforged.neoforge.event.EventHooks.onExplosionDetonate(this.level, this, list, blocks);
+ for (Entity entity : list) {
if (!entity.ignoreExplosion(this)) {
double d0 = Math.sqrt(entity.distanceToSqr(this.center)) / (double)f;
Expand All @@ -15,7 +24,16 @@
d2 *= d6;
d3 *= d6;
Vec3 vec3 = new Vec3(d1, d2, d3);
+ vec3 = net.neoforged.neoforge.event.EventHooks.getExplosionKnockback(this.level, this, entity, vec3);
+ vec3 = net.neoforged.neoforge.event.EventHooks.getExplosionKnockback(this.level, this, entity, vec3, blocks);
entity.setDeltaMovement(entity.getDeltaMovement().add(vec3));
if (entity instanceof Player) {
Player player = (Player)entity;
@@ -246,7 +_,7 @@
public void explode() {
this.level.gameEvent(this.source, GameEvent.EXPLODE, this.center);
List<BlockPos> list = this.calculateExplodedPositions();
- this.hurtEntities();
+ this.hurtEntities(list);
if (this.interactsWithBlocks()) {
ProfilerFiller profilerfiller = Profiler.get();
profilerfiller.push("explosion_blocks");
8 changes: 4 additions & 4 deletions src/main/java/net/neoforged/neoforge/event/EventHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ public static boolean onExplosionStart(Level level, ServerExplosion explosion) {
return NeoForge.EVENT_BUS.post(new ExplosionEvent.Start(level, explosion)).isCanceled();
}

public static void onExplosionDetonate(Level level, ServerExplosion explosion, List<Entity> list, double diameter) {
public static void onExplosionDetonate(Level level, ServerExplosion explosion, List<Entity> entities, List<BlockPos> blocks) {
//Filter entities to only those who are effected, to prevent modders from seeing more then will be hurt.
/* Enable this if we get issues with modders looping to much.
Iterator<Entity> itr = list.iterator();
Expand All @@ -600,7 +600,7 @@ public static void onExplosionDetonate(Level level, ServerExplosion explosion, L
if (e.isImmuneToExplosions() || dist > 1.0F) itr.remove();
}
*/
NeoForge.EVENT_BUS.post(new ExplosionEvent.Detonate(level, explosion, list));
NeoForge.EVENT_BUS.post(new ExplosionEvent.Detonate(level, explosion, entities, blocks));
}

/**
Expand All @@ -613,8 +613,8 @@ public static void onExplosionDetonate(Level level, ServerExplosion explosion, L
* @param initialVelocity The explosion calculated velocity for the entity
* @return The new explosion velocity to add to the entity's existing velocity
*/
public static Vec3 getExplosionKnockback(Level level, ServerExplosion explosion, Entity entity, Vec3 initialVelocity) {
ExplosionKnockbackEvent event = new ExplosionKnockbackEvent(level, explosion, entity, initialVelocity);
public static Vec3 getExplosionKnockback(Level level, ServerExplosion explosion, Entity entity, Vec3 initialVelocity, List<BlockPos> blocks) {
ExplosionKnockbackEvent event = new ExplosionKnockbackEvent(level, explosion, entity, initialVelocity, blocks);
NeoForge.EVENT_BUS.post(event);
return event.getKnockbackVelocity();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package net.neoforged.neoforge.event.level;

import java.util.List;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.ServerExplosion;
Expand Down Expand Up @@ -63,17 +64,18 @@ public Start(Level level, ServerExplosion explosion) {
*/
public static class Detonate extends ExplosionEvent {
private final List<Entity> entityList;
private final List<BlockPos> blockList;

public Detonate(Level level, ServerExplosion explosion, List<Entity> entityList) {
public Detonate(Level level, ServerExplosion explosion, List<Entity> entityList, List<BlockPos> blockList) {
super(level, explosion);
this.entityList = entityList;
this.blockList = blockList;
}

/** return the list of blocks affected by the explosion. */
// FIXME porting: explosions changed a bit
/*public List<BlockPos> getAffectedBlocks() {
return getExplosion().getToBlow();
}*/
public List<BlockPos> getAffectedBlocks() {
return this.blockList;
}

/** return the list of entities affected by the explosion. */
public List<Entity> getAffectedEntities() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package net.neoforged.neoforge.event.level;

import java.util.List;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.ServerExplosion;
Expand All @@ -21,19 +23,20 @@
*/
public class ExplosionKnockbackEvent extends ExplosionEvent {
private final Entity entity;
private final List<BlockPos> blockList;
private Vec3 knockbackVelocity;

public ExplosionKnockbackEvent(Level level, ServerExplosion explosion, Entity entity, Vec3 knockbackVelocity) {
public ExplosionKnockbackEvent(Level level, ServerExplosion explosion, Entity entity, Vec3 knockbackVelocity, List<BlockPos> blockList) {
super(level, explosion);
this.entity = entity;
this.blockList = blockList;
this.knockbackVelocity = knockbackVelocity;
}

/** return the list of blocks affected by the explosion. */
// FIXME porting: explosions changed a bit
/*public List<BlockPos> getAffectedBlocks() {
return getExplosion().getToBlow();
}*/
public List<BlockPos> getAffectedBlocks() {
return this.blockList;
}

/** return the entity affected by the explosion knockback. */
public Entity getAffectedEntity() {
Expand Down

0 comments on commit f4d009d

Please sign in to comment.