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

150 lines
5.4 KiB
Java
Raw Normal View History

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
2016-01-03 16:05:51 +01:00
* http://ellpeck.de/actaddlicense/
2015-09-30 21:03:43 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2016-01-03 16:05:51 +01:00
* © 2016 Ellpeck
2015-09-30 21:03:43 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.tile;
2015-09-30 21:03:43 +02:00
import cofh.api.energy.EnergyStorage;
import cofh.api.energy.IEnergyProvider;
2016-01-23 11:02:35 +01:00
import de.ellpeck.actuallyadditions.mod.config.ConfigValues;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.network.PacketHandler;
import de.ellpeck.actuallyadditions.mod.network.PacketParticle;
2016-01-08 13:31:58 +01:00
import de.ellpeck.actuallyadditions.mod.util.PosUtil;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
2015-09-30 21:03:43 +02:00
import net.minecraft.block.Block;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.BlockPos;
2016-01-07 18:20:59 +01:00
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2015-09-30 21:03:43 +02:00
import java.util.ArrayList;
import java.util.Collections;
2016-03-18 23:47:22 +01:00
import java.util.List;
2015-09-30 21:03:43 +02:00
public class TileEntityLeafGenerator extends TileEntityBase implements IEnergyProvider, IEnergySaver, IEnergyDisplay{
2015-09-30 21:03:43 +02: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;
private int oldEnergy;
2015-09-30 21:03:43 +02:00
public TileEntityLeafGenerator(){
super("leafGenerator");
}
2016-02-01 17:49:55 +01:00
@Override
public void writeSyncableNBT(NBTTagCompound compound, boolean sync){
super.writeSyncableNBT(compound, sync);
this.storage.writeToNBT(compound);
}
@Override
public void readSyncableNBT(NBTTagCompound compound, boolean sync){
super.readSyncableNBT(compound, sync);
this.storage.readFromNBT(compound);
}
2015-09-30 21:03:43 +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){
if(this.nextUseCounter >= 5){
this.nextUseCounter = 0;
if(ENERGY_PRODUCED <= this.storage.getMaxEnergyStored()-this.storage.getEnergyStored()){
2016-03-18 23:47:22 +01:00
List<BlockPos> breakPositions = new ArrayList<BlockPos>();
for(int reachX = -RANGE; reachX < RANGE+1; reachX++){
for(int reachZ = -RANGE; reachZ < RANGE+1; reachZ++){
for(int reachY = -RANGE; reachY < RANGE+1; reachY++){
2016-01-08 13:31:58 +01:00
BlockPos pos = PosUtil.offset(this.pos, reachX, reachY, reachZ);
2016-05-02 17:46:53 +02:00
Block block = PosUtil.getBlock(pos, this.worldObj);
2016-03-18 23:47:22 +01:00
if(block != null && block.isLeaves(this.worldObj.getBlockState(pos), this.worldObj, pos)){
breakPositions.add(pos);
}
2015-09-30 21:03:43 +02:00
}
}
}
if(!breakPositions.isEmpty()){
Collections.shuffle(breakPositions);
2016-01-08 13:31:58 +01:00
BlockPos theCoord = breakPositions.get(0);
2015-09-30 21:03:43 +02:00
2016-01-23 11:02:35 +01:00
if(!ConfigValues.lessBlockBreakingEffects){
2016-05-02 17:46:53 +02:00
this.worldObj.playAuxSFX(2001, theCoord, Block.getStateId(this.worldObj.getBlockState(theCoord)));
2016-01-23 11:02:35 +01:00
}
2015-09-30 21:03:43 +02:00
this.worldObj.setBlockToAir(theCoord);
2015-09-30 21:03:43 +02:00
this.storage.receiveEnergy(ENERGY_PRODUCED, false);
2015-12-16 20:54:50 +01:00
2016-01-23 11:02:35 +01:00
if(!ConfigValues.lessParticles){
2016-05-02 17:46:53 +02:00
PacketHandler.theNetwork.sendToAllAround(new PacketParticle(this.getPos().getX(), this.getPos().getY(), this.getPos().getZ(), theCoord.getX(), theCoord.getY(), theCoord.getZ(), new float[]{62F/255F, 163F/255F, 74F/255F}, 5, 1.0F), new NetworkRegistry.TargetPoint(this.worldObj.provider.getDimension(), this.pos.getX(), this.pos.getY(), this.pos.getZ(), 64));
2016-01-23 11:02:35 +01:00
}
}
2015-09-30 21:03:43 +02:00
}
}
else{
this.nextUseCounter++;
}
2015-09-30 21:03:43 +02:00
}
if(this.storage.getEnergyStored() > 0){
2016-05-02 17:46:53 +02:00
WorldUtil.pushEnergyToAllSides(this.worldObj, this.pos, this.storage);
2015-09-30 21:03:43 +02:00
}
if(this.oldEnergy != this.storage.getEnergyStored() && this.sendUpdateWithInterval()){
this.oldEnergy = this.storage.getEnergyStored();
}
2015-09-30 21:03:43 +02:00
}
}
@Override
public int extractEnergy(EnumFacing from, int maxReceive, boolean simulate){
2015-09-30 21:03:43 +02:00
return this.storage.extractEnergy(maxReceive, simulate);
}
@Override
public int getEnergyStored(EnumFacing from){
2015-09-30 21:03:43 +02:00
return this.storage.getEnergyStored();
}
@Override
public int getMaxEnergyStored(EnumFacing from){
2015-09-30 21:03:43 +02:00
return this.storage.getMaxEnergyStored();
}
@Override
public boolean canConnectEnergy(EnumFacing from){
2015-09-30 21:03:43 +02:00
return true;
}
@Override
public int getEnergy(){
return this.storage.getEnergyStored();
}
@Override
2015-12-21 22:46:23 +01:00
public void setEnergy(int energy){
this.storage.setEnergyStored(energy);
}
@Override
2015-12-21 22:46:23 +01:00
@SideOnly(Side.CLIENT)
public int getMaxEnergy(){
return this.storage.getMaxEnergyStored();
}
2015-09-30 21:03:43 +02:00
}