1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

Merge pull request #2911 from Rothes/master

Check lore to determine whether (Dis)Enchantable
This commit is contained in:
Daniel Walsh 2021-04-26 14:19:09 +01:00 committed by GitHub
commit a8e84a7eff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 7 deletions

View File

@ -480,4 +480,4 @@
</exclusions>
</dependency>
</dependencies>
</project>
</project>

View File

@ -9,6 +9,8 @@ import io.github.thebusybiscuit.cscorelib2.config.Config;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
import java.util.List;
/**
* This class represents a Setting for a {@link SlimefunItem} that can be modified via
* the {@code Items.yml} {@link Config} file.
@ -149,7 +151,7 @@ public class ItemSetting<T> {
SlimefunPlugin.getItemCfg().setDefaultValue(item.getId() + '.' + getKey(), getDefaultValue());
Object configuredValue = SlimefunPlugin.getItemCfg().getValue(item.getId() + '.' + getKey());
if (defaultValue.getClass().isInstance(configuredValue)) {
if (defaultValue.getClass().isInstance(configuredValue) || (configuredValue instanceof List && defaultValue instanceof List)) {
// We can do an unsafe cast here, we did an isInstance(...) check before!
T newValue = (T) configuredValue;

View File

@ -16,13 +16,18 @@ import me.mrCookieSlime.Slimefun.Objects.Category;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.AContainer;
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.Collections;
import java.util.List;
/**
* This is a super class of the {@link AutoEnchanter} and {@link AutoDisenchanter} which is
* used to streamline some methods and combine common attributes to reduce redundancy.
*
*
* @author TheBusyBiscuit
*
* @author Rothes
*
* @see AutoEnchanter
* @see AutoDisenchanter
*
@ -31,6 +36,8 @@ abstract class AbstractEnchantmentMachine extends AContainer {
private final ItemSetting<Boolean> useLevelLimit = new ItemSetting<>(this, "use-enchant-level-limit", false);
private final IntRangeSetting levelLimit = new IntRangeSetting(this, "enchant-level-limit", 0, 10, Short.MAX_VALUE);
private final ItemSetting<Boolean> useIgnoredLores = new ItemSetting<>(this, "use-ignored-lores", false);
private final ItemSetting<List<String>> ignoredLores = new ItemSetting<>(this, "ignored-lores", Collections.singletonList("&7- &cCan't be used in " + this.getItemName()));
@ParametersAreNonnullByDefault
protected AbstractEnchantmentMachine(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) {
@ -38,6 +45,8 @@ abstract class AbstractEnchantmentMachine extends AContainer {
addItemSetting(useLevelLimit);
addItemSetting(levelLimit);
addItemSetting(useIgnoredLores);
addItemSetting(ignoredLores);
}
protected boolean isEnchantmentLevelAllowed(int enchantmentLevel) {
@ -55,4 +64,20 @@ abstract class AbstractEnchantmentMachine extends AContainer {
menu.replaceExistingItem(22, progressBar);
}
protected boolean hasIgnoredLore(@Nonnull ItemStack itemStack) {
if (useIgnoredLores.getValue() && itemStack.hasItemMeta()) {
ItemMeta itemMeta = itemStack.getItemMeta();
if (!itemMeta.hasLore()) {
return false;
}
List<String> itemLore = itemMeta.getLore();
List<String> ignoredLore = ignoredLores.getValue();
for (String lore : ignoredLore) {
if (itemLore.contains(ChatColors.color(lore))) {
return true;
}
}
}
return false;
}
}

View File

@ -142,12 +142,12 @@ public class AutoDisenchanter extends AbstractEnchantmentMachine {
private boolean isDisenchantable(@Nullable ItemStack item) {
if (item == null || item.getType().isAir()) {
return false;
} else if (item.getType() != Material.BOOK) {
} else if (item.getType() != Material.BOOK && !hasIgnoredLore(item)) {
// ^ This stops endless checks of getByItem for books
SlimefunItem sfItem = SlimefunItem.getByItem(item);
return sfItem == null || sfItem.isDisenchantable();
} else {
return true;
return false;
}
}

View File

@ -117,7 +117,7 @@ public class AutoEnchanter extends AbstractEnchantmentMachine {
private boolean isEnchantable(@Nullable ItemStack item) {
// stops endless checks of getByItem for enchanted book stacks.
if (item != null && !item.getType().isAir() && item.getType() != Material.ENCHANTED_BOOK) {
if (item != null && item.getType() != Material.ENCHANTED_BOOK && !item.getType().isAir() && !hasIgnoredLore(item)) {
SlimefunItem sfItem = SlimefunItem.getByItem(item);
return sfItem == null || sfItem.isEnchantable();
} else {