Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TileQuarry extends TileEntity implements ITickable {
- private IEnergyStorage energy = new EnergyStorage(8000);
- BlockPos q1, q2, last;
- int speed = 0, ticks = 0, range = 8;
- boolean built = false;
- QuarryTier tier;
- public TileQuarry() {
- energy.receiveEnergy(1, false);
- tier = new QuarryTier(this);
- }
- @Override
- public void update() {
- if (!worldObj.isRemote) {
- tier.tick();
- if (tier.TierNumber == 3)
- range = 8;
- else if (tier.TierNumber == 1)
- range = 4;
- else
- range = 1;
- if (q1 == null) {
- q1 = pos.add(-range, -2, -range);
- last = q1;
- }
- if (q2 == null)
- q2 = pos.add(range, -2, range);
- if (!worldObj.isBlockPowered(pos)) {
- return;
- }
- if (range == 1) {
- if (!built) {
- build();
- built = true;
- return;
- }
- }
- if (speed == 0 || ticks % speed == 0) {
- Block block = worldObj.getBlockState(last).getBlock();
- if (block != null && block != Blocks.AIR && block != Blocks.BEDROCK && worldObj.getTileEntity(last) == null) {
- TileEntity inv = worldObj.getTileEntity(pos.add(0, 1, 0));
- List<ItemStack> drops = block.getDrops(worldObj, last, (IBlockState) block.getDefaultState(), 5);
- if (inv != null && inv instanceof IInventory) {
- if (putInInventory(drops))
- breakBlock(last);
- else
- return;
- } else {
- spawnItems(drops);
- breakBlock(last);
- }
- }
- if (last.getX() < q2.getX())
- last = last.add(1, 0, 0);
- else if (last.getZ() < q2.getZ())
- last = new BlockPos(q1.getX(), last.getY(), last.getZ() + 1);
- else if (last.getY() - 1 > 0)
- last = new BlockPos(q1.getX(), last.getY() - 1, q1.getZ());
- }
- ticks++;
- }
- }
- public ItemStack[] getUpgrades() {
- return tier.upgrades;
- }
- public int getTier() {
- return tier.TierNumber;
- }
- public BlockPos getCurrentLocation() {
- return last;
- }
- private void build() {
- placeBlock(Blocks.COBBLESTONE, pos.add(1, 0, 0));
- placeBlock(Blocks.COBBLESTONE, pos.add(2, 0, 0));
- placeBlock(Blocks.COBBLESTONE, pos.add(-1, 0, 0));
- placeBlock(Blocks.COBBLESTONE, pos.add(-2, 0, 0));
- placeBlock(Blocks.COBBLESTONE, pos.add(0, 0, 1));
- placeBlock(Blocks.COBBLESTONE, pos.add(0, 0, 2));
- placeBlock(Blocks.COBBLESTONE, pos.add(0, 0, -1));
- placeBlock(Blocks.COBBLESTONE, pos.add(0, 0, -2));
- }
- private boolean putInInventory(List<ItemStack> items) {
- IInventory inv = TileEntityHopper.getInventoryAtPosition(worldObj, pos.getX(), pos.getY() + 1, pos.getZ());
- if (inv == null) {
- return false;
- }
- for (ItemStack stack : items) {
- ItemStack leftover = TileEntityHopper.putStackInInventoryAllSlots(inv, stack, EnumFacing.DOWN);
- List<ItemStack> is = new ArrayList<>();
- if (items.contains(leftover))
- is.add(leftover);
- spawnItems(is);
- }
- return true;
- }
- private void spawnItems(List<ItemStack> items) {
- EntityItem entItem = new EntityItem(worldObj, pos.getX() + 0.5, pos.getY() + 3, pos.getZ() + 0.5);
- entItem.motionX = 0;
- entItem.motionY = Math.random();
- entItem.motionZ = 0;
- for (ItemStack item : items) {
- entItem.setEntityItemStack(item);
- worldObj.spawnEntityInWorld(entItem);
- }
- entItem = null;
- }
- @SuppressWarnings("deprecation")
- private void breakBlock(BlockPos pos) {
- worldObj.setBlockState(pos, Blocks.AIR.getDefaultState());
- worldObj.playSound(null, pos.getX(), pos.getY(), pos.getZ(), worldObj.getBlockState(pos).getBlock().getSoundType().getBreakSound(), SoundCategory.BLOCKS, 1F, 1F);
- }
- @SuppressWarnings("deprecation")
- private void placeBlock(Block block, BlockPos position) {
- position = position.add(0, -1, 0);
- worldObj.setBlockState(position, block.getDefaultState());
- worldObj.playSound(null, position, block.getSoundType().getBreakSound(), SoundCategory.BLOCKS, 1F, 1F);
- }
- @Override
- public NBTTagCompound writeToNBT(NBTTagCompound tag) {
- super.writeToNBT(tag);
- tag.setLong("q1", q1.toLong());
- tag.setLong("q2", q2.toLong());
- tag.setLong("last", last.toLong());
- tag.setInteger("range", range);
- tag.setBoolean("built", built);
- tag.setInteger("Energy", energy.getEnergyStored());
- return tag;
- }
- @Override
- public void readFromNBT(NBTTagCompound tag) {
- super.readFromNBT(tag);
- if (tag.hasKey("Energy"))
- energy.receiveEnergy(tag.getInteger("Energy"), false);
- if (tag.hasKey("q1"))
- q1 = BlockPos.fromLong(tag.getLong("q1"));
- if (tag.hasKey("q2"))
- q2 = BlockPos.fromLong(tag.getLong("q2"));
- if (tag.hasKey("last"))
- last = BlockPos.fromLong(tag.getLong("last"));
- if (tag.hasKey("range"))
- range = tag.getInteger("range");
- if (tag.hasKey("built"))
- built = tag.getBoolean("built");
- }
- @Override
- public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) {
- return capability == CapabilityEnergy.ENERGY || super.hasCapability(capability, facing);
- }
- @SuppressWarnings("unchecked")
- @Override
- public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing) {
- if (capability == CapabilityEnergy.ENERGY)
- return (T) energy;
- return super.getCapability(capability, facing);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement