2015-08-29 14:33:25 +02:00
|
|
|
/*
|
2016-05-16 22:52:27 +02:00
|
|
|
* This file ("TileEntityHeatCollector.java") is part of the Actually Additions mod for Minecraft.
|
2015-08-29 14:33:25 +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-08-29 14:33:25 +02:00
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
2017-01-01 16:23:26 +01:00
|
|
|
* © 2015-2017 Ellpeck
|
2015-08-29 14:33:25 +02:00
|
|
|
*/
|
|
|
|
|
2016-01-05 04:47:35 +01:00
|
|
|
package de.ellpeck.actuallyadditions.mod.tile;
|
2015-03-31 20:37:55 +02:00
|
|
|
|
2021-11-13 18:16:25 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
|
2016-01-05 04:47:35 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
|
2024-03-02 21:23:08 +01:00
|
|
|
import net.minecraft.core.BlockPos;
|
|
|
|
import net.minecraft.core.Direction;
|
|
|
|
import net.minecraft.nbt.CompoundTag;
|
|
|
|
import net.minecraft.world.level.Level;
|
|
|
|
import net.minecraft.world.level.block.Block;
|
|
|
|
import net.minecraft.world.level.block.Blocks;
|
|
|
|
import net.minecraft.world.level.block.MagmaBlock;
|
|
|
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
|
|
import net.minecraft.world.level.material.Material;
|
2021-02-27 16:33:00 +01:00
|
|
|
import net.minecraftforge.common.util.LazyOptional;
|
2016-11-26 08:58:42 +01:00
|
|
|
import net.minecraftforge.energy.IEnergyStorage;
|
2015-03-31 20:37:55 +02:00
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public class TileEntityHeatCollector extends TileEntityBase implements ISharingEnergyProvider, IEnergyDisplay {
|
2015-03-31 20:37:55 +02:00
|
|
|
|
2015-11-28 19:02:01 +01:00
|
|
|
public static final int ENERGY_PRODUCE = 40;
|
|
|
|
public static final int BLOCKS_NEEDED = 4;
|
2016-11-26 20:43:50 +01:00
|
|
|
public final CustomEnergyStorage storage = new CustomEnergyStorage(30000, 0, 80);
|
2021-02-27 16:33:00 +01:00
|
|
|
public final LazyOptional<IEnergyStorage> lazyEnergy = LazyOptional.of(() -> this.storage);
|
2015-12-21 22:18:33 +01:00
|
|
|
private int oldEnergy;
|
2016-11-28 13:17:20 +01:00
|
|
|
private int disappearTime;
|
2015-11-28 19:02:01 +01:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public TileEntityHeatCollector(BlockPos pos, BlockState state) {
|
|
|
|
super(ActuallyBlocks.HEAT_COLLECTOR.getTileEntityType(), pos, state);
|
2016-05-06 23:23:29 +02:00
|
|
|
}
|
|
|
|
|
2016-02-01 17:49:55 +01:00
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public void writeSyncableNBT(CompoundTag compound, NBTType type) {
|
2016-07-02 15:01:46 +02:00
|
|
|
super.writeSyncableNBT(compound, type);
|
2016-11-28 13:17:20 +01:00
|
|
|
|
2016-02-01 17:49:55 +01:00
|
|
|
this.storage.writeToNBT(compound);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (type == NBTType.SAVE_TILE) {
|
2021-02-27 16:33:00 +01:00
|
|
|
compound.putInt("DisappearTime", this.disappearTime);
|
2016-11-28 13:17:20 +01:00
|
|
|
}
|
2016-02-01 17:49:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public void readSyncableNBT(CompoundTag compound, NBTType type) {
|
2016-07-02 15:01:46 +02:00
|
|
|
super.readSyncableNBT(compound, type);
|
2016-11-28 13:17:20 +01:00
|
|
|
|
2016-02-01 17:49:55 +01:00
|
|
|
this.storage.readFromNBT(compound);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (type == NBTType.SAVE_TILE) {
|
2021-02-27 16:33:00 +01:00
|
|
|
this.disappearTime = compound.getInt("DisappearTime");
|
2016-11-28 13:17:20 +01:00
|
|
|
}
|
2016-02-01 17:49:55 +01:00
|
|
|
}
|
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public static <T extends BlockEntity> void clientTick(Level level, BlockPos pos, BlockState state, T t) {
|
|
|
|
if (t instanceof TileEntityHeatCollector tile) {
|
|
|
|
tile.clientTick();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <T extends BlockEntity> void serverTick(Level level, BlockPos pos, BlockState state, T t) {
|
|
|
|
if (t instanceof TileEntityHeatCollector tile) {
|
|
|
|
tile.serverTick();
|
|
|
|
|
2019-02-27 19:53:05 +01:00
|
|
|
ArrayList<Integer> blocksAround = new ArrayList<>();
|
2024-03-02 21:23:08 +01:00
|
|
|
if (ENERGY_PRODUCE <= tile.storage.getMaxEnergyStored() - tile.storage.getEnergyStored()) {
|
2019-05-02 09:10:29 +02:00
|
|
|
for (int i = 1; i <= 5; i++) {
|
2024-03-02 21:23:08 +01:00
|
|
|
BlockPos coords = pos.relative(WorldUtil.getDirectionBySidesInOrder(i));
|
|
|
|
BlockState relativeState = level.getBlockState(coords);
|
|
|
|
Block block = relativeState.getBlock();
|
|
|
|
if (block != null && level.getBlockState(coords).getMaterial() == Material.LAVA || level.getBlockState(coords).getBlock() instanceof MagmaBlock) {
|
2016-04-20 21:39:03 +02:00
|
|
|
blocksAround.add(i);
|
2015-03-31 20:37:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (blocksAround.size() >= BLOCKS_NEEDED) {
|
2024-03-02 21:23:08 +01:00
|
|
|
tile.storage.receiveEnergyInternal(ENERGY_PRODUCE, false);
|
|
|
|
tile.setChanged();
|
2015-03-31 20:37:55 +02:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
tile.disappearTime++;
|
|
|
|
if (tile.disappearTime >= 1000) {
|
|
|
|
tile.disappearTime = 0;
|
2016-11-28 13:17:20 +01:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
if (level.random.nextInt(200) == 0) {
|
|
|
|
int randomSide = blocksAround.get(level.random.nextInt(blocksAround.size()));
|
|
|
|
level.setBlockAndUpdate(pos.relative(WorldUtil.getDirectionBySidesInOrder(randomSide)), Blocks.AIR.defaultBlockState());
|
2016-11-28 13:17:20 +01:00
|
|
|
}
|
2015-05-20 22:39:43 +02:00
|
|
|
}
|
|
|
|
}
|
2015-10-31 17:14:56 +01:00
|
|
|
}
|
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
if (tile.oldEnergy != tile.storage.getEnergyStored() && tile.sendUpdateWithInterval()) {
|
|
|
|
tile.oldEnergy = tile.storage.getEnergyStored();
|
2015-12-21 22:18:33 +01:00
|
|
|
}
|
2015-03-31 20:37:55 +02:00
|
|
|
}
|
2015-05-20 22:39:43 +02:00
|
|
|
}
|
|
|
|
|
2015-12-13 00:54:25 +01:00
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public CustomEnergyStorage getEnergyStorage() {
|
2016-07-21 13:22:55 +02:00
|
|
|
return this.storage;
|
2015-12-13 00:54:25 +01:00
|
|
|
}
|
2016-08-02 13:08:22 +02:00
|
|
|
|
2016-07-02 18:39:47 +02:00
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public boolean needsHoldShift() {
|
2016-07-02 18:39:47 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-08-31 20:16:36 +02:00
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public int getEnergyToSplitShare() {
|
2016-08-31 20:16:36 +02:00
|
|
|
return this.storage.getEnergyStored();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public boolean doesShareEnergy() {
|
2016-08-31 20:16:36 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-27 13:24:45 +01:00
|
|
|
public Direction[] getEnergyShareSides() {
|
|
|
|
return Direction.values();
|
2016-08-31 20:16:36 +02:00
|
|
|
}
|
2016-11-26 08:58:42 +01:00
|
|
|
|
2016-12-18 17:28:29 +01:00
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public boolean canShareTo(BlockEntity tile) {
|
2016-12-18 17:28:29 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-26 08:58:42 +01:00
|
|
|
@Override
|
2021-02-27 16:33:00 +01:00
|
|
|
public LazyOptional<IEnergyStorage> getEnergyStorage(Direction facing) {
|
|
|
|
return this.lazyEnergy;
|
2016-11-26 08:58:42 +01:00
|
|
|
}
|
|
|
|
|
2015-03-31 20:37:55 +02:00
|
|
|
}
|