Skip to content

Commit 65da208

Browse files
committed
Fix oversight from PR #2712 where the non tool (wrenches, hammers, etc) but usable items (spray cans, lighters, etc) weren't accounted for
1 parent 3201860 commit 65da208

File tree

8 files changed

+45
-19
lines changed

8 files changed

+45
-19
lines changed

src/main/java/gregtech/api/items/metaitem/stats/IItemDurabilityManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
public interface IItemDurabilityManager extends IItemComponent {
1111

12-
/** The durability remaining on this item (double from 0 to 1). */
12+
/** The durability remaining on this item (double from 0 to 1 as the durability is used up). */
1313
double getDurabilityForDisplay(ItemStack itemStack);
1414

1515
/** The first and last colors of a gradient. Default to Green durability gradient (null Pair). */

src/main/java/gregtech/api/items/toolitem/IGTTool.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ default AoESymmetrical getAoEDefinition(ItemStack stack) {
649649
default double definition$getDurabilityForDisplay(ItemStack stack) {
650650
int damage = stack.getItem().getDamage(stack);
651651
int maxDamage = stack.getItem().getMaxDamage(stack);
652-
return (double) damage / (double) maxDamage;
652+
return GTUtility.calculateDurabilityFromDamageTaken(damage, maxDamage);
653653
}
654654

655655
@Nullable

src/main/java/gregtech/api/util/GTUtility.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,4 +1023,26 @@ public static int combineRGB(@Range(from = 0, to = 255) int r, @Range(from = 0,
10231023
}
10241024
return map.get(key.toWildcard());
10251025
}
1026+
1027+
/**
1028+
* Calculate the percentage of durability
1029+
*
1030+
* @param damageTaken how many points of durability damage the item has
1031+
* @param maxDurability the maximum durability the item can have
1032+
* @return 0 = full durability, 1 = zero durability
1033+
*/
1034+
public static double calculateDurabilityFromDamageTaken(int damageTaken, int maxDurability) {
1035+
return (double) damageTaken / maxDurability;
1036+
}
1037+
1038+
/**
1039+
* Calculate the percentage of durability
1040+
*
1041+
* @param remainingDurability how much durability out of the maximum the item has left
1042+
* @param maxDurability the maximum durability the item can have
1043+
* @return 0 = full durability, 1 = zero durability
1044+
*/
1045+
public static double calculateDurabilityFromRemaining(int remainingDurability, int maxDurability) {
1046+
return (double) (maxDurability - remainingDurability) / maxDurability;
1047+
}
10261048
}

src/main/java/gregtech/common/items/armor/PowerlessJetpack.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,11 @@ public Behaviour(int internalCapacity) {
283283
public double getDurabilityForDisplay(@NotNull ItemStack itemStack) {
284284
IFluidHandlerItem fluidHandlerItem = itemStack
285285
.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null);
286-
if (fluidHandlerItem == null) return 0;
286+
if (fluidHandlerItem == null) return 1.0d;
287287
IFluidTankProperties fluidTankProperties = fluidHandlerItem.getTankProperties()[0];
288288
FluidStack fluidStack = fluidTankProperties.getContents();
289-
return fluidStack == null ? 0 : (double) fluidStack.amount / (double) fluidTankProperties.getCapacity();
289+
return fluidStack == null ? 1.0d :
290+
GTUtility.calculateDurabilityFromRemaining(fluidStack.amount, fluidTankProperties.getCapacity());
290291
}
291292

292293
@Nullable

src/main/java/gregtech/common/items/behaviors/AbstractMaterialPartBehavior.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import gregtech.api.unification.material.Material;
99
import gregtech.api.unification.material.Materials;
1010
import gregtech.api.unification.material.properties.PropertyKey;
11+
import gregtech.api.util.GTUtility;
1112
import gregtech.api.util.LocalizationUtils;
1213

1314
import net.minecraft.client.resources.I18n;
@@ -89,7 +90,6 @@ public int getItemStackColor(ItemStack itemStack, int tintIndex) {
8990

9091
@Override
9192
public double getDurabilityForDisplay(ItemStack itemStack) {
92-
int maxDurability = getPartMaxDurability(itemStack);
93-
return (double) (maxDurability - getPartDamage(itemStack)) / (double) maxDurability;
93+
return GTUtility.calculateDurabilityFromDamageTaken(getPartDamage(itemStack), getPartMaxDurability(itemStack));
9494
}
9595
}

src/main/java/gregtech/common/items/behaviors/FoamSprayerBehavior.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import gregtech.api.items.metaitem.stats.ISubItemHandler;
99
import gregtech.api.recipes.ModHandler;
1010
import gregtech.api.unification.material.Materials;
11+
import gregtech.api.util.GTUtility;
1112
import gregtech.api.util.GradientUtil;
1213
import gregtech.common.blocks.BlockFrame;
1314
import gregtech.common.blocks.MetaBlocks;
@@ -90,7 +91,8 @@ public double getDurabilityForDisplay(ItemStack itemStack) {
9091
if (fluidHandlerItem == null) return 0;
9192
IFluidTankProperties fluidTankProperties = fluidHandlerItem.getTankProperties()[0];
9293
FluidStack fluidStack = fluidTankProperties.getContents();
93-
return fluidStack == null ? 0 : (double) fluidStack.amount / (double) fluidTankProperties.getCapacity();
94+
return fluidStack == null ? 0 :
95+
GTUtility.calculateDurabilityFromRemaining(fluidStack.amount, fluidTankProperties.getCapacity());
9496
}
9597

9698
@Nullable

src/main/java/gregtech/common/items/behaviors/LighterBehaviour.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,14 @@ public double getDurabilityForDisplay(ItemStack itemStack) {
227227
IFluidHandlerItem fluidHandlerItem = itemStack
228228
.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null);
229229
if (fluidHandlerItem == null) return 0.0;
230-
IFluidTankProperties properties = fluidHandlerItem.getTankProperties()[0];
231-
FluidStack fluidStack = properties.getContents();
232-
return fluidStack == null ? 0.0 : (double) fluidStack.amount / (double) properties.getCapacity();
230+
IFluidTankProperties fluidTankProperties = fluidHandlerItem.getTankProperties()[0];
231+
FluidStack fluidStack = fluidTankProperties.getContents();
232+
return fluidStack == null ? 0.0 :
233+
GTUtility.calculateDurabilityFromRemaining(fluidStack.amount, fluidTankProperties.getCapacity());
233234
}
234235
if (hasMultipleUses) {
235236
// Matchbox
236-
return (double) getUsesLeft(itemStack) / (double) maxUses;
237+
return GTUtility.calculateDurabilityFromRemaining(getUsesLeft(itemStack), maxUses);
237238
}
238239
// no durability for Match
239240
return 0.0;

src/main/java/gregtech/common/items/behaviors/spray/DurabilitySprayBehavior.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ public class DurabilitySprayBehavior extends AbstractSprayBehavior implements II
3434
private final Pair<Color, Color> durabilityBarColors;
3535
@NotNull
3636
private final ItemStack replacementStack;
37-
public final int totalUses;
37+
public final int maxUses;
3838

39-
public DurabilitySprayBehavior(@NotNull ItemStack replacementStack, int totalUses, @Nullable EnumDyeColor color) {
39+
public DurabilitySprayBehavior(@NotNull ItemStack replacementStack, int maxUses, @Nullable EnumDyeColor color) {
4040
this.color = color;
4141
this.replacementStack = replacementStack;
42-
this.totalUses = totalUses;
42+
this.maxUses = maxUses;
4343
this.durabilityBarColors = GradientUtil.getGradient(color == null ? 0x969696 : color.colorValue, 10);
4444
}
4545

@@ -75,7 +75,7 @@ public EnumActionResult useFromToolbelt(@NotNull EntityPlayer player, @NotNull W
7575

7676
public void useItemDurability(@NotNull EntityPlayer player, @NotNull EnumHand hand, @NotNull ItemStack stack,
7777
@NotNull ItemStack replacementStack) {
78-
int usesLeft = getUsesLeft(stack, totalUses);
78+
int usesLeft = getUsesLeft(stack);
7979
if (!player.capabilities.isCreativeMode) {
8080
if (--usesLeft <= 0) {
8181
if (replacementStack.isEmpty()) {
@@ -92,10 +92,10 @@ public void useItemDurability(@NotNull EntityPlayer player, @NotNull EnumHand ha
9292
}
9393
}
9494

95-
protected static int getUsesLeft(@NotNull ItemStack stack, int defaultUsesLeft) {
95+
protected int getUsesLeft(@NotNull ItemStack stack) {
9696
NBTTagCompound tagCompound = stack.getTagCompound();
9797
if (tagCompound == null || !tagCompound.hasKey(NBT_KEY, Constants.NBT.TAG_INT)) {
98-
return defaultUsesLeft;
98+
return maxUses;
9999
}
100100

101101
return tagCompound.getInteger(NBT_KEY);
@@ -107,7 +107,7 @@ protected static void setUsesLeft(@NotNull ItemStack itemStack, int usesLeft) {
107107

108108
@Override
109109
public void addInformation(ItemStack itemStack, List<String> lines) {
110-
int remainingUses = getUsesLeft(itemStack, totalUses);
110+
int remainingUses = getUsesLeft(itemStack);
111111
EnumDyeColor color = getColor();
112112

113113
if (color != null) {
@@ -125,7 +125,7 @@ public void addInformation(ItemStack itemStack, List<String> lines) {
125125

126126
@Override
127127
public double getDurabilityForDisplay(ItemStack itemStack) {
128-
return (double) getUsesLeft(itemStack, totalUses) / totalUses;
128+
return GTUtility.calculateDurabilityFromRemaining(getUsesLeft(itemStack), maxUses);
129129
}
130130

131131
@Nullable

0 commit comments

Comments
 (0)