Advertisement
Guest User

Untitled

a guest
Oct 30th, 2015
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.31 KB | None | 0 0
  1. package com.cout970.magneticraft.tileentity.shelf;
  2.  
  3. import com.cout970.magneticraft.ManagerBlocks;
  4. import com.cout970.magneticraft.api.util.MgDirection;
  5. import com.cout970.magneticraft.api.util.VecInt;
  6. import com.cout970.magneticraft.api.util.VecIntUtil;
  7. import com.cout970.magneticraft.block.BlockMg;
  8. import com.cout970.magneticraft.tileentity.TileShelf;
  9. import com.cout970.magneticraft.util.InventoryResizable;
  10. import net.minecraft.block.Block;
  11. import net.minecraft.entity.player.EntityPlayer;
  12. import net.minecraft.init.Blocks;
  13. import net.minecraft.item.ItemStack;
  14. import net.minecraft.nbt.NBTTagCompound;
  15. import net.minecraft.util.AxisAlignedBB;
  16. import net.minecraft.util.ChatComponentText;
  17.  
  18. import java.util.ArrayList;
  19. import java.util.List;
  20.  
  21. public class TileShelvingUnit extends TileShelf {
  22.     public static final int MAX_CRATES = 24;
  23.     public static final int MAX_SHELVES = 3;
  24.     public static final int CRATE_SIZE = 27;
  25.     public static final int SHELF_CRATES = MAX_CRATES / MAX_SHELVES;
  26.     private int crates;
  27.     private InventoryResizable[] rowInv = new InventoryResizable[MAX_SHELVES];
  28.     private boolean placing;
  29.  
  30.     public TileShelvingUnit() {
  31.         crates = 0;
  32.         for (int i = 0; i < MAX_SHELVES; i++) {
  33.             rowInv[i] = new InventoryResizable(this, CRATE_SIZE * SHELF_CRATES, "Shelf " + i);
  34.             rowInv[i].lock();
  35.         }
  36.     }
  37.  
  38.     @Override
  39.     public void updateEntity() {
  40.         if ((crates > (MAX_CRATES - SHELF_CRATES))) {
  41.             createTopShelf();
  42.         }
  43.     }
  44.  
  45.     public InventoryResizable getInv(int i) {
  46.         return rowInv[i];
  47.     }
  48.  
  49.     @Override
  50.     public VecInt getOffset() {
  51.         return VecInt.NULL_VECTOR;
  52.     }
  53.  
  54.     @Override
  55.     public TileShelvingUnit getMainTile() {
  56.         return this;
  57.     }
  58.  
  59.     public boolean addCrate() {
  60.         if (crates < MAX_CRATES) {
  61.             if (crates == (MAX_CRATES - SHELF_CRATES) && !createTopShelf()) {
  62.                 return false;
  63.             }
  64.             if (rowInv[crates / SHELF_CRATES].resize(CRATE_SIZE)) {
  65.                 crates++;
  66.                 return true;
  67.             }
  68.         }
  69.         return false;
  70.     }
  71.  
  72.     private boolean createTopShelf() {
  73.         List<VecInt> placeCoords = new ArrayList<>();
  74.         for (int r = -2; r <= 2; r++) {
  75.             for (int b = 0; b < 2; b++) {
  76.                 VecInt coord = VecIntUtil.getRotatedOffset(MgDirection.getDirection(getBlockMetadata()), r, 3, b).add(xCoord, yCoord, zCoord);
  77.                 Block block = coord.getBlock(worldObj);
  78.                 if (block != ManagerBlocks.shelving_unit) {
  79.                     if (block == null || coord.isBlockReplaceable(worldObj)) {
  80.                         placeCoords.add(coord);
  81.                     } else {
  82.                         return false;
  83.                     }
  84.                 }
  85.             }
  86.         }
  87.         for (VecInt coord : placeCoords) {
  88.             if (!coord.blockExists(worldObj))
  89.                 return false;
  90.         }
  91.         for (VecInt coord : placeCoords) {
  92.             coord.setBlockWithMetadata(worldObj, ManagerBlocks.shelving_unit, 10, 7);
  93.             ((TileShelfFiller) coord.getTileEntity(worldObj)).setOffset(coord.copy().add(-xCoord, -yCoord, -zCoord));
  94.         }
  95.         return true;
  96.     }
  97.  
  98.     public boolean removeCrate() {
  99.         if (crates > 0) {
  100.             if (rowInv[(crates - 1) / SHELF_CRATES].resize(-CRATE_SIZE)) {
  101.                 crates--;
  102.                 if (crates == (MAX_CRATES - SHELF_CRATES)) {
  103.                     clearTopShelf();
  104.                 }
  105.                 return true;
  106.             }
  107.         }
  108.         return false;
  109.     }
  110.  
  111.     private void clearTopShelf() {
  112.         for (int r = -2; r <= 2; r++) {
  113.             for (int b = 0; b < 2; b++) {
  114.                 VecInt coord = VecIntUtil.getRotatedOffset(MgDirection.getDirection(getBlockMetadata()), r, 3, b).add(xCoord, yCoord, zCoord);
  115.                 if ((coord.getBlock(worldObj) == ManagerBlocks.shelving_unit) && (coord.getBlockMetadata(worldObj) == 10)) {
  116.                     ((TileShelfFiller) coord.getTileEntity(worldObj)).silentRemoval = true;
  117.                     worldObj.setBlockToAir(coord.getX(), coord.getY(), coord.getZ());
  118.                 }
  119.             }
  120.         }
  121.     }
  122.  
  123.     @Override
  124.     public void onBlockBreaks() {
  125.         if (worldObj.isRemote) return;
  126.         for (InventoryResizable inv : rowInv) {
  127.             for (int i = 0; i < inv.getSizeInventory(); i++) {
  128.                 BlockMg.dropItem(inv.getStackInSlot(i), worldObj.rand, xCoord, yCoord, zCoord, worldObj);
  129.             }
  130.         }
  131.         if (getCrateCount() > 0) {
  132.             BlockMg.dropItem(new ItemStack(Blocks.chest, getCrateCount()), worldObj.rand, xCoord, yCoord, zCoord, worldObj);
  133.         }
  134.     }
  135.  
  136.     public int getCrateCount() {
  137.         return crates;
  138.     }
  139.  
  140.     public MgDirection getDirection() {
  141.         return MgDirection.getDirection(getBlockMetadata());
  142.     }
  143.  
  144.     @Override
  145.     public AxisAlignedBB getRenderBoundingBox() {
  146.         VecInt v1 = VecIntUtil.getRotatedOffset(MgDirection.getDirection(getBlockMetadata()), -2, 0, 0);
  147.         VecInt v2 = VecIntUtil.getRotatedOffset(MgDirection.getDirection(getBlockMetadata()), 2, 3, 1);
  148.         VecInt block = new VecInt(xCoord, yCoord, zCoord);
  149.  
  150.         return VecIntUtil.getAABBFromVectors(v1.add(block), v2.add(block));
  151.     }
  152.  
  153.     @Override
  154.     public void readFromNBT(NBTTagCompound nbt) {
  155.         super.readFromNBT(nbt);
  156.         crates = nbt.getInteger("crates");
  157.         placing = nbt.getBoolean("isPlacing");
  158.         for (int i = 0; i < MAX_SHELVES; i++) {
  159.             rowInv[i].readFromNBT(nbt, rowInv[i].name);
  160.         }
  161.     }
  162.  
  163.     @Override
  164.     public void writeToNBT(NBTTagCompound nbt) {
  165.         super.writeToNBT(nbt);
  166.         nbt.setInteger("crates", crates);
  167.         nbt.setBoolean("isPlacing", placing);
  168.         for (int i = 0; i < MAX_SHELVES; i++) {
  169.             rowInv[i].writeToNBT(nbt, rowInv[i].name);
  170.         }
  171.     }
  172.  
  173.     public void setPlacing(boolean placing, EntityPlayer p) {
  174.         if (p != null && !worldObj.isRemote) {
  175.             p.addChatComponentMessage(new ChatComponentText(placing ? "Switched to placement mode" : "Switched to inventory mode"));
  176.         }
  177.         this.placing = placing;
  178.     }
  179.  
  180.     public boolean isPlacing() {
  181.         return placing;
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement