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

161 lines
5.6 KiB
Java
Raw Normal View History

2015-10-05 16:53:28 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("TileEntityDirectionalBreaker.java") is part of the Actually Additions mod for Minecraft.
2015-10-05 16:53:28 +02:00
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-05-16 22:52:27 +02:00
* http://ellpeck.de/actaddlicense
2015-10-05 16:53:28 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2016-05-16 22:54:42 +02:00
* © 2015-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 de.ellpeck.actuallyadditions.mod.config.values.ConfigBoolValues;
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;
2016-07-04 20:15:41 +02:00
import net.minecraft.block.state.IBlockState;
2015-10-05 16:53:28 +02:00
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 ICustomEnergyReceiver{
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;
2016-05-19 20:05:12 +02:00
public final EnergyStorage storage = new EnergyStorage(10000);
2015-10-05 16:53:28 +02:00
private int lastEnergy;
private int currentTime;
public TileEntityDirectionalBreaker(){
super(9, "directionalBreaker");
}
2016-02-01 17:49:55 +01:00
@Override
public void writeSyncableNBT(NBTTagCompound compound, NBTType type){
super.writeSyncableNBT(compound, type);
2016-02-01 17:49:55 +01:00
this.storage.writeToNBT(compound);
if(type != NBTType.SAVE_BLOCK){
compound.setInteger("CurrentTime", this.currentTime);
}
2016-02-01 17:49:55 +01:00
}
@Override
public void readSyncableNBT(NBTTagCompound compound, NBTType type){
super.readSyncableNBT(compound, type);
2016-02-01 17:49:55 +01:00
this.storage.readFromNBT(compound);
if(type != NBTType.SAVE_BLOCK){
this.currentTime = compound.getInteger("CurrentTime");
}
2016-02-01 17:49:55 +01:00
}
2015-10-05 16:53:28 +02:00
@Override
public void updateEntity(){
super.updateEntity();
2016-05-02 17:46:53 +02:00
if(!this.worldObj.isRemote){
if(!this.isRedstonePowered && !this.isPulseMode){
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){
2016-07-04 20:15:41 +02:00
IBlockState state = this.worldObj.getBlockState(this.pos);
EnumFacing sideToManipulate = WorldUtil.getDirectionByPistonRotation(state.getBlock().getMetaFromState(state));
for(int i = 0; i < RANGE; i++){
BlockPos coordsBlock = this.pos.offset(sideToManipulate, i+1);
2016-07-04 20:15:41 +02:00
Block blockToBreak = this.worldObj.getBlockState(coordsBlock).getBlock();
2016-05-16 22:52:27 +02:00
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(!ConfigBoolValues.LESS_BLOCK_BREAKING_EFFECTS.isEnabled()){
this.worldObj.playEvent(2001, coordsBlock, Block.getStateId(this.worldObj.getBlockState(coordsBlock)));
}
2016-07-04 20:15:41 +02:00
this.worldObj.setBlockToAir(coordsBlock);
2016-05-16 22:52:27 +02:00
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
public boolean isItemValidForSlot(int i, ItemStack stack){
2016-02-01 17:49:55 +01:00
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 boolean isRedstoneToggle(){
return true;
}
@Override
public void activateOnPulse(){
this.doWork();
}
2015-10-05 16:53:28 +02:00
}