Advertisement
ViiRuS

Gui Tutorial #5 (Slots)

Jan 29th, 2012
2,114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.02 KB | None | 0 0
  1. mod_ file (exerpts)
  2. ============================
  3. public static final Block guiBlock = new BlockGuiBlock(250, net.minecraft.src.AwesomeTileEntity.class).setHardness(0.5F).setBlockName("Gui Block");
  4.  
  5. ModLoader.RegisterTileEntity(net.minecraft.src.AwesomeTileEntity.class, "AwesomeTileEntity");
  6.  
  7. ============================
  8. BlockGuiBlock (Whole file)
  9. ================================
  10. package net.minecraft.src;
  11.  
  12.  
  13. // Referenced classes of package net.minecraft.src:
  14. //            Block, Material
  15.  
  16. public class BlockGuiBlock extends BlockContainer
  17. {
  18.    
  19.     private Class AwesomeEntityClass;
  20.  
  21.     protected BlockGuiBlock(int i, Class classt)
  22.     {
  23.         super(i, Material.ground);
  24.         AwesomeEntityClass = classt;
  25.         blockIndexInTexture = ModLoader.addOverride("/terrain.png", "/mods/guiblock.png");
  26.     }
  27.    
  28.     public void onBlockClicked(World world, int i, int j, int k, EntityPlayer entityplayer)
  29.     {
  30.          Object obj = (AwesomeTileEntity)world.getBlockTileEntity(i, j, k);
  31.          ModLoader.OpenGUI(entityplayer, new BlockGUISlot(entityplayer.inventory,(AwesomeTileEntity)(obj)));
  32.         //ModLoader.OpenGUI(entityplayer, new GuiWhatever());
  33.     }
  34.  
  35.     public TileEntity getBlockEntity()
  36.     {
  37.             try{
  38.                     return (TileEntity)AwesomeEntityClass.newInstance();
  39.             }
  40.             catch(Exception exception){
  41.                     throw new RuntimeException(exception);
  42.             }
  43.     }
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50. }
  51. ===========================
  52. AwesomeContainer (Whole File)
  53. ===========================
  54. package net.minecraft.src;
  55.  
  56. import java.util.List;
  57.  
  58. public class AwesomeContainer extends Container
  59. {
  60.     private AwesomeTileEntity awesome;
  61.  
  62.     public AwesomeContainer(InventoryPlayer inventoryplayer, AwesomeTileEntity tileentityawesome)
  63.     {
  64.         awesome = tileentityawesome;
  65.         addSlot(new Slot(tileentityawesome, 0, 79, 38));
  66.         for (int i = 0; i < 3; i++)
  67.         {
  68.             for (int k = 0; k < 9; k++)
  69.             {
  70.                 addSlot(new Slot(inventoryplayer, k + i * 9 + 9, 8 + k * 18, 84 + i * 18));
  71.             }
  72.         }
  73.  
  74.         for (int j = 0; j < 9; j++)
  75.         {
  76.             addSlot(new Slot(inventoryplayer, j, 8 + j * 18, 142));
  77.         }
  78.     }
  79.  
  80.     public boolean canInteractWith(EntityPlayer entityplayer)
  81.     {
  82.         return awesome.isUseableByPlayer(entityplayer);
  83.     }
  84.  
  85.    
  86. }
  87. ===============================
  88. AwesomeTileEntity (Whole File)
  89. ===============================
  90.  
  91. package net.minecraft.src;
  92.  
  93. public class AwesomeTileEntity extends TileEntity implements IInventory{
  94.  
  95.     public ItemStack awesomeItemStacks[];
  96.    
  97.     public AwesomeTileEntity(){
  98.         awesomeItemStacks = new ItemStack[1];
  99.     }
  100.     @Override
  101.     public int getSizeInventory()
  102.     {  
  103.         return  awesomeItemStacks.length;
  104.     }
  105.  
  106.  
  107.     @Override
  108.     public ItemStack getStackInSlot(int i) {
  109.         return awesomeItemStacks[i];
  110.     }
  111.  
  112.     @Override
  113.     public ItemStack decrStackSize(int i, int j) {
  114.         if (awesomeItemStacks[i] != null)
  115.         {
  116.             if (awesomeItemStacks[i].stackSize <= j)
  117.             {
  118.                 ItemStack itemstack = awesomeItemStacks[i];
  119.                 awesomeItemStacks[i] = null;
  120.                 return itemstack;
  121.             }
  122.             ItemStack itemstack1 = awesomeItemStacks[i].splitStack(j);
  123.             if (awesomeItemStacks[i].stackSize == 0)
  124.             {
  125.                 awesomeItemStacks[i] = null;
  126.             }
  127.             return itemstack1;
  128.         }
  129.         else
  130.         {
  131.             return null;
  132.         }
  133.     }
  134.  
  135.     @Override
  136.     public void setInventorySlotContents(int i, ItemStack itemstack) {
  137.         awesomeItemStacks[i] = itemstack;
  138.         if (itemstack != null && itemstack.stackSize > getInventoryStackLimit())
  139.         {
  140.             itemstack.stackSize = getInventoryStackLimit();
  141.         }
  142.     }
  143.  
  144.     @Override
  145.     public String getInvName()
  146.     {
  147.         return "awesome";
  148.     }
  149.  
  150.     @Override
  151.     public int getInventoryStackLimit() {
  152.         // TODO Auto-generated method stub
  153.         return 64;
  154.     }
  155.  
  156.     @Override
  157.     public void openChest() {
  158.        
  159.     }
  160.  
  161.     @Override
  162.     public void closeChest() {
  163.         // TODO Auto-generated method stub
  164.        
  165.     }
  166.     public boolean done = false;
  167.    
  168.        public void updateEntity()
  169.         {
  170.            ItemStack itemstack = awesomeItemStacks[0];
  171.            if (itemstack != null)
  172.            {
  173.                int i = itemstack.getItem().shiftedIndex;
  174.                 if(i == Item.stick.shiftedIndex)
  175.                     {
  176.                     done = true;
  177.                     itemstack.stackSize = 2;
  178.                     }
  179.            }
  180.            else
  181.            {
  182.                done = false;
  183.            }
  184.            
  185.         }
  186.        
  187.        public boolean isUseableByPlayer(EntityPlayer entityplayer)
  188.         {
  189.             if(worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) != this)
  190.             {
  191.                 return false;
  192.             }
  193.             return entityplayer.getDistanceSq((double)xCoord + 0.5D, (double)yCoord + 0.5D, (double)zCoord + 0.5D) <= 64D;
  194.         }
  195.  
  196. }
  197. =================================
  198. BlockGuiSlot (Whole File)
  199. ================================
  200.  
  201. package net.minecraft.src;
  202.  
  203. import org.lwjgl.opengl.GL11;
  204.  
  205. public class BlockGUISlot extends GuiContainer{
  206.    
  207.     public EntityPlayer player;
  208.     public AwesomeTileEntity tile;
  209.    
  210.     public BlockGUISlot(InventoryPlayer inventoryplayer, AwesomeTileEntity entity)
  211.     {
  212.         super(new AwesomeContainer(inventoryplayer, entity));    
  213.         tile = entity;
  214.         player = ModLoader.getMinecraftInstance().thePlayer;
  215.     }
  216.  
  217.     protected void drawGuiContainerForegroundLayer()
  218.     {
  219.         if(tile.done == true)
  220.         {
  221.         drawString(fontRenderer, "Done", 50, 0, 0xFFFFFF);
  222.         }
  223.     }
  224.  
  225.     protected void drawGuiContainerBackgroundLayer(float f, int i, int j)
  226.     {
  227.         drawDefaultBackground();
  228.         int k = mc.renderEngine.getTexture("/mods/awesome.png");
  229.         GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
  230.         mc.renderEngine.bindTexture(k);
  231.         int l = (width - xSize) / 2;
  232.         int i1 = (height - ySize) / 2;
  233.         drawTexturedModalRect(l, i1, 0, 0, xSize, ySize);
  234.     }
  235.    
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement