Skip to content

Commit

Permalink
finish update function
Browse files Browse the repository at this point in the history
  • Loading branch information
MCTBL committed Feb 11, 2025
1 parent 183877f commit 523323c
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 55 deletions.
14 changes: 5 additions & 9 deletions src/main/java/appeng/container/AEBaseContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
import appeng.helpers.InventoryAction;
import appeng.items.materials.ItemMultiMaterial;
import appeng.parts.automation.StackUpgradeInventory;
import appeng.parts.automation.UpgradeInventory;
import appeng.util.InventoryAdaptor;
import appeng.util.Platform;
import appeng.util.inv.AdaptorPlayerHand;
Expand Down Expand Up @@ -526,14 +525,11 @@ public ItemStack transferStackInSlot(final EntityPlayer p, final int idx) {
}

// For shift click upgrade card logic
if (ItemMultiMaterial.instance.getType(tis) != null) {
// Check now container is upgradeable or it's subclass
if (ContainerUpgradeable.class.isAssignableFrom(this.getClass())) {
// Check source or target
if (!((d.inventory instanceof UpgradeInventory)
|| (clickSlot.inventory instanceof StackUpgradeInventory))) {
continue;
}
if (ItemMultiMaterial.instance.getType(tis) != null && this instanceof ContainerUpgradeable) {
// Check source or target
if (!((d.inventory instanceof StackUpgradeInventory)
|| (clickSlot.inventory instanceof StackUpgradeInventory))) {
continue;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@

import appeng.api.networking.crafting.ICraftingCPU;
import appeng.api.storage.data.IAEItemStack;
import appeng.util.IWideReadableNumberConverter;
import appeng.util.ItemSorters;
import appeng.util.Platform;
import appeng.util.ReadableNumberConverter;
import appeng.util.item.AEItemStack;
import io.netty.buffer.ByteBuf;

Expand All @@ -24,6 +26,8 @@
*/
public class CraftingCPUStatus implements Comparable<CraftingCPUStatus> {

private static final IWideReadableNumberConverter NUMBER_CONVERTER = ReadableNumberConverter.INSTANCE;

@Nullable
private final ICraftingCPU serverCluster;

Expand Down Expand Up @@ -183,7 +187,8 @@ public String formatCoprocessors() {
}

public String formatShorterCoprocessors() {
return Platform.formatNumberLong(getCoprocessors());
return NUMBER_CONVERTER.toWideReadableForm(getCoprocessors());
// return Platform.formatNumberLong(getCoprocessors());
}

public String formatStorage() {
Expand Down
96 changes: 76 additions & 20 deletions src/main/java/appeng/parts/reporting/PartThroughputMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Vec3;

import org.lwjgl.opengl.GL11;

import appeng.api.networking.IGridNode;
import appeng.api.networking.ticking.IGridTickable;
import appeng.api.networking.ticking.TickRateModulation;
import appeng.api.networking.ticking.TickingRequest;
import appeng.api.storage.data.IAEItemStack;
import appeng.client.texture.CableBusTextures;
import appeng.helpers.Reflected;
Expand All @@ -24,7 +29,7 @@
* @version rv3-beta-538-GTNH
* @since rv3-beta-538-GTNH
*/
public class PartThroughputMonitor extends AbstractPartMonitor {
public class PartThroughputMonitor extends AbstractPartMonitor implements IGridTickable {

private static final IWideReadableNumberConverter NUMBER_CONVERTER = ReadableNumberConverter.INSTANCE;

Expand All @@ -33,16 +38,19 @@ public class PartThroughputMonitor extends AbstractPartMonitor {
private static final CableBusTextures FRONT_COLORED_ICON = CableBusTextures.PartThroughputMonitor_Colored;
private static final CableBusTextures FRONT_COLORED_ICON_LOCKED = CableBusTextures.PartThroughputMonitor_Dark_Locked;

private long lastitemNums;
private long itemNumsChange;
private static final String[] TIME_UNIT = { "/t", "/s", "/m", "/h" };
private static final float[] NUMBER_MULTIPLIER = { 1, 20, 1_200, 72_000 };

private double itemNumsChange;
private int timeMode;
private long lastStackSize;

@Reflected
public PartThroughputMonitor(final ItemStack is) {
super(is);
this.lastitemNums = 0;
this.itemNumsChange = 0;
this.timeMode = 0;
this.lastStackSize = -1;
}

@Override
Expand Down Expand Up @@ -76,12 +84,14 @@ public void writeToNBT(final NBTTagCompound data) {
public void writeToStream(final ByteBuf data) throws IOException {
super.writeToStream(data);
data.writeInt(this.timeMode);
data.writeDouble(this.itemNumsChange);
}

@Override
public boolean readFromStream(final ByteBuf data) throws IOException {
boolean needRedraw = super.readFromStream(data);
this.timeMode = data.readInt();
this.itemNumsChange = data.readDouble();
return needRedraw;
}

Expand All @@ -99,25 +109,60 @@ public boolean onPartShiftActivate(final EntityPlayer player, final Vec3 pos) {
return false;
}

this.timeMode = this.timeMode == 0 ? 1 : 0;
final TileEntity te = this.getTile();
final ItemStack eq = player.getCurrentEquippedItem();

return true;
if (!Platform.isWrench(player, eq, te.xCoord, te.yCoord, te.zCoord) && this.isLocked()) {
this.timeMode = (this.timeMode + TIME_UNIT.length - 1) % TIME_UNIT.length;
return true;
} else {
return super.onPartShiftActivate(player, pos);
}
}

public void updateThroughput() {
if (this.getDisplayed() != null) {
long nowNums = ((IAEItemStack) this.getDisplayed()).getStackSize();
this.itemNumsChange = (nowNums - lastitemNums);
// If is tick mode
if (this.timeMode == 1) {
this.itemNumsChange *= 20;
}
this.lastitemNums = nowNums;
@Override
public boolean onPartActivate(final EntityPlayer player, final Vec3 pos) {
if (Platform.isClient()) {
return true;
}

if (!this.getProxy().isActive()) {
return false;
}

if (!Platform.hasPermissions(this.getLocation(), player)) {
return false;
}

final TileEntity te = this.getTile();
final ItemStack eq = player.getCurrentEquippedItem();

if (!Platform.isWrench(player, eq, te.xCoord, te.yCoord, te.zCoord) && this.isLocked()) {
this.timeMode = (this.timeMode + 1) % TIME_UNIT.length;
return true;
} else {
this.itemNumsChange = 0;
this.lastitemNums = 0;
return super.onPartActivate(player, pos);
}
}

public void updateThroughput(int tick) {
if (Platform.isClient()) {
return;
}

if (this.getDisplayed() == null) {
this.lastStackSize = -1;
this.host.markForUpdate();
return;
} else {
long nowStackSize = this.getDisplayed().getStackSize();
if (this.lastStackSize != -1) {
long changeStackSize = nowStackSize - this.lastStackSize;
this.itemNumsChange = (changeStackSize * NUMBER_MULTIPLIER[this.timeMode]) / tick;
this.host.markForUpdate();
}
this.lastStackSize = nowStackSize;
}
this.getHost().markForUpdate();
}

@Override
Expand All @@ -129,8 +174,8 @@ public void tesrRenderItemNumber(final IAEItemStack ais) {
final String renderedStackSize = NUMBER_CONVERTER.toWideReadableForm(stackSize);

final String renderedStackSizeChange = (this.itemNumsChange > 0 ? "+" : "")
+ Platform.formatNumberLong(this.itemNumsChange)
+ (this.timeMode == 0 ? "/s" : "/t");
+ (Platform.formatNumberLongRestrictedByWidth(this.itemNumsChange, 3))
+ (TIME_UNIT[this.timeMode]);

final FontRenderer fr = Minecraft.getMinecraft().fontRenderer;
int width = fr.getStringWidth(renderedStackSize);
Expand All @@ -149,4 +194,15 @@ public void tesrRenderItemNumber(final IAEItemStack ais) {
fr.drawString(renderedStackSizeChange, 0, 0, color);
}

@Override
public TickingRequest getTickingRequest(IGridNode node) {
return new TickingRequest(20, 100, false, false);
}

@Override
public TickRateModulation tickingRequest(IGridNode node, int TicksSinceLastCall) {
this.updateThroughput(TicksSinceLastCall);
return TickRateModulation.SAME;
}

}
15 changes: 0 additions & 15 deletions src/main/java/appeng/tile/networking/TileCableBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import appeng.integration.IntegrationType;
import appeng.integration.abstraction.IImmibisMicroblocks;
import appeng.parts.CableBusContainer;
import appeng.parts.reporting.PartThroughputMonitor;
import appeng.tile.AEBaseTile;
import appeng.tile.TileEvent;
import appeng.tile.events.TileEventType;
Expand All @@ -50,8 +49,6 @@ public class TileCableBus extends AEBaseTile implements AEMultiTile, ICustomColl

private CableBusContainer cb = new CableBusContainer(this);

private int tickCounter = 1;

/**
* Immibis MB Support
*/
Expand Down Expand Up @@ -324,16 +321,4 @@ public CableBusContainer getCableBus() {
private void setCableBus(final CableBusContainer cb) {
this.cb = cb;
}

@TileEvent(TileEventType.TICK)
public void update() {
if (++tickCounter >= 20) {
for (ForgeDirection side : ForgeDirection.values()) {
if (this.cb.getPart(side) instanceof PartThroughputMonitor ptm) {
ptm.updateThroughput();
}
}
tickCounter = 0;
}
}
}
56 changes: 46 additions & 10 deletions src/main/java/appeng/util/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ public class Platform {
UUID.fromString("839eb18c-50bc-400c-8291-9383f09763e7"),
"[AE2Player]");
private static final String[] BYTE_UNIT = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", "BB" };
private static final String[] NUM_UNIT = { "K", "M", "B", "T" };
private static final char[] NUM_UNIT = "kMGTPE".toCharArray();
private static final double[] BYTE_LIMIT;
private static final long[] NUM_LIMIT = { 1_000, 1_000_000, 1_000_000_000, 1_000_000_000_000l };
private static final int DIVISION_BASE = 1000;
private static final DecimalFormat df = new DecimalFormat("#.##");

static {
Expand Down Expand Up @@ -1887,15 +1887,51 @@ public static String formatByteDouble(final double n) {
return (n / BYTE_LIMIT[0]) + " " + BYTE_UNIT[0];
}

public static String formatNumberLong(final long n) {
if (n > 1_000) {
for (int i = 1; i < NUM_LIMIT.length; i++) {
if (n < NUM_LIMIT[i]) {
return String.valueOf(n / NUM_LIMIT[i - 1]) + " " + NUM_UNIT[i - 1];
}
}
/**
* From large double to num with unit
*
* @param n number wait to format
* @return String
*/
public static String formatNumberLong(final double n) {
return formatNumberLongRestrictedByWidth(n, 3);
}

/**
* From large double to num with unit
*
* @param n number wait to format
* @return String
*/
public static String formatNumberLongRestrictedByWidth(final double n, final int width) {
final String numberString = df.format(n);
int numberSize = numberString.length();
if (numberSize <= width) {
return numberString;
}

double base = n;
double last = base * 1000;
int exponent = -1;
String postFix = "";

while (base >= 1000) {
last = base;
base /= DIVISION_BASE;

// adds +1 due to the postfix
exponent++;
postFix = String.valueOf(NUM_UNIT[exponent]);
}

return String.valueOf(n);
final String withPrecision = df.format(last / DIVISION_BASE) + postFix;
final String withoutPrecision = Long.toString((long) base) + postFix;

final String slimResult = (withPrecision.length() <= width) ? withPrecision : withoutPrecision;

// post condition
assert slimResult.length() <= width;

return slimResult;
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 523323c

Please sign in to comment.