From 3ed3c99c1541e9b1affed9c2c5a3df6bdcab3931 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Mon, 6 May 2024 12:56:22 +0200 Subject: [PATCH] Speeds up smith presets by using caching and sets --- .../blacksmith/command/PresetCommand.java | 3 +- .../blacksmith/BlackSmithConfigCommand.java | 4 +- .../blacksmith/config/SmithPreset.java | 118 +++++++++++------- .../knarcraft/blacksmith/util/ItemHelper.java | 14 ++- 4 files changed, 87 insertions(+), 52 deletions(-) diff --git a/src/main/java/net/knarcraft/blacksmith/command/PresetCommand.java b/src/main/java/net/knarcraft/blacksmith/command/PresetCommand.java index 8dfa3d7..fd4b264 100644 --- a/src/main/java/net/knarcraft/blacksmith/command/PresetCommand.java +++ b/src/main/java/net/knarcraft/blacksmith/command/PresetCommand.java @@ -12,6 +12,7 @@ import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.List; +import java.util.Set; /** * The command for displaying which materials are contained in a preset @@ -26,7 +27,7 @@ public class PresetCommand implements CommandExecutor { } String presetName = args[0].toUpperCase().replace('-', '_'); - List includedMaterials; + Set includedMaterials; try { //Display the preset with the filter applied diff --git a/src/main/java/net/knarcraft/blacksmith/command/blacksmith/BlackSmithConfigCommand.java b/src/main/java/net/knarcraft/blacksmith/command/blacksmith/BlackSmithConfigCommand.java index cd897f2..576d95f 100644 --- a/src/main/java/net/knarcraft/blacksmith/command/blacksmith/BlackSmithConfigCommand.java +++ b/src/main/java/net/knarcraft/blacksmith/command/blacksmith/BlackSmithConfigCommand.java @@ -19,7 +19,7 @@ import org.bukkit.enchantments.Enchantment; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.List; +import java.util.Set; /** * The command used for changing global blacksmith configuration options @@ -215,7 +215,7 @@ public class BlackSmithConfigCommand implements CommandExecutor { private void updateAllMatchedPrices(@NotNull GlobalBlacksmithSettings settings, @NotNull BlacksmithSetting blacksmithSetting, @NotNull String materialName, double newPrice) { - List materials = ItemHelper.getWildcardMatch(materialName, false); + Set materials = ItemHelper.getWildcardMatch(materialName, false); for (Material material : materials) { if (blacksmithSetting == BlacksmithSetting.BASE_PRICE) { settings.setBasePrice(material, newPrice); diff --git a/src/main/java/net/knarcraft/blacksmith/config/SmithPreset.java b/src/main/java/net/knarcraft/blacksmith/config/SmithPreset.java index e6fd9df..765896b 100644 --- a/src/main/java/net/knarcraft/blacksmith/config/SmithPreset.java +++ b/src/main/java/net/knarcraft/blacksmith/config/SmithPreset.java @@ -7,7 +7,10 @@ import org.bukkit.Tag; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.logging.Level; @@ -41,6 +44,12 @@ public enum SmithPreset { SmithPresetFilter.AXE, SmithPresetFilter.HOE, SmithPresetFilter.SHOVEL, SmithPresetFilter.MISC}); private final SmithPresetFilter[] filters; + private static final Map> presetMaterialNames = new HashMap<>(); + private static final Map> filterMaterialNames = new HashMap<>(); + private static Set armor = null; + private static Set ranged = null; + private static Set weapons = null; + private static Set tools = null; /** * Instantiates a new smith preset @@ -128,12 +137,27 @@ public enum SmithPreset { return ""; } - //Return the list of materials included in the preset - List materialNames; - if (filter != null) { - materialNames = preset.getMaterialNames(filter); + // Check if the preset result has been stored + Set materialNames = null; + if (filter == null) { + if (presetMaterialNames.containsKey(preset)) { + materialNames = presetMaterialNames.get(preset); + } } else { - materialNames = preset.getMaterialNames(); + if (filterMaterialNames.containsKey(filter)) { + materialNames = filterMaterialNames.get(filter); + } + } + + //Return the list of materials included in the preset + if (materialNames == null) { + if (filter != null) { + materialNames = preset.getMaterialNames(filter); + filterMaterialNames.put(filter, materialNames); + } else { + materialNames = preset.getMaterialNames(); + presetMaterialNames.put(preset, materialNames); + } } if (negated) { materialNames = negateMaterials(materialNames); @@ -148,8 +172,8 @@ public enum SmithPreset { * @return

The materials included in this preset, filtered using the given filter

*/ @NotNull - public List getFilteredMaterials(SmithPresetFilter filter) { - List materials = new ArrayList<>(this.getMaterials()); + public Set getFilteredMaterials(SmithPresetFilter filter) { + Set materials = new HashSet<>(this.getMaterials()); materials.removeIf((item) -> !filter.isIncluded(item)); return materials; } @@ -160,7 +184,7 @@ public enum SmithPreset { * @return

All materials in this preset

*/ @NotNull - public List getMaterials() { + public Set getMaterials() { return switch (this) { case BLACKSMITH -> ItemHelper.getAllReforgeAbleMaterials(); case WEAPON_SMITH -> getWeapons(); @@ -175,11 +199,13 @@ public enum SmithPreset { * @return

All ranged weapon materials

*/ @NotNull - private List getRanged() { - List ranged = new ArrayList<>(); - ranged.add(Material.TRIDENT); - ranged.add(Material.BOW); - ranged.add(Material.CROSSBOW); + private Set getRanged() { + if (ranged == null) { + ranged = new HashSet<>(); + ranged.add(Material.TRIDENT); + ranged.add(Material.BOW); + ranged.add(Material.CROSSBOW); + } return ranged; } @@ -189,15 +215,17 @@ public enum SmithPreset { * @return

All tool materials

*/ @NotNull - private List getTools() { - List tools = new ArrayList<>(); - tools.addAll(Tag.ITEMS_HOES.getValues()); - tools.addAll(Tag.ITEMS_SHOVELS.getValues()); - tools.addAll(Tag.ITEMS_AXES.getValues()); - tools.addAll(Tag.ITEMS_PICKAXES.getValues()); - tools.add(Material.FLINT_AND_STEEL); - tools.add(Material.FISHING_ROD); - tools.add(Material.SHEARS); + private Set getTools() { + if (tools == null) { + tools = new HashSet<>(); + tools.addAll(Tag.ITEMS_HOES.getValues()); + tools.addAll(Tag.ITEMS_SHOVELS.getValues()); + tools.addAll(Tag.ITEMS_AXES.getValues()); + tools.addAll(Tag.ITEMS_PICKAXES.getValues()); + tools.add(Material.FLINT_AND_STEEL); + tools.add(Material.FISHING_ROD); + tools.add(Material.SHEARS); + } return tools; } @@ -207,10 +235,12 @@ public enum SmithPreset { * @return

All weapon materials

*/ @NotNull - private List getWeapons() { - List weapons = new ArrayList<>(getSwords()); - weapons.addAll(getRanged()); - weapons.add(Material.SHIELD); + private Set getWeapons() { + if (weapons == null) { + weapons = new HashSet<>(getSwords()); + weapons.addAll(getRanged()); + weapons.add(Material.SHIELD); + } return weapons; } @@ -230,13 +260,15 @@ public enum SmithPreset { * @return

All armor types

*/ @NotNull - private List getArmor() { - List armor = new ArrayList<>(); - armor.addAll(getMaterialsEndingWith("HELMET")); - armor.addAll(getMaterialsEndingWith("CHESTPLATE")); - armor.addAll(getMaterialsEndingWith("LEGGINGS")); - armor.addAll(getMaterialsEndingWith("BOOTS")); - armor.add(Material.ELYTRA); + private Set getArmor() { + if (armor == null) { + armor = new HashSet<>(); + armor.addAll(getMaterialsEndingWith("HELMET")); + armor.addAll(getMaterialsEndingWith("CHESTPLATE")); + armor.addAll(getMaterialsEndingWith("LEGGINGS")); + armor.addAll(getMaterialsEndingWith("BOOTS")); + armor.add(Material.ELYTRA); + } return armor; } @@ -247,8 +279,8 @@ public enum SmithPreset { * @return

The resulting materials

*/ @NotNull - private List getMaterialsEndingWith(@NotNull String end) { - List matchedMaterials = new ArrayList<>(); + private Set getMaterialsEndingWith(@NotNull String end) { + Set matchedMaterials = new HashSet<>(); for (Material material : ItemHelper.getAllReforgeAbleMaterials()) { if (!material.name().startsWith("LEGACY") && material.name().endsWith(end)) { matchedMaterials.add(material); @@ -258,12 +290,12 @@ public enum SmithPreset { } /** - * Gets material names of all materials reforge-able by this smith + * Gets material names of all materials reforge-able by this smith preset * - * @return

All material names for this smith

+ * @return

All material names for this smith preset

*/ @NotNull - private List getMaterialNames() { + private Set getMaterialNames() { return getNames(this.getMaterials()); } @@ -274,7 +306,7 @@ public enum SmithPreset { * @return

All material names for this smith

*/ @NotNull - private List getMaterialNames(@NotNull SmithPresetFilter filter) { + private Set getMaterialNames(@NotNull SmithPresetFilter filter) { return getNames(this.getFilteredMaterials(filter)); } @@ -285,8 +317,8 @@ public enum SmithPreset { * @return

The names of the materials

*/ @NotNull - private List getNames(@NotNull List materials) { - List items = new ArrayList<>(); + private Set getNames(@NotNull Set materials) { + Set items = new HashSet<>(); for (Material material : materials) { items.add(material.name().toLowerCase().replace("_", "-")); } @@ -300,8 +332,8 @@ public enum SmithPreset { * @return

The negated material names

*/ @NotNull - private static List negateMaterials(@NotNull List materials) { - List negatedMaterials = new ArrayList<>(materials.size()); + private static Set negateMaterials(@NotNull Set materials) { + Set negatedMaterials = new HashSet<>(materials.size()); materials.forEach((material) -> { if (material != null && !material.isBlank()) { negatedMaterials.add("-" + material); diff --git a/src/main/java/net/knarcraft/blacksmith/util/ItemHelper.java b/src/main/java/net/knarcraft/blacksmith/util/ItemHelper.java index 26d295b..b36a2da 100644 --- a/src/main/java/net/knarcraft/blacksmith/util/ItemHelper.java +++ b/src/main/java/net/knarcraft/blacksmith/util/ItemHelper.java @@ -101,10 +101,12 @@ public final class ItemHelper { /** * Gets a complete list of all reforge-able materials * + *

Note: As this loops through all materials, the result should be cached

+ * * @return

A complete list of reforge-able materials

*/ - public static @NotNull List getAllReforgeAbleMaterials() { - List reforgeAbleMaterials = new ArrayList<>(); + public static @NotNull Set getAllReforgeAbleMaterials() { + Set reforgeAbleMaterials = new HashSet<>(); for (Material material : Material.values()) { ItemStack item = new ItemStack(material); if (isRepairable(item)) { @@ -207,12 +209,12 @@ public final class ItemHelper { * @param extended

Whether to use an extended match, allowing any material

* @return

The matched material(s)

*/ - public static @NotNull List getWildcardMatch(@NotNull String materialName, boolean extended) { + public static @NotNull Set getWildcardMatch(@NotNull String materialName, boolean extended) { String search = InputParsingHelper.regExIfy(materialName); - List materials = new ArrayList<>(); - List all; + Set materials = new HashSet<>(); + Set all; if (extended) { - all = List.of(Material.values()); + all = Set.of(Material.values()); } else { all = ItemHelper.getAllReforgeAbleMaterials(); }