@FieldDefaults(level = AccessLevel.PRIVATE)
@RequiredArgsConstructor
public class MediaplayerRenderer implements ElementRenderer {
final Minecraft mc = Minecraft.getInstance();
IMediaSession session = null;
final Dragging drag;
String title;
String artist;
int duration;
int position;
private final ExecutorService executorService = Executors.newSingleThreadExecutor();
private static final List<String> ownerPriority = List.of("SPOTIFY", "CHROME");
public float animationStep;
float ez;
private boolean isMediaPlayerInfoEnabled;
@Override
public void render(EventDisplay eventDisplay) {
if (Expensive.getInstance().getFunctionRegistry() != null) {
isMediaPlayerInfoEnabled = Expensive.getInstance().getFunctionRegistry().getHud().isState() && HUD.elements.getValueByName("Медиаплеер (мощные пк)").get();
}
MatrixStack ms = eventDisplay.getMatrixStack();
float posX = drag.getX();
float posY = drag.getY();
float headSize = 0;
float spacing = 5;
float width = 110;
float height = 59 / 2f;
drag.setWidth(width);
drag.setHeight(height);
GlStateManager.pushMatrix();
Style style = Expensive.getInstance().getStyleManager().getCurrentStyle();
drawStyledRect(posX, posY, width, height, 6, 255);
if (isMediaPlayerInfoEnabled) {
executorService.submit(() -> {
try {
List<IMediaSession> sessions = MediaPlayerInfo.Instance != null ? MediaPlayerInfo.Instance.getMediaSessions() : null;
if (sessions == null || sessions.isEmpty()) {
//throw new IllegalStateException("Не удалось получить сессии.");
}
sessions = sessions.stream()
.sorted((s1, s2) -> Integer.compare(getOwnerPriorityIndex(s1), getOwnerPriorityIndex(s2)))
.collect(Collectors.toList());
IMediaSession mediaSession = sessions.stream()
.filter(s -> s.getMedia() != null && (!s.getMedia().getArtist().isEmpty() || !s.getMedia().getTitle().isEmpty()))
.findFirst()
.orElse(null);
if (mediaSession != null && mediaSession.getMedia() != null) {
MediaInfo media = mediaSession.getMedia();
title = media.getTitle();
artist = media.getArtist();
duration = (int) media.getDuration();
position = (int) media.getPosition();
} else {
title = "No track";
artist = "Unknown artist";
duration = 0;
position = 0;
}
} catch (Throwable e) {
title = "loading...";
artist = "loading...";
duration = 0;
position = 0;
e.printStackTrace();
}
});
} else {
title = "Media Info Disabled";
artist = "N/A";
duration = 0;
position = 0;
}
String displayTitle = title != null ? (title.length() > 22 ? title.substring(0, 22) + "..." : title) : "No track";
String displayArtist = artist != null ? (artist.length() > 18 ? artist.substring(0, 18) + "..." : artist) : "Unknown artist";
Fonts.sfMedium2.drawText(eventDisplay.getMatrixStack(), displayTitle, posX + spacing + spacing, posY + spacing + 1, -1, 7);
Fonts.sfMedium2.drawText(eventDisplay.getMatrixStack(), displayArtist, posX + spacing + spacing, posY + spacing + spacing + spacing - 1, ColorUtils.rgb(200, 200, 200), 7);
if (duration > 0) {
float adjustedWidth = width + 20;
float progress = (float) position / duration;
ez = 0;
animationStep = MathUtil.fast(animationStep, progress, 6);
Vector4i vector4i = new Vector4i(style.getFirstColor().getRGB(), style.getFirstColor().getRGB(), style.getSecondColor().getRGB(), style.getSecondColor().getRGB());
Vector4i newvec = new Vector4i(ColorUtils.rgb(255,255,255),ColorUtils.rgb(255,255,255),ColorUtils.rgb(255,255,255),ColorUtils.rgb(255,255,255));
DisplayUtils.drawRoundedRect(posX + headSize + spacing + spacing, posY + height - spacing * 2 + 5, (adjustedWidth - 42), 1f, new Vector4f(2,2,2,2), ColorUtils.rgb(32, 32, 32));
DisplayUtils.drawRoundedRect(posX + headSize + spacing + spacing, posY + height - spacing * 2 + 5, (adjustedWidth - 42) * animationStep, 1f, new Vector4f(2,2,2,2), newvec);
}
GlStateManager.popMatrix();
}
private void drawStyledRect(float x, float y, float width, float height, float radius, int alpha) {
Style style = Expensive.getInstance().getStyleManager().getCurrentStyle();
DisplayUtils.drawRoundedRect(x, y, width + ez, height, radius, ColorUtils.rgba(21, 21, 21, alpha));
}
private int getOwnerPriorityIndex(IMediaSession session) {
for (int i = 0; i < ownerPriority.size(); i++) {
if (session.getOwner().toUpperCase().contains(ownerPriority.get(i))) {
return i;
}
}
return ownerPriority.size();
}
private String formatDuration(int duration) {
int minutes = duration / 60;
int seconds = duration % 60;
return String.format("%02d:%02d", minutes, seconds);
}
}