From 9aea47e5c8d76060af9da7171f0801d72651ef20 Mon Sep 17 00:00:00 2001 From: miozune Date: Sat, 14 Oct 2023 13:30:30 +0900 Subject: [PATCH] Optimize FuzzyMode creation --- .../java/appeng/api/config/FuzzyMode.java | 19 +++++++++++++++++++ .../items/storage/ItemBasicStorageCell.java | 7 +------ .../appeng/items/storage/ItemViewCell.java | 7 +------ .../items/storage/ItemVoidStorageCell.java | 7 +------ .../tools/powered/ToolColorApplicator.java | 7 +------ .../items/tools/powered/ToolMassCannon.java | 7 +------ .../items/tools/powered/ToolPortableCell.java | 7 +------ 7 files changed, 25 insertions(+), 36 deletions(-) diff --git a/src/main/java/appeng/api/config/FuzzyMode.java b/src/main/java/appeng/api/config/FuzzyMode.java index a96d2c9fdf7..9423f9bed03 100644 --- a/src/main/java/appeng/api/config/FuzzyMode.java +++ b/src/main/java/appeng/api/config/FuzzyMode.java @@ -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. @@ -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; + } + } } diff --git a/src/main/java/appeng/items/storage/ItemBasicStorageCell.java b/src/main/java/appeng/items/storage/ItemBasicStorageCell.java index 9f297d3d106..bea7bad5c51 100644 --- a/src/main/java/appeng/items/storage/ItemBasicStorageCell.java +++ b/src/main/java/appeng/items/storage/ItemBasicStorageCell.java @@ -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 diff --git a/src/main/java/appeng/items/storage/ItemViewCell.java b/src/main/java/appeng/items/storage/ItemViewCell.java index f5d7f9f1ba3..3fd50df7b37 100644 --- a/src/main/java/appeng/items/storage/ItemViewCell.java +++ b/src/main/java/appeng/items/storage/ItemViewCell.java @@ -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 diff --git a/src/main/java/appeng/items/storage/ItemVoidStorageCell.java b/src/main/java/appeng/items/storage/ItemVoidStorageCell.java index 7d048b52f2d..0d55a85d86b 100644 --- a/src/main/java/appeng/items/storage/ItemVoidStorageCell.java +++ b/src/main/java/appeng/items/storage/ItemVoidStorageCell.java @@ -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 diff --git a/src/main/java/appeng/items/tools/powered/ToolColorApplicator.java b/src/main/java/appeng/items/tools/powered/ToolColorApplicator.java index 4174fe30914..f75c589ac89 100644 --- a/src/main/java/appeng/items/tools/powered/ToolColorApplicator.java +++ b/src/main/java/appeng/items/tools/powered/ToolColorApplicator.java @@ -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 diff --git a/src/main/java/appeng/items/tools/powered/ToolMassCannon.java b/src/main/java/appeng/items/tools/powered/ToolMassCannon.java index 50e31255be4..3e11e70248b 100644 --- a/src/main/java/appeng/items/tools/powered/ToolMassCannon.java +++ b/src/main/java/appeng/items/tools/powered/ToolMassCannon.java @@ -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 diff --git a/src/main/java/appeng/items/tools/powered/ToolPortableCell.java b/src/main/java/appeng/items/tools/powered/ToolPortableCell.java index 34319e6ffa2..fbc6b4f8b4a 100644 --- a/src/main/java/appeng/items/tools/powered/ToolPortableCell.java +++ b/src/main/java/appeng/items/tools/powered/ToolPortableCell.java @@ -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