Search in sources :

Example 1 with ComponentStack

use of com.teamwizardry.librarianlib.features.gui.components.ComponentStack in project Wizardry by TeamWizardry.

the class ComponentSpellRecipe method components.

public static List<PaginationContext> components(GuiBook book) {
    List<PaginationContext> contexts = Lists.newArrayList();
    if (book.getBookItemStack().isEmpty())
        return contexts;
    ItemStack bookStack = book.getBookItemStack();
    if (!NBTHelper.getBoolean(bookStack, "has_spell", false))
        return contexts;
    NBTTagList moduleList = NBTHelper.getList(bookStack, NBTConstants.NBT.SPELL, net.minecraftforge.common.util.Constants.NBT.TAG_STRING);
    if (moduleList == null)
        return contexts;
    List<List<ModuleInstance>> spellModules = SpellUtils.deserializeModuleList(moduleList);
    List<ItemStack> spellItems = SpellUtils.getSpellItems(spellModules);
    spellModules = SpellUtils.getEssentialModules(spellModules);
    FontRenderer fr = Minecraft.getMinecraft().fontRenderer;
    int widthOfSpace = fr.getStringWidth(" ");
    StringBuilder builder = new StringBuilder(LibrarianLib.PROXY.translate("wizardry.book.spell_recipe_structure") + "\n");
    for (List<ModuleInstance> spellModuleList : spellModules) {
        String margin = null;
        for (ModuleInstance module : spellModuleList) {
            if (margin == null) {
                margin = " - ";
                builder.append(margin).append(module.getReadableName()).append("\n");
            } else {
                int realLength = fr.getStringWidth(margin);
                int nbOfSpace = MathHelper.clamp(realLength / widthOfSpace, 0, 17);
                margin = StringUtils.repeat(" ", nbOfSpace) + "|_ ";
                builder.append(margin).append(module.getReadableName()).append("\n");
                if (nbOfSpace >= 16) {
                    builder.append("   ________________|").append("\n");
                    margin = "   ";
                }
            }
        }
    }
    String[] lines = builder.toString().split("\n");
    StringBuilder pageChunk = new StringBuilder();
    int count = 0;
    for (String line : lines) {
        pageChunk.append(line).append("\n");
        if (++count >= 16) {
            count = 0;
            pageFromString(book, contexts, pageChunk);
            pageChunk = new StringBuilder();
        }
    }
    if (count != 0)
        pageFromString(book, contexts, pageChunk);
    Consumer<ComponentVoid> applier = component -> {
    };
    for (int i = 0; i < spellItems.size(); i++) {
        ItemStack stack = spellItems.get(i);
        int index = i;
        applier = applier.andThen(component -> {
            ComponentStack componentStack = new ComponentStack((index % 4) * 32, (index / 4) * 16);
            componentStack.getStack().setValue(stack);
            component.add(componentStack);
            if (index != spellItems.size() - 1 && (index % 4) < 3) {
                ComponentSprite nextItem = new ComponentSprite(book.getHomeSprite(), 32 + (index % 4) * 32, (index / 4) * 16 + 13, 16, 8);
                nextItem.getColor().setValue(book.getBook().getHighlightColor());
                nextItem.getTransform().setRotate(Math.toRadians(180));
                component.add(nextItem);
            }
        });
        if ((index / 4) >= 9) {
            Consumer<ComponentVoid> spellApplier = applier;
            contexts.add(new PaginationContext(() -> {
                ComponentVoid component = new ComponentVoid(16, 16, book.getMainBookComponent().getSize().getXi() - 32, book.getMainBookComponent().getSize().getYi() - 32);
                spellApplier.accept(component);
                return component;
            }));
            applier = component -> {
            };
        }
    }
    Consumer<ComponentVoid> spellApplier = applier;
    contexts.add(new PaginationContext(() -> {
        ComponentVoid component = new ComponentVoid(16, 16, book.getMainBookComponent().getSize().getXi() - 32, book.getMainBookComponent().getSize().getYi() - 32);
        spellApplier.accept(component);
        return component;
    }));
    return contexts;
}
Also used : ComponentStack(com.teamwizardry.librarianlib.features.gui.components.ComponentStack) IBookGui(com.teamwizardry.librarianlib.features.gui.provided.book.IBookGui) LibrarianLib(com.teamwizardry.librarianlib.core.LibrarianLib) StringUtils(org.apache.commons.lang3.StringUtils) ComponentVoid(com.teamwizardry.librarianlib.features.gui.components.ComponentVoid) ItemStack(net.minecraft.item.ItemStack) NBTTagList(net.minecraft.nbt.NBTTagList) Lists(com.google.common.collect.Lists) Minecraft(net.minecraft.client.Minecraft) SpellUtils(com.teamwizardry.wizardry.api.spell.SpellUtils) ComponentSprite(com.teamwizardry.librarianlib.features.gui.components.ComponentSprite) PaginationContext(com.teamwizardry.librarianlib.features.gui.provided.book.context.PaginationContext) ModuleInstance(com.teamwizardry.wizardry.api.spell.module.ModuleInstance) Consumer(java.util.function.Consumer) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Bookmark(com.teamwizardry.librarianlib.features.gui.provided.book.context.Bookmark) FontRenderer(net.minecraft.client.gui.FontRenderer) Book(com.teamwizardry.librarianlib.features.gui.provided.book.hierarchy.book.Book) IBookElement(com.teamwizardry.librarianlib.features.gui.provided.book.hierarchy.IBookElement) MathHelper(net.minecraft.util.math.MathHelper) NBTHelper(com.teamwizardry.librarianlib.features.helpers.NBTHelper) ComponentText(com.teamwizardry.librarianlib.features.gui.components.ComponentText) NBTConstants(com.teamwizardry.wizardry.api.NBTConstants) NotNull(org.jetbrains.annotations.NotNull) ComponentStack(com.teamwizardry.librarianlib.features.gui.components.ComponentStack) ComponentSprite(com.teamwizardry.librarianlib.features.gui.components.ComponentSprite) PaginationContext(com.teamwizardry.librarianlib.features.gui.provided.book.context.PaginationContext) ComponentVoid(com.teamwizardry.librarianlib.features.gui.components.ComponentVoid) NBTTagList(net.minecraft.nbt.NBTTagList) NBTTagList(net.minecraft.nbt.NBTTagList) List(java.util.List) FontRenderer(net.minecraft.client.gui.FontRenderer) ItemStack(net.minecraft.item.ItemStack) ModuleInstance(com.teamwizardry.wizardry.api.spell.module.ModuleInstance)

Aggregations

Lists (com.google.common.collect.Lists)1 LibrarianLib (com.teamwizardry.librarianlib.core.LibrarianLib)1 ComponentSprite (com.teamwizardry.librarianlib.features.gui.components.ComponentSprite)1 ComponentStack (com.teamwizardry.librarianlib.features.gui.components.ComponentStack)1 ComponentText (com.teamwizardry.librarianlib.features.gui.components.ComponentText)1 ComponentVoid (com.teamwizardry.librarianlib.features.gui.components.ComponentVoid)1 IBookGui (com.teamwizardry.librarianlib.features.gui.provided.book.IBookGui)1 Bookmark (com.teamwizardry.librarianlib.features.gui.provided.book.context.Bookmark)1 PaginationContext (com.teamwizardry.librarianlib.features.gui.provided.book.context.PaginationContext)1 IBookElement (com.teamwizardry.librarianlib.features.gui.provided.book.hierarchy.IBookElement)1 Book (com.teamwizardry.librarianlib.features.gui.provided.book.hierarchy.book.Book)1 NBTHelper (com.teamwizardry.librarianlib.features.helpers.NBTHelper)1 NBTConstants (com.teamwizardry.wizardry.api.NBTConstants)1 SpellUtils (com.teamwizardry.wizardry.api.spell.SpellUtils)1 ModuleInstance (com.teamwizardry.wizardry.api.spell.module.ModuleInstance)1 List (java.util.List)1 Consumer (java.util.function.Consumer)1 Minecraft (net.minecraft.client.Minecraft)1 FontRenderer (net.minecraft.client.gui.FontRenderer)1 ItemStack (net.minecraft.item.ItemStack)1