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
|
|
|
|
*
|
2015-11-02 20:55:19 +01:00
|
|
|
* © 2015 Ellpeck
|
2015-09-30 21:03:43 +02:00
|
|
|
*/
|
|
|
|
|
2015-12-30 22:02:15 +01:00
|
|
|
package de.ellpeck.actuallyadditions.tile;
|
2015-09-30 21:03:43 +02:00
|
|
|
|
|
|
|
import cofh.api.energy.EnergyStorage;
|
|
|
|
import cofh.api.energy.IEnergyProvider;
|
2015-12-16 20:54:50 +01:00
|
|
|
import cpw.mods.fml.common.network.NetworkRegistry;
|
2015-12-21 22:22:02 +01:00
|
|
|
import cpw.mods.fml.relauncher.Side;
|
|
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
2015-12-30 22:02:15 +01:00
|
|
|
import de.ellpeck.actuallyadditions.network.PacketHandler;
|
|
|
|
import de.ellpeck.actuallyadditions.network.PacketParticle;
|
|
|
|
import de.ellpeck.actuallyadditions.util.Position;
|
|
|
|
import de.ellpeck.actuallyadditions.util.WorldUtil;
|
2015-09-30 21:03:43 +02:00
|
|
|
import net.minecraft.block.Block;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
|
2015-12-21 22:18:33 +01:00
|
|
|
public class TileEntityLeafGenerator extends TileEntityBase implements IEnergyProvider, IEnergySaver, IEnergyDisplay{
|
2015-09-30 21:03:43 +02:00
|
|
|
|
2015-11-28 19:02:01 +01:00
|
|
|
public static final int RANGE = 7;
|
|
|
|
public static final int ENERGY_PRODUCED = 300;
|
2015-12-01 19:48:09 +01:00
|
|
|
public EnergyStorage storage = new EnergyStorage(35000);
|
2015-09-30 21:03:43 +02:00
|
|
|
private int nextUseCounter;
|
2015-12-21 22:18:33 +01:00
|
|
|
private int oldEnergy;
|
2015-09-30 21:03:43 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public void updateEntity(){
|
2015-11-18 23:11:24 +01:00
|
|
|
super.updateEntity();
|
2015-09-30 21:03:43 +02:00
|
|
|
if(!worldObj.isRemote){
|
2015-12-03 18:40:43 +01:00
|
|
|
if(!this.isRedstonePowered){
|
2015-10-05 11:27:53 +02:00
|
|
|
|
2015-11-28 19:02:01 +01:00
|
|
|
if(this.nextUseCounter >= 5){
|
2015-10-05 11:27:53 +02:00
|
|
|
this.nextUseCounter = 0;
|
|
|
|
|
2015-11-28 19:02:01 +01:00
|
|
|
if(ENERGY_PRODUCED <= this.storage.getMaxEnergyStored()-this.storage.getEnergyStored()){
|
2015-12-23 01:43:49 +01:00
|
|
|
ArrayList<Position> breakPositions = new ArrayList<Position>();
|
2015-10-05 11:27:53 +02:00
|
|
|
|
2015-11-28 19:02:01 +01:00
|
|
|
for(int reachX = -RANGE; reachX < RANGE+1; reachX++){
|
|
|
|
for(int reachZ = -RANGE; reachZ < RANGE+1; reachZ++){
|
|
|
|
for(int reachY = -RANGE; reachY < RANGE+1; reachY++){
|
2015-10-05 11:27:53 +02:00
|
|
|
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)){
|
2015-12-23 01:43:49 +01:00
|
|
|
breakPositions.add(new Position(this.xCoord+reachX, this.yCoord+reachY, this.zCoord+reachZ));
|
2015-10-05 11:27:53 +02:00
|
|
|
}
|
2015-09-30 21:03:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-05 11:27:53 +02:00
|
|
|
if(!breakPositions.isEmpty()){
|
|
|
|
Collections.shuffle(breakPositions);
|
2015-12-23 01:43:49 +01:00
|
|
|
Position 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-11-28 19:02:01 +01:00
|
|
|
this.storage.receiveEnergy(ENERGY_PRODUCED, false);
|
2015-12-16 20:54:50 +01:00
|
|
|
|
|
|
|
PacketHandler.theNetwork.sendToAllAround(new PacketParticle(xCoord, yCoord, zCoord, theCoord.getX(), theCoord.getY(), theCoord.getZ(), new float[]{62F/255F, 163F/255F, 74F/255F}, 5, 1.0F), new NetworkRegistry.TargetPoint(worldObj.provider.dimensionId, xCoord, yCoord, zCoord, 64));
|
2015-10-05 11:27:53 +02:00
|
|
|
}
|
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);
|
|
|
|
}
|
2015-12-21 22:18:33 +01:00
|
|
|
|
|
|
|
if(this.oldEnergy != this.storage.getEnergyStored() && this.sendUpdateWithInterval()){
|
|
|
|
this.oldEnergy = this.storage.getEnergyStored();
|
|
|
|
}
|
2015-09-30 21:03:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-01 19:48:09 +01:00
|
|
|
@Override
|
|
|
|
public void writeSyncableNBT(NBTTagCompound compound, boolean sync){
|
2015-12-21 22:18:33 +01:00
|
|
|
super.writeSyncableNBT(compound, sync);
|
2015-12-01 19:48:09 +01:00
|
|
|
this.storage.writeToNBT(compound);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readSyncableNBT(NBTTagCompound compound, boolean sync){
|
2015-12-21 22:18:33 +01:00
|
|
|
super.readSyncableNBT(compound, sync);
|
2015-12-01 19:48:09 +01:00
|
|
|
this.storage.readFromNBT(compound);
|
|
|
|
}
|
|
|
|
|
2015-09-30 21:03:43 +02:00
|
|
|
@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;
|
|
|
|
}
|
2015-12-13 00:54:25 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getEnergy(){
|
|
|
|
return this.storage.getEnergyStored();
|
|
|
|
}
|
|
|
|
|
2015-12-21 22:18:33 +01:00
|
|
|
@Override
|
2015-12-21 22:46:23 +01:00
|
|
|
public void setEnergy(int energy){
|
|
|
|
this.storage.setEnergyStored(energy);
|
2015-12-21 22:18:33 +01:00
|
|
|
}
|
|
|
|
|
2015-12-13 00:54:25 +01:00
|
|
|
@Override
|
2015-12-21 22:46:23 +01:00
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public int getMaxEnergy(){
|
|
|
|
return this.storage.getMaxEnergyStored();
|
2015-12-13 00:54:25 +01:00
|
|
|
}
|
2015-09-30 21:03:43 +02:00
|
|
|
}
|