2015-09-30 21:03:43 +02:00
|
|
|
/*
|
2016-05-16 22:52:27 +02:00
|
|
|
* This file ("TileEntityLeafGenerator.java") is part of the Actually Additions mod for Minecraft.
|
2015-09-30 21:03:43 +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-09-30 21:03:43 +02:00
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
2017-01-01 16:23:26 +01:00
|
|
|
* © 2015-2017 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
|
|
|
|
2021-08-30 22:51:41 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
|
2019-11-08 18:07:11 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.config.values.ConfigIntValues;
|
2016-06-15 16:43:59 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.AssetUtil;
|
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.LeavesBlock;
|
|
|
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
2024-03-04 20:21:48 +01:00
|
|
|
import net.neoforged.neoforge.energy.IEnergyStorage;
|
2015-09-30 21:03:43 +02:00
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public class TileEntityLeafGenerator extends TileEntityBase implements ISharingEnergyProvider, IEnergyDisplay {
|
2015-09-30 21:03:43 +02:00
|
|
|
|
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;
|
2015-12-21 22:18:33 +01:00
|
|
|
private int oldEnergy;
|
2015-09-30 21:03:43 +02:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public TileEntityLeafGenerator(BlockPos pos, BlockState state) {
|
|
|
|
super(ActuallyBlocks.LEAF_GENERATOR.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-02-01 17:49:55 +01:00
|
|
|
this.storage.writeToNBT(compound);
|
|
|
|
}
|
|
|
|
|
|
|
|
@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-02-01 17:49:55 +01:00
|
|
|
this.storage.readFromNBT(compound);
|
|
|
|
}
|
|
|
|
|
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 TileEntityLeafGenerator tile) {
|
|
|
|
tile.clientTick();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <T extends BlockEntity> void serverTick(Level level, BlockPos pos, BlockState state, T t) {
|
|
|
|
if (t instanceof TileEntityLeafGenerator tile) {
|
|
|
|
tile.serverTick();
|
|
|
|
|
|
|
|
if (!tile.isRedstonePowered) {
|
2015-10-05 11:27:53 +02:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
if (tile.nextUseCounter >= ConfigIntValues.LEAF_GENERATOR_COOLDOWN.getValue()) {
|
|
|
|
tile.nextUseCounter = 0;
|
2015-10-05 11:27:53 +02:00
|
|
|
|
2019-11-08 18:07:11 +01:00
|
|
|
int energyProduced = ConfigIntValues.LEAF_GENERATOR_CF_PER_LEAF.getValue();
|
2024-03-02 21:23:08 +01:00
|
|
|
if (energyProduced > 0 && energyProduced <= tile.storage.getMaxEnergyStored() - tile.storage.getEnergyStored()) {
|
2019-02-27 19:53:05 +01:00
|
|
|
List<BlockPos> breakPositions = new ArrayList<>();
|
2015-10-05 11:27:53 +02:00
|
|
|
|
2019-11-08 18:07:11 +01:00
|
|
|
int range = ConfigIntValues.LEAF_GENERATOR_AREA.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++) {
|
2024-03-02 21:23:08 +01:00
|
|
|
BlockPos offsetPos = pos.offset(reachX, reachY, reachZ);
|
|
|
|
Block block = level.getBlockState(offsetPos).getBlock();
|
|
|
|
if (block instanceof LeavesBlock) { // TODO: [port] validate tile is a good way of checking if something is a leaf
|
|
|
|
breakPositions.add(offsetPos);
|
2015-10-05 11:27:53 +02:00
|
|
|
}
|
2015-09-30 21:03:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (!breakPositions.isEmpty()) {
|
2015-10-05 11:27:53 +02:00
|
|
|
Collections.shuffle(breakPositions);
|
2016-01-08 13:31:58 +01:00
|
|
|
BlockPos theCoord = breakPositions.get(0);
|
2015-09-30 21:03:43 +02:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
level.levelEvent(2001, theCoord, Block.getId(level.getBlockState(theCoord)));
|
2015-09-30 21:03:43 +02:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
level.setBlockAndUpdate(theCoord, Blocks.AIR.defaultBlockState());
|
2015-09-30 21:03:43 +02:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
tile.storage.receiveEnergyInternal(energyProduced, false);
|
2015-12-16 20:54:50 +01:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
AssetUtil.spawnLaserWithTimeServer(level, pos.getX(), pos.getY(), pos.getZ(), theCoord.getX(), theCoord.getY(), theCoord.getZ(), 0x3EA34A, 25, 0, 0.075F, 0.8F);
|
2015-10-05 11:27:53 +02:00
|
|
|
}
|
2015-09-30 21:03:43 +02:00
|
|
|
}
|
2019-05-02 09:10:29 +02:00
|
|
|
} else {
|
2024-03-02 21:23:08 +01:00
|
|
|
tile.nextUseCounter++;
|
2015-10-05 11:27:53 +02:00
|
|
|
}
|
2015-09-30 21:03:43 +02: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-09-30 21:03: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-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
|
2024-03-04 20:21:48 +01:00
|
|
|
public IEnergyStorage getEnergyStorage(Direction facing) {
|
2024-03-04 21:38:02 +01:00
|
|
|
return this.storage;
|
2016-11-26 08:58:42 +01:00
|
|
|
}
|
2015-09-30 21:03:43 +02:00
|
|
|
}
|