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

122 lines
4.1 KiB
Java
Raw Normal View History

2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.tile;
2015-09-30 21:03:43 +02:00
2019-05-02 09:10:29 +02:00
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import de.ellpeck.actuallyadditions.mod.util.AssetUtil;
2015-09-30 21:03:43 +02:00
import net.minecraft.block.Block;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.BlockPos;
2016-11-26 08:58:42 +01:00
import net.minecraftforge.energy.IEnergyStorage;
2015-09-30 21:03:43 +02:00
2019-05-02 09:10:29 +02:00
public class TileEntityLeafGenerator extends TileEntityBase implements ISharingEnergyProvider, IEnergyDisplay {
2015-09-30 21:03:43 +02:00
public static final int RANGE = 7;
public static final int ENERGY_PRODUCED = 300;
2016-11-26 20:43:50 +01:00
public final CustomEnergyStorage storage = new CustomEnergyStorage(35000, 0, 450);
2015-09-30 21:03:43 +02:00
private int nextUseCounter;
private int oldEnergy;
2015-09-30 21:03:43 +02:00
2019-05-02 09:10:29 +02:00
public TileEntityLeafGenerator() {
super("leafGenerator");
}
2016-02-01 17:49:55 +01:00
@Override
2019-05-02 09:10:29 +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);
}
@Override
2019-05-02 09:10:29 +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);
}
2015-09-30 21:03:43 +02:00
@Override
2019-05-02 09:10:29 +02:00
public void updateEntity() {
super.updateEntity();
2019-05-02 09:10:29 +02:00
if (!this.world.isRemote) {
if (!this.isRedstonePowered) {
2019-05-02 09:10:29 +02:00
if (this.nextUseCounter >= 5) {
this.nextUseCounter = 0;
2019-05-02 09:10:29 +02:00
if (ENERGY_PRODUCED <= this.storage.getMaxEnergyStored() - this.storage.getEnergyStored()) {
2019-02-27 19:53:05 +01:00
List<BlockPos> breakPositions = new ArrayList<>();
2019-05-02 09:10:29 +02: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++) {
2016-07-04 20:15:41 +02:00
BlockPos pos = this.pos.add(reachX, reachY, reachZ);
2016-11-26 21:32:27 +01:00
Block block = this.world.getBlockState(pos).getBlock();
2019-05-02 09:10:29 +02:00
if (block != null && block.isLeaves(this.world.getBlockState(pos), this.world, pos)) {
breakPositions.add(pos);
}
2015-09-30 21:03:43 +02:00
}
}
}
2019-05-02 09:10:29 +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-11-26 21:32:27 +01:00
this.world.playEvent(2001, theCoord, Block.getStateId(this.world.getBlockState(theCoord)));
2015-09-30 21:03:43 +02:00
2016-11-26 21:32:27 +01:00
this.world.setBlockToAir(theCoord);
2015-09-30 21:03:43 +02:00
this.storage.receiveEnergyInternal(ENERGY_PRODUCED, false);
2015-12-16 20:54:50 +01:00
2019-05-02 09:10:29 +02:00
AssetUtil.spawnLaserWithTimeServer(this.world, this.getPos().getX(), this.getPos().getY(), this.getPos().getZ(), theCoord.getX(), theCoord.getY(), theCoord.getZ(), new float[] { 62F / 255F, 163F / 255F, 74F / 255F }, 25, 0, 0.075F, 0.8F);
}
2015-09-30 21:03:43 +02:00
}
2019-05-02 09:10:29 +02:00
} else {
this.nextUseCounter++;
}
2015-09-30 21:03:43 +02:00
}
2019-05-02 09:10:29 +02:00
if (this.oldEnergy != this.storage.getEnergyStored() && this.sendUpdateWithInterval()) {
this.oldEnergy = this.storage.getEnergyStored();
}
2015-09-30 21:03:43 +02:00
}
}
@Override
2019-05-02 09:10:29 +02:00
public CustomEnergyStorage getEnergyStorage() {
return this.storage;
}
@Override
2019-05-02 09:10:29 +02:00
public boolean needsHoldShift() {
return false;
}
@Override
2019-05-02 09:10:29 +02:00
public int getEnergyToSplitShare() {
return this.storage.getEnergyStored();
}
@Override
2019-05-02 09:10:29 +02:00
public boolean doesShareEnergy() {
return true;
}
@Override
2019-05-02 09:10:29 +02:00
public EnumFacing[] getEnergyShareSides() {
return EnumFacing.values();
}
2016-11-26 08:58:42 +01:00
@Override
2019-05-02 09:10:29 +02:00
public boolean canShareTo(TileEntity tile) {
return true;
}
2016-11-26 08:58:42 +01:00
@Override
2019-05-02 09:10:29 +02:00
public IEnergyStorage getEnergyStorage(EnumFacing facing) {
2016-11-26 08:58:42 +01:00
return this.storage;
}
2015-09-30 21:03:43 +02:00
}