Advertisement
Mouamle

TileQuarry.java

Jan 28th, 2017
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.18 KB | None | 0 0
  1.  
  2. public class TileQuarry extends TileEntity implements ITickable {
  3.  
  4.     private IEnergyStorage energy = new EnergyStorage(8000);
  5.     BlockPos q1, q2, last;
  6.  
  7.     int speed = 0, ticks = 0, range = 8;
  8.     boolean built = false;
  9.  
  10.     QuarryTier tier;
  11.  
  12.     public TileQuarry() {
  13.         energy.receiveEnergy(1, false);
  14.         tier = new QuarryTier(this);
  15.     }
  16.  
  17.     @Override
  18.     public void update() {
  19.         if (!worldObj.isRemote) {
  20.  
  21.             tier.tick();
  22.  
  23.             if (tier.TierNumber == 3)
  24.                 range = 8;
  25.             else if (tier.TierNumber == 1)
  26.                 range = 4;
  27.             else
  28.                 range = 1;
  29.  
  30.             if (q1 == null) {
  31.                 q1 = pos.add(-range, -2, -range);
  32.                 last = q1;
  33.             }
  34.             if (q2 == null)
  35.                 q2 = pos.add(range, -2, range);
  36.  
  37.             if (!worldObj.isBlockPowered(pos)) {
  38.                 return;
  39.             }
  40.  
  41.             if (range == 1) {
  42.                 if (!built) {
  43.                     build();
  44.                     built = true;
  45.                     return;
  46.                 }
  47.             }
  48.  
  49.             if (speed == 0 || ticks % speed == 0) {
  50.  
  51.                 Block block = worldObj.getBlockState(last).getBlock();
  52.                 if (block != null && block != Blocks.AIR && block != Blocks.BEDROCK && worldObj.getTileEntity(last) == null) {
  53.  
  54.                     TileEntity inv = worldObj.getTileEntity(pos.add(0, 1, 0));
  55.  
  56.                     List<ItemStack> drops = block.getDrops(worldObj, last, (IBlockState) block.getDefaultState(), 5);
  57.  
  58.                     if (inv != null && inv instanceof IInventory) {
  59.                         if (putInInventory(drops))
  60.                             breakBlock(last);
  61.                         else
  62.                             return;
  63.                     } else {
  64.                         spawnItems(drops);
  65.                         breakBlock(last);
  66.                     }
  67.  
  68.                 }
  69.  
  70.                 if (last.getX() < q2.getX())
  71.                     last = last.add(1, 0, 0);
  72.                 else if (last.getZ() < q2.getZ())
  73.                     last = new BlockPos(q1.getX(), last.getY(), last.getZ() + 1);
  74.                 else if (last.getY() - 1 > 0)
  75.                     last = new BlockPos(q1.getX(), last.getY() - 1, q1.getZ());
  76.             }
  77.             ticks++;
  78.         }
  79.     }
  80.  
  81.     public ItemStack[] getUpgrades() {
  82.         return tier.upgrades;
  83.     }
  84.  
  85.     public int getTier() {
  86.         return tier.TierNumber;
  87.     }
  88.  
  89.     public BlockPos getCurrentLocation() {
  90.         return last;
  91.     }
  92.  
  93.     private void build() {
  94.         placeBlock(Blocks.COBBLESTONE, pos.add(1, 0, 0));
  95.         placeBlock(Blocks.COBBLESTONE, pos.add(2, 0, 0));
  96.  
  97.         placeBlock(Blocks.COBBLESTONE, pos.add(-1, 0, 0));
  98.         placeBlock(Blocks.COBBLESTONE, pos.add(-2, 0, 0));
  99.  
  100.         placeBlock(Blocks.COBBLESTONE, pos.add(0, 0, 1));
  101.         placeBlock(Blocks.COBBLESTONE, pos.add(0, 0, 2));
  102.  
  103.         placeBlock(Blocks.COBBLESTONE, pos.add(0, 0, -1));
  104.         placeBlock(Blocks.COBBLESTONE, pos.add(0, 0, -2));
  105.     }
  106.  
  107.     private boolean putInInventory(List<ItemStack> items) {
  108.         IInventory inv = TileEntityHopper.getInventoryAtPosition(worldObj, pos.getX(), pos.getY() + 1, pos.getZ());
  109.  
  110.         if (inv == null) {
  111.             return false;
  112.         }
  113.  
  114.         for (ItemStack stack : items) {
  115.             ItemStack leftover = TileEntityHopper.putStackInInventoryAllSlots(inv, stack, EnumFacing.DOWN);
  116.             List<ItemStack> is = new ArrayList<>();
  117.             if (items.contains(leftover))
  118.                 is.add(leftover);
  119.             spawnItems(is);
  120.         }
  121.  
  122.         return true;
  123.     }
  124.  
  125.     private void spawnItems(List<ItemStack> items) {
  126.         EntityItem entItem = new EntityItem(worldObj, pos.getX() + 0.5, pos.getY() + 3, pos.getZ() + 0.5);
  127.  
  128.         entItem.motionX = 0;
  129.         entItem.motionY = Math.random();
  130.         entItem.motionZ = 0;
  131.  
  132.         for (ItemStack item : items) {
  133.             entItem.setEntityItemStack(item);
  134.             worldObj.spawnEntityInWorld(entItem);
  135.         }
  136.  
  137.         entItem = null;
  138.     }
  139.  
  140.     @SuppressWarnings("deprecation")
  141.     private void breakBlock(BlockPos pos) {
  142.         worldObj.setBlockState(pos, Blocks.AIR.getDefaultState());
  143.         worldObj.playSound(null, pos.getX(), pos.getY(), pos.getZ(), worldObj.getBlockState(pos).getBlock().getSoundType().getBreakSound(), SoundCategory.BLOCKS, 1F, 1F);
  144.     }
  145.  
  146.     @SuppressWarnings("deprecation")
  147.     private void placeBlock(Block block, BlockPos position) {
  148.         position = position.add(0, -1, 0);
  149.         worldObj.setBlockState(position, block.getDefaultState());
  150.         worldObj.playSound(null, position, block.getSoundType().getBreakSound(), SoundCategory.BLOCKS, 1F, 1F);
  151.     }
  152.  
  153.     @Override
  154.     public NBTTagCompound writeToNBT(NBTTagCompound tag) {
  155.         super.writeToNBT(tag);
  156.         tag.setLong("q1", q1.toLong());
  157.         tag.setLong("q2", q2.toLong());
  158.         tag.setLong("last", last.toLong());
  159.         tag.setInteger("range", range);
  160.         tag.setBoolean("built", built);
  161.         tag.setInteger("Energy", energy.getEnergyStored());
  162.         return tag;
  163.     }
  164.  
  165.     @Override
  166.     public void readFromNBT(NBTTagCompound tag) {
  167.         super.readFromNBT(tag);
  168.  
  169.         if (tag.hasKey("Energy"))
  170.             energy.receiveEnergy(tag.getInteger("Energy"), false);
  171.  
  172.         if (tag.hasKey("q1"))
  173.             q1 = BlockPos.fromLong(tag.getLong("q1"));
  174.         if (tag.hasKey("q2"))
  175.             q2 = BlockPos.fromLong(tag.getLong("q2"));
  176.  
  177.         if (tag.hasKey("last"))
  178.             last = BlockPos.fromLong(tag.getLong("last"));
  179.  
  180.         if (tag.hasKey("range"))
  181.             range = tag.getInteger("range");
  182.  
  183.         if (tag.hasKey("built"))
  184.             built = tag.getBoolean("built");
  185.     }
  186.  
  187.     @Override
  188.     public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) {
  189.         return capability == CapabilityEnergy.ENERGY || super.hasCapability(capability, facing);
  190.     }
  191.  
  192.     @SuppressWarnings("unchecked")
  193.     @Override
  194.     public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing) {
  195.         if (capability == CapabilityEnergy.ENERGY)
  196.             return (T) energy;
  197.         return super.getCapability(capability, facing);
  198.     }
  199.  
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement