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

179 lines
5.9 KiB
Java
Raw Normal View History

/*
* This file ("TileEntityBatteryBox.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://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
*/
package de.ellpeck.actuallyadditions.mod.tile;
2021-08-31 00:44:39 +02:00
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
import de.ellpeck.actuallyadditions.mod.items.ItemBattery;
2018-08-10 05:04:07 +02:00
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor;
import de.ellpeck.actuallyadditions.mod.util.ItemUtil;
2024-03-02 21:23:08 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
2024-03-03 01:20:53 +01:00
import net.minecraftforge.common.capabilities.ForgeCapabilities;
2021-02-27 16:33:00 +01:00
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.energy.IEnergyStorage;
2021-02-27 16:33:00 +01:00
import java.util.ArrayList;
import java.util.List;
2019-05-02 09:10:29 +02:00
public class TileEntityBatteryBox extends TileEntityInventoryBase implements ISharingEnergyProvider {
private int lastEnergyStored;
private int lastCompare;
2024-03-02 21:23:08 +01:00
public TileEntityBatteryBox(BlockPos pos, BlockState state) {
super(ActuallyBlocks.BATTERY_BOX.getTileEntityType(), pos, state, 1);
}
@Override
2021-02-27 16:33:00 +01:00
public LazyOptional<IEnergyStorage> getEnergyStorage(Direction facing) {
ItemStack stack = this.inv.getStackInSlot(0);
2023-01-15 17:56:02 +01:00
if (stack.getItem() instanceof ItemBattery) {
2024-03-03 01:20:53 +01:00
return stack.getCapability(ForgeCapabilities.ENERGY, null);
}
2021-02-27 16:33:00 +01:00
return LazyOptional.empty();
}
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 TileEntityBatteryBox tile) {
tile.clientTick();
}
}
2024-03-02 21:23:08 +01:00
public static <T extends BlockEntity> void serverTick(Level level, BlockPos pos, BlockState state, T t) {
if (t instanceof TileEntityBatteryBox tile) {
tile.serverTick();
LazyOptional<IEnergyStorage> cap = tile.getEnergyStorage(null);
2021-02-27 16:33:00 +01:00
int currStorage = cap.map(storage -> {
2024-03-02 21:23:08 +01:00
ItemStack stack = tile.inv.getStackInSlot(0);
2023-01-15 17:56:02 +01:00
if (!stack.isEmpty() && ItemUtil.isEnabled(stack)) {
2019-05-02 09:10:29 +02:00
if (storage.getEnergyStored() > 0) {
2019-02-27 19:53:05 +01:00
List<TileEntityBatteryBox> tiles = new ArrayList<>();
2024-03-02 21:23:08 +01:00
tile.energyPushOffLoop(tile, tiles);
2019-05-02 09:10:29 +02:00
if (!tiles.isEmpty()) {
int amount = tiles.size();
2019-05-02 09:10:29 +02:00
int energyPer = storage.getEnergyStored() / amount;
if (energyPer <= 0) {
energyPer = storage.getEnergyStored();
}
int maxPer = storage.extractEnergy(energyPer, true);
2024-03-02 21:23:08 +01:00
for (TileEntityBatteryBox te : tiles) {
ItemStack battery = te.inv.getStackInSlot(0);
2023-01-15 17:56:02 +01:00
if (!battery.isEmpty() && !ItemUtil.isEnabled(battery)) {
2024-03-03 01:20:53 +01:00
int received = te.getCapability(ForgeCapabilities.ENERGY, null).map(e -> e.receiveEnergy(maxPer, false)).orElse(0);
2021-02-27 16:33:00 +01:00
storage.extractEnergy(received, false);
if (storage.getEnergyStored() <= 0) {
break;
}
}
}
}
}
}
2021-02-27 16:33:00 +01:00
return storage.getEnergyStored();
}).orElse(0);
2024-03-02 21:23:08 +01:00
if (tile.lastCompare != tile.getComparatorStrength()) {
tile.lastCompare = tile.getComparatorStrength();
tile.setChanged();
}
2024-03-02 21:23:08 +01:00
if (tile.lastEnergyStored != currStorage && tile.sendUpdateWithInterval()) {
tile.lastEnergyStored = currStorage;
}
}
}
2024-03-02 21:23:08 +01:00
@Override
public void serverTick() {
super.serverTick();
}
@Override
2019-05-02 09:10:29 +02:00
public int getComparatorStrength() {
2021-02-27 16:33:00 +01:00
return this.getEnergyStorage(null)
.map(cap -> (int) ((float) cap.getEnergyStored() / (float) cap.getMaxEnergyStored() * 15F))
.orElse(0);
}
@Override
2019-05-02 09:10:29 +02:00
public boolean respondsToPulses() {
return true;
}
@Override
2019-05-02 09:10:29 +02:00
public void activateOnPulse() {
ItemStack stack = this.inv.getStackInSlot(0);
2023-01-15 17:56:02 +01:00
if (!stack.isEmpty()) {
ItemUtil.changeEnabled(stack);
this.setChanged();
}
}
2019-05-02 09:10:29 +02:00
private void energyPushOffLoop(TileEntityBatteryBox startTile, List<TileEntityBatteryBox> pushOffTo) {
2021-02-27 16:33:00 +01:00
if (pushOffTo.size() >= 15) {
return;
}
2024-03-02 21:23:08 +01:00
for (BlockEntity tile : startTile.tilesAround) {
2024-03-03 01:20:53 +01:00
if (tile instanceof TileEntityBatteryBox box) {
if (!pushOffTo.contains(box)) {
pushOffTo.add(box);
this.energyPushOffLoop(box, pushOffTo);
}
}
}
}
@Override
2019-05-02 09:10:29 +02:00
public IAcceptor getAcceptor() {
2018-08-10 05:04:07 +02:00
return (slot, stack, automation) -> stack.getItem() instanceof ItemBattery;
}
@Override
2019-05-02 09:10:29 +02:00
public boolean shouldSyncSlots() {
return true;
}
@Override
2019-05-02 09:10:29 +02:00
public int getEnergyToSplitShare() {
2021-02-27 16:33:00 +01:00
return this.getEnergyStorage(null)
.map(IEnergyStorage::getEnergyStored)
.orElse(0);
}
@Override
2019-05-02 09:10:29 +02:00
public boolean doesShareEnergy() {
return true;
}
@Override
public Direction[] getEnergyShareSides() {
return Direction.values();
}
@Override
2024-03-02 21:23:08 +01:00
public boolean canShareTo(BlockEntity tile) {
return !(tile instanceof TileEntityBatteryBox);
}
}