Начинающий
Начинающий
- Статус
- Онлайн
- Регистрация
- 22 Июл 2025
- Сообщения
- 3
- Реакции
- 0
- Выберите загрузчик игры
- Fabric
Пожалуйста, авторизуйтесь для просмотра ссылки.
Код:
package ru.cultclient.implement.features.draggables;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.texture.NativeImage;
import net.minecraft.client.texture.NativeImageBackedTexture;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.Identifier;
import ru.cultclient.api.feature.draggable.AbstractDraggable;
import ru.cultclient.api.system.font.FontRenderer;
import ru.cultclient.api.system.font.Fonts;
import ru.cultclient.api.system.shape.ShapeProperties;
import ru.cultclient.common.QuickImports;
import ru.cultclient.common.util.color.ColorUtil;
import ru.cultclient.common.util.math.MathUtil;
import ru.cultclient.common.util.render.Render2DUtil;
import ru.cultclient.core.Main;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import static net.minecraft.client.texture.NativeImage.Format.RGBA;
public class ClientInfoHud extends AbstractDraggable {
private int fpsCount = 0;
private boolean centered = false;
private static Identifier textureIdentifier = Identifier.of("cultclient", "clientinfo_cult");
private static boolean textureRegistered = false;
private static boolean registrationAttempted = false;
public ClientInfoHud() {
super("Watermark2", 0, 0, 140, 32, true);
}
@Override
public void tick() {
if (!centered && mc != null && mc.getWindow() != null) {
int windowWidth = mc.getWindow().getScaledWidth();
int windowHeight = mc.getWindow().getScaledHeight();
if (windowWidth > 0 && windowHeight > 0) {
setX((windowWidth - getWidth()) / 2);
setY((windowHeight - getHeight()) / 2);
centered = true;
}
}
fpsCount = (int) MathUtil.interpolate(fpsCount, mc.getCurrentFps());
if (!textureRegistered && !registrationAttempted && mc != null && mc.getTextureManager() != null) {
registerTexture();
}
}
private void registerTexture() {
System.out.println("[ClientInfoHud] registerTexture() вызван. textureRegistered=" + textureRegistered +
", registrationAttempted=" + registrationAttempted);
if (textureRegistered || registrationAttempted || mc == null || mc.getTextureManager() == null) {
System.out.println("[ClientInfoHud] registerTexture() пропущен из-за проверки условий");
return;
}
registrationAttempted = true;
System.out.println("[ClientInfoHud] Начинаем загрузку изображения cult.png");
try (InputStream inputStream = Main.class.getResourceAsStream("cult.png")) {
if (inputStream == null) {
System.out.println("[ClientInfoHud] Ошибка: файл cult.png не найден в ресурсах (попытка 1)");
try (InputStream altStream = Main.class.getClassLoader().getResourceAsStream("cult.png")) {
if (altStream == null) {
System.out.println("[ClientInfoHud] Ошибка: файл cult.png не найден в ресурсах (попытка 2)");
return;
}
System.out.println("[ClientInfoHud] Поток найден через getClassLoader(), загружаем изображение...");
loadImageFromStream(altStream);
}
return;
}
System.out.println("[ClientInfoHud] Поток найден, загружаем изображение...");
loadImageFromStream(inputStream);
} catch (Exception e) {
System.out.println("[ClientInfoHud] Ошибка при загрузке изображения из ресурсов: " + e.getMessage());
e.printStackTrace();
}
}
private void loadImageFromStream(InputStream inputStream) {
try {
System.out.println("[ClientInfoHud] Поток открыт, загружаем изображение...");
BufferedImage bufferedImage = ImageIO.read(inputStream);
if (bufferedImage == null) {
System.out.println("[ClientInfoHud] Ошибка: ImageIO не смог прочитать изображение");
return;
}
int imageWidth = bufferedImage.getWidth();
int imageHeight = bufferedImage.getHeight();
if (imageWidth <= 0 || imageHeight <= 0) {
System.out.println("[ClientInfoHud] Ошибка: изображение пустое или имеет неверный размер");
return;
}
System.out.println("[ClientInfoHud] Изображение загружено: " + imageWidth + "x" + imageHeight);
NativeImage nativeImage = new NativeImage(RGBA, imageWidth, imageHeight, false);
for (int y = 0; y < imageHeight; y++) {
for (int x = 0; x < imageWidth; x++) {
int rgb = bufferedImage.getRGB(x, y);
int alpha = (rgb >> 24) & 0xFF;
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;
nativeImage.setColorArgb(x, y, net.minecraft.util.math.ColorHelper.getArgb(alpha, blue, green, red));
}
}
NativeImageBackedTexture texture = new NativeImageBackedTexture(nativeImage);
mc.execute(() -> {
try {
System.out.println("[ClientInfoHud] Регистрируем текстуру: " + textureIdentifier);
if (mc.getTextureManager().getTexture(textureIdentifier) != null) {
try {
mc.getTextureManager().destroyTexture(textureIdentifier);
System.out.println("[ClientInfoHud] Старая текстура удалена");
} catch (Exception ignored) {}
}
texture.upload();
if (RenderSystem.isOnRenderThread()) {
mc.getTextureManager().registerTexture(textureIdentifier, texture);
textureRegistered = true;
System.out.println("[ClientInfoHud] Текстура успешно зарегистрирована (на рендер-потоке)!");
} else {
RenderSystem.recordRenderCall(() -> {
mc.getTextureManager().registerTexture(textureIdentifier, texture);
textureRegistered = true;
System.out.println("[ClientInfoHud] Текстура успешно зарегистрирована (через recordRenderCall)!");
});
}
} catch (Exception e) {
System.out.println("[ClientInfoHud] Ошибка при регистрации текстуры: " + e.getMessage());
e.printStackTrace();
}
});
} catch (Exception e) {
System.out.println("[ClientInfoHud] Ошибка при чтении изображения из потока: " + e.getMessage());
e.printStackTrace();
}
}
private boolean isTextureLoaded() {
if (mc == null || mc.getTextureManager() == null) return false;
try {
var texture = mc.getTextureManager().getTexture(textureIdentifier);
return texture != null && textureRegistered;
} catch (Exception e) {
return false;
}
}
@Override
public void drawDraggable(DrawContext context) {
MatrixStack matrix = context.getMatrices();
FontRenderer font = Fonts.getSize(14);
blur.render(ShapeProperties.create(matrix, getX(), getY(), getWidth(), getHeight())
.round(5).softness(1).thickness(2)
.outlineColor(ColorUtil.getOutline())
.color(ColorUtil.getRect(0.7F))
.build());
float iconSize = 22;
float padding = 5;
float iconX = getX() + padding;
float iconY = getY() + (getHeight() / 2F) - (iconSize / 2F);
float maxIconSize = getHeight() - (padding * 2);
if (iconSize > maxIconSize) {
iconSize = maxIconSize;
iconY = getY() + (getHeight() / 2F) - (iconSize / 2F);
}
float iconRound = 5F;
if (isTextureLoaded()) {
blur.render(ShapeProperties.create(matrix, iconX, iconY, iconSize, iconSize)
.round(iconRound)
.color(ColorUtil.getRect(0.5F))
.build());
Render2DUtil.drawTexture(context, textureIdentifier, iconX, iconY, (int)iconSize);
} else {
blur.render(ShapeProperties.create(matrix, iconX, iconY, iconSize, iconSize)
.round(iconRound)
.color(ColorUtil.getRect(0.5F))
.build());
}
String clientName = Main.getInstance().getClientInfoProvider().clientName();
String clientNameText = "💕" + clientName;
float textX = iconX + iconSize + 6;
String fps = String.format("💻 %d FPS", fpsCount);
float lineHeight = font.getStringHeight("A") / 2F;
float spacing = 3;
float totalTextHeight = (lineHeight * 2) + spacing;
float textY = getY() + (getHeight() / 2F) - (totalTextHeight / 2F) + 1.5F;
float fpsY = textY + lineHeight + spacing;
font.drawString(matrix, clientNameText, textX, textY, ColorUtil.getText());
font.drawString(matrix, fps, textX, fpsY, ColorUtil.getText(0.9F));
float totalWidth = Math.max(
textX + font.getStringWidth(clientNameText) - getX() + padding,
textX + font.getStringWidth(fps) - getX() + padding
);
setWidth((int) totalWidth);
}
}