ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityDirectionalBreaker.java

175 lines
6 KiB
Java
Raw Normal View History

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
2016-01-03 16:05:51 +01:00
* http://ellpeck.de/actaddlicense/
2015-10-05 16:53:28 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2016-01-03 16:05:51 +01:00
* © 2016 Ellpeck
2015-10-05 16:53:28 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.tile;
2015-10-05 16:53:28 +02:00
import cofh.api.energy.EnergyStorage;
import cofh.api.energy.IEnergyReceiver;
2016-01-23 11:02:35 +01:00
import de.ellpeck.actuallyadditions.mod.config.ConfigValues;
2016-01-08 13:31:58 +01:00
import de.ellpeck.actuallyadditions.mod.util.PosUtil;
import de.ellpeck.actuallyadditions.mod.util.Util;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
2015-10-05 16:53:28 +02:00
import net.minecraft.block.Block;
import net.minecraft.block.BlockAir;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.event.ForgeEventFactory;
2016-01-07 18:20:59 +01:00
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2015-10-05 16:53:28 +02:00
import java.util.List;
2015-10-05 16:53:28 +02: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");
}
2016-02-01 17:49:55 +01:00
@Override
public void writeSyncableNBT(NBTTagCompound compound, boolean sync){
super.writeSyncableNBT(compound, sync);
this.storage.writeToNBT(compound);
compound.setInteger("CurrentTime", this.currentTime);
}
@Override
public void readSyncableNBT(NBTTagCompound compound, boolean sync){
super.readSyncableNBT(compound, sync);
this.storage.readFromNBT(compound);
this.currentTime = compound.getInteger("CurrentTime");
}
2015-10-05 16:53:28 +02:00
@Override
@SuppressWarnings("unchecked")
public void updateEntity(){
super.updateEntity();
2016-05-02 17:46:53 +02:00
if(!this.worldObj.isRemote){
if(!this.isRedstonePowered && !this.activateOnceWithSignal){
if(this.currentTime > 0){
this.currentTime--;
if(this.currentTime <= 0){
this.doWork();
2015-10-05 16:53:28 +02:00
}
}
else{
this.currentTime = 15;
}
2015-10-05 16:53:28 +02:00
}
if(this.storage.getEnergyStored() != this.lastEnergy && this.sendUpdateWithInterval()){
2015-10-05 16:53:28 +02:00
this.lastEnergy = this.storage.getEnergyStored();
}
}
}
private void doWork(){
if(this.storage.getEnergyStored() >= ENERGY_USE*RANGE){
EnumFacing sideToManipulate = WorldUtil.getDirectionByPistonRotation(PosUtil.getMetadata(this.pos, this.worldObj));
for(int i = 0; i < RANGE; i++){
BlockPos coordsBlock = WorldUtil.getCoordsFromSide(sideToManipulate, this.pos, i);
if(coordsBlock != null){
Block blockToBreak = PosUtil.getBlock(coordsBlock, this.worldObj);
if(blockToBreak != null && !(blockToBreak instanceof BlockAir) && blockToBreak.getBlockHardness(this.worldObj.getBlockState(coordsBlock), this.worldObj, this.pos) > -1.0F){
List<ItemStack> drops = blockToBreak.getDrops(this.worldObj, coordsBlock, this.worldObj.getBlockState(coordsBlock), 0);
float chance = ForgeEventFactory.fireBlockHarvesting(drops, this.worldObj, coordsBlock, this.worldObj.getBlockState(coordsBlock), 0, 1, false, null);
if(Util.RANDOM.nextFloat() <= chance){
if(WorldUtil.addToInventory(this, drops, false, true)){
if(!ConfigValues.lessBlockBreakingEffects){
this.worldObj.playAuxSFX(2001, coordsBlock, Block.getStateId(this.worldObj.getBlockState(coordsBlock)));
}
WorldUtil.breakBlockAtSide(sideToManipulate, this.worldObj, this.getPos(), i);
WorldUtil.addToInventory(this, drops, true, true);
this.storage.extractEnergy(ENERGY_USE, false);
this.markDirty();
}
2016-01-23 11:02:35 +01:00
}
}
}
}
}
}
2015-10-05 16:53:28 +02:00
@Override
2016-02-01 17:49:55 +01:00
public boolean isItemValidForSlot(int i, ItemStack stack){
return false;
2015-10-18 15:31:01 +02:00
}
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, EnumFacing side){
2015-10-05 16:53:28 +02:00
return this.isItemValidForSlot(slot, stack);
}
@Override
public boolean canExtractItem(int slot, ItemStack stack, EnumFacing side){
2015-10-05 16:53:28 +02:00
return true;
}
@Override
public int receiveEnergy(EnumFacing from, int maxReceive, boolean simulate){
2015-10-05 16:53:28 +02:00
return this.storage.receiveEnergy(maxReceive, simulate);
}
@Override
public int getEnergyStored(EnumFacing from){
2015-10-05 16:53:28 +02:00
return this.storage.getEnergyStored();
}
@Override
public int getMaxEnergyStored(EnumFacing from){
2015-10-05 16:53:28 +02:00
return this.storage.getMaxEnergyStored();
}
@Override
public boolean canConnectEnergy(EnumFacing from){
2015-10-05 16:53:28 +02:00
return true;
}
@Override
public int getEnergy(){
return this.storage.getEnergyStored();
}
@Override
public void setEnergy(int energy){
this.storage.setEnergyStored(energy);
}
@Override
public void toggle(boolean to){
this.activateOnceWithSignal = to;
}
@Override
public boolean isPulseMode(){
return this.activateOnceWithSignal;
}
@Override
public void activateOnPulse(){
this.doWork();
}
2015-10-05 16:53:28 +02:00
}