aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/cowtipper/cowlection/config/gui/MooConfigPreview.java
blob: 32fb6d9cba881b8056c984567a795d625bdecd25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package de.cowtipper.cowlection.config.gui;

import de.cowtipper.cowlection.util.MooChatComponent;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.GuiUtilRenderComponents;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.*;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.IChatComponent;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.client.config.GuiUtils;
import org.apache.commons.lang3.text.WordUtils;

import java.util.HashMap;
import java.util.Map;

public class MooConfigPreview {
    private final Type type;
    private ItemStack[] items;
    private IChatComponent chatComponent;
    private Runnable previewRunnable;
    private long nextPreviewAt;
    public static ItemStack hoveredItem;
    public static IChatComponent hoveredChatComponent;
    public static MooConfigGui parent;

    public MooConfigPreview(IChatComponent chatComponent) {
        this.type = Type.CHAT;
        this.chatComponent = chatComponent;
    }

    public MooConfigPreview(ItemStack... items) {
        this.type = Type.ITEM;
        this.items = items;
    }

    public MooConfigPreview(String sound, float volume, float pitch) {
        this.type = Type.RUNNABLE_ON_HOVER;
        previewRunnable = () -> {
            if (Minecraft.getMinecraft().thePlayer != null) {
                Minecraft.getMinecraft().thePlayer.playSound(sound, volume, pitch);
            }
        };
    }

    public void drawPreview(int x, int y, int mouseX, int mouseY, boolean enablePreview) {
        switch (type) {
            case ITEM:
                drawItemsPreview(x, y, mouseX, mouseY, enablePreview);
                break;
            case CHAT:
                drawChatPreview(x, y, mouseX, mouseY, enablePreview);
                break;
            case RUNNABLE_ON_HOVER:
                drawButtonWithRunnablePreview(x, y, mouseX, mouseY, enablePreview);
                break;
            default:
                // do nothing
                break;
        }
    }

    public static void drawPreviewHover(int mouseX, int mouseY) {
        if (hoveredItem != null) {
            // draw preview item tool tip
            parent.renderToolTip(hoveredItem, mouseX, mouseY);
            hoveredItem = null;
        } else if (hoveredChatComponent != null) {
            // draw hover event of hovered chat component
            parent.handleComponentHover(hoveredChatComponent, mouseX, mouseY);
            hoveredChatComponent = null;
        }
    }

    private void drawItemsPreview(int x, int yFakeHotbar, int mouseX, int mouseY, boolean enablePreview) {
        int xFakeHotbar = x + 15;
        GlStateManager.pushMatrix();
        GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
        Minecraft.getMinecraft().getTextureManager().bindTexture(new ResourceLocation("textures/gui/container/inventory.png"));

        GuiUtils.drawTexturedModalRect(xFakeHotbar, yFakeHotbar, 87, 25, 18 * items.length, 18, 0);

        GlStateManager.enableRescaleNormal();
        GlStateManager.enableBlend();
        GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0);
        RenderHelper.enableGUIStandardItemLighting();

        int xItem = xFakeHotbar + 1;
        int yItem = yFakeHotbar + 1;
        for (ItemStack item : items) {
            Minecraft.getMinecraft().getRenderItem().renderItemAndEffectIntoGUI(item, xItem, yItem);

            // check mouse hover to draw item tooltip
            if (enablePreview && mouseX >= xItem - 1 && mouseX < xItem + 18 - 1 && mouseY >= yItem - 1 && mouseY < yItem + 18 - 1) {
                hoveredItem = item;
                GlStateManager.disableLighting();
                GlStateManager.disableDepth();
                GlStateManager.colorMask(true, true, true, false);
                // draw white slot hover
                Gui.drawRect(xItem, yItem, xItem + 16, yItem + 16, 0x80FFFFFF);
                GlStateManager.colorMask(true, true, true, true);
                GlStateManager.enableDepth();
                GlStateManager.enableLighting();
            }
            xItem += 18;
        }
        RenderHelper.disableStandardItemLighting();
        GlStateManager.disableRescaleNormal();
        GlStateManager.disableBlend();
        GlStateManager.popMatrix();
    }

    public static ItemStack createDemoItem(String itemId, String itemName, String[] lore, Map<String, NBTBase> extraAttributes) {
        return createDemoItem(itemId, itemName, lore, extraAttributes, -1);
    }

    public static ItemStack createDemoItem(String itemId, String itemName, String[] lore, Map<String, NBTBase> extraAttributes, int color) {
        ItemStack demoItem = new ItemStack(Item.getByNameOrId(itemId));

        // lore
        NBTTagCompound display = new NBTTagCompound();
        demoItem.setTagInfo("display", display);
        NBTTagList loreTag = new NBTTagList();
        display.setTag("Lore", loreTag);
        for (String loreEntry : lore) {
            loreTag.appendTag(new NBTTagString(loreEntry));
        }
        // color
        if (color >= 0) {
            display.setInteger("color", color);
        }
        // SkyBlock extra attributes
        NBTTagCompound extraAttributesTag = new NBTTagCompound();
        demoItem.setTagInfo("ExtraAttributes", extraAttributesTag);
        for (Map.Entry<String, NBTBase> extraAttribute : extraAttributes.entrySet()) {
            extraAttributesTag.setTag(extraAttribute.getKey(), extraAttribute.getValue());
        }
        demoItem.setStackDisplayName(itemName);

        return demoItem;
    }

    public static ItemStack createDungeonItem(String modifier, String timestamp, String... lore) {
        Map<String, NBTBase> extraAttributes = new HashMap<>();
        extraAttributes.put("modifier", new NBTTagString(modifier));
        extraAttributes.put("baseStatBoostPercentage", new NBTTagInt(48));
        extraAttributes.put("item_tier", new NBTTagInt(1));
        extraAttributes.put("id", new NBTTagString("SKELETON_SOLDIER_LEGGINGS"));
        extraAttributes.put("timestamp", new NBTTagString(timestamp));


        return createDemoItem("leather_leggings", EnumChatFormatting.DARK_PURPLE + WordUtils.capitalize(modifier) + " Skeleton Soldier Leggings",
                lore, extraAttributes, 16759819);
    }

    private void drawChatPreview(int x, int y, int mouseX, int mouseY, boolean enablePreview) {
        FontRenderer fontRenderer = Minecraft.getMinecraft().fontRendererObj;
        int currentX = x + 15;
        int chatY = y + 20 / 2 - fontRenderer.FONT_HEIGHT / 2;

        fontRenderer.drawStringWithShadow(this.chatComponent.getFormattedText(), currentX, chatY, 0xffffffff);
        if (!enablePreview) {
            Gui.drawRect(currentX - 2, chatY - 2, currentX + fontRenderer.getStringWidth(chatComponent.getUnformattedText()) + 1, chatY + fontRenderer.FONT_HEIGHT + 1, 0xdd444444);
        }

        // hover checker for online best friends (partially taken from GuiNewChat#getChatComponent)
        if (enablePreview && mouseY >= chatY && mouseY <= chatY + fontRenderer.FONT_HEIGHT) {
            for (IChatComponent chatComponent : chatComponent) {
                if (chatComponent instanceof ChatComponentText) {
                    currentX += fontRenderer.getStringWidth(GuiUtilRenderComponents.func_178909_a(chatComponent.getUnformattedTextForChat(), false));
                    if (currentX > mouseX) {
                        hoveredChatComponent = chatComponent;
                        break;
                    }
                }
            }
        }
    }

    public static IChatComponent createDemoOnline(String name, String gameMode, String onlineTime) {
        return new MooChatComponent(name).darkGreen().setHover(new MooChatComponent(gameMode).yellow().appendFreshSibling(new MooChatComponent("Online for " + onlineTime).white()));
    }

    private void drawButtonWithRunnablePreview(int x, int y, int mouseX, int mouseY, boolean enablePreview) {
        FontRenderer fontRenderer = Minecraft.getMinecraft().fontRendererObj;
        int currentX = x + 15;
        int chatY = y + 20 / 2 - fontRenderer.FONT_HEIGHT / 2;

        String previewText = "(hover me to preview)";
        fontRenderer.drawStringWithShadow(previewText, currentX, chatY, 0xffffffff);
        if (nextPreviewAt < Minecraft.getSystemTime()
                && mouseY >= chatY && mouseY <= chatY + fontRenderer.FONT_HEIGHT
                && mouseX > currentX && mouseX < currentX + fontRenderer.getStringWidth(previewText)) {
            nextPreviewAt = Minecraft.getSystemTime() + 3000;
            previewRunnable.run();
        }
    }

    private enum Type {
        CHAT, ITEM, RUNNABLE_ON_HOVER
    }
}