Add option to consider book enchantment as stored enchantment

This commit is contained in:
alexcrea 2025-05-28 18:07:48 +02:00
parent a74470bd0d
commit 27604dbb81
No known key found for this signature in database
GPG key ID: 027DD67D2D3280C5
6 changed files with 38 additions and 19 deletions

View file

@ -16,7 +16,7 @@ plugins {
} }
group = "xyz.alexcrea" group = "xyz.alexcrea"
version = "1.11.2" version = "1.11.3"
repositories { repositories {
// EcoEnchants // EcoEnchants

View file

@ -207,7 +207,6 @@ public interface CAEnchantment {
@NotNull ItemMeta meta, @NotNull ItemMeta meta,
@NotNull Map<CAEnchantment, Integer> enchantments, @NotNull Map<CAEnchantment, Integer> enchantments,
@NotNull Collection<CAEnchantment> enchantmentToTest){ @NotNull Collection<CAEnchantment> enchantmentToTest){
for (CAEnchantment enchantment : enchantmentToTest) { for (CAEnchantment enchantment : enchantmentToTest) {
if(enchantment.isEnchantmentPresent(item, meta)){ if(enchantment.isEnchantmentPresent(item, meta)){
enchantments.put(enchantment, enchantment.getLevel(item, meta)); enchantments.put(enchantment, enchantment.getLevel(item, meta));

View file

@ -1,6 +1,7 @@
package xyz.alexcrea.cuanvil.enchant.bulk; package xyz.alexcrea.cuanvil.enchant.bulk;
import io.delilaheve.CustomAnvil; import io.delilaheve.CustomAnvil;
import io.delilaheve.util.ConfigOptions;
import io.delilaheve.util.ItemUtil; import io.delilaheve.util.ItemUtil;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
@ -17,11 +18,14 @@ public class BukkitEnchantBulkOperation implements BulkGetEnchantOperation, Bulk
@Override @Override
public void bulkGet(@NotNull Map<CAEnchantment, Integer> enchantmentMap, @NotNull ItemStack item, @NotNull ItemMeta meta) { public void bulkGet(@NotNull Map<CAEnchantment, Integer> enchantmentMap, @NotNull ItemStack item, @NotNull ItemMeta meta) {
if (ItemUtil.INSTANCE.isEnchantedBook(item)) { boolean isBook = ItemUtil.INSTANCE.isEnchantedBook(item);
if (isBook) {
((EnchantmentStorageMeta) meta).getStoredEnchants().forEach((enchantment, level) -> ((EnchantmentStorageMeta) meta).getStoredEnchants().forEach((enchantment, level) ->
addEnchantment(enchantmentMap, enchantment, level) addEnchantment(enchantmentMap, enchantment, level)
); );
} else { }
if(!isBook || ConfigOptions.INSTANCE.getAddBookEnchantmentAsStoredEnchantment()){
item.getEnchantments().forEach((enchantment, level) -> item.getEnchantments().forEach((enchantment, level) ->
addEnchantment(enchantmentMap, enchantment, level) addEnchantment(enchantmentMap, enchantment, level)
); );
@ -41,7 +45,7 @@ public class BukkitEnchantBulkOperation implements BulkGetEnchantOperation, Bulk
@Override @Override
public void bulkClear(@NotNull ItemStack item) { public void bulkClear(@NotNull ItemStack item) {
if (item.getType() != Material.ENCHANTED_BOOK) { if (item.getType() != Material.ENCHANTED_BOOK || ConfigOptions.INSTANCE.getAddBookEnchantmentAsStoredEnchantment()) {
item.getEnchantments().forEach((enchantment, level) -> item.getEnchantments().forEach((enchantment, level) ->
item.removeEnchantment(enchantment) item.removeEnchantment(enchantment)

View file

@ -1,6 +1,7 @@
package xyz.alexcrea.cuanvil.enchant.wrapped; package xyz.alexcrea.cuanvil.enchant.wrapped;
import io.delilaheve.CustomAnvil; import io.delilaheve.CustomAnvil;
import io.delilaheve.util.ConfigOptions;
import io.delilaheve.util.ItemUtil; import io.delilaheve.util.ItemUtil;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.enchantments.EnchantmentTarget; import org.bukkit.enchantments.EnchantmentTarget;
@ -63,7 +64,8 @@ public class CABukkitEnchantment extends CAEnchantmentBase {
if (ItemUtil.INSTANCE.isEnchantedBook(item)) { if (ItemUtil.INSTANCE.isEnchantedBook(item)) {
EnchantmentStorageMeta bookMeta = ((EnchantmentStorageMeta) meta); EnchantmentStorageMeta bookMeta = ((EnchantmentStorageMeta) meta);
return bookMeta.getStoredEnchants().containsKey(this.bukkit); return bookMeta.getStoredEnchants().containsKey(this.bukkit) ||
(ConfigOptions.INSTANCE.getAddBookEnchantmentAsStoredEnchantment() && item.containsEnchantment(this.bukkit));
} else { } else {
return item.containsEnchantment(this.bukkit); return item.containsEnchantment(this.bukkit);
} }
@ -90,6 +92,7 @@ public class CABukkitEnchantment extends CAEnchantmentBase {
assert bookMeta != null; assert bookMeta != null;
bookMeta.removeStoredEnchant(this.bukkit); bookMeta.removeStoredEnchant(this.bukkit);
bookMeta.removeEnchant(this.bukkit);
item.setItemMeta(bookMeta); item.setItemMeta(bookMeta);
} else { } else {
item.removeEnchantment(this.bukkit); item.removeEnchantment(this.bukkit);
@ -112,6 +115,7 @@ public class CABukkitEnchantment extends CAEnchantmentBase {
} }
private static Method getAnvilCostMethod; private static Method getAnvilCostMethod;
static { static {
Class<Enchantment> clazz = Enchantment.class; Class<Enchantment> clazz = Enchantment.class;
try { try {

View file

@ -20,7 +20,7 @@ import java.util.function.Consumer;
* *
* @param <T> Type of the factory of the type of setting the gui should edit. * @param <T> Type of the factory of the type of setting the gui should edit.
*/ */
public abstract class AbstractEnchantConfigGui<T extends SettingGui.SettingGuiFactory> extends SettingGuiListConfigGui<CAEnchantment, T> implements ValueUpdatableGui { public abstract class AbstractEnchantConfigGui<T extends SettingGui.SettingGuiFactory> extends SettingGuiListConfigGui<CAEnchantment, T>{
/** /**
* Constructor for a gui displaying available enchantment to edit a enchantment setting. * Constructor for a gui displaying available enchantment to edit a enchantment setting.

View file

@ -31,6 +31,7 @@ object ConfigOptions {
const val ITEM_RENAME_COST = "item_rename_cost" const val ITEM_RENAME_COST = "item_rename_cost"
const val SACRIFICE_ILLEGAL_COST = "sacrifice_illegal_enchant_cost" const val SACRIFICE_ILLEGAL_COST = "sacrifice_illegal_enchant_cost"
const val ADD_BOOK_ENCHANTMENT_AS_STORED_ENCHANTMENT = "add_book_enchantment_as_stored_enchantment"
// Color related config // Color related config
const val ALLOW_COLOR_CODE = "allow_color_code" const val ALLOW_COLOR_CODE = "allow_color_code"
@ -78,6 +79,7 @@ object ConfigOptions {
const val DEFAULT_ITEM_RENAME_COST = 1 const val DEFAULT_ITEM_RENAME_COST = 1
const val DEFAULT_SACRIFICE_ILLEGAL_COST = 1 const val DEFAULT_SACRIFICE_ILLEGAL_COST = 1
const val DEFAULT_ADD_BOOK_ENCHANTMENT_AS_STORED_ENCHANTMENT = false;
// Color related config // Color related config
const val DEFAULT_ALLOW_COLOR_CODE = false const val DEFAULT_ALLOW_COLOR_CODE = false
@ -223,6 +225,16 @@ object ConfigOptions {
?: DEFAULT_SACRIFICE_ILLEGAL_COST ?: DEFAULT_SACRIFICE_ILLEGAL_COST
} }
/**
* Consider book enchantment as book stored enchantment
*/
val addBookEnchantmentAsStoredEnchantment : Boolean
get(){
return ConfigHolder.DEFAULT_CONFIG
.config
.getBoolean(ADD_BOOK_ENCHANTMENT_AS_STORED_ENCHANTMENT, DEFAULT_ADD_BOOK_ENCHANTMENT_AS_STORED_ENCHANTMENT)
}
/** /**
* Allow usage of color code * Allow usage of color code
*/ */