Search in sources :

Example 1 with FontStorage

use of net.minecraft.client.font.FontStorage 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 2 with FontStorage

use of net.minecraft.client.font.FontStorage in project SpeedRunIGT by RedLime.

the class MinecraftClientMixin method onInit.

/**
 * Add import font system
 */
@Inject(method = "<init>", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/debug/DebugRenderer;<init>(Lnet/minecraft/client/MinecraftClient;)V", shift = At.Shift.BEFORE))
public void onInit(RunArgs args, CallbackInfo ci) {
    this.resourceManager.registerListener(new SinglePreparationResourceReloadListener<Map<Identifier, List<Font>>>() {

        @Override
        protected Map<Identifier, List<Font>> prepare(ResourceManager manager, Profiler profiler) {
            SpeedRunIGT.FONT_MAPS.clear();
            try {
                HashMap<Identifier, List<Font>> map = new HashMap<>();
                File[] fontFiles = SpeedRunIGT.FONT_PATH.toFile().listFiles();
                if (fontFiles == null)
                    return new HashMap<>();
                for (File file : Arrays.stream(fontFiles).filter(file -> file.getName().endsWith(".ttf")).collect(Collectors.toList())) {
                    File config = SpeedRunIGT.FONT_PATH.resolve(file.getName().substring(0, file.getName().length() - 4) + ".json").toFile();
                    if (config.exists()) {
                        FontUtils.addFont(map, file, config);
                    } else {
                        FontUtils.addFont(map, file, null);
                    }
                }
                return map;
            } catch (Throwable e) {
                return new HashMap<>();
            }
        }

        @Override
        protected void apply(Map<Identifier, List<Font>> loader, ResourceManager manager, Profiler profiler) {
            try {
                FontManagerAccessor fontManager = (FontManagerAccessor) ((MinecraftClientAccessor) MinecraftClient.getInstance()).getFontManager();
                for (Map.Entry<Identifier, List<Font>> listEntry : loader.entrySet()) {
                    FontStorage fontStorage = new FontStorage(fontManager.getTextureManager(), listEntry.getKey());
                    fontStorage.setFonts(listEntry.getValue());
                    fontManager.getFontStorages().put(listEntry.getKey(), fontStorage);
                }
                TimerDrawer.fontHeightMap.clear();
            } catch (Throwable e) {
                SpeedRunIGT.error("Error! failed import timer fonts!");
                e.printStackTrace();
            }
        }
    });
}
Also used : MinecraftClientAccessor(com.redlimerl.speedrunigt.mixins.access.MinecraftClientAccessor) HashMap(java.util.HashMap) ReloadableResourceManager(net.minecraft.resource.ReloadableResourceManager) ResourceManager(net.minecraft.resource.ResourceManager) Font(net.minecraft.client.font.Font) Identifier(net.minecraft.util.Identifier) Profiler(net.minecraft.util.profiler.Profiler) FontStorage(net.minecraft.client.font.FontStorage) FontManagerAccessor(com.redlimerl.speedrunigt.mixins.access.FontManagerAccessor) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) Inject(org.spongepowered.asm.mixin.injection.Inject)

Aggregations

FontStorage (net.minecraft.client.font.FontStorage)2 Identifier (net.minecraft.util.Identifier)2 GsonBuilder (com.google.gson.GsonBuilder)1 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 FontManagerAccessor (com.redlimerl.speedrunigt.mixins.access.FontManagerAccessor)1 MinecraftClientAccessor (com.redlimerl.speedrunigt.mixins.access.MinecraftClientAccessor)1 File (java.io.File)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Font (net.minecraft.client.font.Font)1 TextRenderer (net.minecraft.client.font.TextRenderer)1 ReloadableResourceManager (net.minecraft.resource.ReloadableResourceManager)1 ResourceManager (net.minecraft.resource.ResourceManager)1 Profiler (net.minecraft.util.profiler.Profiler)1 Inject (org.spongepowered.asm.mixin.injection.Inject)1