diff --git a/src/main/java/appeng/container/AEBaseContainer.java b/src/main/java/appeng/container/AEBaseContainer.java index cad78dfba47..2c62db9e204 100644 --- a/src/main/java/appeng/container/AEBaseContainer.java +++ b/src/main/java/appeng/container/AEBaseContainer.java @@ -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; @@ -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; } } diff --git a/src/main/java/appeng/container/implementations/CraftingCPUStatus.java b/src/main/java/appeng/container/implementations/CraftingCPUStatus.java index 48f5a7404e1..6f140edc184 100644 --- a/src/main/java/appeng/container/implementations/CraftingCPUStatus.java +++ b/src/main/java/appeng/container/implementations/CraftingCPUStatus.java @@ -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; @@ -24,6 +26,8 @@ */ public class CraftingCPUStatus implements Comparable { + private static final IWideReadableNumberConverter NUMBER_CONVERTER = ReadableNumberConverter.INSTANCE; + @Nullable private final ICraftingCPU serverCluster; @@ -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() { diff --git a/src/main/java/appeng/parts/reporting/PartThroughputMonitor.java b/src/main/java/appeng/parts/reporting/PartThroughputMonitor.java index 53dbf99188a..39a53b0229f 100644 --- a/src/main/java/appeng/parts/reporting/PartThroughputMonitor.java +++ b/src/main/java/appeng/parts/reporting/PartThroughputMonitor.java @@ -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; @@ -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; @@ -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 @@ -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; } @@ -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 @@ -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); @@ -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; + } + } diff --git a/src/main/java/appeng/tile/networking/TileCableBus.java b/src/main/java/appeng/tile/networking/TileCableBus.java index 93ee343c3a6..7e9d97d7a76 100644 --- a/src/main/java/appeng/tile/networking/TileCableBus.java +++ b/src/main/java/appeng/tile/networking/TileCableBus.java @@ -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; @@ -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 */ @@ -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; - } - } } diff --git a/src/main/java/appeng/util/Platform.java b/src/main/java/appeng/util/Platform.java index ef3ef45dc8e..1dc3746b780 100644 --- a/src/main/java/appeng/util/Platform.java +++ b/src/main/java/appeng/util/Platform.java @@ -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 { @@ -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; } } diff --git a/src/main/resources/assets/appliedenergistics2/textures/blocks/PartThroughputMonitor_Dark_Locked.png b/src/main/resources/assets/appliedenergistics2/textures/blocks/PartThroughputMonitor_Dark_Locked.png index 6fa4085e0a2..bcbffb787f2 100644 Binary files a/src/main/resources/assets/appliedenergistics2/textures/blocks/PartThroughputMonitor_Dark_Locked.png and b/src/main/resources/assets/appliedenergistics2/textures/blocks/PartThroughputMonitor_Dark_Locked.png differ