<commit_before>/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014 Ordinastie
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package net.malisis.core.renderer.font;

import java.awt.Font;
import java.lang.reflect.Field;

import net.malisis.core.MalisisCore;
import net.malisis.core.asm.AsmUtils;
import net.malisis.core.renderer.MalisisRenderer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.WorldRenderer;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.client.FMLClientHandler;

import org.apache.commons.lang3.StringUtils;

/**
 * @author Ordinastie
 *
 */
public class MinecraftFont extends MalisisFont
{
	private int[] mcCharWidth;
	private float[] optifineCharWidth;
	private byte[] glyphWidth;
	private ResourceLocation[] unicodePages;
	private ResourceLocation lastFontTexture;
	private FontRenderer fontRenderer;
	private MCCharData mcCharData = new MCCharData();
	private UnicodeCharData unicodeCharData = new UnicodeCharData();
	private MalisisRenderer renderer;

	public MinecraftFont()
	{
		super((Font) null);

		this.options = new FontGeneratorOptions();
		this.options.fontSize = 9F;
		this.textureRl = new ResourceLocation("textures/font/ascii.png");
		this.size = 256;

		fontRenderer = Minecraft.getMinecraft().fontRendererObj;
		setFields();
	}

	private void setFields()
	{
		String srg = "field_78286_d";
		if (FMLClientHandler.instance().hasOptifine())
			srg = "d";

		Field charWidthField = AsmUtils.changeFieldAccess(FontRenderer.class, "charWidth", srg);
		Field glyphWidthField = AsmUtils.changeFieldAccess(FontRenderer.class, "glyphWidth", "field_78287_e");
		Field unicodePagesField = AsmUtils.changeFieldAccess(FontRenderer.class, "unicodePageLocations", "field_111274_c");

		try
		{

			if (FMLClientHandler.instance().hasOptifine())
				optifineCharWidth = (float[]) charWidthField.get(fontRenderer);
			else
				mcCharWidth = (int[]) charWidthField.get(fontRenderer);
			glyphWidth = (byte[]) glyphWidthField.get(fontRenderer);
			unicodePages = (ResourceLocation[]) unicodePagesField.get(fontRenderer);

		}
		catch (IllegalArgumentException | IllegalAccessException e)
		{
			MalisisCore.log.error("[MinecraftFont] Failed to gets the FontRenderer fields :", e);
		}
	}

	private void bindFontTexture(CharData data)
	{
		ResourceLocation rl = textureRl;
		if (data instanceof UnicodeCharData)
		{
			int i = data.c / 256;
			if (unicodePages[i] == null)
				unicodePages[i] = new ResourceLocation(String.format("textures/font/unicode_page_%02x.png", i));
			rl = unicodePages[i];
		}
		if (rl != lastFontTexture)
		{
			renderer.next();
			Minecraft.getMinecraft().getTextureManager().bindTexture(rl);
			lastFontTexture = rl;
		}
	}

	@Override
	protected void prepare(MalisisRenderer renderer, float x, float y, float z, FontRenderOptions fro)
	{
		super.prepare(renderer, x, y, z, fro);
		this.renderer = renderer;
	}
<commit_msg>Added more explicit error messages<commit_after>/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014 Ordinastie
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package net.malisis.core.renderer.font;

import java.awt.Font;
import java.lang.reflect.Field;

import net.malisis.core.MalisisCore;
import net.malisis.core.asm.AsmUtils;
import net.malisis.core.renderer.MalisisRenderer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.WorldRenderer;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.client.FMLClientHandler;

import org.apache.commons.lang3.StringUtils;

/**
 * @author Ordinastie
 *
 */
public class MinecraftFont extends MalisisFont
{
	private int[] mcCharWidth;
	private float[] optifineCharWidth;
	private byte[] glyphWidth;
	private ResourceLocation[] unicodePages;
	private ResourceLocation lastFontTexture;
	private FontRenderer fontRenderer;
	private MCCharData mcCharData = new MCCharData();
	private UnicodeCharData unicodeCharData = new UnicodeCharData();
	private MalisisRenderer renderer;

	public MinecraftFont()
	{
		super((Font) null);

		this.options = new FontGeneratorOptions();
		this.options.fontSize = 9F;
		this.textureRl = new ResourceLocation("textures/font/ascii.png");
		this.size = 256;

		fontRenderer = Minecraft.getMinecraft().fontRendererObj;
		setFields();
	}

	private void setFields()
	{
		String srg = "field_78286_d";
		//		if (FMLClientHandler.instance().hasOptifine())
		//			srg = "d";

		Field charWidthField = AsmUtils.changeFieldAccess(FontRenderer.class, "charWidth", srg);
		Field glyphWidthField = AsmUtils.changeFieldAccess(FontRenderer.class, "glyphWidth", "field_78287_e");
		Field unicodePagesField = AsmUtils.changeFieldAccess(FontRenderer.class, "unicodePageLocations", "field_111274_c");

		try
		{
			if (charWidthField == null)
				throw new IllegalStateException("charWidthField (" + srg + ") is null");
			if (fontRenderer == null)
				throw new IllegalStateException("fontRenderer not initialized");

			if (FMLClientHandler.instance().hasOptifine() && charWidthField != null)
				optifineCharWidth = (float[]) charWidthField.get(fontRenderer);
			else
				mcCharWidth = (int[]) charWidthField.get(fontRenderer);
			glyphWidth = (byte[]) glyphWidthField.get(fontRenderer);
			unicodePages = (ResourceLocation[]) unicodePagesField.get(fontRenderer);

		}
		catch (IllegalStateException | IllegalArgumentException | IllegalAccessException e)
		{
			MalisisCore.log.error("[MinecraftFont] Failed to gets the FontRenderer fields :", e);
		}
	}

	private void bindFontTexture(CharData data)
	{
		ResourceLocation rl = textureRl;
		if (data instanceof UnicodeCharData)
		{
			int i = data.c / 256;
			if (unicodePages[i] == null)
				unicodePages[i] = new ResourceLocation(String.format("textures/font/unicode_page_%02x.png", i));
			rl = unicodePages[i];
		}
		if (rl != lastFontTexture)
		{
			renderer.next();
			Minecraft.getMinecraft().getTextureManager().bindTexture(rl);
			lastFontTexture = rl;
		}
	}

	@Override
	protected void prepare(MalisisRenderer renderer, float x, float y, float z, FontRenderOptions fro)
	{
		super.prepare(renderer, x, y, z, fro);
		this.renderer = renderer;
	}

{'fraction_non_alphanumeric': 0.09779630732578916, 'fraction_numerical': 0.00714711137581894, 'mean_word_length': 4.912676056338028, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2014109', 'n_tokens_mistral': 2707, 'n_tokens_neox': 2463, 'n_words': 736}