package Fever.Visual.ui.display.impl;
import Fever.Visual.FeverVisual;
import Fever.Visual.events.EventDisplay;
import Fever.Visual.functions.impl.render.HUD;
import Fever.Visual.ui.display.ElementRenderer;
import Fever.Visual.ui.styles.Style;
import Fever.Visual.utils.animations.Animation;
import Fever.Visual.utils.animations.Direction;
import Fever.Visual.utils.animations.impl.EaseBackIn;
import Fever.Visual.utils.client.ClientUtil;
import Fever.Visual.utils.drag.Dragging;
import Fever.Visual.utils.math.MathUtil;
import Fever.Visual.utils.math.StopWatch;
import Fever.Visual.utils.math.Vector4i;
import Fever.Visual.utils.render.ColorUtils;
import Fever.Visual.utils.render.DisplayUtils;
import Fever.Visual.utils.render.Scissor;
import Fever.Visual.utils.render.Stencil;
import Fever.Visual.utils.render.font.Fonts;
import Fever.Visual.utils.projections.ProjectionUtility;
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;
import net.minecraft.client.entity.player.AbstractClientPlayerEntity;
import net.minecraft.client.gui.AbstractGui;
import net.minecraft.client.gui.screen.ChatScreen;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.AirItem;
import net.minecraft.item.ItemStack;
import net.minecraft.scoreboard.Score;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.EntityRayTraceResult;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.vector.Vector4f;
import org.joml.Vector2d;
import org.lwjgl.opengl.GL11;
import java.awt.*;
import java.util.*;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
@FieldDefaults(level = AccessLevel.PRIVATE)
@RequiredArgsConstructor
public class TargetInfoRenderer implements ElementRenderer {
final StopWatch stopWatch = new StopWatch();
final Dragging drag;
LivingEntity entity = null;
boolean allow;
final Animation animation = new EaseBackIn(200, 1, 1);
float healthAnimation = 0.0f;
float absorptionAnimation = 0.0f;
private float health = 0.0f, health2 = 0.0f, abhealth = 0.0f;
private float posX, posY;
private float animatedX = 0;
private float animatedY = 0;
private float dragSpeed = 0.15f;
private float animationSpeed = 5.0f;
private final List<Particle> particles = new ArrayList<>();
private final Random random = new Random();
private float animatedHealth = 0.0f;
private float lastHurtPercent = 0.0f;
private ResourceLocation lastSkin = null;
private String lastTargetName = "";
private boolean lastWasPlayer = false;
private List<ItemStack> lastItemStacks = new ArrayList<>();
private float fastLerp(float current, float target, float speed) {
return current + (target - current) / (speed + 1);
}
private float fixHealth(float original) {
if (entity == null) return original;
Score score = mc.world.getScoreboard().getOrCreateScore(entity.getScoreboardName(), mc.world.getScoreboard().getObjectiveInDisplaySlot(2));
String header = mc.ingameGUI.getTabList().header == null ? " " : mc.ingameGUI.getTabList().header.getString().toLowerCase();
return (mc.getCurrentServerData() != null && mc.getCurrentServerData().serverIP.contains("funtime")
&& (header.contains("анархия") || header.contains("гриферский")) && entity instanceof PlayerEntity) ? score.getScorePoints() : original;
}
@Override
public void render(EventDisplay eventDisplay) {
HUD hud = FeverVisual.getInstance().getFunctionRegistry().getHud();
if (entity != null) {
if (entity instanceof AbstractClientPlayerEntity) {
lastSkin = ((AbstractClientPlayerEntity) entity).getLocationSkin();
lastWasPlayer = true;
} else {
lastSkin = null;
lastWasPlayer = false;
}
lastTargetName = entity.getName().getString();
lastHurtPercent = (entity.hurtTime - (entity.hurtTime != 0 ? mc.timer.renderPartialTicks : 0.0f)) / 10.0f;
lastItemStacks.clear();
lastItemStacks.add(entity.getHeldItemMainhand().copy());
lastItemStacks.add(entity.getHeldItemOffhand().copy());
entity.getArmorInventoryList().forEach(stack -> lastItemStacks.add(stack.copy()));
}
entity = getTarget(entity);
float rounding = 6;
boolean out = !allow || stopWatch.isReached(420L);
animation.setDuration(out ? 400 : 300);
animation.setDirection(out ? Direction.BACKWARDS : Direction.FORWARDS);
if (animation.getOutput() == 0.0f) {
entity = null;
}
float targetX = drag.getX();
float targetY = drag.getY();
animatedX += (targetX - animatedX) * dragSpeed;
animatedY += (targetY - animatedY) * dragSpeed;
if (hud != null && hud.d3.get() && entity != mc.player && entity != null) {
Vector2d vec = ProjectionUtility.project2D(entity.getPosX(), entity.getPosY() + entity.getHeight() / 2f, entity.getPosZ());
if (vec != null) {
posX = (float) vec.x + 172 / 1.5f / 3f + entity.getDistance(mc.player) / (entity.getWidth() * 12) + 30;
posY = (float) vec.y;
} else {
posX = animatedX;
posY = animatedY;
}
} else {
posX = animatedX;
posY = animatedY;
}
if (entity != null && !entity.isInvisible()) {
String name = entity.getName().getString();
float renderPosX = posX;
float renderPosY = posY;
float headSize = 34;
float spacing = 5;
float width = 172 / 1.5f;
float height = 59 / 1.5f;
drag.setWidth(width);
drag.setHeight(height);
float shrinking = 1.5f;
Score score = mc.world.getScoreboard().getOrCreateScore(entity.getScoreboardName(), mc.world.getScoreboard().getObjectiveInDisplaySlot(2));
float hp = entity.getHealth();
float maxHp = entity.getMaxHealth();
String header = mc.ingameGUI.getTabList().header == null ? " " : mc.ingameGUI.getTabList().header.getString().toLowerCase();
if (mc.getCurrentServerData() != null && mc.getCurrentServerData().serverIP.contains("funtime")
&& (header.contains("анархия") || header.contains("гриферский")) && entity instanceof PlayerEntity) {
hp = score.getScorePoints();
maxHp = 20;
}
float currentHealth = entity != null ? fixHealth(entity.getHealth()) : 0;
float maxHealth = entity != null ? entity.getMaxHealth() : 20.0f;
float absorption = entity != null ? entity.getAbsorptionAmount() : 0.0f;
if (mc.getCurrentServerData() != null && mc.getCurrentServerData().serverIP.contains("funtime") && entity != mc.player) {
absorption = 0;
}
health = fastLerp(health, MathHelper.clamp(currentHealth / maxHealth, 0, 1), animationSpeed);
health2 = fastLerp(health2, health, 2.0f);
abhealth = fastLerp(abhealth, MathHelper.clamp(absorption / maxHealth, 0, 1), animationSpeed);
animatedHealth = fastLerp(animatedHealth, currentHealth, animationSpeed);
healthAnimation = MathUtil.fast(healthAnimation, MathHelper.clamp(hp / maxHp, 0, 1), 5);
absorptionAnimation = MathUtil.fast(absorptionAnimation, MathHelper.clamp(entity.getAbsorptionAmount() / maxHp, 0, 1), 10);
if (mc.getCurrentServerData() != null && mc.getCurrentServerData().serverIP.contains("funtime")
&& (header.contains("анархия") || header.contains("гриферский")) && entity instanceof PlayerEntity) {
hp = score.getScorePoints();
maxHp = 20;
}
float animationValue = (float) animation.getOutput();
float halfAnimationValueRest = (1 - animationValue) / 2f;
float testX = renderPosX + (width * halfAnimationValueRest);
float testY = renderPosY + (height * halfAnimationValueRest);
float testW = width * animationValue;
float testH = height * animationValue;
int windowWidth = ClientUtil.calc(mc.getMainWindow().getScaledWidth());
GlStateManager.pushMatrix();
Style style = FeverVisual.getInstance().getStyleManager().getCurrentStyle();
sizeAnimation(renderPosX + (width / 2), renderPosY + (height / 2), animation.getOutput());
DisplayUtils.drawShadow(renderPosX, renderPosY, 1, 1, 11, Color.BLACK.getRGB());
String advancehealth = ".0";
if (hp / maxHp > 0.99) {
advancehealth = ".0";
} else if (hp / maxHp > 0.89) {
advancehealth = ".7";
} else if (hp / maxHp > 0.85) {
advancehealth = ".8";
} else if (hp / maxHp > 0.79) {
advancehealth = ".5";
} else if (hp / maxHp > 0.75) {
advancehealth = ".2";
} else if (hp / maxHp > 0.69) {
advancehealth = ".4";
} else if (hp / maxHp > 0.65) {
advancehealth = ".1";
} else if (hp / maxHp > 0.59) {
advancehealth = ".2";
} else if (hp / maxHp > 0.55) {
advancehealth = ".5";
} else if (hp / maxHp > 0.49) {
advancehealth = ".6";
} else if (hp / maxHp > 0.45) {
advancehealth = ".1";
} else if (hp / maxHp > 0.39) {
advancehealth = ".4";
} else if (hp / maxHp > 0.35) {
advancehealth = ".6";
} else if (hp / maxHp > 0.29) {
advancehealth = ".8";
} else if (hp / maxHp > 0.25) {
advancehealth = ".7";
} else if (hp / maxHp > 0.19) {
advancehealth = ".2";
} else if (hp / maxHp > 0.15) {
advancehealth = ".3";
} else if (hp / maxHp > 0.9) {
advancehealth = ".5";
} else if (hp / maxHp > 0.5) {
advancehealth = ".7";
} else if (hp / maxHp > 0.3) {
advancehealth = ".5";
} else {
advancehealth = ".0";
}
int backgroundColor, textColor, headBackgroundColor, healthBarBackground;
if (FeverVisual.getInstance().getFunctionRegistry().getWhiteRecolor().isState()) {
backgroundColor = Color.WHITE.getRGB();
textColor = Color.BLACK.getRGB();
headBackgroundColor = ColorUtils.rgba(240, 240, 240, 215);
healthBarBackground = ColorUtils.rgb(220, 220, 220);
} else {
backgroundColor = ColorUtils.rgba(21, 21, 21, 190);
textColor = Color.WHITE.getRGB();
headBackgroundColor = ColorUtils.rgba(21, 21, 21, 215);
healthBarBackground = ColorUtils.rgb(32, 32, 32);
}
if (HUD.hatrender1) {
drawStyledRect1(posX, posY, width, height, rounding, 255, backgroundColor);
}
drawStyledRect(posX, posY, width, height, rounding, 255, backgroundColor);
Stencil.initStencilToWrite();
DisplayUtils.drawRoundedRect(posX + 2.5f, posY + 2.5f, headSize, headSize, 6, headBackgroundColor);
Stencil.readStencilBuffer(1);
drawTargetHead(entity, posX + 2.5f, posY + 2.5f, headSize, headSize);
Stencil.uninitStencilBuffer();
Vector4i colors = new Vector4i(HUD.getColor(0, 1), HUD.getColor(90, 1), HUD.getColor(180, 1), HUD.getColor(270, 1));
Scissor.push();
Scissor.setFromComponentCoordinates(testX, testY, testW - 6, testH);
Fonts.sfui.drawText(eventDisplay.getMatrixStack(), entity.getName().getString(),
posX + 28.0f + spacing + spacing, posY + spacing + 1, textColor, 8);
float animatedHP = Math.round(animatedHealth * 10) / 10f;
if (animatedHP > 999) {
Fonts.sfMedium.drawText(eventDisplay.getMatrixStack(), "HP: " + "Неизвестно",
posX + 28.0f + spacing + spacing, posY + spacing + 1 + spacing + spacing, textColor, 7);
} else {
Fonts.sfMedium.drawText(eventDisplay.getMatrixStack(), "HP: " + animatedHP,
posX + 28.0f + spacing + spacing, posY + spacing + 1 + spacing + spacing, textColor, 7);
}
Scissor.unset();
Scissor.pop();
float barWidth = width - 44;
float hpBarX = posX + 30.0f + spacing + spacing;
float hpBarY = posY + height - spacing * 2 - 3;
float hpBarHeight = 8.3f;
int color1 = ColorUtils.interpolateColor(
new Color(139, 0, 0).getRGB(), new Color(0, 100, 0).getRGB(), currentHealth / maxHealth);
int color2 = ColorUtils.interpolateColor(
new Color(255, 0, 0).getRGB(), new Color(0, 255, 0).getRGB(), currentHealth / maxHealth);
Vector4f cornerRadius = new Vector4f(2.5f, 2.5f, 2.5f, 2.5f);
int color3 = ColorUtils.interpolateColor(
ColorUtils.getColor(139, 0, 0, 90), ColorUtils.getColor(0, 100, 0, 90), currentHealth / maxHealth);
int color4 = ColorUtils.interpolateColor(
ColorUtils.getColor(255, 0, 0, 90), ColorUtils.getColor(0, 255, 0, 90), currentHealth / maxHealth);
DisplayUtils.drawRoundedRect(hpBarX, hpBarY, barWidth, hpBarHeight, cornerRadius,
new Vector4i(color3, color3, color4, color4));
DisplayUtils.drawRoundedRect(hpBarX, hpBarY, barWidth * health, hpBarHeight, cornerRadius,
new Vector4i(color1, color1, color2, color2));
if (abhealth > 0.01f) {
int goldDark = new Color(184, 134, 11).getRGB();
int goldLight = new Color(255, 215, 0).getRGB();
float absorptionWidth = barWidth * abhealth;
DisplayUtils.drawRoundedRect(hpBarX, hpBarY, absorptionWidth, hpBarHeight, cornerRadius,
new Vector4i(goldDark, goldDark, goldLight, goldLight));
}
if (entity != null && entity.hurtTime > 0) {
spawnParticles(lastHurtPercent, hpBarX + barWidth * health, hpBarY + hpBarHeight / 2);
}
updateAndRenderParticles(1.0f);
drawItemStack(posX + 40.0F, posY - 14.5F, 15.0F);
GlStateManager.popMatrix();
}
}
private LivingEntity getTarget(LivingEntity currentTarget) {
RayTraceResult rayTraceResult = mc.objectMouseOver;
LivingEntity target = currentTarget;
if (rayTraceResult != null && rayTraceResult.getType() == RayTraceResult.Type.ENTITY) {
Entity entityHit = ((EntityRayTraceResult) rayTraceResult).getEntity();
if (entityHit instanceof LivingEntity) {
LivingEntity potentialTarget = (LivingEntity) entityHit;
if (potentialTarget.isInvisible()) {
allow = false;
return null;
}
double distanceToTarget = mc.player.getDistance(potentialTarget);
if (distanceToTarget <= 6.0) {
target = potentialTarget;
stopWatch.reset();
allow = true;
} else {
allow = false;
}
}
} else if (mc.currentScreen instanceof ChatScreen) {
target = mc.player;
stopWatch.reset();
allow = true;
} else if (stopWatch.isReached(10000)) {
target = null;
allow = false;
}
return target;
}
public void drawTargetHead(LivingEntity entity, float x, float y, float width, float height) {
if (entity != null && !entity.isInvisible()) {
EntityRenderer<? super LivingEntity> rendererManager = mc.getRenderManager().getRenderer(entity);
drawFace(rendererManager.getEntityTexture(entity), x, y, 8F, 8F, 8F, 8F, width, height, 64F, 64F, entity);
}
}
public static void sizeAnimation(double width, double height, double scale) {
GlStateManager.translated(width, height, 0);
GlStateManager.scaled(scale, scale, scale);
GlStateManager.translated(-width, -height, 0);
}
public void drawFace(ResourceLocation res, float d, float y, float u, float v, float uWidth, float vHeight,
float width, float height, float tileWidth, float tileHeight, LivingEntity target) {
GL11.glPushMatrix();
GL11.glEnable(GL11.GL_BLEND);
mc.getTextureManager().bindTexture(res);
float hurtPercent = (target.hurtTime - (target.hurtTime != 0 ? mc.timer.renderPartialTicks : 0.0f)) / 10.0f;
GL11.glColor4f(1, 1 - hurtPercent, 1 - hurtPercent, 1);
AbstractGui.drawScaledCustomSizeModalRect(d, y, u, v, uWidth, vHeight, width, height, tileWidth, tileHeight);
GL11.glColor4f(1, 1, 1, 1);
GL11.glPopMatrix();
}
private void drawStyledRect(float x, float y, float width, float height, float radius, int alpha, int color) {
DisplayUtils.drawRoundedRect(x, y, width, height, radius, color);
}
private void drawStyledRect1(float x, float y, float width, float height, float radius, int alpha, int color) {
DisplayUtils.drawRoundedRect(x, y, width, height, radius, color);
}
private void drawItemStack(float x, float y, float offset) {
List<ItemStack> armorStacks = new ArrayList<>();
entity.getArmorInventoryList().forEach(armorStacks::add);
armorStacks.removeIf(stack -> stack.getItem() instanceof AirItem);
Collections.reverse(armorStacks);
AtomicReference<Float> posX = new AtomicReference<>(x);
armorStacks.forEach(stack -> {
drawItemStack(stack, posX.getAndAccumulate(offset, Float::sum), y, true, true, 0.8F);
});
}
private void spawnParticles(float hurtPercent, float x, float y) {
int particleCount = Math.min((int) (5 * hurtPercent), 10);
if (particles.size() + particleCount > 30) return;
for (int i = 0; i < particleCount; i++) {
float vx = random.nextFloat() * 0.4f + 0.1f;
float vy = random.nextFloat() * 0.2f - 0.1f;
particles.add(new Particle(x, y, vx, vy, 2500));
}
}
private void updateAndRenderParticles(float alpha) {
GL11.glPushMatrix();
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
particles.removeIf(p -> p.lifetime <= 0);
for (Particle particle : particles) {
particle.update();
float size = 5f * (particle.lifetime / 2500f);
float particleAlpha = particle.lifetime / 2500f * alpha;
DisplayUtils.drawShadow(particle.x - size / 2, particle.y - size / 2 - 1, size, size + 3, 4,
ColorUtils.rgba(255, 100, 100, (int) (50 * particleAlpha)));
DisplayUtils.drawCircle(particle.x, particle.y + 1, size / 2,
ColorUtils.rgba(255, 150, 150, (int) (255 * particleAlpha)));
}
GL11.glDisable(GL11.GL_BLEND);
GL11.glPopMatrix();
}
private class Particle {
float x, y, vx, vy;
float lifetime;
Particle(float x, float y, float vx, float vy, float lifetime) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.lifetime = lifetime;
}
void update() {
x += vx * 0.7f;
y += vy * 0.7f;
lifetime -= 16.67f;
}
}
public static void drawItemStack(ItemStack stack, float x, float y, boolean withoutOverlay, boolean scale, float scaleValue) {
RenderSystem.pushMatrix();
RenderSystem.translatef(x, y, 0.0F);
if (scale) {
GL11.glScaled(scaleValue, scaleValue, scaleValue);
}
mc.getItemRenderer().renderItemAndEffectIntoGUI(stack, 0, 0);
if (withoutOverlay) {
mc.getItemRenderer().renderItemOverlays(mc.fontRenderer, stack, 0, 0);
}
RenderSystem.popMatrix();
}
}