GuiMerchant .java

net.minecraft.client.gui.GuiMerchant (class): members: selectedMerchantRecipe, MERCHANT_GUI_TEXTURE, MerchantButton, previousButton, getMerchant, merchant.

net.minecraft.client.guiclass275 linesclientextends GuiContainer10.6 KB

API Surface (8)

void initGui()
void drawGuiContainerForegroundLayer(int mouseX, int mouseY)
void updateScreen()
void actionPerformed(GuiButton button)
void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY)
void drawScreen(int mouseX, int mouseY, float partialTicks)
IMerchant getMerchant()
void drawButton(Minecraft mc, int mouseX, int mouseY)

Fields & Constants (7)

logger
MERCHANT_GUI_TEXTURE
merchant
nextButton
previousButton
selectedMerchantRecipe
chatComponent

String literals (4)

"textures/gui/container/villager.png"
"container.inventory"
"MC|TrSel"
"merchant.deprecated"

Full Source

GuiMerchant.java275 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.client.gui;
2
3
import io.netty.buffer.Unpooled;
4
import java.io.IOException;
5
import net.minecraft.client.Minecraft;
6
import net.minecraft.client.gui.inventory.GuiContainer;
7
import net.minecraft.client.renderer.GlStateManager;
8
import net.minecraft.client.renderer.RenderHelper;
9
import net.minecraft.client.resources.I18n;
10
import net.minecraft.entity.IMerchant;
11
import net.minecraft.entity.player.InventoryPlayer;
12
import net.minecraft.inventory.ContainerMerchant;
13
import net.minecraft.item.ItemStack;
14
import net.minecraft.network.PacketBuffer;
15
import net.minecraft.network.play.client.C17PacketCustomPayload;
16
import net.minecraft.util.IChatComponent;
17
import net.minecraft.util.ResourceLocation;
18
import net.minecraft.village.MerchantRecipe;
19
import net.minecraft.village.MerchantRecipeList;
20
import net.minecraft.world.World;
21
import org.apache.logging.log4j.LogManager;
22
import org.apache.logging.log4j.Logger;
23
24
public class GuiMerchant extends GuiContainer
25
{
26
    private static final Logger logger = LogManager.getLogger();
27
28
    /** The GUI texture for the villager merchant GUI. */
29
    private static final ResourceLocation MERCHANT_GUI_TEXTURE = new ResourceLocation("textures/gui/container/villager.png");
30
31
    /** The current IMerchant instance in use for this specific merchant. */
32
    private IMerchant merchant;
33
34
    /** The button which proceeds to the next available merchant recipe. */
35
    private GuiMerchant.MerchantButton nextButton;
36
37
    /** Returns to the previous Merchant recipe if one is applicable. */
38
    private GuiMerchant.MerchantButton previousButton;
39
40
    /**
41
     * The integer value corresponding to the currently selected merchant recipe.
42
     */
43
    private int selectedMerchantRecipe;
44
45
    /** The chat component utilized by this GuiMerchant instance. */
46
    private IChatComponent chatComponent;
47
48
    public GuiMerchant(InventoryPlayer p_i45500_1_, IMerchant p_i45500_2_, World worldIn)
49
    {
50
        super(new ContainerMerchant(p_i45500_1_, p_i45500_2_, worldIn));
51
        this.merchant = p_i45500_2_;
52
        this.chatComponent = p_i45500_2_.getDisplayName();
53
    }
54
55
    /**
56
     * Adds the buttons (and other controls) to the screen in question. Called when the GUI is displayed and when the
57
     * window resizes, the buttonList is cleared beforehand.
58
     */
59
    public void initGui()
60
    {
61
        super.initGui();
62
        int i = (this.width - this.xSize) / 2;
63
        int j = (this.height - this.ySize) / 2;
64
        this.buttonList.add(this.nextButton = new GuiMerchant.MerchantButton(1, i + 120 + 27, j + 24 - 1, true));
65
        this.buttonList.add(this.previousButton = new GuiMerchant.MerchantButton(2, i + 36 - 19, j + 24 - 1, false));
66
        this.nextButton.enabled = false;
67
        this.previousButton.enabled = false;
68
    }
69
70
    /**
71
     * Draw the foreground layer for the GuiContainer (everything in front of the items). Args : mouseX, mouseY
72
     */
73
    protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY)
74
    {
75
        String s = this.chatComponent.getUnformattedText();
76
        this.fontRendererObj.drawString(s, this.xSize / 2 - this.fontRendererObj.getStringWidth(s) / 2, 6, 4210752);
77
        this.fontRendererObj.drawString(I18n.format("container.inventory", new Object[0]), 8, this.ySize - 96 + 2, 4210752);
78
    }
79
80
    /**
81
     * Called from the main game loop to update the screen.
82
     */
83
    public void updateScreen()
84
    {
85
        super.updateScreen();
86
        MerchantRecipeList merchantrecipelist = this.merchant.getRecipes(this.mc.thePlayer);
87
88
        if (merchantrecipelist != null)
89
        {
90
            this.nextButton.enabled = this.selectedMerchantRecipe < merchantrecipelist.size() - 1;
91
            this.previousButton.enabled = this.selectedMerchantRecipe > 0;
92
        }
93
    }
94
95
    /**
96
     * Called by the controls from the buttonList when activated. (Mouse pressed for buttons)
97
     */
98
    protected void actionPerformed(GuiButton button) throws IOException
99
    {
100
        boolean flag = false;
101
102
        if (button == this.nextButton)
103
        {
104
            ++this.selectedMerchantRecipe;
105
            MerchantRecipeList merchantrecipelist = this.merchant.getRecipes(this.mc.thePlayer);
106
107
            if (merchantrecipelist != null && this.selectedMerchantRecipe >= merchantrecipelist.size())
108
            {
109
                this.selectedMerchantRecipe = merchantrecipelist.size() - 1;
110
            }
111
112
            flag = true;
113
        }
114
        else if (button == this.previousButton)
115
        {
116
            --this.selectedMerchantRecipe;
117
118
            if (this.selectedMerchantRecipe < 0)
119
            {
120
                this.selectedMerchantRecipe = 0;
121
            }
122
123
            flag = true;
124
        }
125
126
        if (flag)
127
        {
128
            ((ContainerMerchant)this.inventorySlots).setCurrentRecipeIndex(this.selectedMerchantRecipe);
129
            PacketBuffer packetbuffer = new PacketBuffer(Unpooled.buffer());
130
            packetbuffer.writeInt(this.selectedMerchantRecipe);
131
            this.mc.getNetHandler().addToSendQueue(new C17PacketCustomPayload("MC|TrSel", packetbuffer));
132
        }
133
    }
134
135
    /**
136
     * Args : renderPartialTicks, mouseX, mouseY
137
     */
138
    protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY)
139
    {
140
        GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
141
        this.mc.getTextureManager().bindTexture(MERCHANT_GUI_TEXTURE);
142
        int i = (this.width - this.xSize) / 2;
143
        int j = (this.height - this.ySize) / 2;
144
        this.drawTexturedModalRect(i, j, 0, 0, this.xSize, this.ySize);
145
        MerchantRecipeList merchantrecipelist = this.merchant.getRecipes(this.mc.thePlayer);
146
147
        if (merchantrecipelist != null && !merchantrecipelist.isEmpty())
148
        {
149
            int k = this.selectedMerchantRecipe;
150
151
            if (k < 0 || k >= merchantrecipelist.size())
152
            {
153
                return;
154
            }
155
156
            MerchantRecipe merchantrecipe = (MerchantRecipe)merchantrecipelist.get(k);
157
158
            if (merchantrecipe.isRecipeDisabled())
159
            {
160
                this.mc.getTextureManager().bindTexture(MERCHANT_GUI_TEXTURE);
161
                GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
162
                GlStateManager.disableLighting();
163
                this.drawTexturedModalRect(this.guiLeft + 83, this.guiTop + 21, 212, 0, 28, 21);
164
                this.drawTexturedModalRect(this.guiLeft + 83, this.guiTop + 51, 212, 0, 28, 21);
165
            }
166
        }
167
    }
168
169
    /**
170
     * Draws the screen and all the components in it. Args : mouseX, mouseY, renderPartialTicks
171
     */
172
    public void drawScreen(int mouseX, int mouseY, float partialTicks)
173
    {
174
        super.drawScreen(mouseX, mouseY, partialTicks);
175
        MerchantRecipeList merchantrecipelist = this.merchant.getRecipes(this.mc.thePlayer);
176
177
        if (merchantrecipelist != null && !merchantrecipelist.isEmpty())
178
        {
179
            int i = (this.width - this.xSize) / 2;
180
            int j = (this.height - this.ySize) / 2;
181
            int k = this.selectedMerchantRecipe;
182
            MerchantRecipe merchantrecipe = (MerchantRecipe)merchantrecipelist.get(k);
183
            ItemStack itemstack = merchantrecipe.getItemToBuy();
184
            ItemStack itemstack1 = merchantrecipe.getSecondItemToBuy();
185
            ItemStack itemstack2 = merchantrecipe.getItemToSell();
186
            GlStateManager.pushMatrix();
187
            RenderHelper.enableGUIStandardItemLighting();
188
            GlStateManager.disableLighting();
189
            GlStateManager.enableRescaleNormal();
190
            GlStateManager.enableColorMaterial();
191
            GlStateManager.enableLighting();
192
            this.itemRender.zLevel = 100.0F;
193
            this.itemRender.renderItemAndEffectIntoGUI(itemstack, i + 36, j + 24);
194
            this.itemRender.renderItemOverlays(this.fontRendererObj, itemstack, i + 36, j + 24);
195
196
            if (itemstack1 != null)
197
            {
198
                this.itemRender.renderItemAndEffectIntoGUI(itemstack1, i + 62, j + 24);
199
                this.itemRender.renderItemOverlays(this.fontRendererObj, itemstack1, i + 62, j + 24);
200
            }
201
202
            this.itemRender.renderItemAndEffectIntoGUI(itemstack2, i + 120, j + 24);
203
            this.itemRender.renderItemOverlays(this.fontRendererObj, itemstack2, i + 120, j + 24);
204
            this.itemRender.zLevel = 0.0F;
205
            GlStateManager.disableLighting();
206
207
            if (this.isPointInRegion(36, 24, 16, 16, mouseX, mouseY) && itemstack != null)
208
            {
209
                this.renderToolTip(itemstack, mouseX, mouseY);
210
            }
211
            else if (itemstack1 != null && this.isPointInRegion(62, 24, 16, 16, mouseX, mouseY) && itemstack1 != null)
212
            {
213
                this.renderToolTip(itemstack1, mouseX, mouseY);
214
            }
215
            else if (itemstack2 != null && this.isPointInRegion(120, 24, 16, 16, mouseX, mouseY) && itemstack2 != null)
216
            {
217
                this.renderToolTip(itemstack2, mouseX, mouseY);
218
            }
219
            else if (merchantrecipe.isRecipeDisabled() && (this.isPointInRegion(83, 21, 28, 21, mouseX, mouseY) || this.isPointInRegion(83, 51, 28, 21, mouseX, mouseY)))
220
            {
221
                this.drawCreativeTabHoveringText(I18n.format("merchant.deprecated", new Object[0]), mouseX, mouseY);
222
            }
223
224
            GlStateManager.popMatrix();
225
            GlStateManager.enableLighting();
226
            GlStateManager.enableDepth();
227
            RenderHelper.enableStandardItemLighting();
228
        }
229
    }
230
231
    public IMerchant getMerchant()
232
    {
233
        return this.merchant;
234
    }
235
236
    static class MerchantButton extends GuiButton
237
    {
238
        private final boolean field_146157_o;
239
240
        public MerchantButton(int buttonID, int x, int y, boolean p_i1095_4_)
241
        {
242
            super(buttonID, x, y, 12, 19, "");
243
            this.field_146157_o = p_i1095_4_;
244
        }
245
246
        public void drawButton(Minecraft mc, int mouseX, int mouseY)
247
        {
248
            if (this.visible)
249
            {
250
                mc.getTextureManager().bindTexture(GuiMerchant.MERCHANT_GUI_TEXTURE);
251
                GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
252
                boolean flag = mouseX >= this.xPosition && mouseY >= this.yPosition && mouseX < this.xPosition + this.width && mouseY < this.yPosition + this.height;
253
                int i = 0;
254
                int j = 176;
255
256
                if (!this.enabled)
257
                {
258
                    j += this.width * 2;
259
                }
260
                else if (flag)
261
                {
262
                    j += this.width;
263
                }
264
265
                if (!this.field_146157_o)
266
                {
267
                    i += this.height;
268
                }
269
270
                this.drawTexturedModalRect(this.xPosition, this.yPosition, j, i, this.width, this.height);
271
            }
272
        }
273
    }
274
}
275

Deobfuscated MCP 1.8.9 source of GuiMerchant from Marcelektro/MavenMCP-1.8.9 (net/minecraft/client/gui/GuiMerchant.java), stored verbatim.