<commit_before>package joshie.crafting.gui;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import joshie.crafting.CraftAPIRegistry;
import joshie.crafting.api.CraftingAPI;
import joshie.crafting.api.ICriteria;
import joshie.crafting.api.ITab;
import joshie.crafting.api.ITreeEditor;
import joshie.crafting.helpers.ClientHelper;
import joshie.crafting.json.Options;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;

public class GuiTreeEditor extends GuiBase {
    public static final GuiTreeEditor INSTANCE = new GuiTreeEditor();
    public String currentTabName;
    public ITab currentTab;

    private static class SortIndex implements Comparator {
        @Override
        public int compare(Object o1, Object o2) {
            ITab tab1 = ((ITab) o1);
            ITab tab2 = ((ITab) o2);
            if (tab1.getSortIndex() == tab2.getSortIndex()) {
                return tab1.getDisplayName().compareTo(tab2.getDisplayName());
            }

            return tab1.getSortIndex() < tab2.getSortIndex() ? 1 : -1;
        }
    }

    @Override
    public void initGui() {
        super.initGui();

        buttonList = new ArrayList(); //Recreate the button list, in order to reposition it
        int number = 0;
        int pos = y - 5;
        if (ClientHelper.canEdit()) {
            buttonList.add(new ButtonNewCriteria(pos));
            pos += 28;
            number++;
        }

        //Sort tabs alphabetically or by sort index
        ArrayList<ITab> tabs = new ArrayList(CraftAPIRegistry.tabs.values());
        Collections.sort(tabs, new SortIndex());

        for (ITab tab : tabs) {
            if (tab.isVisible() || ClientHelper.canEdit()) {
                if (number <= 8) {
                    buttonList.add(new ButtonTab(tab, 0, pos));
                } else buttonList.add(new ButtonTab(tab, res.getScaledWidth() - 25, pos));

                pos += 28;

                if (number == 8) {
                    pos = y - 5;
                }

                number++;
            }
        }

        if (currentTabName == null) {
            currentTabName = Options.defaultTab;
        }

        currentTab = CraftingAPI.registry.getTabFromName(currentTabName);
        if (currentTab == null) {
            for (ITab tab : CraftAPIRegistry.tabs.values()) {
                currentTab = tab;
                break;
            }
            
            if (currentTab == null) {
            	currentTab = CraftingAPI.registry.newTab(CraftAPIRegistry.getNextUnique()).setDisplayName("New Tab").setStack(new ItemStack(Items.book)).setVisibility(true);
            	currentTabName = currentTab.getUniqueName();
            	initGui();
            }
        }
        
        currentTabName = currentTab.getUniqueName();
    }

    @Override
    public void drawForeground() {
        for (ICriteria criteria : currentTab.getCriteria()) {
            if (criteria.getTreeEditor().isCriteriaVisible() || ClientHelper.canEdit()) {
                ITreeEditor editor = criteria.getTreeEditor();
                List<ICriteria> prereqs = criteria.getRequirements();
                for (ICriteria p : prereqs) {
                    int y1 = p.getTreeEditor().getY();
                    int y2 = editor.getY();
                    int x1 = p.getTreeEditor().getX();
                    int x2 = editor.getX();
                    
                    int width = 0;
                    int textWidth = mc.fontRenderer.getStringWidth(p.getDisplayName());
                    int iconWidth = 9 + (p.getRewards().size() * 12);
                    if (textWidth >= iconWidth) {
                        width = textWidth + 9;
                    } else width = iconWidth;     
                    
                    width -= 3;
                    
                    drawLine(offsetX + width + x1, y + 12 + y1 - 1, offsetX + 5 + x2, y + 12 + y2 - 1, 1, theme.connectLineColor1);
                    drawLine(offsetX + width + x1, y + 12 + y1 + 1, offsetX + 5 + x2, y + 12 + y2 + 1, 1, theme.connectLineColor2); //#636C69
                    drawLine(offsetX + width + x1, y + 12 + y1, offsetX + 5 + x2, y + 12 + y2, 1, theme.connectLineColor3);
                }
            }
        }

        GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT);<commit_msg>Force initGui if currentTab is null<commit_after>package joshie.crafting.gui;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import joshie.crafting.CraftAPIRegistry;
import joshie.crafting.api.CraftingAPI;
import joshie.crafting.api.ICriteria;
import joshie.crafting.api.ITab;
import joshie.crafting.api.ITreeEditor;
import joshie.crafting.helpers.ClientHelper;
import joshie.crafting.json.Options;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;

public class GuiTreeEditor extends GuiBase {
    public static final GuiTreeEditor INSTANCE = new GuiTreeEditor();
    public String currentTabName;
    public ITab currentTab;

    private static class SortIndex implements Comparator {
        @Override
        public int compare(Object o1, Object o2) {
            ITab tab1 = ((ITab) o1);
            ITab tab2 = ((ITab) o2);
            if (tab1.getSortIndex() == tab2.getSortIndex()) {
                return tab1.getDisplayName().compareTo(tab2.getDisplayName());
            }

            return tab1.getSortIndex() < tab2.getSortIndex() ? 1 : -1;
        }
    }

    @Override
    public void initGui() {
        super.initGui();

        buttonList = new ArrayList(); //Recreate the button list, in order to reposition it
        int number = 0;
        int pos = y - 5;
        if (ClientHelper.canEdit()) {
            buttonList.add(new ButtonNewCriteria(pos));
            pos += 28;
            number++;
        }

        //Sort tabs alphabetically or by sort index
        ArrayList<ITab> tabs = new ArrayList(CraftAPIRegistry.tabs.values());
        Collections.sort(tabs, new SortIndex());

        for (ITab tab : tabs) {
            if (tab.isVisible() || ClientHelper.canEdit()) {
                if (number <= 8) {
                    buttonList.add(new ButtonTab(tab, 0, pos));
                } else buttonList.add(new ButtonTab(tab, res.getScaledWidth() - 25, pos));

                pos += 28;

                if (number == 8) {
                    pos = y - 5;
                }

                number++;
            }
        }

        if (currentTabName == null) {
            currentTabName = Options.defaultTab;
        }

        currentTab = CraftingAPI.registry.getTabFromName(currentTabName);
        if (currentTab == null) {
            for (ITab tab : CraftAPIRegistry.tabs.values()) {
                currentTab = tab;
                break;
            }
            
            if (currentTab == null) {
            	currentTab = CraftingAPI.registry.newTab(CraftAPIRegistry.getNextUnique()).setDisplayName("New Tab").setStack(new ItemStack(Items.book)).setVisibility(true);
            	currentTabName = currentTab.getUniqueName();
            	initGui();
            }
        }
        
        currentTabName = currentTab.getUniqueName();
    }

    @Override
    public void drawForeground() {
        if (currentTab == null) initGui();
        for (ICriteria criteria : currentTab.getCriteria()) {
            if (criteria.getTreeEditor().isCriteriaVisible() || ClientHelper.canEdit()) {
                ITreeEditor editor = criteria.getTreeEditor();
                List<ICriteria> prereqs = criteria.getRequirements();
                for (ICriteria p : prereqs) {
                    int y1 = p.getTreeEditor().getY();
                    int y2 = editor.getY();
                    int x1 = p.getTreeEditor().getX();
                    int x2 = editor.getX();
                    
                    int width = 0;
                    int textWidth = mc.fontRenderer.getStringWidth(p.getDisplayName());
                    int iconWidth = 9 + (p.getRewards().size() * 12);
                    if (textWidth >= iconWidth) {
                        width = textWidth + 9;
                    } else width = iconWidth;     
                    
                    width -= 3;
                    
                    drawLine(offsetX + width + x1, y + 12 + y1 - 1, offsetX + 5 + x2, y + 12 + y2 - 1, 1, theme.connectLineColor1);
                    drawLine(offsetX + width + x1, y + 12 + y1 + 1, offsetX + 5 + x2, y + 12 + y2 + 1, 1, theme.connectLineColor2); //#636C69
                    drawLine(offsetX + width + x1, y + 12 + y1, offsetX + 5 + x2, y + 12 + y2, 1, theme.connectLineColor3);
                }
            }
        }

        GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT);
{'fraction_non_alphanumeric': 0.10923059792896114, 'fraction_numerical': 0.018706157443491817, 'mean_word_length': 1.7560601411475913, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, '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': '6434861', 'n_tokens_mistral': 2714, 'n_tokens_neox': 2553, 'n_words': 586}