Начинающий
- Статус
- Оффлайн
- Регистрация
- 30 Ноя 2025
- Сообщения
- 11
- Реакции
- 0
- Выберите загрузчик игры
- Fabric
1.Сам код MusicBar
2.Утилка для отображения музыки
Java:
[/B]
package sweetie.evaware.client.ui.widget.overlay;
import com.nimbusds.oauth2.sdk.id.Identifier;
import net.minecraft.client.texture.AbstractTexture;
import net.minecraft.client.util.math.MatrixStack;
import sweetie.evaware.api.utils.media.MediaUtils;
import sweetie.evaware.api.utils.render.RenderUtil;
import sweetie.evaware.api.utils.render.fonts.Font;
import sweetie.evaware.api.utils.render.fonts.Fonts;
import sweetie.evaware.client.features.modules.render.InterfaceModule;
import sweetie.evaware.client.ui.widget.Widget;
import java.awt.*;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class MusicBarWidget extends Widget {
private static final char WIFI_GLYPH = 'D';
private Font iconFont;
private float[] waveHeights;
private float[] waveTargets;
private long lastWaveUpdate = 0L;
public MusicBarWidget() {
super(0f, 8f);
}
@Override
public String getName() {
return "MusicBar";
}
@Override
public void render(MatrixStack matrixStack) {
if (!InterfaceModule.getInstance().watermarkElements.isEnabled("MusicBar")) {
return;
}
MediaUtils.MediaInfo mediaInfo = MediaUtils.getCurrentMedia();
if (mediaInfo == null) return;
String title = mediaInfo.title == null ? "" : mediaInfo.title;
String artist = mediaInfo.artist == null ? "" : mediaInfo.artist;
String label = artist.isEmpty() ? title : (title + " - " + artist);
if (label.isEmpty()) return;
AbstractTexture artTexture = mediaInfo.getTexture();
if (artTexture == null) return;
float screenWidth = mc.getWindow().getScaledWidth();
Font font = Fonts.SF_REGULAR;
float fontSize = scaled(8.0f);
float padding = scaled(7.0f);
float artSize = scaled(14.0f);
float radius = scaled(6.6f);
String timeStr = LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm"));
float timeWidth = font.getWidth(timeStr, fontSize);
float timeGap = scaled(7.0f);
int bars = 4;
float barWidth = scaled(3.0f);
float barGap = scaled(1.7f);
float waveBlockWidth = bars * barWidth + (bars - 1) * barGap + scaled(4.0f);
float maxTextWidth = scaled(170.0f);
String text = trimToWidth(font, label, fontSize, maxTextWidth);
float textWidth = font.getWidth(text, fontSize);
float wifiSize = scaled(9.0f);
Font wifiFont = getIconFont();
float wifiWidth = wifiFont != null ? wifiFont.getWidth(String.valueOf(WIFI_GLYPH), wifiSize) : 0.0f;
float wifiGap = scaled(8.0f);
float panelWidth = padding + artSize + scaled(7.0f) + textWidth + scaled(9.0f)
+ waveBlockWidth + padding - scaled(5.9f);
float height = scaled(19.6f) - scaled(2.1f);
float totalWidth = timeWidth + timeGap + panelWidth + wifiGap + wifiWidth;
float groupX = (screenWidth - totalWidth) / 2.0f;
float y = getDraggable().getY();
float barX = groupX + timeWidth + timeGap;
float centerY = y + height / 2.0f;
font.drawText(matrixStack, timeStr, groupX, centerY +1.0f - (fontSize / 2.0f) - scaled(0.9f), fontSize, new Color(0, 0, 0, 255));
RenderUtil.BLUR_RECT.draw(matrixStack, barX, y, panelWidth, height, radius, new Color(255, 255, 255, 200));
RenderUtil.RECT.draw(matrixStack, barX, y, panelWidth, height, radius, new Color(0, 0, 0, 120));
float currentX = barX + padding;
drawTexture(matrixStack, artTexture, currentX, centerY - artSize / 2.0f, artSize, artSize, scaled(5.5f));
currentX += artSize + scaled(3.0f);
font.drawText(matrixStack, text, currentX, centerY +1.1f - (fontSize / 2.0f) - scaled(1.0f), fontSize, new Color(255, 255, 255, 255));
currentX += textWidth + scaled(9.0f);
updateMusicWave(bars);
float barBaseY = centerY + scaled(5.0f);
for (int i = 0; i < bars; i++) {
float barHeight = waveHeights[i];
RenderUtil.RECT.draw(matrixStack,
currentX + i * (barWidth + barGap),
barBaseY - barHeight,
barWidth,
barHeight,
scaled(1.0f),
new Color(255, 255, 255, 255));
}
if (wifiFont != null) {
float wifiX = barX + panelWidth + wifiGap;
wifiFont.drawText(matrixStack, String.valueOf(WIFI_GLYPH), wifiX, centerY - (wifiSize / 2.0f) - scaled(1.0f), wifiSize,
new Color(0, 0, 0, 200));
}
getDraggable().setWidth(totalWidth);
getDraggable().setHeight(height);
getDraggable().setX(groupX);
}
private void drawTexture(MatrixStack matrixStack, AbstractTexture texture, float x, float y, float w, float h, float radius) {
RenderUtil.TEXTURE_RECT.draw(matrixStack, x, y, w, h, radius, new Color(255, 255, 255, 255), 0f, 0f, 1f, 1f, texture.getGlId());
}
private String trimToWidth(Font font, String text, float size, float maxWidth) {
if (font.getWidth(text, size) <= maxWidth) return text;
String trimmed = text;
while (trimmed.length() > 3 && font.getWidth(trimmed + "...", size) > maxWidth) {
trimmed = trimmed.substring(0, trimmed.length() - 1);
}
return trimmed.length() > 3 ? trimmed + "..." : trimmed;
}
private void updateMusicWave(int bars) {
if (waveHeights == null || waveHeights.length != bars) {
waveHeights = new float[bars];
waveTargets = new float[bars];
for (int i = 0; i < bars; i++) {
waveHeights[i] = scaled(5.0f);
waveTargets[i] = scaled(5.0f);
}
}
if (System.currentTimeMillis() - lastWaveUpdate > 150) {
lastWaveUpdate = System.currentTimeMillis();
for (int i = 0; i < bars; i++) {
waveTargets[i] = scaled(3.0f) + (float) (Math.random() * scaled(10.0f));
}
}
for (int i = 0; i < bars; i++) {
waveHeights[i] += (waveTargets[i] - waveHeights[i]) * 0.5f;
}
}
private Font getIconFont() {
if (iconFont != null) return iconFont;
if (mc.getResourceManager() == null) return null;
iconFont = Font.builder().find("dettex/iconf").load();
return iconFont;
}
}
[B]
Java:
[/B][/B]
package sweetie.evaware.api.utils.media;
import by.bonenaut7.mediatransport4j.api.MediaSession;
import by.bonenaut7.mediatransport4j.api.MediaTransport;
import net.minecraft.client.texture.AbstractTexture;
import net.minecraft.client.texture.NativeImage;
import net.minecraft.client.texture.NativeImageBackedTexture;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class MediaUtils {
private static boolean initialized = false;
private static final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
private static volatile MediaInfo mediaInfo = null;
private static final Map<String, AbstractTexture> textureCache = new ConcurrentHashMap<>();
private static String previousHash = "";
public static class MediaInfo {
public final String title;
public final String artist;
public final String textureHash;
public MediaInfo(String title, String artist, String textureHash) {
this.title = title;
this.artist = artist;
this.textureHash = textureHash;
}
public AbstractTexture getTexture() {
return textureCache.get(textureHash);
}
}
public static MediaInfo getCurrentMedia() {
if (!initialized) {
MediaTransport.init();
initialized = true;
scheduler.scheduleAtFixedRate(() -> {
try {
List<MediaSession> sessions = MediaTransport.getMediaSessions();
if (sessions != null && !sessions.isEmpty()) {
MediaSession session = sessions.get(0);
String hash = "";
if (session.hasThumbnail()) {
ByteBuffer buffer = session.getThumbnail();
hash = hashBuffer(buffer);
if (!hash.equals(previousHash)) {
AbstractTexture old = textureCache.remove(previousHash);
if (old != null) old.close();
AbstractTexture texture = convertTexture(buffer);
if (texture != null) {
textureCache.put(hash, texture);
}
previousHash = hash;
}
}
mediaInfo = new MediaInfo(session.getTitle(), session.getArtist(), hash);
} else {
clearCache();
mediaInfo = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}, 0, 50, TimeUnit.MILLISECONDS);
}
return mediaInfo;
}
private static AbstractTexture convertTexture(ByteBuffer buffer) {
try {
byte[] bytes = toByteArray(buffer);
BufferedImage img = ImageIO.read(new ByteArrayInputStream(bytes));
if (img != null) {
return convert(img);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static AbstractTexture convert(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
NativeImage img = new NativeImage(width, height, false);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
img.setColorArgb(x, y, image.getRGB(x, y));
}
}
return new NativeImageBackedTexture(img);
}
private static String hashBuffer(ByteBuffer buffer) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(toByteArray(buffer));
return toHex(md.digest());
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
private static byte[] toByteArray(ByteBuffer buffer) {
ByteBuffer duplicate = buffer.asReadOnlyBuffer();
duplicate.clear();
byte[] bytes = new byte[duplicate.remaining()];
duplicate.get(bytes);
return bytes;
}
private static void clearCache() {
textureCache.values().forEach(AbstractTexture::close);
textureCache.clear();
previousHash = "";
}
private static String toHex(byte[] bytes) {
StringBuilder sb = new StringBuilder(bytes.length * 2);
for (byte b : bytes) {
sb.append(Character.forDigit((b >> 4) & 0xF, 16));
sb.append(Character.forDigit((b & 0xF), 16));
}
return sb.toString();
}
}
[B]
Если что то понадобиться отпишите под этим постом скину ниже.
