001    package net.minecraft.src;
002    
003    import cpw.mods.fml.common.Side;
004    import cpw.mods.fml.common.asm.SideOnly;
005    import java.util.ArrayList;
006    import java.util.Iterator;
007    import java.util.List;
008    import org.lwjgl.input.Keyboard;
009    import org.lwjgl.input.Mouse;
010    import org.lwjgl.opengl.GL11;
011    import org.lwjgl.opengl.GL12;
012    
013    @SideOnly(Side.CLIENT)
014    public class GuiContainerCreative extends InventoryEffectRenderer
015    {
016        private static InventoryBasic inventory = new InventoryBasic("tmp", 45);
017        private static int field_74241_p = CreativeTabs.tabBlock.getTabIndex();
018    
019        /** Amount scrolled in Creative mode inventory (0 = top, 1 = bottom) */
020        private float currentScroll = 0.0F;
021    
022        /** True if the scrollbar is being dragged */
023        private boolean isScrolling = false;
024    
025        /**
026         * True if the left mouse button was held down last time drawScreen was called.
027         */
028        private boolean wasClicking;
029        private GuiTextField field_74237_t;
030        private List field_74236_u;
031        private Slot field_74235_v = null;
032        private boolean field_74234_w = false;
033    
034        public GuiContainerCreative(EntityPlayer par1EntityPlayer)
035        {
036            super(new ContainerCreative(par1EntityPlayer));
037            par1EntityPlayer.craftingInventory = this.inventorySlots;
038            this.allowUserInput = true;
039            par1EntityPlayer.addStat(AchievementList.openInventory, 1);
040            this.ySize = 136;
041            this.xSize = 195;
042        }
043    
044        /**
045         * Called from the main game loop to update the screen.
046         */
047        public void updateScreen()
048        {
049            if (!this.mc.playerController.isInCreativeMode())
050            {
051                this.mc.displayGuiScreen(new GuiInventory(this.mc.thePlayer));
052            }
053        }
054    
055        protected void handleMouseClick(Slot par1Slot, int par2, int par3, boolean par4)
056        {
057            this.field_74234_w = true;
058            ItemStack var6;
059            InventoryPlayer var9;
060    
061            if (par1Slot != null)
062            {
063                int var5;
064    
065                if (par1Slot == this.field_74235_v && par4)
066                {
067                    for (var5 = 0; var5 < this.mc.thePlayer.inventorySlots.getInventory().size(); ++var5)
068                    {
069                        this.mc.playerController.sendSlotPacket((ItemStack)null, var5);
070                    }
071                }
072                else
073                {
074                    ItemStack var7;
075    
076                    if (field_74241_p == CreativeTabs.tabInventory.getTabIndex())
077                    {
078                        if (par1Slot == this.field_74235_v)
079                        {
080                            this.mc.thePlayer.inventory.setItemStack((ItemStack)null);
081                        }
082                        else
083                        {
084                            var5 = SlotCreativeInventory.func_75240_a((SlotCreativeInventory)par1Slot).slotNumber;
085                            var6 = this.mc.thePlayer.inventorySlots.getSlot(var5).getStack();
086    
087                            if (par4 && (var6 == null || !(var6.getItem() instanceof ItemArmor)))
088                            {
089                                this.mc.playerController.sendSlotPacket((ItemStack)null, var5);
090                            }
091                            else
092                            {
093                                this.mc.thePlayer.inventorySlots.slotClick(var5, par3, par4, this.mc.thePlayer);
094                                var7 = this.mc.thePlayer.inventorySlots.getSlot(var5).getStack();
095                                this.mc.playerController.sendSlotPacket(var7, var5);
096                            }
097                        }
098                    }
099                    else if (par1Slot.inventory == inventory)
100                    {
101                        var9 = this.mc.thePlayer.inventory;
102                        var6 = var9.getItemStack();
103                        var7 = par1Slot.getStack();
104    
105                        if (var6 != null && var7 != null && var6.isItemEqual(var7))
106                        {
107                            if (par3 == 0)
108                            {
109                                if (par4)
110                                {
111                                    var6.stackSize = var6.getMaxStackSize();
112                                }
113                                else if (var6.stackSize < var6.getMaxStackSize())
114                                {
115                                    ++var6.stackSize;
116                                }
117                            }
118                            else if (var6.stackSize <= 1)
119                            {
120                                var9.setItemStack((ItemStack)null);
121                            }
122                            else
123                            {
124                                --var6.stackSize;
125                            }
126                        }
127                        else if (var7 != null && var6 == null)
128                        {
129                            boolean var8 = false;
130    
131                            if (!var8)
132                            {
133                                var9.setItemStack(ItemStack.copyItemStack(var7));
134                                var6 = var9.getItemStack();
135    
136                                if (par4)
137                                {
138                                    var6.stackSize = var6.getMaxStackSize();
139                                }
140                            }
141                        }
142                        else
143                        {
144                            var9.setItemStack((ItemStack)null);
145                        }
146                    }
147                    else
148                    {
149                        this.inventorySlots.slotClick(par1Slot.slotNumber, par3, par4, this.mc.thePlayer);
150                        ItemStack var10 = this.inventorySlots.getSlot(par1Slot.slotNumber).getStack();
151                        this.mc.playerController.sendSlotPacket(var10, par1Slot.slotNumber - this.inventorySlots.inventorySlots.size() + 9 + 36);
152                    }
153                }
154            }
155            else
156            {
157                var9 = this.mc.thePlayer.inventory;
158    
159                if (var9.getItemStack() != null)
160                {
161                    if (par3 == 0)
162                    {
163                        this.mc.thePlayer.dropPlayerItem(var9.getItemStack());
164                        this.mc.playerController.func_78752_a(var9.getItemStack());
165                        var9.setItemStack((ItemStack)null);
166                    }
167    
168                    if (par3 == 1)
169                    {
170                        var6 = var9.getItemStack().splitStack(1);
171                        this.mc.thePlayer.dropPlayerItem(var6);
172                        this.mc.playerController.func_78752_a(var6);
173    
174                        if (var9.getItemStack().stackSize == 0)
175                        {
176                            var9.setItemStack((ItemStack)null);
177                        }
178                    }
179                }
180            }
181        }
182    
183        /**
184         * Adds the buttons (and other controls) to the screen in question.
185         */
186        public void initGui()
187        {
188            if (this.mc.playerController.isInCreativeMode())
189            {
190                super.initGui();
191                this.controlList.clear();
192                Keyboard.enableRepeatEvents(true);
193                this.field_74237_t = new GuiTextField(this.fontRenderer, this.guiLeft + 82, this.guiTop + 6, 89, this.fontRenderer.FONT_HEIGHT);
194                this.field_74237_t.setMaxStringLength(15);
195                this.field_74237_t.setEnableBackgroundDrawing(false);
196                this.field_74237_t.func_73790_e(false);
197                this.field_74237_t.func_73794_g(16777215);
198                int var1 = field_74241_p;
199                field_74241_p = -1;
200                this.func_74227_b(CreativeTabs.creativeTabArray[var1]);
201            }
202            else
203            {
204                this.mc.displayGuiScreen(new GuiInventory(this.mc.thePlayer));
205            }
206        }
207    
208        /**
209         * Called when the screen is unloaded. Used to disable keyboard repeat events
210         */
211        public void onGuiClosed()
212        {
213            super.onGuiClosed();
214            Keyboard.enableRepeatEvents(false);
215        }
216    
217        /**
218         * Fired when a key is typed. This is the equivalent of KeyListener.keyTyped(KeyEvent e).
219         */
220        protected void keyTyped(char par1, int par2)
221        {
222            if (field_74241_p != CreativeTabs.tabAllSearch.getTabIndex())
223            {
224                if (Keyboard.isKeyDown(this.mc.gameSettings.keyBindChat.keyCode))
225                {
226                    this.func_74227_b(CreativeTabs.tabAllSearch);
227                }
228                else
229                {
230                    super.keyTyped(par1, par2);
231                }
232            }
233            else
234            {
235                if (this.field_74234_w)
236                {
237                    this.field_74234_w = false;
238                    this.field_74237_t.setText("");
239                }
240    
241                if (this.field_74237_t.textboxKeyTyped(par1, par2))
242                {
243                    this.func_74228_j();
244                }
245                else
246                {
247                    super.keyTyped(par1, par2);
248                }
249            }
250        }
251    
252        private void func_74228_j()
253        {
254            ContainerCreative var1 = (ContainerCreative)this.inventorySlots;
255            var1.itemList.clear();
256            Item[] var2 = Item.itemsList;
257            int var3 = var2.length;
258    
259            for (int var4 = 0; var4 < var3; ++var4)
260            {
261                Item var5 = var2[var4];
262    
263                if (var5 != null && var5.getCreativeTab() != null)
264                {
265                    var5.getSubItems(var5.shiftedIndex, (CreativeTabs)null, var1.itemList);
266                }
267            }
268    
269            Iterator var8 = var1.itemList.iterator();
270            String var9 = this.field_74237_t.getText().toLowerCase();
271    
272            while (var8.hasNext())
273            {
274                ItemStack var10 = (ItemStack)var8.next();
275                boolean var11 = false;
276                Iterator var6 = var10.getItemNameandInformation().iterator();
277    
278                while (true)
279                {
280                    if (var6.hasNext())
281                    {
282                        String var7 = (String)var6.next();
283    
284                        if (!var7.toLowerCase().contains(var9))
285                        {
286                            continue;
287                        }
288    
289                        var11 = true;
290                    }
291    
292                    if (!var11)
293                    {
294                        var8.remove();
295                    }
296    
297                    break;
298                }
299            }
300    
301            this.currentScroll = 0.0F;
302            var1.scrollTo(0.0F);
303        }
304    
305        /**
306         * Draw the foreground layer for the GuiContainer (everything in front of the items)
307         */
308        protected void drawGuiContainerForegroundLayer()
309        {
310            CreativeTabs var1 = CreativeTabs.creativeTabArray[field_74241_p];
311    
312            if (var1.drawInForegroundOfTab())
313            {
314                this.fontRenderer.drawString(var1.getTranslatedTabLabel(), 8, 6, 4210752);
315            }
316        }
317    
318        /**
319         * Called when the mouse is clicked.
320         */
321        protected void mouseClicked(int par1, int par2, int par3)
322        {
323            if (par3 == 0)
324            {
325                int var4 = par1 - this.guiLeft;
326                int var5 = par2 - this.guiTop;
327                CreativeTabs[] var6 = CreativeTabs.creativeTabArray;
328                int var7 = var6.length;
329    
330                for (int var8 = 0; var8 < var7; ++var8)
331                {
332                    CreativeTabs var9 = var6[var8];
333    
334                    if (this.func_74232_a(var9, var4, var5))
335                    {
336                        this.func_74227_b(var9);
337                        return;
338                    }
339                }
340            }
341    
342            super.mouseClicked(par1, par2, par3);
343        }
344    
345        /**
346         * returns (if you are not on the inventoryTab) and (the flag isn't set) and( you have more than 1 page of items)
347         */
348        private boolean needsScrollBars()
349        {
350            return field_74241_p != CreativeTabs.tabInventory.getTabIndex() && CreativeTabs.creativeTabArray[field_74241_p].shouldHidePlayerInventory() && ((ContainerCreative)this.inventorySlots).hasMoreThan1PageOfItemsInList();
351        }
352    
353        private void func_74227_b(CreativeTabs par1CreativeTabs)
354        {
355            int var2 = field_74241_p;
356            field_74241_p = par1CreativeTabs.getTabIndex();
357            ContainerCreative var3 = (ContainerCreative)this.inventorySlots;
358            var3.itemList.clear();
359            par1CreativeTabs.displayAllReleventItems(var3.itemList);
360    
361            if (par1CreativeTabs == CreativeTabs.tabInventory)
362            {
363                Container var4 = this.mc.thePlayer.inventorySlots;
364    
365                if (this.field_74236_u == null)
366                {
367                    this.field_74236_u = var3.inventorySlots;
368                }
369    
370                var3.inventorySlots = new ArrayList();
371    
372                for (int var5 = 0; var5 < var4.inventorySlots.size(); ++var5)
373                {
374                    SlotCreativeInventory var6 = new SlotCreativeInventory(this, (Slot)var4.inventorySlots.get(var5), var5);
375                    var3.inventorySlots.add(var6);
376                    int var7;
377                    int var8;
378                    int var9;
379    
380                    if (var5 >= 5 && var5 < 9)
381                    {
382                        var7 = var5 - 5;
383                        var8 = var7 / 2;
384                        var9 = var7 % 2;
385                        var6.xDisplayPosition = 9 + var8 * 54;
386                        var6.yDisplayPosition = 6 + var9 * 27;
387                    }
388                    else if (var5 >= 0 && var5 < 5)
389                    {
390                        var6.yDisplayPosition = -2000;
391                        var6.xDisplayPosition = -2000;
392                    }
393                    else if (var5 < var4.inventorySlots.size())
394                    {
395                        var7 = var5 - 9;
396                        var8 = var7 % 9;
397                        var9 = var7 / 9;
398                        var6.xDisplayPosition = 9 + var8 * 18;
399    
400                        if (var5 >= 36)
401                        {
402                            var6.yDisplayPosition = 112;
403                        }
404                        else
405                        {
406                            var6.yDisplayPosition = 54 + var9 * 18;
407                        }
408                    }
409                }
410    
411                this.field_74235_v = new Slot(inventory, 0, 173, 112);
412                var3.inventorySlots.add(this.field_74235_v);
413            }
414            else if (var2 == CreativeTabs.tabInventory.getTabIndex())
415            {
416                var3.inventorySlots = this.field_74236_u;
417                this.field_74236_u = null;
418            }
419    
420            if (this.field_74237_t != null)
421            {
422                if (par1CreativeTabs == CreativeTabs.tabAllSearch)
423                {
424                    this.field_74237_t.func_73790_e(true);
425                    this.field_74237_t.setCanLoseFocus(false);
426                    this.field_74237_t.setFocused(true);
427                    this.field_74237_t.setText("");
428                    this.func_74228_j();
429                }
430                else
431                {
432                    this.field_74237_t.func_73790_e(false);
433                    this.field_74237_t.setCanLoseFocus(true);
434                    this.field_74237_t.setFocused(false);
435                }
436            }
437    
438            this.currentScroll = 0.0F;
439            var3.scrollTo(0.0F);
440        }
441    
442        /**
443         * Handles mouse input.
444         */
445        public void handleMouseInput()
446        {
447            super.handleMouseInput();
448            int var1 = Mouse.getEventDWheel();
449    
450            if (var1 != 0 && this.needsScrollBars())
451            {
452                int var2 = ((ContainerCreative)this.inventorySlots).itemList.size() / 9 - 5 + 1;
453    
454                if (var1 > 0)
455                {
456                    var1 = 1;
457                }
458    
459                if (var1 < 0)
460                {
461                    var1 = -1;
462                }
463    
464                this.currentScroll = (float)((double)this.currentScroll - (double)var1 / (double)var2);
465    
466                if (this.currentScroll < 0.0F)
467                {
468                    this.currentScroll = 0.0F;
469                }
470    
471                if (this.currentScroll > 1.0F)
472                {
473                    this.currentScroll = 1.0F;
474                }
475    
476                ((ContainerCreative)this.inventorySlots).scrollTo(this.currentScroll);
477            }
478        }
479    
480        /**
481         * Draws the screen and all the components in it.
482         */
483        public void drawScreen(int par1, int par2, float par3)
484        {
485            boolean var4 = Mouse.isButtonDown(0);
486            int var5 = this.guiLeft;
487            int var6 = this.guiTop;
488            int var7 = var5 + 175;
489            int var8 = var6 + 18;
490            int var9 = var7 + 14;
491            int var10 = var8 + 112;
492    
493            if (!this.wasClicking && var4 && par1 >= var7 && par2 >= var8 && par1 < var9 && par2 < var10)
494            {
495                this.isScrolling = this.needsScrollBars();
496            }
497    
498            if (!var4)
499            {
500                this.isScrolling = false;
501            }
502    
503            this.wasClicking = var4;
504    
505            if (this.isScrolling)
506            {
507                this.currentScroll = ((float)(par2 - var8) - 7.5F) / ((float)(var10 - var8) - 15.0F);
508    
509                if (this.currentScroll < 0.0F)
510                {
511                    this.currentScroll = 0.0F;
512                }
513    
514                if (this.currentScroll > 1.0F)
515                {
516                    this.currentScroll = 1.0F;
517                }
518    
519                ((ContainerCreative)this.inventorySlots).scrollTo(this.currentScroll);
520            }
521    
522            super.drawScreen(par1, par2, par3);
523            CreativeTabs[] var11 = CreativeTabs.creativeTabArray;
524            int var12 = var11.length;
525    
526            for (int var13 = 0; var13 < var12; ++var13)
527            {
528                CreativeTabs var14 = var11[var13];
529    
530                if (this.func_74231_b(var14, par1, par2))
531                {
532                    break;
533                }
534            }
535    
536            if (this.field_74235_v != null && field_74241_p == CreativeTabs.tabInventory.getTabIndex() && this.func_74188_c(this.field_74235_v.xDisplayPosition, this.field_74235_v.yDisplayPosition, 16, 16, par1, par2))
537            {
538                this.func_74190_a(StringTranslate.getInstance().translateKey("inventory.binSlot"), par1, par2);
539            }
540    
541            GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
542            GL11.glDisable(GL11.GL_LIGHTING);
543        }
544    
545        /**
546         * Draw the background layer for the GuiContainer (everything behind the items)
547         */
548        protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3)
549        {
550            GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
551            RenderHelper.enableGUIStandardItemLighting();
552            int var4 = this.mc.renderEngine.getTexture("/gui/allitems.png");
553            CreativeTabs var5 = CreativeTabs.creativeTabArray[field_74241_p];
554            int var6 = this.mc.renderEngine.getTexture("/gui/creative_inv/" + var5.getBackgroundImageName());
555            CreativeTabs[] var7 = CreativeTabs.creativeTabArray;
556            int var8 = var7.length;
557            int var9;
558    
559            for (var9 = 0; var9 < var8; ++var9)
560            {
561                CreativeTabs var10 = var7[var9];
562                this.mc.renderEngine.bindTexture(var4);
563    
564                if (var10.getTabIndex() != field_74241_p)
565                {
566                    this.func_74233_a(var10);
567                }
568            }
569    
570            this.mc.renderEngine.bindTexture(var6);
571            this.drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize);
572            this.field_74237_t.drawTextBox();
573            GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
574            int var11 = this.guiLeft + 175;
575            var8 = this.guiTop + 18;
576            var9 = var8 + 112;
577            this.mc.renderEngine.bindTexture(var4);
578    
579            if (var5.shouldHidePlayerInventory())
580            {
581                this.drawTexturedModalRect(var11, var8 + (int)((float)(var9 - var8 - 17) * this.currentScroll), 232 + (this.needsScrollBars() ? 0 : 12), 0, 12, 15);
582            }
583    
584            this.func_74233_a(var5);
585    
586            if (var5 == CreativeTabs.tabInventory)
587            {
588                GuiInventory.func_74223_a(this.mc, this.guiLeft + 43, this.guiTop + 45, 20, (float)(this.guiLeft + 43 - par2), (float)(this.guiTop + 45 - 30 - par3));
589            }
590        }
591    
592        protected boolean func_74232_a(CreativeTabs par1CreativeTabs, int par2, int par3)
593        {
594            int var4 = par1CreativeTabs.getTabColumn();
595            int var5 = 28 * var4;
596            byte var6 = 0;
597    
598            if (var4 == 5)
599            {
600                var5 = this.xSize - 28 + 2;
601            }
602            else if (var4 > 0)
603            {
604                var5 += var4;
605            }
606    
607            int var7;
608    
609            if (par1CreativeTabs.isTabInFirstRow())
610            {
611                var7 = var6 - 32;
612            }
613            else
614            {
615                var7 = var6 + this.ySize;
616            }
617    
618            return par2 >= var5 && par2 <= var5 + 28 && par3 >= var7 && par3 <= var7 + 32;
619        }
620    
621        protected boolean func_74231_b(CreativeTabs par1CreativeTabs, int par2, int par3)
622        {
623            int var4 = par1CreativeTabs.getTabColumn();
624            int var5 = 28 * var4;
625            byte var6 = 0;
626    
627            if (var4 == 5)
628            {
629                var5 = this.xSize - 28 + 2;
630            }
631            else if (var4 > 0)
632            {
633                var5 += var4;
634            }
635    
636            int var7;
637    
638            if (par1CreativeTabs.isTabInFirstRow())
639            {
640                var7 = var6 - 32;
641            }
642            else
643            {
644                var7 = var6 + this.ySize;
645            }
646    
647            if (this.func_74188_c(var5 + 3, var7 + 3, 23, 27, par2, par3))
648            {
649                this.func_74190_a(par1CreativeTabs.getTranslatedTabLabel(), par2, par3);
650                return true;
651            }
652            else
653            {
654                return false;
655            }
656        }
657    
658        protected void func_74233_a(CreativeTabs par1CreativeTabs)
659        {
660            boolean var2 = par1CreativeTabs.getTabIndex() == field_74241_p;
661            boolean var3 = par1CreativeTabs.isTabInFirstRow();
662            int var4 = par1CreativeTabs.getTabColumn();
663            int var5 = var4 * 28;
664            int var6 = 0;
665            int var7 = this.guiLeft + 28 * var4;
666            int var8 = this.guiTop;
667            byte var9 = 32;
668    
669            if (var2)
670            {
671                var6 += 32;
672            }
673    
674            if (var4 == 5)
675            {
676                var7 = this.guiLeft + this.xSize - 28;
677            }
678            else if (var4 > 0)
679            {
680                var7 += var4;
681            }
682    
683            if (var3)
684            {
685                var8 -= 28;
686            }
687            else
688            {
689                var6 += 64;
690                var8 += this.ySize - 4;
691            }
692    
693            GL11.glDisable(GL11.GL_LIGHTING);
694            this.drawTexturedModalRect(var7, var8, var5, var6, 28, var9);
695            this.zLevel = 100.0F;
696            itemRenderer.zLevel = 100.0F;
697            var7 += 6;
698            var8 += 8 + (var3 ? 1 : -1);
699            GL11.glEnable(GL11.GL_LIGHTING);
700            GL11.glEnable(GL12.GL_RESCALE_NORMAL);
701            ItemStack var10 = new ItemStack(par1CreativeTabs.getTabIconItem());
702            itemRenderer.renderItemIntoGUI(this.fontRenderer, this.mc.renderEngine, var10, var7, var8);
703            itemRenderer.renderItemOverlayIntoGUI(this.fontRenderer, this.mc.renderEngine, var10, var7, var8);
704            GL11.glDisable(GL11.GL_LIGHTING);
705            itemRenderer.zLevel = 0.0F;
706            this.zLevel = 0.0F;
707        }
708    
709        /**
710         * Fired when a control is clicked. This is the equivalent of ActionListener.actionPerformed(ActionEvent e).
711         */
712        protected void actionPerformed(GuiButton par1GuiButton)
713        {
714            if (par1GuiButton.id == 0)
715            {
716                this.mc.displayGuiScreen(new GuiAchievements(this.mc.statFileWriter));
717            }
718    
719            if (par1GuiButton.id == 1)
720            {
721                this.mc.displayGuiScreen(new GuiStats(this, this.mc.statFileWriter));
722            }
723        }
724    
725        public int func_74230_h()
726        {
727            return field_74241_p;
728        }
729    
730        /**
731         * Returns the creative inventory
732         */
733        static InventoryBasic getInventory()
734        {
735            return inventory;
736        }
737    }