Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize FuzzyMode creation #403

Merged
merged 1 commit into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/main/java/appeng/api/config/FuzzyMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@

package appeng.api.config;

import javax.annotation.Nonnull;

import net.minecraft.item.ItemStack;

import appeng.util.Platform;

public enum FuzzyMode {

// Note that percentage damaged, is the inverse of percentage durability.
Expand All @@ -33,4 +39,17 @@ public enum FuzzyMode {
public int calculateBreakPoint(final int maxDamage) {
return (int) ((this.percentage * maxDamage) / 100.0f);
}

@Nonnull
public static FuzzyMode fromItemStack(@Nonnull ItemStack is) {
final String fz = Platform.openNbtData(is).getString("FuzzyMode");
if (fz == null || fz.isEmpty()) {
return FuzzyMode.IGNORE_ALL;
}
try {
return FuzzyMode.valueOf(fz);
} catch (IllegalArgumentException ignored) {
return FuzzyMode.IGNORE_ALL;
}
}
}
7 changes: 1 addition & 6 deletions src/main/java/appeng/items/storage/ItemBasicStorageCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,7 @@ public IInventory getConfigInventory(final ItemStack is) {

@Override
public FuzzyMode getFuzzyMode(final ItemStack is) {
final String fz = Platform.openNbtData(is).getString("FuzzyMode");
try {
return FuzzyMode.valueOf(fz);
} catch (final Throwable t) {
return FuzzyMode.IGNORE_ALL;
}
return FuzzyMode.fromItemStack(is);
}

@Override
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/appeng/items/storage/ItemViewCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,7 @@ public IInventory getConfigInventory(final ItemStack is) {

@Override
public FuzzyMode getFuzzyMode(final ItemStack is) {
final String fz = Platform.openNbtData(is).getString("FuzzyMode");
try {
return FuzzyMode.valueOf(fz);
} catch (final Throwable t) {
return FuzzyMode.IGNORE_ALL;
}
return FuzzyMode.fromItemStack(is);
}

@Override
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/appeng/items/storage/ItemVoidStorageCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,7 @@ public IInventory getConfigInventory(ItemStack is) {

@Override
public FuzzyMode getFuzzyMode(ItemStack is) {
final String fz = Platform.openNbtData(is).getString("FuzzyMode");
try {
return FuzzyMode.valueOf(fz);
} catch (final Throwable t) {
return FuzzyMode.IGNORE_ALL;
}
return FuzzyMode.fromItemStack(is);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,12 +473,7 @@ public IInventory getConfigInventory(final ItemStack is) {

@Override
public FuzzyMode getFuzzyMode(final ItemStack is) {
final String fz = Platform.openNbtData(is).getString("FuzzyMode");
try {
return FuzzyMode.valueOf(fz);
} catch (final Throwable t) {
return FuzzyMode.IGNORE_ALL;
}
return FuzzyMode.fromItemStack(is);
}

@Override
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/appeng/items/tools/powered/ToolMassCannon.java
Original file line number Diff line number Diff line change
Expand Up @@ -444,12 +444,7 @@ public IInventory getConfigInventory(final ItemStack is) {

@Override
public FuzzyMode getFuzzyMode(final ItemStack is) {
final String fz = Platform.openNbtData(is).getString("FuzzyMode");
try {
return FuzzyMode.valueOf(fz);
} catch (final Throwable t) {
return FuzzyMode.IGNORE_ALL;
}
return FuzzyMode.fromItemStack(is);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,7 @@ public IInventory getConfigInventory(final ItemStack is) {

@Override
public FuzzyMode getFuzzyMode(final ItemStack is) {
final String fz = Platform.openNbtData(is).getString("FuzzyMode");
try {
return FuzzyMode.valueOf(fz);
} catch (final Throwable t) {
return FuzzyMode.IGNORE_ALL;
}
return FuzzyMode.fromItemStack(is);
}

@Override
Expand Down