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;
|
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;
|
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;
|
2016-01-07 19:04:29 +01:00
|
|
|
import net.minecraft.util.EnumFacing;
|
2016-03-18 23:47:22 +01:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2016-05-04 13:15:30 +02:00
|
|
|
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
|
|
|
|
2016-05-04 13:15:30 +02:00
|
|
|
import java.util.List;
|
2015-10-05 16:53:28 +02:00
|
|
|
|
2016-08-31 20:16:36 +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
|
2016-07-02 15:01:46 +02:00
|
|
|
public void writeSyncableNBT(NBTTagCompound compound, NBTType type){
|
|
|
|
super.writeSyncableNBT(compound, type);
|
2016-02-01 17:49:55 +01:00
|
|
|
this.storage.writeToNBT(compound);
|
2016-07-02 15:01:46 +02:00
|
|
|
if(type != NBTType.SAVE_BLOCK){
|
|
|
|
compound.setInteger("CurrentTime", this.currentTime);
|
|
|
|
}
|
2016-02-01 17:49:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-07-02 15:01:46 +02:00
|
|
|
public void readSyncableNBT(NBTTagCompound compound, NBTType type){
|
|
|
|
super.readSyncableNBT(compound, type);
|
2016-02-01 17:49:55 +01:00
|
|
|
this.storage.readFromNBT(compound);
|
2016-07-02 15:01:46 +02:00
|
|
|
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(){
|
2015-11-18 23:11:24 +01:00
|
|
|
super.updateEntity();
|
2016-05-02 17:46:53 +02:00
|
|
|
if(!this.worldObj.isRemote){
|
2016-07-02 15:01:46 +02:00
|
|
|
if(!this.isRedstonePowered && !this.isPulseMode){
|
2016-05-05 13:22:58 +02:00
|
|
|
if(this.currentTime > 0){
|
|
|
|
this.currentTime--;
|
|
|
|
if(this.currentTime <= 0){
|
|
|
|
this.doWork();
|
2015-10-05 16:53:28 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-05 13:22:58 +02:00
|
|
|
else{
|
|
|
|
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(){
|
2016-05-05 13:22:58 +02:00
|
|
|
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));
|
2016-05-05 13:22:58 +02:00
|
|
|
|
|
|
|
for(int i = 0; i < RANGE; i++){
|
2016-07-06 21:56:15 +02:00
|
|
|
BlockPos coordsBlock = this.pos.offset(sideToManipulate, i+1);
|
2016-07-04 20:15:41 +02:00
|
|
|
Block blockToBreak = this.worldObj.getBlockState(coordsBlock).getBlock();
|
2016-09-13 17:27:39 +02:00
|
|
|
if(blockToBreak != null && !this.worldObj.isAirBlock(coordsBlock) && blockToBreak.getBlockHardness(this.worldObj.getBlockState(coordsBlock), this.worldObj, this.pos) > -1.0F){
|
2016-05-16 22:52:27 +02:00
|
|
|
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);
|
|
|
|
|
2016-11-02 19:36:32 +01:00
|
|
|
if(this.worldObj.rand.nextFloat() <= chance){
|
2016-05-16 22:52:27 +02:00
|
|
|
if(WorldUtil.addToInventory(this, drops, false, true)){
|
2016-11-07 19:58:34 +01:00
|
|
|
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-12-17 18:47:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-05 16:53:28 +02:00
|
|
|
@Override
|
2016-05-29 23:49:35 +02:00
|
|
|
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
|
2016-05-29 23:49:35 +02:00
|
|
|
public boolean canInsertItem(int slot, ItemStack stack, EnumFacing side){
|
2015-10-05 16:53:28 +02:00
|
|
|
return this.isItemValidForSlot(slot, stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-05-29 23:49:35 +02:00
|
|
|
public boolean canExtractItem(int slot, ItemStack stack, EnumFacing side){
|
2015-10-05 16:53:28 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-01-07 19:04:29 +01:00
|
|
|
public int receiveEnergy(EnumFacing from, int maxReceive, boolean simulate){
|
2015-10-05 16:53:28 +02:00
|
|
|
return this.storage.receiveEnergy(maxReceive, simulate);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-01-07 19:04:29 +01:00
|
|
|
public int getEnergyStored(EnumFacing from){
|
2015-10-05 16:53:28 +02:00
|
|
|
return this.storage.getEnergyStored();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-01-07 19:04:29 +01:00
|
|
|
public int getMaxEnergyStored(EnumFacing from){
|
2015-10-05 16:53:28 +02:00
|
|
|
return this.storage.getMaxEnergyStored();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-01-07 19:04:29 +01:00
|
|
|
public boolean canConnectEnergy(EnumFacing from){
|
2015-10-05 16:53:28 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-12-13 00:54:25 +01:00
|
|
|
|
2015-12-17 18:47:46 +01:00
|
|
|
|
|
|
|
@Override
|
2016-07-02 15:01:46 +02:00
|
|
|
public boolean isRedstoneToggle(){
|
|
|
|
return true;
|
2015-12-17 18:47:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void activateOnPulse(){
|
|
|
|
this.doWork();
|
|
|
|
}
|
2015-10-05 16:53:28 +02:00
|
|
|
}
|