From e7386f412a312f8cc343f5148daf95d76b772cb1 Mon Sep 17 00:00:00 2001 From: Matthias Mann Date: Fri, 16 Jan 2015 00:48:39 +0100 Subject: [PATCH] simplify grinding ball synchronization and fix wrong display (issue #1673) when switching between different grinding ball types --- .../enderio/machine/crusher/TileCrusher.java | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/main/java/crazypants/enderio/machine/crusher/TileCrusher.java b/src/main/java/crazypants/enderio/machine/crusher/TileCrusher.java index 359ea105..3254e22c 100644 --- a/src/main/java/crazypants/enderio/machine/crusher/TileCrusher.java +++ b/src/main/java/crazypants/enderio/machine/crusher/TileCrusher.java @@ -16,7 +16,7 @@ public class TileCrusher extends AbstractPoweredTaskEntity { protected IGrindingMultiplier gb; protected int currGbUse = 0; - protected int maxGbUse = 0; // client only + protected int maxGbUse = 0; protected int lastSendGbScaled = 0; private boolean useGrindingBall; @@ -44,40 +44,42 @@ public class TileCrusher extends AbstractPoweredTaskEntity { } public int getBallDurationScaled(int scale) { - if(gb == null) { - return worldObj.isRemote ? calcScaled(currGbUse, maxGbUse, scale) : 0; - } - return calcScaled(currGbUse, gb.getDurationMJ(), scale); - } - - private int calcScaled(float cur, float max, int scale) { - return max == 0 ? 0 : (int) (scale * (1 - ((float) cur / (float) max))); + return (maxGbUse > 0) ? (int) (scale * (1 - ((float) currGbUse / (float) maxGbUse))) : 0; } @Override protected double usePower() { double res = super.usePower(); + boolean sendGB = false; + if(gb != null && useGrindingBall) { currGbUse += res; - int newScaled = getBallDurationScaled(16); - if(newScaled != lastSendGbScaled) { - PacketHandler.sendToAllAround(new PacketGrindingBall(this), this); - lastSendGbScaled = newScaled; - } - - if(currGbUse > gb.getDurationMJ()) { + if(currGbUse >= gb.getDurationMJ()) { currGbUse = 0; + maxGbUse = 0; gb = null; + sendGB = true; + } else { + int newScaled = getBallDurationScaled(16); + if(newScaled != lastSendGbScaled) { + sendGB = true; + } } } if(gb == null) { gb = CrusherRecipeManager.getInstance().getGrindballFromStack(inventory[1]); if(gb != null) { + maxGbUse = gb.getDurationMJ(); decrStackSize(1, 1); markDirty(); + sendGB = false; // the tile update will also sync the grinding ball } } + if(sendGB) { + PacketHandler.sendToAllAround(new PacketGrindingBall(this), this); + lastSendGbScaled = getBallDurationScaled(16); + } return res; } @@ -124,6 +126,7 @@ public class TileCrusher extends AbstractPoweredTaskEntity { super.readCustomNBT(nbtRoot); gb = GrindingMultiplierNBT.readFromNBT(nbtRoot); currGbUse = nbtRoot.getInteger("currGbUse"); + maxGbUse = (gb != null) ? gb.getDurationMJ() : 0; } @Override