Remove energy API

This commit is contained in:
raoulvdberge
2019-10-12 17:27:09 +02:00
parent 8471ebf676
commit c657efe437
7 changed files with 29 additions and 229 deletions

View File

@@ -1,51 +0,0 @@
package com.raoulvdberge.refinedstorage.api.energy;
import com.raoulvdberge.refinedstorage.api.util.Action;
import java.util.UUID;
/**
* An energy container.
*/
public interface IEnergy {
/**
* @param id id of the storage
* @param amount the amount
*/
void removeCapacity(UUID id, int amount);
/**
* @param id id of the storage
* @param amount the amount
*/
void addCapacity(UUID id, int amount);
/**
* @return the capacity
*/
int getCapacity();
/**
* @return the amount stored
*/
int getStored();
/**
* @param amount the amount stored
*/
void setStored(int amount);
/**
* @param amount the amount
* @param action the action
* @return the energy extracted
*/
int extract(int amount, Action action);
/**
* @param amount the amount
* @param action the action
* @return the energy inserted
*/
int insert(int amount, Action action);
}

View File

@@ -1,7 +1,6 @@
package com.raoulvdberge.refinedstorage.api.network; package com.raoulvdberge.refinedstorage.api.network;
import com.raoulvdberge.refinedstorage.api.autocrafting.ICraftingManager; import com.raoulvdberge.refinedstorage.api.autocrafting.ICraftingManager;
import com.raoulvdberge.refinedstorage.api.energy.IEnergy;
import com.raoulvdberge.refinedstorage.api.network.grid.handler.IFluidGridHandler; import com.raoulvdberge.refinedstorage.api.network.grid.handler.IFluidGridHandler;
import com.raoulvdberge.refinedstorage.api.network.grid.handler.IItemGridHandler; import com.raoulvdberge.refinedstorage.api.network.grid.handler.IItemGridHandler;
import com.raoulvdberge.refinedstorage.api.network.item.INetworkItemHandler; import com.raoulvdberge.refinedstorage.api.network.item.INetworkItemHandler;
@@ -55,11 +54,6 @@ public interface INetwork {
*/ */
ICraftingManager getCraftingManager(); ICraftingManager getCraftingManager();
/**
* @return the {@link IEnergy} of this network
*/
IEnergy getEnergy();
/** /**
* @return the {@link IItemGridHandler} of this network * @return the {@link IItemGridHandler} of this network
*/ */

View File

@@ -1,97 +0,0 @@
package com.raoulvdberge.refinedstorage.apiimpl.energy;
import com.raoulvdberge.refinedstorage.api.energy.IEnergy;
import com.raoulvdberge.refinedstorage.api.util.Action;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import java.util.Map;
import java.util.UUID;
public final class Energy implements IEnergy {
private static final UUID DEFAULT_UUID = new UUID(0L, 0L);
protected int capacity;
protected int energy;
private final Map<UUID, Integer> storages;
public Energy(int controllerCapacity) {
this.storages = new Object2ObjectOpenHashMap<>();
this.storages.put(DEFAULT_UUID, controllerCapacity);
calculateCapacity();
}
private void calculateCapacity() {
long newCapacity = storages.values().stream().mapToLong(Long::valueOf).sum();
this.capacity = (int) Math.min(newCapacity, Integer.MAX_VALUE);
}
@Override
public void removeCapacity(UUID id, int amount) {
if (id.equals(DEFAULT_UUID)) {
return;
}
this.storages.remove(id);
calculateCapacity();
}
@Override
public int extract(int maxExtract, Action action) {
if (maxExtract <= 0) {
return 0;
}
int extracted = Math.min(energy, maxExtract);
if (action == Action.PERFORM) {
energy -= extracted;
}
return extracted;
}
@Override
public int getCapacity() {
return this.capacity;
}
@Override
public int getStored() {
return this.energy;
}
@Override
public void addCapacity(UUID id, int amount) {
if (id.equals(DEFAULT_UUID) || amount <= 0) {
return;
}
this.storages.merge(id, amount, (k, v) -> amount);
calculateCapacity();
}
@Override
public int insert(int maxReceive, Action action) {
if (maxReceive <= 0) {
return 0;
}
int inserted = Math.min(capacity - energy, maxReceive);
if (action == Action.PERFORM) {
energy += inserted;
}
return inserted;
}
@Override
public void setStored(int amount) {
this.energy = Math.min(amount, this.capacity);
}
}

View File

@@ -48,7 +48,7 @@ public class ServerConfig {
public class Controller { public class Controller {
private final ForgeConfigSpec.IntValue baseUsage; private final ForgeConfigSpec.IntValue baseUsage;
private final ForgeConfigSpec.IntValue maxReceive; private final ForgeConfigSpec.IntValue maxTransfer;
private final ForgeConfigSpec.IntValue capacity; private final ForgeConfigSpec.IntValue capacity;
private final ForgeConfigSpec.BooleanValue useEnergy; private final ForgeConfigSpec.BooleanValue useEnergy;
@@ -56,7 +56,7 @@ public class ServerConfig {
builder.push("controller"); builder.push("controller");
baseUsage = builder.comment("The base energy used by the Controller").defineInRange("baseUsage", 0, 0, Integer.MAX_VALUE); baseUsage = builder.comment("The base energy used by the Controller").defineInRange("baseUsage", 0, 0, Integer.MAX_VALUE);
maxReceive = builder.comment("The maximum energy the Controller receives per tick").defineInRange("maxReceive", Integer.MAX_VALUE, 0, Integer.MAX_VALUE); maxTransfer = builder.comment("The maximum energy that the Controller can receive").defineInRange("maxTransfer", Integer.MAX_VALUE, 0, Integer.MAX_VALUE);
capacity = builder.comment("The energy capacity of the Controller").defineInRange("capacity", 32000, 0, Integer.MAX_VALUE); capacity = builder.comment("The energy capacity of the Controller").defineInRange("capacity", 32000, 0, Integer.MAX_VALUE);
useEnergy = builder.comment("Whether the Controller uses energy").define("useEnergy", true); useEnergy = builder.comment("Whether the Controller uses energy").define("useEnergy", true);
@@ -67,8 +67,8 @@ public class ServerConfig {
return baseUsage.get(); return baseUsage.get();
} }
public int getMaxReceive() { public int getMaxTransfer() {
return maxReceive.get(); return maxTransfer.get();
} }
public int getCapacity() { public int getCapacity() {

View File

@@ -0,0 +1,13 @@
package com.raoulvdberge.refinedstorage.integration.forgeenergy;
import net.minecraftforge.energy.EnergyStorage;
public class BaseEnergyStorage extends EnergyStorage {
public BaseEnergyStorage(int capacity, int maxTransfer) {
super(capacity, maxTransfer);
}
public void setStored(int energy) {
this.energy = energy;
}
}

View File

@@ -1,51 +0,0 @@
package com.raoulvdberge.refinedstorage.integration.forgeenergy;
import com.raoulvdberge.refinedstorage.api.energy.IEnergy;
import com.raoulvdberge.refinedstorage.api.util.Action;
import net.minecraftforge.energy.IEnergyStorage;
public final class EnergyProxy implements IEnergyStorage {
private final int maxReceive;
private final int maxExtract;
private final IEnergy energy;
public EnergyProxy(IEnergy energy, int maxTransfer) {
this(energy, maxTransfer, maxTransfer);
}
public EnergyProxy(IEnergy energy, int maxReceive, int maxExtract) {
this.energy = energy;
this.maxReceive = maxReceive;
this.maxExtract = maxExtract;
}
@Override
public int receiveEnergy(int maxReceive, boolean simulate) {
return !canReceive() ? 0 : this.energy.insert(Math.min(this.maxReceive, maxReceive), simulate ? Action.SIMULATE : Action.PERFORM);
}
@Override
public int extractEnergy(int maxExtract, boolean simulate) {
return !canExtract() ? 0 : this.energy.extract(Math.min(this.maxExtract, maxExtract), simulate ? Action.SIMULATE : Action.PERFORM);
}
@Override
public int getEnergyStored() {
return this.energy.getStored();
}
@Override
public int getMaxEnergyStored() {
return this.energy.getCapacity();
}
@Override
public boolean canExtract() {
return this.maxExtract > 0;
}
@Override
public boolean canReceive() {
return this.maxReceive > 0;
}
}

View File

@@ -4,7 +4,6 @@ import com.google.common.base.Preconditions;
import com.raoulvdberge.refinedstorage.RS; import com.raoulvdberge.refinedstorage.RS;
import com.raoulvdberge.refinedstorage.RSTiles; import com.raoulvdberge.refinedstorage.RSTiles;
import com.raoulvdberge.refinedstorage.api.autocrafting.ICraftingManager; import com.raoulvdberge.refinedstorage.api.autocrafting.ICraftingManager;
import com.raoulvdberge.refinedstorage.api.energy.IEnergy;
import com.raoulvdberge.refinedstorage.api.network.INetwork; import com.raoulvdberge.refinedstorage.api.network.INetwork;
import com.raoulvdberge.refinedstorage.api.network.INetworkNodeGraph; import com.raoulvdberge.refinedstorage.api.network.INetworkNodeGraph;
import com.raoulvdberge.refinedstorage.api.network.INetworkNodeVisitor; import com.raoulvdberge.refinedstorage.api.network.INetworkNodeVisitor;
@@ -23,7 +22,6 @@ import com.raoulvdberge.refinedstorage.api.storage.externalstorage.IStorageExter
import com.raoulvdberge.refinedstorage.api.storage.tracker.IStorageTracker; import com.raoulvdberge.refinedstorage.api.storage.tracker.IStorageTracker;
import com.raoulvdberge.refinedstorage.api.util.Action; import com.raoulvdberge.refinedstorage.api.util.Action;
import com.raoulvdberge.refinedstorage.apiimpl.autocrafting.CraftingManager; import com.raoulvdberge.refinedstorage.apiimpl.autocrafting.CraftingManager;
import com.raoulvdberge.refinedstorage.apiimpl.energy.Energy;
import com.raoulvdberge.refinedstorage.apiimpl.network.NetworkNodeGraph; import com.raoulvdberge.refinedstorage.apiimpl.network.NetworkNodeGraph;
import com.raoulvdberge.refinedstorage.apiimpl.network.grid.handler.FluidGridHandler; import com.raoulvdberge.refinedstorage.apiimpl.network.grid.handler.FluidGridHandler;
import com.raoulvdberge.refinedstorage.apiimpl.network.grid.handler.ItemGridHandler; import com.raoulvdberge.refinedstorage.apiimpl.network.grid.handler.ItemGridHandler;
@@ -36,7 +34,7 @@ import com.raoulvdberge.refinedstorage.apiimpl.storage.cache.ItemStorageCache;
import com.raoulvdberge.refinedstorage.apiimpl.storage.tracker.FluidStorageTracker; import com.raoulvdberge.refinedstorage.apiimpl.storage.tracker.FluidStorageTracker;
import com.raoulvdberge.refinedstorage.apiimpl.storage.tracker.ItemStorageTracker; import com.raoulvdberge.refinedstorage.apiimpl.storage.tracker.ItemStorageTracker;
import com.raoulvdberge.refinedstorage.block.ControllerBlock; import com.raoulvdberge.refinedstorage.block.ControllerBlock;
import com.raoulvdberge.refinedstorage.integration.forgeenergy.EnergyProxy; import com.raoulvdberge.refinedstorage.integration.forgeenergy.BaseEnergyStorage;
import com.raoulvdberge.refinedstorage.tile.config.IRedstoneConfigurable; import com.raoulvdberge.refinedstorage.tile.config.IRedstoneConfigurable;
import com.raoulvdberge.refinedstorage.tile.config.RedstoneMode; import com.raoulvdberge.refinedstorage.tile.config.RedstoneMode;
import com.raoulvdberge.refinedstorage.tile.data.RSSerializers; import com.raoulvdberge.refinedstorage.tile.data.RSSerializers;
@@ -82,8 +80,8 @@ public class ControllerTile extends BaseTile implements ITickableTileEntity, INe
public static final TileDataParameter<Integer, ControllerTile> REDSTONE_MODE = RedstoneMode.createParameter(); public static final TileDataParameter<Integer, ControllerTile> REDSTONE_MODE = RedstoneMode.createParameter();
public static final TileDataParameter<Integer, ControllerTile> ENERGY_USAGE = new TileDataParameter<>(DataSerializers.VARINT, 0, ControllerTile::getEnergyUsage); public static final TileDataParameter<Integer, ControllerTile> ENERGY_USAGE = new TileDataParameter<>(DataSerializers.VARINT, 0, ControllerTile::getEnergyUsage);
public static final TileDataParameter<Integer, ControllerTile> ENERGY_STORED = new TileDataParameter<>(DataSerializers.VARINT, 0, t -> t.getEnergy().getStored()); public static final TileDataParameter<Integer, ControllerTile> ENERGY_STORED = new TileDataParameter<>(DataSerializers.VARINT, 0, t -> t.energy.getEnergyStored());
public static final TileDataParameter<Integer, ControllerTile> ENERGY_CAPACITY = new TileDataParameter<>(DataSerializers.VARINT, 0, t -> t.getEnergy().getCapacity()); public static final TileDataParameter<Integer, ControllerTile> ENERGY_CAPACITY = new TileDataParameter<>(DataSerializers.VARINT, 0, t -> t.energy.getMaxEnergyStored());
public static final TileDataParameter<List<ClientNode>, ControllerTile> NODES = new TileDataParameter<>(RSSerializers.CLIENT_NODE_SERIALIZER, new ArrayList<>(), t -> { public static final TileDataParameter<List<ClientNode>, ControllerTile> NODES = new TileDataParameter<>(RSSerializers.CLIENT_NODE_SERIALIZER, new ArrayList<>(), t -> {
List<ClientNode> nodes = new ArrayList<>(); List<ClientNode> nodes = new ArrayList<>();
@@ -140,10 +138,9 @@ public class ControllerTile extends BaseTile implements ITickableTileEntity, INe
private IReaderWriterManager readerWriterManager = new ReaderWriterManager(this); private IReaderWriterManager readerWriterManager = new ReaderWriterManager(this);
private final IEnergy energy = new Energy(RS.SERVER_CONFIG.getController().getCapacity()); private final BaseEnergyStorage energy = new BaseEnergyStorage(RS.SERVER_CONFIG.getController().getCapacity(), RS.SERVER_CONFIG.getController().getMaxTransfer());
private final EnergyProxy energyProxy = new EnergyProxy(this.energy, RS.SERVER_CONFIG.getController().getMaxReceive());
private final LazyOptional<IEnergyStorage> energyProxyCap = LazyOptional.of(() -> energyProxy); private final LazyOptional<IEnergyStorage> energyProxyCap = LazyOptional.of(() -> energy);
private final LazyOptional<INetworkNodeProxy<ControllerTile>> networkNodeProxyCap = LazyOptional.of(() -> this); private final LazyOptional<INetworkNodeProxy<ControllerTile>> networkNodeProxyCap = LazyOptional.of(() -> this);
private boolean throttlingDisabled = true; // Will be enabled after first update private boolean throttlingDisabled = true; // Will be enabled after first update
@@ -180,11 +177,6 @@ public class ControllerTile extends BaseTile implements ITickableTileEntity, INe
nodeGraph.addListener(() -> dataManager.sendParameterToWatchers(ControllerTile.NODES)); nodeGraph.addListener(() -> dataManager.sendParameterToWatchers(ControllerTile.NODES));
} }
@Override
public IEnergy getEnergy() {
return this.energy;
}
@Override @Override
public BlockPos getPosition() { public BlockPos getPosition() {
return pos; return pos;
@@ -192,7 +184,7 @@ public class ControllerTile extends BaseTile implements ITickableTileEntity, INe
@Override @Override
public boolean canRun() { public boolean canRun() {
return this.energy.getStored() > 0 && redstoneMode.isEnabled(world, pos); return this.energy.getEnergyStored() > 0 && redstoneMode.isEnabled(world, pos);
} }
@Override @Override
@@ -225,14 +217,14 @@ public class ControllerTile extends BaseTile implements ITickableTileEntity, INe
if (type == ControllerBlock.Type.NORMAL) { if (type == ControllerBlock.Type.NORMAL) {
if (!RS.SERVER_CONFIG.getController().getUseEnergy()) { if (!RS.SERVER_CONFIG.getController().getUseEnergy()) {
this.energy.setStored(this.energy.getCapacity()); this.energy.setStored(this.energy.getMaxEnergyStored());
} else if (this.energy.extract(getEnergyUsage(), Action.SIMULATE) >= 0) { } else if (this.energy.extractEnergy(getEnergyUsage(), true) >= 0) {
this.energy.extract(getEnergyUsage(), Action.PERFORM); this.energy.extractEnergy(getEnergyUsage(), false);
} else { } else {
this.energy.setStored(0); this.energy.setStored(0);
} }
} else if (type == ControllerBlock.Type.CREATIVE) { } else if (type == ControllerBlock.Type.CREATIVE) {
this.energy.setStored(this.energy.getCapacity()); this.energy.setStored(this.energy.getMaxEnergyStored());
} }
boolean canRun = canRun(); boolean canRun = canRun();
@@ -538,7 +530,7 @@ public class ControllerTile extends BaseTile implements ITickableTileEntity, INe
public CompoundNBT write(CompoundNBT tag) { public CompoundNBT write(CompoundNBT tag) {
super.write(tag); super.write(tag);
tag.putInt(NBT_ENERGY, this.energy.getStored()); tag.putInt(NBT_ENERGY, this.energy.getEnergyStored());
redstoneMode.write(tag); redstoneMode.write(tag);
@@ -584,7 +576,7 @@ public class ControllerTile extends BaseTile implements ITickableTileEntity, INe
return ControllerBlock.EnergyType.OFF; return ControllerBlock.EnergyType.OFF;
} }
return getEnergyType(this.energy.getStored(), this.energy.getCapacity()); return getEnergyType(this.energy.getEnergyStored(), this.energy.getMaxEnergyStored());
} }
public static ControllerBlock.EnergyType getEnergyType(int stored, int capacity) { public static ControllerBlock.EnergyType getEnergyType(int stored, int capacity) {