Fixed strange issue related to item meta.

Added more verbose debug log.
This commit is contained in:
alexcrea 2024-06-16 04:55:23 +02:00
parent 1eac81aef6
commit c7fee98d44
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
6 changed files with 23 additions and 21 deletions

View file

@ -90,17 +90,15 @@ public abstract class WrappedEnchantment {
/** /**
* Force add an enchantment at the provided level. * Force add an enchantment at the provided level.
* @param item The item to set the enchantment level. * @param item The item to set the enchantment level.
* @param meta Meta of the provided item. It can be changed, but will not be set on the item.
* @param level The level to set the enchantment to. * @param level The level to set the enchantment to.
*/ */
public abstract void addEnchantmentUnsafe(@NotNull ItemStack item, @NotNull ItemMeta meta, int level); public abstract void addEnchantmentUnsafe(@NotNull ItemStack item, int level);
/** /**
* Remove this enchantment from the provided ItemStack. * Remove this enchantment from the provided ItemStack.
* @param item The item to remove the enchantment. * @param item The item to remove the enchantment.
* @param meta Meta of the provided item. It can be changed, but will not be set on the item.
*/ */
public abstract void removeFrom(@NotNull ItemStack item, @NotNull ItemMeta meta); public abstract void removeFrom(@NotNull ItemStack item);
// Static functions // Static functions
@ -109,13 +107,10 @@ public abstract class WrappedEnchantment {
* @param item Item to be cleared from enchantments. * @param item Item to be cleared from enchantments.
*/ */
public static void clearEnchants(@NotNull ItemStack item){ //TODO faster method to clear vanilla enchantment public static void clearEnchants(@NotNull ItemStack item){ //TODO faster method to clear vanilla enchantment
ItemMeta meta = item.getItemMeta();
if(meta == null) return;
for (WrappedEnchantment enchant : getEnchants(item).keySet()) { for (WrappedEnchantment enchant : getEnchants(item).keySet()) {
enchant.removeFrom(item, meta); enchant.removeFrom(item);
} }
item.setItemMeta(meta);
} }
/** /**

View file

@ -46,11 +46,13 @@ public class VanillaEnchant extends WrappedEnchantment {
} }
@Override @Override
public void addEnchantmentUnsafe(@NotNull ItemStack item, @NotNull ItemMeta meta, int level) { public void addEnchantmentUnsafe(@NotNull ItemStack item, int level) {
if (isEnchantedBook(item)) { if (isEnchantedBook(item)) {
EnchantmentStorageMeta bookMeta = ((EnchantmentStorageMeta)meta); EnchantmentStorageMeta bookMeta = ((EnchantmentStorageMeta)item.getItemMeta());
assert bookMeta != null;
bookMeta.addStoredEnchant(this.enchantment, level, true); bookMeta.addStoredEnchant(this.enchantment, level, true);
item.setItemMeta(bookMeta);
} else { } else {
item.addUnsafeEnchantment(this.enchantment, level); item.addUnsafeEnchantment(this.enchantment, level);
} }
@ -58,13 +60,16 @@ public class VanillaEnchant extends WrappedEnchantment {
} }
@Override @Override
public void removeFrom(@NotNull ItemStack item, @NotNull ItemMeta meta) { public void removeFrom(@NotNull ItemStack item) {
if (ItemUtil.INSTANCE.isEnchantedBook(item)) { if (ItemUtil.INSTANCE.isEnchantedBook(item)) {
EnchantmentStorageMeta bookMeta = ((EnchantmentStorageMeta)meta); EnchantmentStorageMeta bookMeta = ((EnchantmentStorageMeta)item.getItemMeta());
assert bookMeta != null;
bookMeta.removeStoredEnchant(this.enchantment); bookMeta.removeStoredEnchant(this.enchantment);
item.setItemMeta(bookMeta);
}else{
item.removeEnchantment(this.enchantment);
} }
item.removeEnchantment(this.enchantment);
} }

View file

@ -452,6 +452,7 @@ class AnvilEventListener(private val packetManager: PacketManager) : Listener {
if (ConflictType.BIG_CONFLICT == conflictType) { if (ConflictType.BIG_CONFLICT == conflictType) {
illegalPenalty += ConfigOptions.sacrificeIllegalCost illegalPenalty += ConfigOptions.sacrificeIllegalCost
CustomAnvil.verboseLog("Big conflict. Adding illegal price penalty")
} }
continue continue
} }

View file

@ -29,13 +29,11 @@ object ItemUtil {
fun ItemStack.setEnchantmentsUnsafe(enchantments: Map<WrappedEnchantment, Int>) { fun ItemStack.setEnchantmentsUnsafe(enchantments: Map<WrappedEnchantment, Int>) {
WrappedEnchantment.clearEnchants(this) WrappedEnchantment.clearEnchants(this)
val meta = this.itemMeta ?: return //TODO maybe faster methode to add vanilla enchantment. maybe move this function to wrapped enchantment
enchantments.forEach { (enchantment, level) -> enchantments.forEach { (enchantment, level) ->
enchantment.addEnchantmentUnsafe(this, meta, level) enchantment.addEnchantmentUnsafe(this, level)
} }
this.itemMeta = meta
} }
/** /**

View file

@ -18,6 +18,7 @@ class EnchantConflictGroup(
fun allowed(enchants: Set<WrappedEnchantment>, mat: Material): Boolean { fun allowed(enchants: Set<WrappedEnchantment>, mat: Material): Boolean {
if (enchantments.size < minBeforeBlock) { if (enchantments.size < minBeforeBlock) {
CustomAnvil.verboseLog("Conflicting bc of to many enchantments")
return true return true
} }
@ -31,6 +32,7 @@ class EnchantConflictGroup(
if (enchantment !in enchantments) continue if (enchantment !in enchantments) continue
CustomAnvil.verboseLog("Enchant ${enchantment.key} is in: ${enchantAmount + 1}/$minBeforeBlock ") CustomAnvil.verboseLog("Enchant ${enchantment.key} is in: ${enchantAmount + 1}/$minBeforeBlock ")
if (++enchantAmount > minBeforeBlock) { if (++enchantAmount > minBeforeBlock) {
CustomAnvil.verboseLog("it is not allowed bc of to many enchantment in conflict")
return false return false
} }

View file

@ -160,13 +160,14 @@ class EnchantConflictManager {
var result = ConflictType.NO_CONFLICT var result = ConflictType.NO_CONFLICT
for (conflict in conflictList) { for (conflict in conflictList) {
CustomAnvil.verboseLog("Is against $conflict") CustomAnvil.verboseLog("Is against $conflict")
val conflicting = conflict.allowed(base, mat) val allowed = conflict.allowed(base, mat)
CustomAnvil.verboseLog("Was against $conflict and conflicting: $conflicting ") CustomAnvil.verboseLog("Was against $conflict and conflicting: ${!allowed} ")
if (!conflicting) { if (!allowed) {
if (conflict.getEnchants().size <= 1) { if (conflict.getEnchants().size <= 1) {
result = ConflictType.SMALL_CONFLICT result = ConflictType.SMALL_CONFLICT
CustomAnvil.verboseLog("Small conflict, continuing") CustomAnvil.verboseLog("Small conflict, continuing")
} else { } else {
CustomAnvil.verboseLog("Big conflict, probably stoping")
return ConflictType.BIG_CONFLICT return ConflictType.BIG_CONFLICT
} }
} }