Advertisement
Superloup10

Container qui ne se sauvegarde pas

Mar 21st, 2014
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.01 KB | None | 0 0
  1. package wolf_addons.common.tileentity;
  2.  
  3. import net.minecraft.entity.player.EntityPlayer;
  4. import net.minecraft.inventory.IInventory;
  5. import net.minecraft.inventory.ISidedInventory;
  6. import net.minecraft.item.ItemStack;
  7. import net.minecraft.nbt.NBTTagCompound;
  8. import net.minecraft.nbt.NBTTagList;
  9. import net.minecraft.network.NetworkManager;
  10. import net.minecraft.network.Packet;
  11. import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
  12. import net.minecraft.tileentity.TileEntity;
  13.  
  14. public class TileEntityCompressor extends TileEntity implements IInventory, ISidedInventory
  15. {
  16.     private ItemStack[] compressorInventory = new ItemStack[9];
  17.     private String customName;
  18.    
  19.     @Override
  20.     public int getSizeInventory()
  21.     {
  22.         return this.compressorInventory.length;
  23.     }
  24.  
  25.     @Override
  26.     public ItemStack getStackInSlot(int slot)
  27.     {
  28.         return this.compressorInventory[slot];
  29.     }
  30.  
  31.     @Override
  32.     public ItemStack decrStackSize(int slot, int maxStack)
  33.     {
  34.         if(this.compressorInventory[slot] != null)
  35.         {
  36.             ItemStack itemStack;
  37.             if(this.compressorInventory[slot].stackSize <= maxStack)
  38.             {
  39.                 itemStack = this.compressorInventory[slot];
  40.                 this.compressorInventory[slot] = null;
  41.                 return itemStack;
  42.             }
  43.             else
  44.             {
  45.                 itemStack = this.compressorInventory[slot].splitStack(maxStack);
  46.                 if(this.compressorInventory[slot].stackSize == 0)
  47.                 {
  48.                     this.compressorInventory[slot] = null;
  49.                 }
  50.                 return itemStack;
  51.             }
  52.         }
  53.         else
  54.         {
  55.             return null;
  56.         }
  57.     }
  58.  
  59.     @Override
  60.     public ItemStack getStackInSlotOnClosing(int slot)
  61.     {
  62.         if (this.compressorInventory[slot] != null)
  63.         {
  64.             ItemStack itemstack = this.compressorInventory[slot];
  65.             this.compressorInventory[slot] = null;
  66.             return itemstack;
  67.         }
  68.         else
  69.         {
  70.             return null;
  71.         }
  72.     }
  73.  
  74.     @Override
  75.     public void setInventorySlotContents(int slot, ItemStack itemStack)
  76.     {
  77.         this.compressorInventory[slot] = itemStack;
  78.  
  79.         if (itemStack != null && itemStack.stackSize > this.getInventoryStackLimit())
  80.         {
  81.             itemStack.stackSize = this.getInventoryStackLimit();
  82.         }
  83.     }
  84.    
  85.     @Override
  86.     public void readFromNBT(NBTTagCompound nbtTagCompound)
  87.     {
  88.         super.readFromNBT(nbtTagCompound);
  89.        
  90.         NBTTagList nbtTag = nbtTagCompound.getTagList("Items", 10);
  91.         this.compressorInventory = new ItemStack[this.getSizeInventory()];
  92.        
  93.         if(nbtTagCompound.hasKey("CustomName"))
  94.         {
  95.             this.customName = nbtTagCompound.getString("CustomName");
  96.         }
  97.        
  98.         for(int i = 0; i < nbtTag.tagCount(); ++i)
  99.         {
  100.             NBTTagCompound tagCompound = nbtTag.getCompoundTagAt(i);
  101.             byte slot = tagCompound.getByte("Slot");
  102.            
  103.             if(slot >= 0 && slot < this.compressorInventory.length)
  104.             {
  105.                 this.compressorInventory[slot] = ItemStack.loadItemStackFromNBT(tagCompound);
  106.             }
  107.         }
  108.     }
  109.    
  110.     @Override
  111.     public void writeToNBT(NBTTagCompound nbtTagCompound)
  112.     {
  113.         super.writeToNBT(nbtTagCompound);
  114.        
  115.         NBTTagList nbtTag = new NBTTagList();
  116.        
  117.         for(int j = 0; j < this.compressorInventory.length; ++j)
  118.         {
  119.             ItemStack itemStack = this.compressorInventory[j];
  120.             System.out.println("Taille de l'inventaire : " + Boolean.valueOf(compressorInventory[j] == null));
  121.             //TODO Cette condition est toujours null
  122.             if(itemStack != null)
  123.             {
  124.                 NBTTagCompound tagCompound = new NBTTagCompound();
  125.                 tagCompound.setByte("Slot", (byte)j);
  126.                 itemStack.writeToNBT(tagCompound);
  127.                 nbtTag.appendTag(tagCompound);
  128.             }
  129.         }
  130.         nbtTagCompound.setTag("Items", nbtTag);
  131.        
  132.         if(this.hasCustomInventoryName())
  133.         {
  134.             nbtTagCompound.setString("CustomName", this.customName);
  135.         }
  136.     }
  137.    
  138.     public Packet getDescriptionPacket()
  139.     {
  140.         NBTTagCompound nbttagcompound = new NBTTagCompound();
  141.         this.writeToNBT(nbttagcompound);
  142.         return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, 3, nbttagcompound);
  143.     }
  144.  
  145.     public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity updateTileEntity)
  146.     {
  147.         this.readFromNBT(updateTileEntity.func_148857_g());
  148.     }
  149.  
  150.     @Override
  151.     public String getInventoryName()
  152.     {
  153.         return this.hasCustomInventoryName() ? this.customName : "container.compressor";
  154.     }
  155.  
  156.     @Override
  157.     public boolean hasCustomInventoryName()
  158.     {
  159.         return this.customName != null && this.customName.length() > 0;
  160.     }
  161.  
  162.     @Override
  163.     public int getInventoryStackLimit()
  164.     {
  165.         return 64;
  166.     }
  167.  
  168.     @Override
  169.     public boolean isUseableByPlayer(EntityPlayer player)
  170.     {
  171.         return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : player.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;
  172.     }
  173.  
  174.     @Override
  175.     public void openInventory() {}
  176.  
  177.     @Override
  178.     public void closeInventory() {}
  179.  
  180.     @Override
  181.     public boolean isItemValidForSlot(int slot, ItemStack itemStack)
  182.     {
  183.         return false;
  184.     }
  185.  
  186.     @Override
  187.     public int[] getAccessibleSlotsFromSide(int side)
  188.     {
  189.         return null;
  190.     }
  191.  
  192.     @Override
  193.     public boolean canInsertItem(int slot, ItemStack item, int side)
  194.     {
  195.         return true;
  196.     }
  197.  
  198.     @Override
  199.     public boolean canExtractItem(int slot, ItemStack item, int side)
  200.     {
  201.         return true;
  202.     }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement