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);
}
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();
}
}
});
}
Aggregations