2016-12-18 17:28:29 +01:00
|
|
|
/*
|
|
|
|
* 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
|
2016-12-18 17:28:29 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
package de.ellpeck.actuallyadditions.mod.tile;
|
|
|
|
|
2021-08-31 00:44:39 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
|
2016-12-18 17:28:29 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.items.ItemBattery;
|
2018-08-10 05:04:07 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor;
|
2016-12-18 17:28:29 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ItemUtil;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2021-02-27 13:24:45 +01:00
|
|
|
import net.minecraft.util.Direction;
|
2021-02-27 16:33:00 +01:00
|
|
|
import net.minecraftforge.common.util.LazyOptional;
|
2016-12-18 17:28:29 +01:00
|
|
|
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 {
|
2016-12-18 17:28:29 +01:00
|
|
|
|
|
|
|
private int lastEnergyStored;
|
2016-12-28 19:00:26 +01:00
|
|
|
private int lastCompare;
|
2016-12-18 17:28:29 +01:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public TileEntityBatteryBox() {
|
2021-08-31 00:44:39 +02:00
|
|
|
super(ActuallyBlocks.BATTERY_BOX.getTileEntityType(), 1);
|
2016-12-18 17:28:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-27 16:33:00 +01:00
|
|
|
public LazyOptional<IEnergyStorage> getEnergyStorage(Direction facing) {
|
2018-06-23 01:39:30 +02:00
|
|
|
ItemStack stack = this.inv.getStackInSlot(0);
|
2023-01-15 17:56:02 +01:00
|
|
|
if (stack.getItem() instanceof ItemBattery) {
|
2021-02-27 16:33:00 +01:00
|
|
|
return stack.getCapability(CapabilityEnergy.ENERGY, null);
|
2016-12-18 17:28:29 +01:00
|
|
|
}
|
2021-02-27 16:33:00 +01:00
|
|
|
return LazyOptional.empty();
|
2016-12-18 17:28:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public void updateEntity() {
|
2016-12-18 17:28:29 +01:00
|
|
|
super.updateEntity();
|
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
if (!this.level.isClientSide) {
|
2021-02-27 16:33:00 +01:00
|
|
|
LazyOptional<IEnergyStorage> cap = this.getEnergyStorage(null);
|
|
|
|
int currStorage = cap.map(storage -> {
|
2018-06-23 01:39:30 +02:00
|
|
|
ItemStack stack = this.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<>();
|
2016-12-18 17:28:29 +01:00
|
|
|
this.energyPushOffLoop(this, tiles);
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (!tiles.isEmpty()) {
|
2016-12-18 17:28:29 +01:00
|
|
|
int amount = tiles.size();
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
int energyPer = storage.getEnergyStored() / amount;
|
|
|
|
if (energyPer <= 0) {
|
2016-12-18 17:28:29 +01:00
|
|
|
energyPer = storage.getEnergyStored();
|
|
|
|
}
|
|
|
|
int maxPer = storage.extractEnergy(energyPer, true);
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
for (TileEntityBatteryBox tile : tiles) {
|
2018-06-23 01:39:30 +02:00
|
|
|
ItemStack battery = tile.inv.getStackInSlot(0);
|
2023-01-15 17:56:02 +01:00
|
|
|
if (!battery.isEmpty() && !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;
|
2016-12-18 17:28:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-27 16:33:00 +01:00
|
|
|
return storage.getEnergyStored();
|
|
|
|
}).orElse(0);
|
2016-12-18 17:28:29 +01:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (this.lastCompare != this.getComparatorStrength()) {
|
2016-12-28 19:00:26 +01:00
|
|
|
this.lastCompare = this.getComparatorStrength();
|
2021-08-22 17:09:06 +02:00
|
|
|
this.setChanged();
|
2016-12-28 19:00:26 +01:00
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (this.lastEnergyStored != currStorage && this.sendUpdateWithInterval()) {
|
2016-12-18 17:28:29 +01:00
|
|
|
this.lastEnergyStored = currStorage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-28 19:00:26 +01:00
|
|
|
@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);
|
2016-12-28 19:00:26 +01:00
|
|
|
}
|
|
|
|
|
2016-12-18 17:28:29 +01:00
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public boolean respondsToPulses() {
|
2016-12-18 17:28:29 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public void activateOnPulse() {
|
2018-06-23 01:39:30 +02:00
|
|
|
ItemStack stack = this.inv.getStackInSlot(0);
|
2023-01-15 17:56:02 +01:00
|
|
|
if (!stack.isEmpty()) {
|
2016-12-18 17:28:29 +01:00
|
|
|
ItemUtil.changeEnabled(stack);
|
2021-08-22 17:09:06 +02:00
|
|
|
this.setChanged();
|
2016-12-18 17:28:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2016-12-18 17:28:29 +01:00
|
|
|
|
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)) {
|
2016-12-18 17:28:29 +01:00
|
|
|
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;
|
2016-12-18 17:28:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public boolean shouldSyncSlots() {
|
2016-12-18 17:28:29 +01:00
|
|
|
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);
|
2016-12-18 17:28:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public boolean doesShareEnergy() {
|
2016-12-18 17:28:29 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-27 13:24:45 +01:00
|
|
|
public Direction[] getEnergyShareSides() {
|
|
|
|
return Direction.values();
|
2016-12-18 17:28:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public boolean canShareTo(TileEntity tile) {
|
2016-12-18 17:28:29 +01:00
|
|
|
return !(tile instanceof TileEntityBatteryBox);
|
|
|
|
}
|
|
|
|
}
|