package im.expensive.ui.display.impl;
import com.mojang.blaze3d.matrix.MatrixStack;
import im.expensive.Expensive;
import im.expensive.events.EventDisplay;
import im.expensive.ui.styles.Style;
import im.expensive.utils.math.Vector4i;
import im.expensive.utils.drag.Dragging;
import im.expensive.utils.render.ColorUtils;
import im.expensive.utils.render.DisplayUtils;
import im.expensive.utils.render.Scissor;
import im.expensive.utils.render.font.Fonts;
import im.expensive.utils.GaussianBlur;
import im.expensive.ui.display.ElementRenderer;
import im.expensive.functions.impl.render.HUD;
import im.expensive.functions.api.FunctionRegistry;
import dev.redstones.mediaplayerinfo.IMediaSession;
import dev.redstones.mediaplayerinfo.MediaPlayerInfo;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.DynamicTexture;
import net.minecraft.client.renderer.texture.NativeImage;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.vector.Vector4f;
import java.awt.image.BufferedImage;
import java.util.List;
@FieldDefaults(level = AccessLevel.PRIVATE)
@RequiredArgsConstructor
@SuppressWarnings("all")
public class MusicInformation implements ElementRenderer {
final Dragging dragging;
private final MediaPlayerInfo mediaPlayerInfo = MediaPlayerInfo.Instance;
private String currentTrackTitle = "";
private String previousTrackTitle = "";
private String artist = "";
private ResourceLocation trackIcon = new ResourceLocation("expensive/images/hud/music.jpg");
private DynamicTexture dynamicTexture = null;
private long posit;
private long durat;
boolean session1;
String getFormattedTime;
private long lastUpdate = System.currentTimeMillis();
private static final long UPDATE_INTERVAL = 1000;
private float textX = 0;
@Override
public void render(EventDisplay eventRender2D) {
MatrixStack ms = eventRender2D.getMatrixStack();
float x = dragging.getX();
float y = dragging.getY();
float width = 110;
float height = 30;
float lineW = width - 10 - 25;
if (System.currentTimeMillis() - lastUpdate >= UPDATE_INTERVAL) {
lastUpdate = System.currentTimeMillis();
updateTrackTitle();
}
Style style = Expensive.getInstance().getStyleManager().getCurrentStyle();
Vector4i vector4ii = new Vector4i(HUD.getColor(0), HUD.getColor(90), HUD.getColor(180), HUD.getColor(170));
FunctionRegistry moduleRegistry = Expensive.getInstance().getFunctionRegistry();
HUD interFace = moduleRegistry.getHud();
if (interFace.blur.get()) {
int megaradiun = 4;
GaussianBlur.startBlur();
DisplayUtils.drawRoundedRect(x, y, width, height , new Vector4f((float) megaradiun, (float) megaradiun, (float) megaradiun, (float) megaradiun), new Vector4i(ColorUtils.rgba(33, 32, 40, 130), ColorUtils.rgba(30, 29, 35, 155), ColorUtils.rgba(21, 21, 23, 130), ColorUtils.rgba(18, 18, 18, 130)));
GaussianBlur.endBlur(4,2);
DisplayUtils.drawRoundedRect(x, y, width, height, new Vector4f((float) megaradiun, (float) megaradiun, (float) megaradiun, (float) megaradiun), new Vector4i(ColorUtils.rgba(33, 32, 40, 130), ColorUtils.rgba(30, 29, 35, 155), ColorUtils.rgba(21, 21, 23, 130), ColorUtils.rgba(18, 18, 18, 130)));
} else {
int megaradiun = 4;
DisplayUtils.drawRoundedRect(x, y, width, height, new Vector4f((float) megaradiun, (float) megaradiun, (float) megaradiun, (float) megaradiun), new Vector4i(ColorUtils.rgba(33, 32, 40, 255), ColorUtils.rgba(30, 29, 35, 255), ColorUtils.rgba(21, 21, 23, 255), ColorUtils.rgba(18, 18, 18, 255)));
}
String displayText = session1 ? "Ожидаю" : currentTrackTitle;
Vector4i vector4i = new Vector4i(style.getFirstColor().getRGB(), style.getFirstColor().getRGB(), style.getSecondColor().getRGB(), style.getSecondColor().getRGB());
Scissor.push();
Scissor.setFromComponentCoordinates(x + 10 + 20, y, lineW, height);
Fonts.sfui.drawText(ms, displayText.substring(0, Math.min(displayText.length(), 20)).replace("Ë", "E").replace("ë", "ё"),
x + 10 + 20, y + 5, ColorUtils.rgba(210, 210, 210, 255), 6.5f, 0.1f);
Fonts.sfui.drawText(ms, session1 ? "" : artist, x + 10 + 20, y + 5 + 8.5f, ColorUtils.rgb(100, 100, 100), 5.5f, 0.1f);
Scissor.unset();
Scissor.pop();
DisplayUtils.drawRoundedRect(x + 10 + 20, y + 22.5f, lineW, 4, 1, ColorUtils.rgba(60, 60, 60, 255));
DisplayUtils.drawRoundedRect(x + 10 + 20, y + 22.5f, lineW * gettimepos(), 4, 2f, vector4i);
DisplayUtils.drawImage(trackIcon, x + 2.5f, y + 2.5f, height - 5, height - 5, -1);
dragging.setWidth(width);
dragging.setHeight(height);
}
public float gettimepos() {
return !session1 ? (float) posit / (float) durat : 0.0F;
}
private void updateTrackTitle() {
new Thread(() -> {
try {
List<IMediaSession> sessions = mediaPlayerInfo.getMediaSessions();
session1 = sessions.isEmpty();
if (!sessions.isEmpty()) {
IMediaSession currentSession = sessions.get(0);
String newTrackTitle = currentSession.getMedia().getTitle();
String newArtist = currentSession.getMedia().getArtist();
BufferedImage oblozka = currentSession.getMedia().getArtwork();
long position = currentSession.getMedia().getPosition();
long duration = currentSession.getMedia().getDuration();
synchronized (this) {
if (!newTrackTitle.equals(previousTrackTitle)) {
currentTrackTitle = newTrackTitle;
previousTrackTitle = newTrackTitle;
artist = newArtist;
if (oblozka != null) {
trackIcon = createTextureFromBufferedImage(oblozka);
} else {
trackIcon = new ResourceLocation("expensive/images/hud/music.jpg");
}
}
posit = position;
durat = duration;
long currentMinutes = (posit / 1000) / 60;
long currentSeconds = (posit / 1000) % 60;
long totalMinutes = (durat / 1000) / 60;
long totalSeconds = (durat / 1000) % 60;
getFormattedTime = String.format("%02d:%02d / %02d:%02d", currentMinutes, currentSeconds, totalMinutes, totalSeconds);
}
} else {
synchronized (this) {
previousTrackTitle = null;
currentTrackTitle = "null";
artist = "";
posit = 0;
durat = 0;
trackIcon = new ResourceLocation("expensive/images/hud/music.jpg");
}
}
} catch (Exception e) {
System.out.println("Ошибка в updateTrackTitle: " + e.getMessage());
}
}).start();
}
private ResourceLocation createTextureFromBufferedImage(BufferedImage image) {
try {
NativeImage nativeImage = new NativeImage(image.getWidth(), image.getHeight(), false);
for (int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
int color = image.getRGB(x, y);
int abgr = ((color & 0xFF) << 24) |
((color & 0xFF0000) >> 16) |
(color & 0xFF00) |
((color & 0xFF) << 16);
nativeImage.setPixelRGBA(x, y, abgr);
}
}
if (dynamicTexture != null) {
dynamicTexture.close();
}
dynamicTexture = new DynamicTexture(nativeImage);
String textureId = "track_icon_" + System.currentTimeMillis();
ResourceLocation resourceLocation = new ResourceLocation("expensive", textureId);
Minecraft.getInstance().getTextureManager().bindTexture(resourceLocation);
Minecraft.getInstance().getTextureManager().loadTexture(resourceLocation, dynamicTexture);
return resourceLocation;
} catch (Exception e) {
System.out.println("Ошибка при создании текстуры: " + e.getMessage());
return new ResourceLocation("expensive/images/hud/music.jpg");
}
}
public void close() {
if (dynamicTexture != null) {
dynamicTexture.close();
dynamicTexture = null;
}
}
}