diff options
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/mixins')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/mixins/hooks/RenderLivingEntityHelper.kt | 63 | ||||
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/mixins/transformers/renderer/MixinRendererLivingEntity.java | 60 |
2 files changed, 66 insertions, 57 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/mixins/hooks/RenderLivingEntityHelper.kt b/src/main/java/at/hannibal2/skyhanni/mixins/hooks/RenderLivingEntityHelper.kt new file mode 100644 index 000000000..371f97a68 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/mixins/hooks/RenderLivingEntityHelper.kt @@ -0,0 +1,63 @@ +package at.hannibal2.skyhanni.mixins.hooks + +import at.hannibal2.skyhanni.events.RenderMobColoredEvent +import at.hannibal2.skyhanni.events.ResetEntityHurtEvent +import net.minecraft.entity.EntityLivingBase +import net.minecraftforge.event.world.WorldEvent +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent + +class RenderLivingEntityHelper { + + @SubscribeEvent + fun onWorldChange(event: WorldEvent.Load) { + entityColorMap.clear() + entityNoHurTime.clear() + } + + companion object { + private val entityColorMap = mutableMapOf<EntityLivingBase, Int>() + private val entityNoHurTime = mutableListOf<EntityLivingBase>() + + fun <T : EntityLivingBase> setEntityColor(entity: T, color: Int) { + entityColorMap[entity] = color + } + + fun <T : EntityLivingBase> setNoHurtTime(entity: T) { + entityNoHurTime.add(entity) + } + + fun <T : EntityLivingBase> setColorMultiplier(entity: T, ): Int { + if (entityColorMap.containsKey(entity)) { + return entityColorMap[entity]!! + } + + //TODO remove event + val event = RenderMobColoredEvent(entity, 0) + event.postAndCatch() + val color = event.color + if (color != 0) { + //TODO remove? + entityColorMap[entity] = color + } + + return color + } + + fun <T : EntityLivingBase> changeHurtTime(entity: T): Int { + if (entityNoHurTime.contains(entity)) { + return 0 + } + + //TODO remove event + val event = ResetEntityHurtEvent(entity, false) + event.postAndCatch() + val shouldReset = event.shouldReset + + if (shouldReset) { + //TODO remove? + entityNoHurTime.add(entity) + } + return if (shouldReset) 0 else entity.hurtTime + } + } +}
\ No newline at end of file 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 5161d55da..2ae268a63 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 @@ -1,7 +1,6 @@ package at.hannibal2.skyhanni.mixins.transformers.renderer; -import at.hannibal2.skyhanni.events.RenderMobColoredEvent; -import at.hannibal2.skyhanni.events.ResetEntityHurtEvent; +import at.hannibal2.skyhanni.mixins.hooks.RenderLivingEntityHelper; import net.minecraft.client.renderer.entity.Render; import net.minecraft.client.renderer.entity.RenderManager; import net.minecraft.client.renderer.entity.RendererLivingEntity; @@ -13,75 +12,22 @@ 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(); - 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); -// } + cir.setReturnValue(RenderLivingEntityHelper.Companion.setColorMultiplier(entity)); } @Redirect(method = "setBrightness", at = @At(value = "FIELD", target = "Lnet/minecraft/entity/EntityLivingBase;hurtTime:I", opcode = Opcodes.GETFIELD)) private int changeHurtTime(EntityLivingBase entity) { - 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 RenderLivingEntityHelper.Companion.changeHurtTime(entity); - return shouldReset ? 0 : entity.hurtTime; } }
\ No newline at end of file |