diff options
author | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2022-12-29 03:09:57 +0100 |
---|---|---|
committer | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2022-12-29 03:09:57 +0100 |
commit | 03c08b1dfa364c21a1118fdfeeea2f4cfa04481e (patch) | |
tree | 79aa40c391de60bdeb206b57139b72eda8b6fe6f /src/main/java | |
parent | 2b330c3505f34b2b9ac7fed116237983eacc098d (diff) | |
download | skyhanni-03c08b1dfa364c21a1118fdfeeea2f4cfa04481e.tar.gz skyhanni-03c08b1dfa364c21a1118fdfeeea2f4cfa04481e.tar.bz2 skyhanni-03c08b1dfa364c21a1118fdfeeea2f4cfa04481e.zip |
optimize RenderMobColoredEvent and ResetEntityHurtEvent
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/mixins/transformers/renderer/MixinRendererLivingEntity.java | 60 |
1 files 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<T extends EntityLivingBase> extends Render<T> { + + private final HashMap<UUID, Long> lastColorCacheTime = new HashMap<>(); + private final HashMap<UUID, Integer> cachedColor = new HashMap<>(); + + private final HashMap<UUID, Long> lastHurtCacheTime = new HashMap<>(); + private final HashMap<UUID, Boolean> cachedHurt = new HashMap<>(); + + // private final LoadingCache<EntityLivingBase, Integer> colorMultiplier; protected MixinRendererLivingEntity(RenderManager renderManager) { super(renderManager); + +// colorMultiplier = CacheBuilder.newBuilder() +//// .maximumSize(1000) +// .expireAfterWrite(2, TimeUnit.SECONDS) +// .build( +// new CacheLoader<EntityLivingBase, Integer>() { +// @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<Integer> 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 |