Search in sources :

Example 26 with TextRenderer

use of net.minecraft.client.font.TextRenderer in project MiniMap by pl3xgaming.

the class Font method generateTextRenderer.

private static TextRenderer generateTextRenderer(Font font) {
    JsonObject data = JsonHelper.deserialize(new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create(), String.format(PROVIDER_JSON, font.name, font.height(), font.oversample), JsonObject.class);
    if (data == null) {
        MiniMap.LOG.error("Could not deserialize internal font!");
        return MiniMap.getClient().textRenderer;
    }
    List<net.minecraft.client.font.Font> list = Lists.newArrayList();
    JsonArray jsonArray = JsonHelper.getArray(data, "providers");
    for (int i = jsonArray.size() - 1; i >= 0; --i) {
        try {
            JsonObject jsonObject = JsonHelper.asObject(jsonArray.get(i), "providers[" + i + "]");
            net.minecraft.client.font.Font mcFont = FontType.byId(JsonHelper.getString(jsonObject, "type")).createLoader(jsonObject).load(MiniMap.getClient().getResourceManager());
            if (mcFont != null) {
                list.add(mcFont);
            }
            MiniMap.LOG.info("Loaded font " + font.name);
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
    }
    FontStorage storage = new FontStorage(MiniMap.getClient().getTextureManager(), new Identifier(MiniMap.MODID, "font/" + font.name + "_" + font.height()));
    storage.setFonts(list);
    return new TextRenderer(id -> storage);
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) JsonObject(com.google.gson.JsonObject) JsonArray(com.google.gson.JsonArray) Identifier(net.minecraft.util.Identifier) FontStorage(net.minecraft.client.font.FontStorage) TextRenderer(net.minecraft.client.font.TextRenderer)

Example 27 with TextRenderer

use of net.minecraft.client.font.TextRenderer in project Potions-HUD-Fabric by Samuel-Martineau.

the class InGameHudMixin method renderStatusEffectOverlay.

/**
 * @author Samuel-Martineau
 * @reason Prevents the default status effects overlay from being rendered
 */
@Overwrite()
public void renderStatusEffectOverlay(MatrixStack matrices) {
    MinecraftClient client = MinecraftClient.getInstance();
    InGameHud inGameHud = client.inGameHud;
    TextRenderer textRenderer = inGameHud.getTextRenderer();
    int spriteSize = 18;
    assert client.player != null;
    Collection<StatusEffectInstance> statusEffectsCollection = client.player.getStatusEffects();
    if (!statusEffectsCollection.isEmpty()) {
        RenderSystem.enableBlend();
        StatusEffectSpriteManager statusEffectSpriteManager = client.getStatusEffectSpriteManager();
        List<Runnable> statusEffectsRunnableList = Lists.newArrayListWithExpectedSize(statusEffectsCollection.size());
        RenderSystem.setShaderTexture(0, HandledScreen.BACKGROUND_TEXTURE);
        Iterator<StatusEffectInstance> statusEffectsIterator = Ordering.natural().reverse().sortedCopy(statusEffectsCollection).iterator();
        int i = 0;
        while (statusEffectsIterator.hasNext()) {
            int x = PotionsHUDMod.CONFIG_MANAGER.X_OFFSET;
            int y = spriteSize * i + PotionsHUDMod.CONFIG_MANAGER.Y_OFFSET;
            StatusEffectInstance statusEffectInstance = statusEffectsIterator.next();
            StatusEffect statusEffect = statusEffectInstance.getEffectType();
            if (statusEffectInstance.shouldShowIcon()) {
                Sprite statusEffectSprite = statusEffectSpriteManager.getSprite(statusEffect);
                statusEffectsRunnableList.add(() -> {
                    RenderSystem.setShaderTexture(0, statusEffectSprite.getAtlas().getId());
                    DrawableHelper.drawSprite(matrices, x, y, inGameHud.getZOffset(), spriteSize, spriteSize, statusEffectSprite);
                    int duration = statusEffectInstance.getDuration() / 20;
                    long mins = TimeUnit.SECONDS.toMinutes(duration);
                    long secs = duration - TimeUnit.MINUTES.toSeconds(mins);
                    String formattedDuration;
                    if (statusEffectInstance.isPermanent())
                        formattedDuration = "∞";
                    else if (mins == 0)
                        formattedDuration = secs + " sec";
                    else
                        formattedDuration = String.format("%d min, %d sec", mins, secs);
                    float textX = x + spriteSize + 3;
                    float textY = y + (spriteSize / 2f - textRenderer.fontHeight / 2.5f);
                    int color;
                    if (duration <= 5)
                        color = 0xFF5555;
                    else if (duration <= 15)
                        color = 0xFFAA00;
                    else if (duration <= 25)
                        color = 0xFFFF55;
                    else
                        color = 0xFFFFFF;
                    textRenderer.draw(matrices, formattedDuration, textX, textY, color);
                });
            }
            i++;
        }
        statusEffectsRunnableList.forEach(Runnable::run);
    }
}
Also used : Sprite(net.minecraft.client.texture.Sprite) StatusEffectInstance(net.minecraft.entity.effect.StatusEffectInstance) StatusEffectSpriteManager(net.minecraft.client.texture.StatusEffectSpriteManager) StatusEffect(net.minecraft.entity.effect.StatusEffect) InGameHud(net.minecraft.client.gui.hud.InGameHud) MinecraftClient(net.minecraft.client.MinecraftClient) TextRenderer(net.minecraft.client.font.TextRenderer) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 28 with TextRenderer

use of net.minecraft.client.font.TextRenderer in project Potions-HUD-Fabric by Samuel-Martineau.

the class HUD method render.

private void render() {
    final PlayerEntity player = client.player;
    final InGameHud inGameHud = client.inGameHud;
    final TextRenderer textRenderer = client.textRenderer;
    final MatrixStack matrixStack = new MatrixStack();
    if (player == null)
        return;
    Collection<StatusEffectInstance> statusEffects = player.getStatusEffects();
    final GameOptions gameOptions = ((MinecraftClientMixin) MinecraftClient.getInstance()).getGameOptions();
    if (!statusEffects.isEmpty() && !gameOptions.debugEnabled) {
        RenderSystem.enableBlend();
        StatusEffectSpriteManager statusEffectSpriteManager = client.getStatusEffectSpriteManager();
        List<Runnable> statusEffectsRunnables = Lists.newArrayListWithExpectedSize(statusEffects.size());
        final int spriteSize = 18;
        for (StatusEffectInstance statusEffectInstance : Ordering.natural().reverse().sortedCopy(statusEffects)) {
            StatusEffect statusEffect = statusEffectInstance.getEffectType();
            final int duration = statusEffectInstance.getDuration() / 20;
            final long mins = TimeUnit.SECONDS.toMinutes(duration);
            final long secs = duration - TimeUnit.MINUTES.toSeconds(mins);
            String formattedDuration;
            if (statusEffectInstance.isPermanent())
                formattedDuration = "∞";
            else if (mins == 0)
                formattedDuration = secs + " sec";
            else
                formattedDuration = String.format("%d min, %d sec", mins, secs);
            final int x = 3;
            final int y = spriteSize * statusEffectsRunnables.size() + 3;
            System.out.println("\n   \n---\n   ");
            Sprite sprite = statusEffectSpriteManager.getSprite(statusEffect);
            System.out.println(sprite);
            System.out.println(sprite.getAtlas().getId());
            System.out.println(x);
            System.out.println(y);
            System.out.println(inGameHud.getZOffset());
            statusEffectsRunnables.add(() -> {
                client.getTextureManager().bindTexture(sprite.getAtlas().getId());
                DrawableHelper.drawSprite(matrixStack, x, y, inGameHud.getZOffset(), 18, 18, sprite);
                final float textYOffset = spriteSize / 2f - textRenderer.fontHeight / 2.5f;
                int color;
                if (duration <= 5)
                    color = 0xFF5555;
                else if (duration <= 15)
                    color = 0xFFAA00;
                else if (duration <= 25)
                    color = 0xFFFF55;
                else
                    color = 0xFFFFFF;
                textRenderer.draw(matrixStack, formattedDuration, x + spriteSize + 3, y + textYOffset, color);
            });
        }
        statusEffectsRunnables.forEach(Runnable::run);
    }
}
Also used : MinecraftClientMixin(me.smartineau.mixin.MinecraftClientMixin) Sprite(net.minecraft.client.texture.Sprite) MatrixStack(net.minecraft.client.util.math.MatrixStack) StatusEffectInstance(net.minecraft.entity.effect.StatusEffectInstance) StatusEffectSpriteManager(net.minecraft.client.texture.StatusEffectSpriteManager) PlayerEntity(net.minecraft.entity.player.PlayerEntity) StatusEffect(net.minecraft.entity.effect.StatusEffect) InGameHud(net.minecraft.client.gui.hud.InGameHud) GameOptions(net.minecraft.client.option.GameOptions) TextRenderer(net.minecraft.client.font.TextRenderer)

Example 29 with TextRenderer

use of net.minecraft.client.font.TextRenderer in project jGui by ReplayMod.

the class StringUtils method splitStringInMultipleRows.

public static String[] splitStringInMultipleRows(String string, int maxWidth) {
    if (string == null)
        return new String[0];
    TextRenderer fontRenderer = MCVer.getFontRenderer();
    List<String> rows = new ArrayList<>();
    String remaining = string;
    while (remaining.length() > 0) {
        String[] split = remaining.split(" ");
        String b = "";
        for (String sp : split) {
            b += sp + " ";
            if (fontRenderer.getWidth(b.trim()) > maxWidth) {
                b = b.substring(0, b.trim().length() - (sp.length()));
                break;
            }
        }
        String trimmed = b.trim();
        rows.add(trimmed);
        try {
            remaining = remaining.substring(trimmed.length() + 1);
        } catch (Exception e) {
            break;
        }
    }
    return rows.toArray(new String[rows.size()]);
}
Also used : ArrayList(java.util.ArrayList) TextRenderer(net.minecraft.client.font.TextRenderer)

Example 30 with TextRenderer

use of net.minecraft.client.font.TextRenderer in project jGui by ReplayMod.

the class AbstractGuiProgressBar method draw.

@Override
public void draw(GuiRenderer renderer, ReadableDimension size, RenderInfo renderInfo) {
    super.draw(renderer, size, renderInfo);
    TextRenderer fontRenderer = MCVer.getFontRenderer();
    int width = size.getWidth();
    int height = size.getHeight();
    int barTotalWidth = width - 2 * BORDER;
    int barDoneWidth = (int) (barTotalWidth * progress);
    // Border
    renderer.drawRect(0, 0, width, height, ReadableColor.BLACK);
    // Background
    renderer.drawRect(BORDER, BORDER, barTotalWidth, height - 2 * BORDER, ReadableColor.WHITE);
    // Progress
    renderer.drawRect(BORDER, BORDER, barDoneWidth, height - 2 * BORDER, ReadableColor.GREY);
    String text = String.format(label, (int) (progress * 100));
    renderer.drawCenteredString(width / 2, size.getHeight() / 2 - fontRenderer.fontHeight / 2, ReadableColor.BLACK, text);
}
Also used : TextRenderer(net.minecraft.client.font.TextRenderer)

Aggregations

TextRenderer (net.minecraft.client.font.TextRenderer)45 Point (de.johni0702.minecraft.gui.utils.lwjgl.Point)9 ReadablePoint (de.johni0702.minecraft.gui.utils.lwjgl.ReadablePoint)9 MinecraftClient (net.minecraft.client.MinecraftClient)7 LiteralText (net.minecraft.text.LiteralText)7 Dimension (de.johni0702.minecraft.gui.utils.lwjgl.Dimension)6 ReadableDimension (de.johni0702.minecraft.gui.utils.lwjgl.ReadableDimension)6 TranslatableText (net.minecraft.text.TranslatableText)5 ReadableColor (de.johni0702.minecraft.gui.utils.lwjgl.ReadableColor)3 GameRenderer (net.minecraft.client.render.GameRenderer)3 MatrixStack (net.minecraft.client.util.math.MatrixStack)3 Text (net.minecraft.text.Text)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Environment (net.fabricmc.api.Environment)2 InGameHud (net.minecraft.client.gui.hud.InGameHud)2 Screen (net.minecraft.client.gui.screen.Screen)2 BufferBuilder (net.minecraft.client.render.BufferBuilder)2 I18n (net.minecraft.client.resource.language.I18n)2 Sprite (net.minecraft.client.texture.Sprite)2