Speeds up smith presets by using caching and sets
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good

This commit is contained in:
Kristian Knarvik 2024-05-06 12:56:22 +02:00
parent 3dd8467a58
commit 3ed3c99c15
4 changed files with 87 additions and 52 deletions

View File

@ -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<Material> includedMaterials;
Set<Material> includedMaterials;
try {
//Display the preset with the filter applied

View File

@ -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<Material> materials = ItemHelper.getWildcardMatch(materialName, false);
Set<Material> materials = ItemHelper.getWildcardMatch(materialName, false);
for (Material material : materials) {
if (blacksmithSetting == BlacksmithSetting.BASE_PRICE) {
settings.setBasePrice(material, newPrice);

View File

@ -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<SmithPreset, Set<String>> presetMaterialNames = new HashMap<>();
private static final Map<SmithPresetFilter, Set<String>> filterMaterialNames = new HashMap<>();
private static Set<Material> armor = null;
private static Set<Material> ranged = null;
private static Set<Material> weapons = null;
private static Set<Material> 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<String> materialNames;
if (filter != null) {
materialNames = preset.getMaterialNames(filter);
// Check if the preset result has been stored
Set<String> 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 <p>The materials included in this preset, filtered using the given filter</p>
*/
@NotNull
public List<Material> getFilteredMaterials(SmithPresetFilter filter) {
List<Material> materials = new ArrayList<>(this.getMaterials());
public Set<Material> getFilteredMaterials(SmithPresetFilter filter) {
Set<Material> materials = new HashSet<>(this.getMaterials());
materials.removeIf((item) -> !filter.isIncluded(item));
return materials;
}
@ -160,7 +184,7 @@ public enum SmithPreset {
* @return <p>All materials in this preset</p>
*/
@NotNull
public List<Material> getMaterials() {
public Set<Material> getMaterials() {
return switch (this) {
case BLACKSMITH -> ItemHelper.getAllReforgeAbleMaterials();
case WEAPON_SMITH -> getWeapons();
@ -175,11 +199,13 @@ public enum SmithPreset {
* @return <p>All ranged weapon materials</p>
*/
@NotNull
private List<Material> getRanged() {
List<Material> ranged = new ArrayList<>();
ranged.add(Material.TRIDENT);
ranged.add(Material.BOW);
ranged.add(Material.CROSSBOW);
private Set<Material> 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 <p>All tool materials</p>
*/
@NotNull
private List<Material> getTools() {
List<Material> 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<Material> 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 <p>All weapon materials</p>
*/
@NotNull
private List<Material> getWeapons() {
List<Material> weapons = new ArrayList<>(getSwords());
weapons.addAll(getRanged());
weapons.add(Material.SHIELD);
private Set<Material> 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 <p>All armor types</p>
*/
@NotNull
private List<Material> getArmor() {
List<Material> 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<Material> 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 <p>The resulting materials</p>
*/
@NotNull
private List<Material> getMaterialsEndingWith(@NotNull String end) {
List<Material> matchedMaterials = new ArrayList<>();
private Set<Material> getMaterialsEndingWith(@NotNull String end) {
Set<Material> 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 <p>All material names for this smith</p>
* @return <p>All material names for this smith preset</p>
*/
@NotNull
private List<String> getMaterialNames() {
private Set<String> getMaterialNames() {
return getNames(this.getMaterials());
}
@ -274,7 +306,7 @@ public enum SmithPreset {
* @return <p>All material names for this smith</p>
*/
@NotNull
private List<String> getMaterialNames(@NotNull SmithPresetFilter filter) {
private Set<String> getMaterialNames(@NotNull SmithPresetFilter filter) {
return getNames(this.getFilteredMaterials(filter));
}
@ -285,8 +317,8 @@ public enum SmithPreset {
* @return <p>The names of the materials</p>
*/
@NotNull
private List<String> getNames(@NotNull List<Material> materials) {
List<String> items = new ArrayList<>();
private Set<String> getNames(@NotNull Set<Material> materials) {
Set<String> items = new HashSet<>();
for (Material material : materials) {
items.add(material.name().toLowerCase().replace("_", "-"));
}
@ -300,8 +332,8 @@ public enum SmithPreset {
* @return <p>The negated material names</p>
*/
@NotNull
private static List<String> negateMaterials(@NotNull List<String> materials) {
List<String> negatedMaterials = new ArrayList<>(materials.size());
private static Set<String> negateMaterials(@NotNull Set<String> materials) {
Set<String> negatedMaterials = new HashSet<>(materials.size());
materials.forEach((material) -> {
if (material != null && !material.isBlank()) {
negatedMaterials.add("-" + material);

View File

@ -101,10 +101,12 @@ public final class ItemHelper {
/**
* Gets a complete list of all reforge-able materials
*
* <p>Note: As this loops through all materials, the result should be cached</p>
*
* @return <p>A complete list of reforge-able materials</p>
*/
public static @NotNull List<Material> getAllReforgeAbleMaterials() {
List<Material> reforgeAbleMaterials = new ArrayList<>();
public static @NotNull Set<Material> getAllReforgeAbleMaterials() {
Set<Material> 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 <p>Whether to use an extended match, allowing any material</p>
* @return <p>The matched material(s)</p>
*/
public static @NotNull List<Material> getWildcardMatch(@NotNull String materialName, boolean extended) {
public static @NotNull Set<Material> getWildcardMatch(@NotNull String materialName, boolean extended) {
String search = InputParsingHelper.regExIfy(materialName);
List<Material> materials = new ArrayList<>();
List<Material> all;
Set<Material> materials = new HashSet<>();
Set<Material> all;
if (extended) {
all = List.of(Material.values());
all = Set.of(Material.values());
} else {
all = ItemHelper.getAllReforgeAbleMaterials();
}