Skip to content

Commit 81c655c

Browse files
SubCommand_Find: Add finding enchantments on enchanted books (PotatoCraft-Studio#306)
* SubCommand_Find: Add finding enchants on books * Reduce nested if's into singular if statement using && * Combine getBooksEnchantments and listContainsString into single function * Add missed imports --------- Co-authored-by: sandtechnology <20417547+sandtechnology@users.noreply.github.com>
1 parent 6024b7c commit 81c655c

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

src/main/java/org/maxgamer/quickshop/command/subcommand/SubCommand_Find.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ public void onCommand(@NotNull Player sender, @NotNull String commandLabel, @Not
8989
double distance = shopVector.distance(playerVector);
9090
//Check distance
9191
if (distance <= maxDistance) {
92-
//Collect valid shop that trading items we want
93-
if (!Util.getItemStackName(shop.getItem()).toLowerCase().contains(lookFor)) {
94-
if (!shop.getItem().getType().name().toLowerCase().contains(lookFor)) {
95-
continue;
96-
}
92+
// Collect valid shop that trading items we want
93+
if (!Util.getItemStackName(shop.getItem()).toLowerCase().contains(lookFor)
94+
&& !shop.getItem().getType().name().toLowerCase().contains(lookFor)
95+
&& !Util.isBookEnchantmentsMatched(shop.getItem(), lookFor)) {
96+
continue;
9797
}
9898
if (excludeOutOfStock) {
9999
if ((shop.isSelling() && shop.getRemainingStock() == 0) || (shop.isBuying() && shop.getRemainingSpace() == 0)) {

src/main/java/org/maxgamer/quickshop/util/Util.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
import java.util.EnumSet;
9595
import java.util.Iterator;
9696
import java.util.List;
97+
import java.util.Locale;
9798
import java.util.Map;
9899
import java.util.Map.Entry;
99100
import java.util.Objects;
@@ -576,6 +577,34 @@ public static String getItemStackName(@NotNull ItemStack itemStack) {
576577
return result == null ? MsgUtil.getItemi18n(itemStack.getType().name()) : result;
577578
}
578579

580+
/**
581+
* Check all enchantments on a book and return true if they contain the
582+
* nameToMatch
583+
*
584+
* @param itemStack The enchanted book itemstack
585+
* @param String The name of the enchant to check the book for
586+
* @return The names of enchants contained on the enchanted book
587+
*/
588+
@NotNull
589+
public static boolean isBookEnchantmentsMatched(@NotNull ItemStack itemStack, @NotNull String nameToMatch) {
590+
if (itemStack.getType() == Material.ENCHANTED_BOOK) {
591+
ItemMeta itemMeta = itemStack.getItemMeta();
592+
if (!(itemMeta instanceof EnchantmentStorageMeta))
593+
return false;
594+
EnchantmentStorageMeta enchantmentStorageMeta = (EnchantmentStorageMeta) itemStack.getItemMeta();
595+
if (enchantmentStorageMeta.hasStoredEnchants()) {
596+
for (Enchantment enchantment : enchantmentStorageMeta.getStoredEnchants().keySet()) {
597+
nameToMatch = nameToMatch.toUpperCase(Locale.ROOT);
598+
if (enchantment.getKey().getKey().toUpperCase(Locale.ROOT).contains(nameToMatch)
599+
|| MsgUtil.getEnchi18n(enchantment).toUpperCase(Locale.ROOT).contains(nameToMatch)) {
600+
return true;
601+
}
602+
}
603+
}
604+
}
605+
return false;
606+
}
607+
579608
@NotNull
580609
public static String getFirstEnchantmentName(@NotNull EnchantmentStorageMeta meta) {
581610
if (!meta.hasStoredEnchants()) {

0 commit comments

Comments
 (0)