2015-10-05 16:53:28 +02:00
|
|
|
/*
|
|
|
|
* This file ("TileEntityDirectionalBreaker.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-11-02 20:55:19 +01:00
|
|
|
* © 2015 Ellpeck
|
2015-10-05 16:53:28 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
package ellpeck.actuallyadditions.tile;
|
|
|
|
|
|
|
|
import cofh.api.energy.EnergyStorage;
|
|
|
|
import cofh.api.energy.IEnergyReceiver;
|
|
|
|
import cpw.mods.fml.relauncher.Side;
|
|
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
2015-12-23 01:43:49 +01:00
|
|
|
import ellpeck.actuallyadditions.util.Position;
|
2015-10-05 16:53:28 +02:00
|
|
|
import ellpeck.actuallyadditions.util.WorldUtil;
|
|
|
|
import net.minecraft.block.Block;
|
|
|
|
import net.minecraft.block.BlockAir;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2015-12-17 18:47:46 +01:00
|
|
|
public class TileEntityDirectionalBreaker extends TileEntityInventoryBase implements IEnergyReceiver, IEnergySaver, IRedstoneToggle{
|
2015-10-05 16:53:28 +02:00
|
|
|
|
2015-12-01 19:48:09 +01:00
|
|
|
public static final int RANGE = 8;
|
|
|
|
public static final int ENERGY_USE = 5;
|
2015-10-05 16:53:28 +02:00
|
|
|
public EnergyStorage storage = new EnergyStorage(10000);
|
|
|
|
private int lastEnergy;
|
|
|
|
private int currentTime;
|
2015-12-19 10:30:39 +01:00
|
|
|
private boolean activateOnceWithSignal;
|
2015-10-05 16:53:28 +02:00
|
|
|
|
|
|
|
public TileEntityDirectionalBreaker(){
|
|
|
|
super(9, "directionalBreaker");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public void updateEntity(){
|
2015-11-18 23:11:24 +01:00
|
|
|
super.updateEntity();
|
2015-10-05 16:53:28 +02:00
|
|
|
if(!worldObj.isRemote){
|
2015-12-17 18:47:46 +01:00
|
|
|
if(!this.isRedstonePowered && !this.activateOnceWithSignal){
|
2015-11-28 19:02:01 +01:00
|
|
|
if(this.storage.getEnergyStored() >= ENERGY_USE*RANGE){
|
2015-10-05 16:53:28 +02:00
|
|
|
if(this.currentTime > 0){
|
|
|
|
this.currentTime--;
|
|
|
|
if(this.currentTime <= 0){
|
2015-12-17 18:47:46 +01:00
|
|
|
this.doWork();
|
2015-10-05 16:53:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
2015-11-28 19:02:01 +01:00
|
|
|
this.currentTime = 15;
|
2015-10-05 16:53:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-01 17:28:50 +01:00
|
|
|
if(this.storage.getEnergyStored() != this.lastEnergy && this.sendUpdateWithInterval()){
|
2015-10-05 16:53:28 +02:00
|
|
|
this.lastEnergy = this.storage.getEnergyStored();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-17 18:47:46 +01:00
|
|
|
private void doWork(){
|
|
|
|
ForgeDirection sideToManipulate = ForgeDirection.getOrientation(worldObj.getBlockMetadata(xCoord, yCoord, zCoord));
|
|
|
|
|
|
|
|
for(int i = 0; i < RANGE; i++){
|
2015-12-23 01:43:49 +01:00
|
|
|
Position coordsBlock = WorldUtil.getCoordsFromSide(sideToManipulate, xCoord, yCoord, zCoord, i);
|
2015-12-17 18:47:46 +01:00
|
|
|
if(coordsBlock != null){
|
|
|
|
Block blockToBreak = worldObj.getBlock(coordsBlock.getX(), coordsBlock.getY(), coordsBlock.getZ());
|
|
|
|
if(blockToBreak != null && !(blockToBreak instanceof BlockAir) && blockToBreak.getBlockHardness(worldObj, coordsBlock.getX(), coordsBlock.getY(), coordsBlock.getZ()) > -1.0F){
|
|
|
|
ArrayList<ItemStack> drops = new ArrayList<ItemStack>();
|
|
|
|
int meta = worldObj.getBlockMetadata(coordsBlock.getX(), coordsBlock.getY(), coordsBlock.getZ());
|
|
|
|
drops.addAll(blockToBreak.getDrops(worldObj, coordsBlock.getX(), coordsBlock.getY(), coordsBlock.getZ(), meta, 0));
|
|
|
|
|
|
|
|
if(WorldUtil.addToInventory(this, drops, false)){
|
|
|
|
worldObj.playAuxSFX(2001, coordsBlock.getX(), coordsBlock.getY(), coordsBlock.getZ(), Block.getIdFromBlock(blockToBreak)+(meta << 12));
|
|
|
|
WorldUtil.breakBlockAtSide(sideToManipulate, worldObj, xCoord, yCoord, zCoord, i);
|
|
|
|
WorldUtil.addToInventory(this, drops, true);
|
|
|
|
this.storage.extractEnergy(ENERGY_USE, false);
|
|
|
|
this.markDirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-05 16:53:28 +02:00
|
|
|
@Override
|
2015-10-18 15:28:06 +02:00
|
|
|
public void writeSyncableNBT(NBTTagCompound compound, boolean sync){
|
|
|
|
super.writeSyncableNBT(compound, sync);
|
2015-10-05 16:53:28 +02:00
|
|
|
this.storage.writeToNBT(compound);
|
|
|
|
compound.setInteger("CurrentTime", this.currentTime);
|
|
|
|
}
|
|
|
|
|
2015-10-18 15:31:01 +02:00
|
|
|
@Override
|
|
|
|
public void readSyncableNBT(NBTTagCompound compound, boolean sync){
|
|
|
|
super.readSyncableNBT(compound, sync);
|
|
|
|
this.storage.readFromNBT(compound);
|
|
|
|
this.currentTime = compound.getInteger("CurrentTime");
|
|
|
|
}
|
|
|
|
|
2015-12-01 19:48:09 +01:00
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public int getEnergyScaled(int i){
|
|
|
|
return this.storage.getEnergyStored()*i/this.storage.getMaxEnergyStored();
|
|
|
|
}
|
|
|
|
|
2015-10-05 16:53:28 +02:00
|
|
|
@Override
|
|
|
|
public boolean canInsertItem(int slot, ItemStack stack, int side){
|
|
|
|
return this.isItemValidForSlot(slot, stack);
|
|
|
|
}
|
|
|
|
|
2015-12-19 10:30:39 +01:00
|
|
|
@Override
|
|
|
|
public boolean isItemValidForSlot(int i, ItemStack stack){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-05 16:53:28 +02:00
|
|
|
@Override
|
|
|
|
public boolean canExtractItem(int slot, ItemStack stack, int side){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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-13 00:54:25 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getEnergy(){
|
|
|
|
return this.storage.getEnergyStored();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setEnergy(int energy){
|
|
|
|
this.storage.setEnergyStored(energy);
|
|
|
|
}
|
2015-12-17 18:47:46 +01:00
|
|
|
|
|
|
|
@Override
|
2015-12-21 22:18:33 +01:00
|
|
|
public void toggle(boolean to){
|
|
|
|
this.activateOnceWithSignal = to;
|
2015-12-17 18:47:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-12-21 22:18:33 +01:00
|
|
|
public boolean isPulseMode(){
|
2015-12-17 18:47:46 +01:00
|
|
|
return this.activateOnceWithSignal;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void activateOnPulse(){
|
|
|
|
this.doWork();
|
|
|
|
}
|
2015-10-05 16:53:28 +02:00
|
|
|
}
|