Guest User

TileEntity

a guest
Dec 12th, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.34 KB | None | 0 0
  1. package com.camerpon900.realauto2.blocks.TileEntities;
  2.  
  3. import com.camerpon900.realauto2.blocks.Blocks;
  4. import com.camerpon900.realauto2.utils.WorldHelper;
  5. import net.minecraft.entity.player.EntityPlayer;
  6. import net.minecraft.inventory.ISidedInventory;
  7. import net.minecraft.item.ItemStack;
  8. import net.minecraft.nbt.NBTTagCompound;
  9. import net.minecraft.nbt.NBTTagList;
  10. import net.minecraft.tileentity.TileEntity;
  11. import net.minecraft.world.World;
  12. import net.minecraft.world.biome.BiomeGenDesert;
  13. import net.minecraftforge.common.util.ForgeDirection;
  14.  
  15. public class TileEntityEnergyCube extends TileEntity implements ISidedInventory {
  16.  
  17.     private ItemStack[] slots = new ItemStack[2];
  18.  
  19.     public int power = 0;
  20.     private int maxPower = 5000000;
  21.     private int inputSpeed = 20; //20 every tick
  22.  
  23.     public TileEntityEnergyCube() {
  24.     }
  25.  
  26.     public void updateEntity() {
  27.         chargeCube(worldObj);
  28.     }
  29.  
  30.     public void readFromNBT(NBTTagCompound nbt) {
  31.         super.readFromNBT(nbt);
  32.  
  33.         System.out.println("slots " + this.slots);
  34.  
  35.         System.out.println("reading nbt");
  36.  
  37.         NBTTagList list = nbt.getTagList("Slots", 10);
  38.         NBTTagList powerList = nbt.getTagList("Power", power);
  39.         this.slots = new ItemStack[getSizeInventory()];
  40.  
  41.         for (int i = 0; i < list.tagCount(); i++) {
  42.             NBTTagCompound item = list.getCompoundTagAt(i);
  43.             NBTTagCompound power = list.getCompoundTagAt(i);
  44.             byte b = item.getByte("Item");
  45.             int p = item.getInteger("Power");
  46.  
  47.             if (b >= 0 && b < this.slots.length) {
  48.                 this.slots[b] = ItemStack.loadItemStackFromNBT(item);
  49.             }
  50.  
  51.             if (p >= 0 && p < this.maxPower) {
  52.                 this.power = power.getInteger("Power");
  53.             }
  54.         }
  55.     }
  56.  
  57.     public void writeToNBT(NBTTagCompound nbt) {
  58.         super.writeToNBT(nbt);
  59.         System.out.println("Writing To NBT at x: " + xCoord + " y: " + yCoord + " z: " + zCoord);
  60.  
  61.         NBTTagList list = new NBTTagList();
  62.  
  63.         for (int i = 0; i < slots.length; i++) {
  64.             if (this.slots[i] != null) {
  65.                 NBTTagCompound item = new NBTTagCompound();
  66.                 item.setByte("Item", (byte) i);
  67.                 this.slots[i].writeToNBT(item);
  68.                 list.appendTag(item);
  69.             }
  70.         }
  71.  
  72.         if (power >= 1) {
  73.             NBTTagCompound power = new NBTTagCompound();
  74.             power.setInteger("Power", this.power);
  75.             list.appendTag(power);
  76.         }
  77.         nbt.setTag("Power", list);
  78.         nbt.setTag("Slots", list);
  79.     }
  80.  
  81.     public int getSizeInventory() {
  82.         return this.slots.length;
  83.     }
  84.  
  85.     public int[] getAccessibleSlotsFromSide(int p_94128_1_) {
  86.         return new int[0];
  87.     }
  88.  
  89.     public boolean canInsertItem(int p_102007_1_, ItemStack p_102007_2_, int p_102007_3_) {
  90.         return this.isItemValidForSlot(p_102007_1_, p_102007_2_);
  91.     }
  92.  
  93.     public boolean canExtractItem(int p_102008_1_, ItemStack p_102008_2_, int p_102008_3_) {
  94.         return true;
  95.     }
  96.  
  97.     public ItemStack getStackInSlot(int i) {
  98.         return this.slots[i];
  99.     }
  100.  
  101.     public ItemStack decrStackSize(int i, int j) {
  102.         if (this.slots[i] != null) {
  103.             System.out.println(this.slots[i]);
  104.             ItemStack itemStack;
  105.             if (this.slots[i].stackSize <= j) {
  106.                 itemStack = this.slots[i];
  107.                 this.slots[i] = null;
  108.                 return itemStack;
  109.             } else {
  110.                 itemStack = this.slots[i].splitStack(j);
  111.                 if (this.slots[i].stackSize == 0) {
  112.                     this.slots[i] = null;
  113.                 }
  114.             }
  115.             return itemStack;
  116.         }
  117.  
  118.         return null;
  119.     }
  120.  
  121.     public ItemStack getStackInSlotOnClosing(int slot) {
  122.         ItemStack stack = getStackInSlot(slot);
  123.         if (stack != null) {
  124.             setInventorySlotContents(slot, null);
  125.         }
  126.         return stack;
  127.     }
  128.  
  129.     public void setInventorySlotContents(int i, ItemStack itemStack) {
  130.         this.slots[i] = itemStack;
  131.  
  132.         if (itemStack != null && itemStack.stackSize > this.getInventoryStackLimit()) {
  133.             itemStack.stackSize = this.getInventoryStackLimit();
  134.         }
  135.     }
  136.  
  137.     public String getInventoryName() {
  138.         return null;
  139.     }
  140.  
  141.     public boolean hasCustomInventoryName() {
  142.         return false;
  143.     }
  144.  
  145.     public int getInventoryStackLimit() {
  146.         return 64;
  147.     }
  148.  
  149.     public boolean isUseableByPlayer(EntityPlayer p_70300_1_) {
  150.         return true;
  151.     }
  152.  
  153.     public void openInventory() {
  154.  
  155.     }
  156.  
  157.     public void closeInventory() {
  158.  
  159.     }
  160.  
  161.     public boolean isItemValidForSlot(int p_94041_1_, ItemStack p_94041_2_) {
  162.         return true;
  163.     }
  164.  
  165.     public void charge() {
  166.  
  167.     }
  168.  
  169.     public void chargeCube(World world) {
  170.         if (world.getBlock(xCoord + 1, yCoord, zCoord) == Blocks.blockBasicSolarPanel) {
  171.             System.out.println("Test");
  172.             TileEntity tileEntity = world.getTileEntity(xCoord + 1, yCoord, zCoord);
  173.         }
  174.         if (world.getBlock(xCoord - 1, yCoord, zCoord) == Blocks.blockBasicSolarPanel) {
  175.             System.out.println("Test");
  176.             TileEntity tileEntity = world.getTileEntity(xCoord - 1, yCoord, zCoord);
  177.         }
  178.         if (world.getBlock(xCoord, yCoord + 1, zCoord) == Blocks.blockBasicSolarPanel) {
  179.             System.out.println("Test");
  180.             TileEntity tileEntity = world.getTileEntity(xCoord, yCoord + 1, zCoord);
  181.         }
  182.         if (world.getBlock(xCoord, yCoord, zCoord + 1) == Blocks.blockBasicSolarPanel) {
  183.             System.out.println("Test");
  184.             TileEntity tileEntity = world.getTileEntity(xCoord, yCoord, zCoord + 1);
  185.         }
  186.         if (world.getBlock(xCoord, yCoord, zCoord - 1) == Blocks.blockBasicSolarPanel) {
  187.             System.out.println("Test");
  188.             TileEntity tileEntity = world.getTileEntity(xCoord, yCoord, zCoord - 1);
  189.             if (tileEntity instanceof TileEntitySolarPanel) {
  190.                 ((TileEntitySolarPanel) tileEntity).power -= this.inputSpeed;
  191.                 if (((TileEntitySolarPanel) tileEntity).power != 0) {
  192.                     this.power += inputSpeed;
  193.                 }
  194.             }
  195.         }
  196.     }
  197.  
  198.     public int getPowerScaled(int scaled) {
  199.         return (int) scaled;
  200.     }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment