From 03c08b1dfa364c21a1118fdfeeea2f4cfa04481e Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Date: Thu, 29 Dec 2022 03:09:57 +0100 Subject: optimize RenderMobColoredEvent and ResetEntityHurtEvent --- .../renderer/MixinRendererLivingEntity.java | 60 ++++++++++++++++++++-- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/src/main/java/at/hannibal2/skyhanni/mixins/transformers/renderer/MixinRendererLivingEntity.java b/src/main/java/at/hannibal2/skyhanni/mixins/transformers/renderer/MixinRendererLivingEntity.java index 657636e87..5161d55da 100644 --- a/src/main/java/at/hannibal2/skyhanni/mixins/transformers/renderer/MixinRendererLivingEntity.java +++ b/src/main/java/at/hannibal2/skyhanni/mixins/transformers/renderer/MixinRendererLivingEntity.java @@ -13,23 +13,75 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import java.util.HashMap; +import java.util.UUID; + +//TODO make guava caches working @Mixin(RendererLivingEntity.class) public abstract class MixinRendererLivingEntity extends Render { + + private final HashMap lastColorCacheTime = new HashMap<>(); + private final HashMap cachedColor = new HashMap<>(); + + private final HashMap lastHurtCacheTime = new HashMap<>(); + private final HashMap cachedHurt = new HashMap<>(); + + // private final LoadingCache colorMultiplier; protected MixinRendererLivingEntity(RenderManager renderManager) { super(renderManager); + +// colorMultiplier = CacheBuilder.newBuilder() +//// .maximumSize(1000) +// .expireAfterWrite(2, TimeUnit.SECONDS) +// .build( +// new CacheLoader() { +// @Override +// public Integer load(@NotNull EntityLivingBase key) { +// RenderMobColoredEvent event = new RenderMobColoredEvent(key, 0); +// event.postAndCatch(); +// return event.getColor(); +// } +// } +// ); } @Inject(method = "getColorMultiplier", at = @At("HEAD"), cancellable = true) private void setColorMultiplier(T entity, float lightBrightness, float partialTickTime, CallbackInfoReturnable cir) { + UUID uuid = entity.getUniqueID(); + if (lastColorCacheTime.getOrDefault(uuid, 0L) + 1_000 > System.currentTimeMillis()) { + cir.setReturnValue(cachedColor.get(uuid)); + return; + } + RenderMobColoredEvent event = new RenderMobColoredEvent(entity, 0); event.postAndCatch(); - cir.setReturnValue(event.getColor()); + int color = event.getColor(); + cachedColor.put(uuid, color); + lastColorCacheTime.put(uuid, System.currentTimeMillis()); + cir.setReturnValue(color); + +// try { +// cir.setReturnValue(colorMultiplier.get(entity)); +// } catch (ExecutionException e) { +// throw new RuntimeException(e); +// } } @Redirect(method = "setBrightness", at = @At(value = "FIELD", target = "Lnet/minecraft/entity/EntityLivingBase;hurtTime:I", opcode = Opcodes.GETFIELD)) private int changeHurtTime(EntityLivingBase entity) { - ResetEntityHurtEvent event = new ResetEntityHurtEvent(entity, false); - event.postAndCatch(); - return event.getShouldReset() ? 0 : entity.hurtTime; + UUID uuid = entity.getUniqueID(); + + boolean shouldReset; + if (lastHurtCacheTime.getOrDefault(uuid, 0L) + 1_000 > System.currentTimeMillis()) { + shouldReset = cachedHurt.get(uuid); + } else { + ResetEntityHurtEvent event = new ResetEntityHurtEvent(entity, false); + event.postAndCatch(); + shouldReset = event.getShouldReset(); + cachedHurt.put(uuid, shouldReset); + lastHurtCacheTime.put(uuid, System.currentTimeMillis()); + } + + return shouldReset ? 0 : entity.hurtTime; } } \ No newline at end of file -- cgit