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

166 lines
5.5 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;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Direction;
2021-02-27 16:33:00 +01:00
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.energy.CapabilityEnergy;
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;
2019-05-02 09:10:29 +02:00
public TileEntityBatteryBox() {
2021-08-31 00:44:39 +02:00
super(ActuallyBlocks.BATTERY_BOX.getTileEntityType(), 1);
}
@Override
2021-02-27 16:33:00 +01:00
public LazyOptional<IEnergyStorage> getEnergyStorage(Direction facing) {
ItemStack stack = this.inv.getStackInSlot(0);
2019-05-02 09:10:29 +02:00
if (StackUtil.isValid(stack) && stack.getItem() instanceof ItemBattery) {
2021-02-27 16:33:00 +01:00
return stack.getCapability(CapabilityEnergy.ENERGY, null);
}
2021-02-27 16:33:00 +01:00
return LazyOptional.empty();
}
@Override
2019-05-02 09:10:29 +02:00
public void updateEntity() {
super.updateEntity();
if (!this.level.isClientSide) {
2021-02-27 16:33:00 +01:00
LazyOptional<IEnergyStorage> cap = this.getEnergyStorage(null);
int currStorage = cap.map(storage -> {
ItemStack stack = this.inv.getStackInSlot(0);
2019-05-02 09:10:29 +02:00
if (StackUtil.isValid(stack) && ItemUtil.isEnabled(stack)) {
if (storage.getEnergyStored() > 0) {
2019-02-27 19:53:05 +01:00
List<TileEntityBatteryBox> tiles = new ArrayList<>();
this.energyPushOffLoop(this, 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);
2019-05-02 09:10:29 +02:00
for (TileEntityBatteryBox tile : tiles) {
ItemStack battery = tile.inv.getStackInSlot(0);
2019-05-02 09:10:29 +02:00
if (StackUtil.isValid(battery) && !ItemUtil.isEnabled(battery)) {
2021-02-27 16:33:00 +01:00
int received = tile.getCapability(CapabilityEnergy.ENERGY, null).map(e -> e.receiveEnergy(maxPer, false)).orElse(0);
storage.extractEnergy(received, false);
if (storage.getEnergyStored() <= 0) {
break;
}
}
}
}
}
}
2021-02-27 16:33:00 +01:00
return storage.getEnergyStored();
}).orElse(0);
2019-05-02 09:10:29 +02:00
if (this.lastCompare != this.getComparatorStrength()) {
this.lastCompare = this.getComparatorStrength();
this.setChanged();
}
2019-05-02 09:10:29 +02:00
if (this.lastEnergyStored != currStorage && this.sendUpdateWithInterval()) {
this.lastEnergyStored = currStorage;
}
}
}
@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);
2019-05-02 09:10:29 +02:00
if (StackUtil.isValid(stack)) {
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;
}
2019-05-02 09:10:29 +02:00
for (TileEntity tile : startTile.tilesAround) {
if (tile instanceof TileEntityBatteryBox) {
TileEntityBatteryBox box = (TileEntityBatteryBox) tile;
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
2019-05-02 09:10:29 +02:00
public boolean canShareTo(TileEntity tile) {
return !(tile instanceof TileEntityBatteryBox);
}
}