From 47699d936da097053e6cf88453ae3eeecc89a601 Mon Sep 17 00:00:00 2001 From: Nikita Tomashevich Date: Mon, 4 Apr 2022 22:32:02 +0300 Subject: [PATCH] Gateway now uses energy per packet sent --- src/main/java/li/cil/oc2/common/Config.java | 4 +++ .../InternetGateWayBlockEntity.java | 26 ++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/main/java/li/cil/oc2/common/Config.java b/src/main/java/li/cil/oc2/common/Config.java index 3e64375a..8ebc8488 100644 --- a/src/main/java/li/cil/oc2/common/Config.java +++ b/src/main/java/li/cil/oc2/common/Config.java @@ -27,6 +27,8 @@ public final class Config { @Path("energy.blocks") public static int monitorEnergyStorage = 2000; @Path("energy.blocks") public static int cardCageEnergyPerTick = 20; @Path("energy.blocks") public static int cardCageEnergyStorage = 2000; + @Path("energy.blocks") public static int gatewayEnergyPerPacket = 20; + @Path("energy.blocks") public static int gatewayEnergyStorage = 2000; @Path("energy.entities") public static int robotEnergyPerTick = 5; @Path("energy.entities") public static int robotEnergyStorage = 750000; @@ -86,5 +88,7 @@ public final class Config { public static boolean monitorsUseEnergy() { return computerEnergyPerTick > 0 && computerEnergyStorage > 0; + public static boolean gatewayUseEnergy() { + return gatewayEnergyPerPacket > 0 && gatewayEnergyStorage > 0; } } diff --git a/src/main/java/li/cil/oc2/common/blockentity/InternetGateWayBlockEntity.java b/src/main/java/li/cil/oc2/common/blockentity/InternetGateWayBlockEntity.java index 3f8fb7c9..c84eb808 100644 --- a/src/main/java/li/cil/oc2/common/blockentity/InternetGateWayBlockEntity.java +++ b/src/main/java/li/cil/oc2/common/blockentity/InternetGateWayBlockEntity.java @@ -9,7 +9,10 @@ import java.util.Objects; import javax.annotation.Nullable; import li.cil.oc2.api.capabilities.NetworkInterface; +import li.cil.oc2.common.Config; +import li.cil.oc2.common.Constants; import li.cil.oc2.common.capabilities.Capabilities; +import li.cil.oc2.common.energy.FixedEnergyStorage; import li.cil.oc2.common.inet.InternetAdapter; import li.cil.oc2.common.inet.InternetConnection; import li.cil.oc2.common.inet.InternetManagerImpl; @@ -31,9 +34,11 @@ public class InternetGateWayBlockEntity extends ModBlockEntity implements Networ private InternetConnection internetConnection; - private static final String STATE_TAG = "InternetAdapter"; + private static final String STATE_TAG = "internet_adapter"; private Tag internetState; + private final FixedEnergyStorage energy = new FixedEnergyStorage(Config.gatewayEnergyStorage); + protected InternetGateWayBlockEntity(final BlockPos pos, final BlockState state) { super(BlockEntities.INTERNET_GATEWAY.get(), pos, state); inboundQueue = new ArrayDeque<>(); @@ -50,6 +55,7 @@ public class InternetGateWayBlockEntity extends ModBlockEntity implements Networ } else { internetState = EndTag.INSTANCE; } + energy.deserializeNBT(tag.getCompound(Constants.ENERGY_TAG_NAME)); } @Override @@ -58,6 +64,7 @@ public class InternetGateWayBlockEntity extends ModBlockEntity implements Networ if (internetConnection != null) { internetConnection.saveAdapterState().ifPresent(adapterState -> tag.put(STATE_TAG, adapterState)); } + tag.put(Constants.ENERGY_TAG_NAME, energy.serializeNBT()); LOGGER.info("State saved"); } @@ -81,6 +88,7 @@ public class InternetGateWayBlockEntity extends ModBlockEntity implements Networ @Override protected void collectCapabilities(final CapabilityCollector collector, @Nullable final Direction direction) { collector.offer(Capabilities.networkInterface(), this); + collector.offer(Capabilities.energyStorage(), energy); } @Override @@ -88,11 +96,21 @@ public class InternetGateWayBlockEntity extends ModBlockEntity implements Networ return outboundQueue.pollFirst(); } + private boolean tryUseEnergy() { + boolean hasEnough = energy.getEnergyStored() >= Config.gatewayEnergyPerPacket; + if (hasEnough) { + energy.extractEnergy(Config.gatewayEnergyPerPacket, false); + } + return hasEnough; + } + @Override public void sendEthernetFrame(byte[] frame) { LOGGER.info("Got inbound packet"); if (inboundQueue.size() < QUEUE_MAX) { - inboundQueue.addLast(frame); + if (tryUseEnergy()) { + inboundQueue.addLast(frame); + } } } @@ -105,7 +123,9 @@ public class InternetGateWayBlockEntity extends ModBlockEntity implements Networ public void writeEthernetFrame(NetworkInterface source, byte[] frame, int timeToLive) { LOGGER.info("Got outbound packet"); if (outboundQueue.size() < QUEUE_MAX) { - outboundQueue.addLast(frame); + if (tryUseEnergy()) { + outboundQueue.addLast(frame); + } } }