Advertisement
Guest User

Untitled

a guest
Feb 6th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.34 KB | None | 0 0
  1. CONTAINER
  2.  
  3. package RpgInventory;
  4.  
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileNotFoundException;
  8. import java.io.FileOutputStream;
  9. import java.io.IOException;
  10.  
  11. import net.minecraft.entity.player.EntityPlayer;
  12. import net.minecraft.inventory.Container;
  13. import net.minecraft.inventory.Slot;
  14. import net.minecraft.item.ItemStack;
  15. import net.minecraft.nbt.CompressedStreamTools;
  16. import net.minecraft.nbt.NBTTagCompound;
  17. public class RpgContainer extends Container {
  18.          File rpgfile = new File("rpg.dat");
  19.          NBTTagCompound nbt1;
  20.          NBTTagCompound nbt2;
  21.          
  22.     public RpgInventory rpgInv;
  23.     public RpgContainer (EntityPlayer p1, RpgInventory rpg)
  24.         {
  25.          super();
  26.          rpgInv = rpg;
  27.          
  28.         this.addSlotToContainer(new SlotRpgArmor( this, rpgInv,  0,  6,  16,  0));// necklace
  29.         this.addSlotToContainer(new SlotRpgArmor( this, rpgInv,  1,  6,  37,  1));//shield
  30.         this.addSlotToContainer(new SlotRpgArmor( this, rpgInv,  2,  82, 16,  2));//cloak
  31.         this.addSlotToContainer(new SlotRpgArmor( this, rpgInv,  3,  82, 38,  3));//gloves
  32.         this.addSlotToContainer(new SlotRpgArmor( this, rpgInv,  4,  82, 59,  4));//ring
  33.         this.addSlotToContainer(new SlotRpgArmor( this, rpgInv,  5,  6,  58,  4));//ring
  34.    
  35.  
  36.          for (int var4 = 0; var4 < 3; ++var4)
  37.             {
  38.                 for (int var5 = 0; var5 < 9; ++var5)
  39.                 {
  40.                     this.addSlotToContainer(new Slot(p1.inventory, (var5 + (var4 + 1) * 9), 8 + var5 * 18, 84 + var4 * 18));
  41.                 }
  42.             }
  43.  
  44.             for (int var4 = 0; var4 < 9; ++var4)
  45.             {
  46.                 this.addSlotToContainer(new Slot(p1.inventory, var4, 8 + var4 * 18, 142));
  47.             }
  48.           this.onGuiOpen(rpgInv);
  49.         }
  50.    
  51.     public void onGuiOpen(RpgInventory inv)
  52.     {
  53.         inv = rpgInv;
  54.         try {
  55.         File var42 = new File("rpg.dat");
  56.         NBTTagCompound var52 = new NBTTagCompound();
  57.         var52 = CompressedStreamTools.readCompressed(new FileInputStream(var42));
  58.         inv.readFromNBT(var52);
  59.     } catch (FileNotFoundException e) {
  60.         e.printStackTrace();
  61.     } catch (IOException e) {
  62.         e.printStackTrace();
  63.     }
  64.     }
  65.    
  66.      @Override
  67.      public void onCraftGuiClosed(EntityPlayer player)
  68.      {
  69.            try {
  70.                File var4 = new File("rpg.dat");
  71.                NBTTagCompound var2 = new NBTTagCompound();
  72.                CompressedStreamTools.writeCompressed(var2, new FileOutputStream(var4));
  73.                var2 = rpgInv.writeToNBT(new NBTTagCompound());
  74.  
  75.         } catch (FileNotFoundException e) {
  76.             e.printStackTrace();
  77.         } catch (IOException e) {
  78.             e.printStackTrace();
  79.         }
  80.      }
  81.  
  82.       public boolean doesGuiPauseGame()
  83.         {
  84.             return false;
  85.         }
  86.      
  87.       @Override
  88.       public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2)
  89.         {
  90.             return null;   
  91.         }
  92.      
  93.     public boolean canInteractWith(EntityPlayer var1) {
  94.         return true;
  95.     }
  96. }
  97.  
  98.  
  99. INVENTORY
  100.  
  101. package RpgInventory;
  102.  
  103. import net.minecraft.entity.player.EntityPlayer;
  104. import net.minecraft.inventory.IInventory;
  105. import net.minecraft.item.ItemStack;
  106. import net.minecraft.nbt.NBTTagCompound;
  107. import net.minecraft.nbt.NBTTagList;
  108.  
  109. public class RpgInventory implements IInventory {
  110.  
  111.      ItemStack[] armorSlots = new ItemStack[6];
  112.      
  113.      private EntityPlayer player;
  114.      public RpgInventory instance;
  115.      public RpgInventory(EntityPlayer par1EntityPlayer)
  116.         {
  117.             this.player = par1EntityPlayer;
  118.             instance = this;
  119.         }
  120.      
  121.      public boolean inventoryChanged = false;
  122.      
  123.    
  124.     public int getSizeInventory() {
  125.         return 6;
  126.     }
  127.    
  128.     public ItemStack getJewelInSlot(int par1)
  129.     {
  130.         return this.armorSlots[par1];
  131.     }
  132.      /**
  133.      * Returns a slot index in main inventory containing a specific itemID
  134.      */
  135.     private int findJewel(int par1)
  136.     {
  137.         for (int var2 = 0; var2 < this.armorSlots.length; ++var2)
  138.         {
  139.             if (this.armorSlots[var2] != null && this.armorSlots[var2].itemID == par1)
  140.             {
  141.                 return var2;
  142.             }
  143.         }
  144.  
  145.         return -1;
  146.     }
  147.    
  148.     public boolean getJewel(int par1)
  149.     {
  150.         int var2 = this.findJewel(par1);
  151.         return var2 >= 0;
  152.     }
  153.    
  154.     public ItemStack getJewelFromStack(int par1)
  155.     {
  156.         ItemStack[] var2 = this.armorSlots;
  157.  
  158.         if (par1 >= var2.length)
  159.         {
  160.             par1 -= var2.length;
  161.             var2 = this.armorSlots;
  162.         }
  163.  
  164.         return var2[par1];
  165.     }
  166.  
  167.    
  168.    
  169.     /**
  170.      * Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a
  171.      * new stack.
  172.      */
  173.       public ItemStack decrStackSize(int par1, int par2)
  174.         {
  175.             ItemStack[] var3 = this.armorSlots;
  176.  
  177.             if (var3[par1] != null)
  178.             {
  179.                 ItemStack var4;
  180.  
  181.                 if (var3[par1].stackSize <= par2)
  182.                 {
  183.                     var4 = var3[par1];
  184.                     var3[par1] = null;
  185.                     return var4;
  186.                 }
  187.                 else
  188.                 {
  189.                     var4 = var3[par1].splitStack(par2);
  190.  
  191.                     if (var3[par1].stackSize == 0)
  192.                     {
  193.                         var3[par1] = null;
  194.                     }
  195.  
  196.                     return var4;
  197.                 }
  198.             }
  199.             else
  200.             {
  201.                 return null;
  202.             }
  203.         }
  204.  
  205.         /**
  206.          * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem -
  207.          * like when you close a workbench GUI.
  208.          */
  209.         public ItemStack getStackInSlotOnClosing(int par1)
  210.         {
  211.             ItemStack[] var2 = this.armorSlots;
  212.  
  213.             if (var2[par1] != null)
  214.             {
  215.                 ItemStack var3 = var2[par1];
  216.                 var2[par1] = null;
  217.                 return var3;
  218.             }
  219.             else
  220.             {
  221.                 return null;
  222.             }
  223.         }
  224.  
  225.         public void setInventorySlotContents(int par1, ItemStack par2ItemStack)
  226.         {
  227.             ItemStack[] var3 = this.armorSlots;
  228.             var3[par1] = par2ItemStack;
  229.         }
  230.        
  231.  
  232.         public ItemStack getStackInSlot(int par1)
  233.         {
  234.             ItemStack[] var2 = this.armorSlots;
  235.             return var2[par1];
  236.         }
  237.  
  238.         public String getInvName()
  239.         {
  240.             return "RpgInventory";
  241.         }
  242.        
  243.         public int getInventoryStackLimit()
  244.         {
  245.             return 64;
  246.         }
  247.        
  248.         public void onInventoryChanged()
  249.         {
  250.             this.inventoryChanged = true;
  251.         }
  252.        
  253.         public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)
  254.         {
  255.             return this.player.isDead ? false : par1EntityPlayer.getDistanceSqToEntity(this.player) <= 64.0D;
  256.         }
  257.        
  258.      
  259.         /**
  260.          * Writes the inventory out as a list of compound tags. This is where the slot indices are used (+100 for armor, +80
  261.          * for crafting).
  262.          */
  263.         public NBTTagCompound writeToNBT(NBTTagCompound par1NBTTagCompound)
  264.         {
  265.             System.out.println("written to NBT");
  266.  
  267.             NBTTagList var2 = new NBTTagList();
  268.  
  269.             for (int var3 = 0; var3 < this.armorSlots.length; ++var3)
  270.             {
  271.                 if (this.armorSlots[var3] != null)
  272.                 {
  273.                     NBTTagCompound compoundSlot = new NBTTagCompound();
  274.                     compoundSlot .setByte("Slot", (byte)var3);
  275.                     this.armorSlots[var3].writeToNBT(compoundSlot );
  276.                     var2.appendTag(compoundSlot);
  277.                 }
  278.             }
  279.  
  280.             par1NBTTagCompound.setTag("ItemInSlotX", var2);
  281.             return par1NBTTagCompound;
  282.         }
  283.  
  284.         /**
  285.          * Reads from the given tag list and fills the slots in the inventory with the correct items.
  286.          */
  287.         public void readFromNBT(NBTTagCompound par1NBTTagCompound)
  288.         {
  289.             System.out.println("read from NBT");
  290.             NBTTagList var2 = par1NBTTagCompound.getTagList("ItemInSlotX");
  291.             this.armorSlots = new ItemStack[6];
  292.  
  293.             for (int var3 = 0; var3 < var2.tagCount(); ++var3)
  294.             {
  295.                     NBTTagCompound compoundSlot = (NBTTagCompound)var2.tagAt(var3);
  296.                     byte var5 = compoundSlot .getByte("Slot");
  297.  
  298.                     if (var5 >= 0 && var5 < this.armorSlots.length)
  299.                     {
  300.                             this.armorSlots[var5] = ItemStack.loadItemStackFromNBT(compoundSlot );
  301.                     }
  302.             }
  303.         }
  304.  
  305.         public void openChest() {}
  306.  
  307.         public void closeChest() {}
  308.  
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement