2015-09-30 21:03:43 +02:00
|
|
|
|
/*
|
|
|
|
|
* This file ("TileEntityLeafGenerator.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
|
|
|
|
|
*
|
|
|
|
|
* <EFBFBD> 2015 Ellpeck
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package ellpeck.actuallyadditions.tile;
|
|
|
|
|
|
|
|
|
|
import cofh.api.energy.EnergyStorage;
|
|
|
|
|
import cofh.api.energy.IEnergyProvider;
|
2015-10-01 12:11:38 +02:00
|
|
|
|
import ellpeck.actuallyadditions.config.values.ConfigIntValues;
|
2015-09-30 21:03:43 +02:00
|
|
|
|
import ellpeck.actuallyadditions.util.WorldPos;
|
|
|
|
|
import ellpeck.actuallyadditions.util.WorldUtil;
|
|
|
|
|
import net.minecraft.block.Block;
|
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
|
|
|
|
|
public class TileEntityLeafGenerator extends TileEntityBase implements IEnergyProvider{
|
|
|
|
|
|
|
|
|
|
public EnergyStorage storage = new EnergyStorage(35000);
|
|
|
|
|
|
|
|
|
|
private int nextUseCounter;
|
|
|
|
|
|
2015-10-03 10:19:40 +02:00
|
|
|
|
@Override
|
2015-10-18 15:31:01 +02:00
|
|
|
|
public void writeSyncableNBT(NBTTagCompound compound, boolean sync){
|
|
|
|
|
this.storage.writeToNBT(compound);
|
2015-10-03 10:19:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2015-10-18 15:31:01 +02:00
|
|
|
|
public void readSyncableNBT(NBTTagCompound compound, boolean sync){
|
|
|
|
|
this.storage.readFromNBT(compound);
|
2015-10-03 10:19:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-09-30 21:03:43 +02:00
|
|
|
|
@Override
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
public void updateEntity(){
|
|
|
|
|
if(!worldObj.isRemote){
|
2015-10-05 11:27:53 +02:00
|
|
|
|
if(!this.worldObj.isBlockIndirectlyGettingPowered(this.xCoord, this.yCoord, this.zCoord)){
|
|
|
|
|
|
|
|
|
|
if(this.nextUseCounter >= ConfigIntValues.LEAF_GENERATOR_COOLDOWN_TIME.getValue()){
|
|
|
|
|
this.nextUseCounter = 0;
|
|
|
|
|
|
|
|
|
|
int energyProducedPerLeaf = ConfigIntValues.LEAF_GENERATOR_ENERGY_PRODUCED.getValue();
|
|
|
|
|
if(energyProducedPerLeaf <= this.storage.getMaxEnergyStored()-this.storage.getEnergyStored()){
|
|
|
|
|
ArrayList<WorldPos> breakPositions = new ArrayList<WorldPos>();
|
|
|
|
|
|
|
|
|
|
int range = ConfigIntValues.LEAF_GENERATOR_RANGE.getValue();
|
|
|
|
|
for(int reachX = -range; reachX < range+1; reachX++){
|
|
|
|
|
for(int reachZ = -range; reachZ < range+1; reachZ++){
|
|
|
|
|
for(int reachY = -range; reachY < range+1; reachY++){
|
|
|
|
|
Block block = this.worldObj.getBlock(this.xCoord+reachX, this.yCoord+reachY, this.zCoord+reachZ);
|
|
|
|
|
if(block != null && block.isLeaves(this.worldObj, this.xCoord+reachX, this.yCoord+reachY, this.zCoord+reachZ)){
|
|
|
|
|
breakPositions.add(new WorldPos(this.worldObj, this.xCoord+reachX, this.yCoord+reachY, this.zCoord+reachZ));
|
|
|
|
|
}
|
2015-09-30 21:03:43 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-05 11:27:53 +02:00
|
|
|
|
if(!breakPositions.isEmpty()){
|
|
|
|
|
Collections.shuffle(breakPositions);
|
|
|
|
|
WorldPos theCoord = breakPositions.get(0);
|
2015-09-30 21:03:43 +02:00
|
|
|
|
|
2015-10-05 11:27:53 +02:00
|
|
|
|
Block theBlock = this.worldObj.getBlock(theCoord.getX(), theCoord.getY(), theCoord.getZ());
|
|
|
|
|
int meta = this.worldObj.getBlockMetadata(theCoord.getX(), theCoord.getY(), theCoord.getZ());
|
|
|
|
|
this.worldObj.playAuxSFX(2001, theCoord.getX(), theCoord.getY(), theCoord.getZ(), Block.getIdFromBlock(theBlock)+(meta << 12));
|
2015-09-30 21:03:43 +02:00
|
|
|
|
|
2015-10-05 11:27:53 +02:00
|
|
|
|
this.worldObj.setBlockToAir(theCoord.getX(), theCoord.getY(), theCoord.getZ());
|
2015-09-30 21:03:43 +02:00
|
|
|
|
|
2015-10-05 11:27:53 +02:00
|
|
|
|
this.storage.receiveEnergy(energyProducedPerLeaf, false);
|
|
|
|
|
}
|
2015-09-30 21:03:43 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-10-05 11:27:53 +02:00
|
|
|
|
else{
|
|
|
|
|
this.nextUseCounter++;
|
|
|
|
|
}
|
2015-09-30 21:03:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(this.getEnergyStored(ForgeDirection.UNKNOWN) > 0){
|
|
|
|
|
WorldUtil.pushEnergy(worldObj, xCoord, yCoord, zCoord, ForgeDirection.UP, storage);
|
|
|
|
|
WorldUtil.pushEnergy(worldObj, xCoord, yCoord, zCoord, ForgeDirection.DOWN, storage);
|
|
|
|
|
WorldUtil.pushEnergy(worldObj, xCoord, yCoord, zCoord, ForgeDirection.NORTH, storage);
|
|
|
|
|
WorldUtil.pushEnergy(worldObj, xCoord, yCoord, zCoord, ForgeDirection.EAST, storage);
|
|
|
|
|
WorldUtil.pushEnergy(worldObj, xCoord, yCoord, zCoord, ForgeDirection.SOUTH, storage);
|
|
|
|
|
WorldUtil.pushEnergy(worldObj, xCoord, yCoord, zCoord, ForgeDirection.WEST, storage);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int extractEnergy(ForgeDirection from, int maxReceive, boolean simulate){
|
|
|
|
|
return this.storage.extractEnergy(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;
|
|
|
|
|
}
|
|
|
|
|
}
|