Speeds up smith presets by using caching and sets
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good
This commit is contained in:
parent
3dd8467a58
commit
3ed3c99c15
@ -12,6 +12,7 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The command for displaying which materials are contained in a preset
|
* 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('-', '_');
|
String presetName = args[0].toUpperCase().replace('-', '_');
|
||||||
List<Material> includedMaterials;
|
Set<Material> includedMaterials;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
//Display the preset with the filter applied
|
//Display the preset with the filter applied
|
||||||
|
@ -19,7 +19,7 @@ import org.bukkit.enchantments.Enchantment;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The command used for changing global blacksmith configuration options
|
* The command used for changing global blacksmith configuration options
|
||||||
@ -215,7 +215,7 @@ public class BlackSmithConfigCommand implements CommandExecutor {
|
|||||||
private void updateAllMatchedPrices(@NotNull GlobalBlacksmithSettings settings,
|
private void updateAllMatchedPrices(@NotNull GlobalBlacksmithSettings settings,
|
||||||
@NotNull BlacksmithSetting blacksmithSetting,
|
@NotNull BlacksmithSetting blacksmithSetting,
|
||||||
@NotNull String materialName, double newPrice) {
|
@NotNull String materialName, double newPrice) {
|
||||||
List<Material> materials = ItemHelper.getWildcardMatch(materialName, false);
|
Set<Material> materials = ItemHelper.getWildcardMatch(materialName, false);
|
||||||
for (Material material : materials) {
|
for (Material material : materials) {
|
||||||
if (blacksmithSetting == BlacksmithSetting.BASE_PRICE) {
|
if (blacksmithSetting == BlacksmithSetting.BASE_PRICE) {
|
||||||
settings.setBasePrice(material, newPrice);
|
settings.setBasePrice(material, newPrice);
|
||||||
|
@ -7,7 +7,10 @@ import org.bukkit.Tag;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
@ -41,6 +44,12 @@ public enum SmithPreset {
|
|||||||
SmithPresetFilter.AXE, SmithPresetFilter.HOE, SmithPresetFilter.SHOVEL, SmithPresetFilter.MISC});
|
SmithPresetFilter.AXE, SmithPresetFilter.HOE, SmithPresetFilter.SHOVEL, SmithPresetFilter.MISC});
|
||||||
|
|
||||||
private final SmithPresetFilter[] filters;
|
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
|
* Instantiates a new smith preset
|
||||||
@ -128,12 +137,27 @@ public enum SmithPreset {
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
//Return the list of materials included in the preset
|
// Check if the preset result has been stored
|
||||||
List<String> materialNames;
|
Set<String> materialNames = null;
|
||||||
if (filter != null) {
|
if (filter == null) {
|
||||||
materialNames = preset.getMaterialNames(filter);
|
if (presetMaterialNames.containsKey(preset)) {
|
||||||
|
materialNames = presetMaterialNames.get(preset);
|
||||||
|
}
|
||||||
} else {
|
} 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) {
|
if (negated) {
|
||||||
materialNames = negateMaterials(materialNames);
|
materialNames = negateMaterials(materialNames);
|
||||||
@ -148,8 +172,8 @@ public enum SmithPreset {
|
|||||||
* @return <p>The materials included in this preset, filtered using the given filter</p>
|
* @return <p>The materials included in this preset, filtered using the given filter</p>
|
||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
public List<Material> getFilteredMaterials(SmithPresetFilter filter) {
|
public Set<Material> getFilteredMaterials(SmithPresetFilter filter) {
|
||||||
List<Material> materials = new ArrayList<>(this.getMaterials());
|
Set<Material> materials = new HashSet<>(this.getMaterials());
|
||||||
materials.removeIf((item) -> !filter.isIncluded(item));
|
materials.removeIf((item) -> !filter.isIncluded(item));
|
||||||
return materials;
|
return materials;
|
||||||
}
|
}
|
||||||
@ -160,7 +184,7 @@ public enum SmithPreset {
|
|||||||
* @return <p>All materials in this preset</p>
|
* @return <p>All materials in this preset</p>
|
||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
public List<Material> getMaterials() {
|
public Set<Material> getMaterials() {
|
||||||
return switch (this) {
|
return switch (this) {
|
||||||
case BLACKSMITH -> ItemHelper.getAllReforgeAbleMaterials();
|
case BLACKSMITH -> ItemHelper.getAllReforgeAbleMaterials();
|
||||||
case WEAPON_SMITH -> getWeapons();
|
case WEAPON_SMITH -> getWeapons();
|
||||||
@ -175,11 +199,13 @@ public enum SmithPreset {
|
|||||||
* @return <p>All ranged weapon materials</p>
|
* @return <p>All ranged weapon materials</p>
|
||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
private List<Material> getRanged() {
|
private Set<Material> getRanged() {
|
||||||
List<Material> ranged = new ArrayList<>();
|
if (ranged == null) {
|
||||||
ranged.add(Material.TRIDENT);
|
ranged = new HashSet<>();
|
||||||
ranged.add(Material.BOW);
|
ranged.add(Material.TRIDENT);
|
||||||
ranged.add(Material.CROSSBOW);
|
ranged.add(Material.BOW);
|
||||||
|
ranged.add(Material.CROSSBOW);
|
||||||
|
}
|
||||||
return ranged;
|
return ranged;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -189,15 +215,17 @@ public enum SmithPreset {
|
|||||||
* @return <p>All tool materials</p>
|
* @return <p>All tool materials</p>
|
||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
private List<Material> getTools() {
|
private Set<Material> getTools() {
|
||||||
List<Material> tools = new ArrayList<>();
|
if (tools == null) {
|
||||||
tools.addAll(Tag.ITEMS_HOES.getValues());
|
tools = new HashSet<>();
|
||||||
tools.addAll(Tag.ITEMS_SHOVELS.getValues());
|
tools.addAll(Tag.ITEMS_HOES.getValues());
|
||||||
tools.addAll(Tag.ITEMS_AXES.getValues());
|
tools.addAll(Tag.ITEMS_SHOVELS.getValues());
|
||||||
tools.addAll(Tag.ITEMS_PICKAXES.getValues());
|
tools.addAll(Tag.ITEMS_AXES.getValues());
|
||||||
tools.add(Material.FLINT_AND_STEEL);
|
tools.addAll(Tag.ITEMS_PICKAXES.getValues());
|
||||||
tools.add(Material.FISHING_ROD);
|
tools.add(Material.FLINT_AND_STEEL);
|
||||||
tools.add(Material.SHEARS);
|
tools.add(Material.FISHING_ROD);
|
||||||
|
tools.add(Material.SHEARS);
|
||||||
|
}
|
||||||
return tools;
|
return tools;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -207,10 +235,12 @@ public enum SmithPreset {
|
|||||||
* @return <p>All weapon materials</p>
|
* @return <p>All weapon materials</p>
|
||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
private List<Material> getWeapons() {
|
private Set<Material> getWeapons() {
|
||||||
List<Material> weapons = new ArrayList<>(getSwords());
|
if (weapons == null) {
|
||||||
weapons.addAll(getRanged());
|
weapons = new HashSet<>(getSwords());
|
||||||
weapons.add(Material.SHIELD);
|
weapons.addAll(getRanged());
|
||||||
|
weapons.add(Material.SHIELD);
|
||||||
|
}
|
||||||
return weapons;
|
return weapons;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -230,13 +260,15 @@ public enum SmithPreset {
|
|||||||
* @return <p>All armor types</p>
|
* @return <p>All armor types</p>
|
||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
private List<Material> getArmor() {
|
private Set<Material> getArmor() {
|
||||||
List<Material> armor = new ArrayList<>();
|
if (armor == null) {
|
||||||
armor.addAll(getMaterialsEndingWith("HELMET"));
|
armor = new HashSet<>();
|
||||||
armor.addAll(getMaterialsEndingWith("CHESTPLATE"));
|
armor.addAll(getMaterialsEndingWith("HELMET"));
|
||||||
armor.addAll(getMaterialsEndingWith("LEGGINGS"));
|
armor.addAll(getMaterialsEndingWith("CHESTPLATE"));
|
||||||
armor.addAll(getMaterialsEndingWith("BOOTS"));
|
armor.addAll(getMaterialsEndingWith("LEGGINGS"));
|
||||||
armor.add(Material.ELYTRA);
|
armor.addAll(getMaterialsEndingWith("BOOTS"));
|
||||||
|
armor.add(Material.ELYTRA);
|
||||||
|
}
|
||||||
return armor;
|
return armor;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -247,8 +279,8 @@ public enum SmithPreset {
|
|||||||
* @return <p>The resulting materials</p>
|
* @return <p>The resulting materials</p>
|
||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
private List<Material> getMaterialsEndingWith(@NotNull String end) {
|
private Set<Material> getMaterialsEndingWith(@NotNull String end) {
|
||||||
List<Material> matchedMaterials = new ArrayList<>();
|
Set<Material> matchedMaterials = new HashSet<>();
|
||||||
for (Material material : ItemHelper.getAllReforgeAbleMaterials()) {
|
for (Material material : ItemHelper.getAllReforgeAbleMaterials()) {
|
||||||
if (!material.name().startsWith("LEGACY") && material.name().endsWith(end)) {
|
if (!material.name().startsWith("LEGACY") && material.name().endsWith(end)) {
|
||||||
matchedMaterials.add(material);
|
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
|
@NotNull
|
||||||
private List<String> getMaterialNames() {
|
private Set<String> getMaterialNames() {
|
||||||
return getNames(this.getMaterials());
|
return getNames(this.getMaterials());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -274,7 +306,7 @@ public enum SmithPreset {
|
|||||||
* @return <p>All material names for this smith</p>
|
* @return <p>All material names for this smith</p>
|
||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
private List<String> getMaterialNames(@NotNull SmithPresetFilter filter) {
|
private Set<String> getMaterialNames(@NotNull SmithPresetFilter filter) {
|
||||||
return getNames(this.getFilteredMaterials(filter));
|
return getNames(this.getFilteredMaterials(filter));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -285,8 +317,8 @@ public enum SmithPreset {
|
|||||||
* @return <p>The names of the materials</p>
|
* @return <p>The names of the materials</p>
|
||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
private List<String> getNames(@NotNull List<Material> materials) {
|
private Set<String> getNames(@NotNull Set<Material> materials) {
|
||||||
List<String> items = new ArrayList<>();
|
Set<String> items = new HashSet<>();
|
||||||
for (Material material : materials) {
|
for (Material material : materials) {
|
||||||
items.add(material.name().toLowerCase().replace("_", "-"));
|
items.add(material.name().toLowerCase().replace("_", "-"));
|
||||||
}
|
}
|
||||||
@ -300,8 +332,8 @@ public enum SmithPreset {
|
|||||||
* @return <p>The negated material names</p>
|
* @return <p>The negated material names</p>
|
||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
private static List<String> negateMaterials(@NotNull List<String> materials) {
|
private static Set<String> negateMaterials(@NotNull Set<String> materials) {
|
||||||
List<String> negatedMaterials = new ArrayList<>(materials.size());
|
Set<String> negatedMaterials = new HashSet<>(materials.size());
|
||||||
materials.forEach((material) -> {
|
materials.forEach((material) -> {
|
||||||
if (material != null && !material.isBlank()) {
|
if (material != null && !material.isBlank()) {
|
||||||
negatedMaterials.add("-" + material);
|
negatedMaterials.add("-" + material);
|
||||||
|
@ -101,10 +101,12 @@ public final class ItemHelper {
|
|||||||
/**
|
/**
|
||||||
* Gets a complete list of all reforge-able materials
|
* 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>
|
* @return <p>A complete list of reforge-able materials</p>
|
||||||
*/
|
*/
|
||||||
public static @NotNull List<Material> getAllReforgeAbleMaterials() {
|
public static @NotNull Set<Material> getAllReforgeAbleMaterials() {
|
||||||
List<Material> reforgeAbleMaterials = new ArrayList<>();
|
Set<Material> reforgeAbleMaterials = new HashSet<>();
|
||||||
for (Material material : Material.values()) {
|
for (Material material : Material.values()) {
|
||||||
ItemStack item = new ItemStack(material);
|
ItemStack item = new ItemStack(material);
|
||||||
if (isRepairable(item)) {
|
if (isRepairable(item)) {
|
||||||
@ -207,12 +209,12 @@ public final class ItemHelper {
|
|||||||
* @param extended <p>Whether to use an extended match, allowing any material</p>
|
* @param extended <p>Whether to use an extended match, allowing any material</p>
|
||||||
* @return <p>The matched material(s)</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);
|
String search = InputParsingHelper.regExIfy(materialName);
|
||||||
List<Material> materials = new ArrayList<>();
|
Set<Material> materials = new HashSet<>();
|
||||||
List<Material> all;
|
Set<Material> all;
|
||||||
if (extended) {
|
if (extended) {
|
||||||
all = List.of(Material.values());
|
all = Set.of(Material.values());
|
||||||
} else {
|
} else {
|
||||||
all = ItemHelper.getAllReforgeAbleMaterials();
|
all = ItemHelper.getAllReforgeAbleMaterials();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user