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