Skip to content

Commit

Permalink
made buffer output energy equally between sides
Browse files Browse the repository at this point in the history
  • Loading branch information
Trytoon committed Jan 1, 2024
1 parent 7c4622b commit 081177e
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 27 deletions.
12 changes: 12 additions & 0 deletions src/core/java/com/enderio/core/common/util/NumberUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,16 @@ public static long getLong(String value) {
return 0;
}
}

public static String formatWithPrefix(int number) {
if (number < 100_000) {
return Long.toString(number);
} else if (number < 1_000_000) {
return String.format("%.1fk", number / 1_000.0);
} else if (number < 1_000_000_000) {
return String.format("%.3fM", number / 1_000_000.0);
} else {
return String.format("%.3fB", number / 1_000_000_000.0);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;

import java.util.Objects;

public class OmniBufferScreen extends EIOScreen<OmniBufferMenu> {

private static final ResourceLocation BG_TEXTURE = EnderIO.loc("textures/gui/omni_buffer.png");
Expand All @@ -39,13 +41,13 @@ protected void init() {

input = new EnergyTextboxWidget(this, () -> this.getMenu().getBlockEntity().getEnergyStorage().getMaxEnergyUse(), this.font, leftPos + 33, topPos + 18, 49, this.font.lineHeight + 2, Component.literal("PBInputBox"));
input.setResponder(this.getMenu().getBlockEntity()::setMaxInputText);
input.setValue(getMenu().getBlockEntity().getMaxInputText());
input.setValue(input.formatEnergy(""+getMenu().getBlockEntity().getMaxInput()));
input.OnFocusStoppedResponder(this::updateInput);
addRenderableOnly(addRenderableWidget(input));

output = new EnergyTextboxWidget(this, () -> this.getMenu().getBlockEntity().getEnergyStorage().getMaxEnergyUse(), this.font, leftPos + 33, topPos + 48, 49, this.font.lineHeight + 2, Component.literal("PBOutputBox"));
output = new EnergyTextboxWidget(this, () -> this.getMenu().getBlockEntity().getEnergyStorage().getMaxEnergyUse(), this.font, leftPos + 33, topPos + 52, 49, this.font.lineHeight + 2, Component.literal("PBOutputBox"));
output.setResponder(this.getMenu().getBlockEntity()::setMaxOutputText);
output.setValue(getMenu().getBlockEntity().getMaxOutputText());
output.setValue(output.formatEnergy(""+getMenu().getBlockEntity().getMaxOutput()));
output.OnFocusStoppedResponder(this::updateOutput);
addRenderableOnly(addRenderableWidget(output));

Expand All @@ -64,8 +66,17 @@ protected Vector2i getBackgroundImageSize() {
@Override
public void render(GuiGraphics guiGraphics, int pMouseX, int pMouseY, float pPartialTicks) {
super.render(guiGraphics, pMouseX, pMouseY, pPartialTicks);
guiGraphics.drawString(font, EIOLang.INPUT.getString() + ":", leftPos + 33, topPos + 18 - font.lineHeight - 2, 1, false);
guiGraphics.drawString(font, EIOLang.OUTPUT.getString() + ":", leftPos + 33, topPos + 48 - font.lineHeight - 2, 1, false);
guiGraphics.drawString(font, EIOLang.INPUT.getString(), leftPos + 33, topPos + 18 - font.lineHeight - 2, 1, false);
guiGraphics.drawString(font, EIOLang.OUTPUT.getString(), leftPos + 33, topPos + 52 - font.lineHeight - 2, 1, false);

String maxIO = "Max: " + NumberUtils.formatWithPrefix(Objects.requireNonNull(getMenu().getBlockEntity()).getEnergyStorage().getMaxEnergyUse());
guiGraphics.drawString(font, maxIO, leftPos + 33 + 49 - font.width(maxIO), topPos + 18 + font.lineHeight + 4, 0xff8b8b8b, false);
guiGraphics.drawString(font, maxIO, leftPos + 33 + 49 - font.width(maxIO), topPos + 52 + font.lineHeight + 4, 0xff8b8b8b, false);

if (!this.getMenu().getBlockEntity().isCapacitorInstalled()) {
input.setValue("0");
output.setValue("0");
}
}

private void updateInput(String val){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;

import java.util.Objects;

public class PowerBufferScreen extends EIOScreen<PowerBufferMenu> {

private static final ResourceLocation BG_TEXTURE = EnderIO.loc("textures/gui/power_buffer.png");
Expand All @@ -39,13 +41,13 @@ protected void init() {

input = new EnergyTextboxWidget(this, () -> this.getMenu().getBlockEntity().getEnergyStorage().getMaxEnergyUse(), this.font, leftPos + 40, topPos + 18, 95, this.font.lineHeight + 2, Component.literal("PBInputBox"));
input.setResponder(this.getMenu().getBlockEntity()::setMaxInputText);
input.setValue(getMenu().getBlockEntity().getMaxInputText());
input.setValue(input.formatEnergy(""+getMenu().getBlockEntity().getMaxInput()));
input.OnFocusStoppedResponder(this::updateInput);
addRenderableOnly(addRenderableWidget(input));

output = new EnergyTextboxWidget(this, () -> this.getMenu().getBlockEntity().getEnergyStorage().getMaxEnergyUse(), this.font, leftPos + 40, topPos + 48, 95, this.font.lineHeight + 2, Component.literal("PBOutputBox"));
output = new EnergyTextboxWidget(this, () -> this.getMenu().getBlockEntity().getEnergyStorage().getMaxEnergyUse(), this.font, leftPos + 40, topPos + 52, 95, this.font.lineHeight + 2, Component.literal("PBOutputBox"));
output.setResponder(this.getMenu().getBlockEntity()::setMaxOutputText);
output.setValue(getMenu().getBlockEntity().getMaxOutputText());
output.setValue(output.formatEnergy(""+getMenu().getBlockEntity().getMaxOutput()));
output.OnFocusStoppedResponder(this::updateOutput);
addRenderableOnly(addRenderableWidget(output));
}
Expand All @@ -63,8 +65,18 @@ protected Vector2i getBackgroundImageSize() {
@Override
public void render(GuiGraphics guiGraphics, int pMouseX, int pMouseY, float pPartialTicks) {
super.render(guiGraphics, pMouseX, pMouseY, pPartialTicks);
guiGraphics.drawString(font, EIOLang.INPUT.getString() +":", leftPos + 40, topPos + 18 - font.lineHeight - 2, 1, false);
guiGraphics.drawString(font, EIOLang.OUTPUT.getString() +":", leftPos + 40, topPos + 48 - font.lineHeight - 2, 1, false);
guiGraphics.drawString(font, EIOLang.INPUT.getString(), leftPos + 40, topPos + 18 - font.lineHeight - 2, 1, false);
guiGraphics.drawString(font, EIOLang.OUTPUT.getString(), leftPos + 40, topPos + 52 - font.lineHeight - 2, 1, false);

String maxIO = "Max: " + NumberUtils.formatWithPrefix(Objects.requireNonNull(getMenu().getBlockEntity()).getEnergyStorage().getMaxEnergyUse());

guiGraphics.drawString(font, maxIO, leftPos + 40 + 95 - font.width(maxIO), topPos + 18 + font.lineHeight + 4, 0xff8b8b8b, false);
guiGraphics.drawString(font, maxIO, leftPos + 40 + 95 - font.width(maxIO), topPos + 52 + font.lineHeight + 4, 0xff8b8b8b, false);

if (!this.getMenu().getBlockEntity().isCapacitorInstalled()) {
input.setValue("0");
output.setValue("0");
}
}

private void updateInput(String val) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float

public void renderToolTip(GuiGraphics guiGraphics, int mouseX, int mouseY) {
if (mouseX >= getX() && mouseX <= getX() + width && mouseY >= getY() && mouseY <= getY() + height && !isFocused() && maxEnergy.get() != 0) {
guiGraphics.renderTooltip(displayOn.getMinecraft().font, Component.literal(getValue() +"/" + maxEnergy.get() +" µI"), mouseX, mouseY);
guiGraphics.renderTooltip(displayOn.getMinecraft().font, Component.literal(getValue() +"/" + maxEnergy.get() +" µI/t"), mouseX, mouseY);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import net.minecraftforge.energy.IEnergyStorage;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;

Expand All @@ -33,7 +35,7 @@ public class PowerBufferBlockEntity extends PoweredMachineBlockEntity {
int maxOutput = 0;
protected IntegerNetworkDataSlot outputDataSlot;

// Stores textbox value beofre being converted to energy value
// Stores textbox value before being converted to energy value
protected StringNetworkDataSlot inputTextDataSlot;
String inputTextValue = "0";
protected StringNetworkDataSlot outputTextDataSlot;
Expand Down Expand Up @@ -116,32 +118,67 @@ public int getMaxOutput() {
return maxOutput;
}

//Override method to be able to configure power output !
//By default, it tries to push as much power as it can.
@Override
public void pushEnergy() {
if (!getExposedEnergyStorage().getIOMode().canOutput())
return;
public List<Direction> getDirections(boolean input) {
List<Direction> dirs = new ArrayList<>();

if (!getExposedEnergyStorage().getIOMode().canOutput() && !input)

Check failure on line 124 in src/machines/java/com/enderio/machines/common/blockentity/PowerBufferBlockEntity.java

View workflow job for this annotation

GitHub Actions / runner / checkstyle

[checkstyle] reported by reviewdog 🐶 'if' construct must use '{}'s. Raw Output: /github/workspace/./src/machines/java/com/enderio/machines/common/blockentity/PowerBufferBlockEntity.java:124:9: error: 'if' construct must use '{}'s. (com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck)
return new ArrayList<>();

for (Direction side : Direction.values()) {
if (!shouldPushEnergyTo(side))

if (!shouldPushEnergyTo(side) && !input)

Check failure on line 129 in src/machines/java/com/enderio/machines/common/blockentity/PowerBufferBlockEntity.java

View workflow job for this annotation

GitHub Actions / runner / checkstyle

[checkstyle] reported by reviewdog 🐶 'if' construct must use '{}'s. Raw Output: /github/workspace/./src/machines/java/com/enderio/machines/common/blockentity/PowerBufferBlockEntity.java:129:13: error: 'if' construct must use '{}'s. (com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck)
continue;

getCapability(ForgeCapabilities.ENERGY, side).resolve().ifPresent(selfHandler -> {
Optional<IEnergyStorage> otherHandler = getNeighbouringCapability(ForgeCapabilities.ENERGY, side).resolve();
if (otherHandler.isPresent()) {

// If the other handler can receive power transmit ours and there is enough energy stored
if (otherHandler.get().canReceive() && getExposedEnergyStorage().getEnergyStored() - getMaxOutput() >= 0) {

int received = otherHandler.get().receiveEnergy(getMaxOutput(), false);
// Consume that energy from our buffer.
getExposedEnergyStorage().extractEnergy(received, false);
if (input) {
if (otherHandler.get().canExtract()) {
dirs.add(side);
}
} else {
if (otherHandler.get().canReceive()) {
dirs.add(side);
}
}
}
});
}
return dirs;
}

//Distributes energy equally between sides (maxEnergy / number of sides)
@Override
public void pushEnergy() {
if (!getExposedEnergyStorage().getIOMode().canOutput())

Check failure on line 154 in src/machines/java/com/enderio/machines/common/blockentity/PowerBufferBlockEntity.java

View workflow job for this annotation

GitHub Actions / runner / checkstyle

[checkstyle] reported by reviewdog 🐶 'if' construct must use '{}'s. Raw Output: /github/workspace/./src/machines/java/com/enderio/machines/common/blockentity/PowerBufferBlockEntity.java:154:9: error: 'if' construct must use '{}'s. (com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck)
return;


List<Direction> directions = getDirections(false);
int energyPerSide = directions.size() > 0 ? (int)getMaxOutput()/directions.size() : 0;

for (int i = 0; i < directions.size(); i++) {

//if last iteration, add remaining energy (for odd number of sides)
if (i == directions.size() -1 ) {
energyPerSide = energyPerSide + (getMaxOutput() - (energyPerSide * directions.size()));
}

Direction side = directions.get(i);
Optional<IEnergyStorage> otherHandler = getNeighbouringCapability(ForgeCapabilities.ENERGY, side).resolve();

if (otherHandler.isPresent()) {

// If the other handler can receive power transmit ours
if (otherHandler.get().canReceive() && getExposedEnergyStorage().getEnergyStored() >= 0) {

int received = otherHandler.get().receiveEnergy(Math.min(energyPerSide, getExposedEnergyStorage().getEnergyStored()), false);
// Consume that energy from our buffer.
getExposedEnergyStorage().extractEnergy(received, false);
}
}
}
}

@Override
Expand All @@ -153,6 +190,9 @@ protected boolean shouldPushEnergyTo(Direction direction) {
protected MachineEnergyStorage createEnergyStorage(EnergyIOMode energyIOMode, Supplier<Integer> capacity, Supplier<Integer> usageRate) {
return new MachineEnergyStorage(getIOConfig(), energyIOMode, capacity, usageRate) {

//helps to distribute energy input
int sideReceived = 0;

@Override
public boolean canExtract() {
return super.canExtract() && canAct();
Expand All @@ -165,10 +205,20 @@ public boolean canReceive() {

@Override
public int receiveEnergy(int maxReceive, boolean simulate) {
int energyReceived = Math.min(getMaxEnergyStored() - getEnergyStored(), Math.min(getMaxInput(), maxReceive));
List<Direction> dirs = getDirections(true);
int energyToReceive = getMaxInput() / dirs.size();

if (sideReceived == 0) {
energyToReceive += getMaxInput() % dirs.size();
}

int energyReceived = Math.min(getMaxEnergyStored() - getEnergyStored(), Math.min(energyToReceive, maxReceive));

if (!simulate) {
addEnergy(energyReceived);
sideReceived = (sideReceived + 1) % dirs.size();
}

return energyReceived;
}
};
Expand Down

0 comments on commit 081177e

Please sign in to comment.