Skip to content

Commit

Permalink
dont pre-emptively calculate exposure
Browse files Browse the repository at this point in the history
  • Loading branch information
RacoonDog committed Apr 3, 2024
1 parent f563a55 commit 0288a09
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
import net.minecraft.entity.Entity;

public interface ExplosionCache {
float lithium_fabric$getCachedExposure();
Entity lithium_fabric$getCachedEntity();
void lithium_fabric$cacheExposure(Entity entity, float exposure);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ private void captureExplosion(Explosion explosion, Entity entity, CallbackInfoRe
*/
@Redirect(method = "calculateDamage", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/explosion/Explosion;getExposure(Lnet/minecraft/util/math/Vec3d;Lnet/minecraft/entity/Entity;)F"))
private float useCachedExposure(Vec3d source, Entity entity) {
if (explosion.lithium_fabric$getCachedEntity() == entity) {
return explosion.lithium_fabric$getCachedExposure();
} else {
return Explosion.getExposure(source, entity);
}
float exposure = Explosion.getExposure(source, entity);
explosion.lithium_fabric$cacheExposure(entity, exposure);
return exposure;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,9 @@
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.explosion.Explosion;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
* Optimizations for Explosions: Remove duplicate {@link Explosion#getExposure(Vec3d, Entity)} calls.
Expand All @@ -27,34 +19,13 @@ public abstract class ExplosionMixin implements ExplosionCache {
@Unique private Entity cachedEntity;

@Override
public float lithium_fabric$getCachedExposure() {
return this.cachedExposure;
}

@Override
public Entity lithium_fabric$getCachedEntity() {
return this.cachedEntity;
}

@Shadow
public static float getExposure(Vec3d source, Entity entity) {
throw new AssertionError();
}

/**
* Since {@link Explosion#getExposure(Vec3d, Entity)} may called either once or twice with the same parameters, we
* calculate the value pre-emptively and redirect the other calls.
* @author Crosby
*/
@SuppressWarnings("InvalidInjectorMethodSignature") // signature is valid
@Inject(method = "collectBlocksAndDamageEntities", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/explosion/ExplosionBehavior;shouldDamage(Lnet/minecraft/world/explosion/Explosion;Lnet/minecraft/entity/Entity;)Z"), locals = LocalCapture.CAPTURE_FAILEXCEPTION)
private void preCalculateExposure(CallbackInfo ci, Set<?> set, int i, float q, int k, int l, int r, int s, int t, int u, List<?> list, Vec3d vec3d, Iterator<?> var12, Entity entity) {
this.cachedExposure = getExposure(vec3d, entity);
public void lithium_fabric$cacheExposure(Entity entity, float exposure) {
this.cachedExposure = exposure;
this.cachedEntity = entity;
}

@Redirect(method = "collectBlocksAndDamageEntities", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/explosion/Explosion;getExposure(Lnet/minecraft/util/math/Vec3d;Lnet/minecraft/entity/Entity;)F"))
private float returnCachedExposure(Vec3d source, Entity entity) {
return this.cachedExposure;
return this.cachedEntity == entity ? this.cachedExposure : Explosion.getExposure(source, entity);
}
}

0 comments on commit 0288a09

Please sign in to comment.