ActuallyAdditions/src/main/java/ellpeck/actuallyadditions/tile/TileEntityMiner.java

190 lines
6.5 KiB
Java
Raw Normal View History

/*
* This file ("TileEntityMiner.java") is part of the Actually Additions Mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* © 2015 Ellpeck
*/
package ellpeck.actuallyadditions.tile;
import cofh.api.energy.EnergyStorage;
import cofh.api.energy.IEnergyReceiver;
import cpw.mods.fml.common.network.NetworkRegistry;
import ellpeck.actuallyadditions.network.PacketHandler;
import ellpeck.actuallyadditions.network.PacketParticle;
2015-12-09 20:24:45 +01:00
import ellpeck.actuallyadditions.network.gui.IButtonReactor;
2015-12-09 16:54:25 +01:00
import ellpeck.actuallyadditions.util.WorldUtil;
import net.minecraft.block.Block;
2015-12-11 16:55:50 +01:00
import net.minecraft.block.BlockLiquid;
2015-12-09 20:24:45 +01:00
import net.minecraft.entity.player.EntityPlayer;
2015-12-09 16:54:25 +01:00
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.ForgeDirection;
2015-12-11 16:56:32 +01:00
import net.minecraftforge.fluids.IFluidBlock;
import net.minecraftforge.oredict.OreDictionary;
2015-12-09 16:54:25 +01:00
import java.util.ArrayList;
public class TileEntityMiner extends TileEntityInventoryBase implements IEnergyReceiver, IButtonReactor, IEnergySaver{
2015-12-09 20:24:45 +01:00
public static final int ENERGY_USE_PER_BLOCK = 500;
2015-12-19 10:30:39 +01:00
public EnergyStorage storage = new EnergyStorage(1000000);
2015-12-09 20:24:45 +01:00
public int layerAt = -1;
public boolean onlyMineOres;
2015-12-09 20:24:45 +01:00
public TileEntityMiner(){
super(9, "miner");
}
@Override
public void updateEntity(){
super.updateEntity();
if(!this.worldObj.isRemote){
2015-12-09 20:24:45 +01:00
if(!this.isRedstonePowered && this.ticksElapsed%5 == 0){
if(this.layerAt == -1){
2015-12-09 16:54:25 +01:00
this.layerAt = this.yCoord-1;
}
2015-12-09 20:24:45 +01:00
if(this.layerAt > 0){
if(this.mine(TileEntityPhantomface.upgradeRange(2, worldObj, xCoord, yCoord, zCoord))){
this.layerAt--;
}
2015-12-09 16:54:25 +01:00
}
}
}
}
2015-12-09 16:54:25 +01:00
private boolean mine(int range){
for(int anX = -range; anX <= range; anX++){
for(int aZ = -range; aZ <= range; aZ++){
2015-12-09 20:24:45 +01:00
int actualUse = ENERGY_USE_PER_BLOCK*(this.onlyMineOres ? 3 : 1);
if(this.storage.getEnergyStored() >= actualUse){
int x = this.xCoord+anX;
int z = this.zCoord+aZ;
2015-12-09 16:54:25 +01:00
int y = this.layerAt;
Block block = this.worldObj.getBlock(x, y, z);
int meta = this.worldObj.getBlockMetadata(x, y, z);
if(block != null && !block.isAir(this.worldObj, x, y, z)){
2015-12-11 16:56:32 +01:00
if(block.getHarvestLevel(meta) <= 3F && block.getBlockHardness(this.worldObj, x, y, z) >= 0F && !(block instanceof BlockLiquid) && !(block instanceof IFluidBlock) && this.isMinable(block, meta)){
2015-12-09 16:54:25 +01:00
ArrayList<ItemStack> drops = new ArrayList<ItemStack>();
drops.addAll(block.getDrops(worldObj, x, y, z, meta, 0));
2015-12-09 20:24:45 +01:00
if(WorldUtil.addToInventory(this, drops, ForgeDirection.UNKNOWN, false)){
worldObj.playAuxSFX(2001, x, y, z, Block.getIdFromBlock(block)+(meta << 12));
worldObj.setBlockToAir(x, y, z);
2015-12-09 16:54:25 +01:00
2015-12-09 20:24:45 +01:00
WorldUtil.addToInventory(this, drops, ForgeDirection.UNKNOWN, true);
this.markDirty();
2015-12-09 20:24:45 +01:00
this.storage.extractEnergy(actualUse, false);
this.shootParticles(x, y, z);
2015-12-09 16:54:25 +01:00
}
2015-12-09 20:24:45 +01:00
return false;
}
}
}
2015-12-09 20:24:45 +01:00
else{
return false;
}
}
}
2015-12-09 20:24:45 +01:00
return true;
}
private boolean isMinable(Block block, int meta){
if(!this.onlyMineOres){
return true;
}
else{
int[] ids = OreDictionary.getOreIDs(new ItemStack(block, 1, meta));
for(int id : ids){
String name = OreDictionary.getOreName(id);
2015-12-09 20:24:45 +01:00
if(name.startsWith("ore") || name.startsWith("denseore")){
return true;
}
}
return false;
}
}
2015-12-09 20:24:45 +01:00
private void shootParticles(int endX, int endY, int endZ){
PacketHandler.theNetwork.sendToAllAround(new PacketParticle(xCoord, yCoord, zCoord, endX, endY, endZ, new float[]{62F/255F, 163F/255F, 74F/255F}, 5, 1.0F), new NetworkRegistry.TargetPoint(worldObj.provider.dimensionId, xCoord, yCoord, zCoord, 96));
}
@Override
public void writeSyncableNBT(NBTTagCompound compound, boolean sync){
super.writeSyncableNBT(compound, sync);
this.storage.writeToNBT(compound);
2015-12-09 16:54:25 +01:00
compound.setInteger("Layer", this.layerAt);
2015-12-09 18:48:46 +01:00
compound.setBoolean("OnlyOres", this.onlyMineOres);
}
@Override
public void readSyncableNBT(NBTTagCompound compound, boolean sync){
super.readSyncableNBT(compound, sync);
this.storage.readFromNBT(compound);
2015-12-09 16:54:25 +01:00
this.layerAt = compound.getInteger("Layer");
2015-12-09 18:48:46 +01:00
this.onlyMineOres = compound.getBoolean("OnlyOres");
}
@Override
public int receiveEnergy(ForgeDirection from, int maxReceive, boolean simulate){
return this.storage.receiveEnergy(maxReceive, simulate);
}
@Override
public int getEnergyStored(ForgeDirection from){
return this.storage.getEnergyStored();
}
@Override
public int getMaxEnergyStored(ForgeDirection from){
return this.storage.getMaxEnergyStored();
}
@Override
public boolean canConnectEnergy(ForgeDirection from){
return true;
}
2015-12-09 20:24:45 +01:00
@Override
public boolean canInsertItem(int slot, ItemStack stack, int side){
return this.isItemValidForSlot(slot, stack);
}
@Override
public boolean canExtractItem(int slot, ItemStack stack, int side){
return true;
}
@Override
public boolean isItemValidForSlot(int slot, ItemStack stack){
return false;
}
@Override
public void onButtonPressed(int buttonID, EntityPlayer player){
if(buttonID == 0){
this.onlyMineOres = !this.onlyMineOres;
this.sendUpdate();
}
else if(buttonID == 1){
this.layerAt = -1;
}
}
@Override
public int getEnergy(){
return this.storage.getEnergyStored();
}
@Override
public void setEnergy(int energy){
this.storage.setEnergyStored(energy);
}
}