Skip to content

Commit

Permalink
PN-89 Makes the anvil ignore the enchantment order. Fix #239
Browse files Browse the repository at this point in the history
It will accept the client's ordering if the actual enchantments
and everything else are the same.
  • Loading branch information
joserobjr committed May 10, 2020
1 parent fdb5fea commit 0f058aa
Showing 1 changed file with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
import cn.nukkit.inventory.transaction.action.SlotChangeAction;
import cn.nukkit.inventory.transaction.action.TakeLevelAction;
import cn.nukkit.item.Item;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.nbt.tag.ListTag;
import cn.nukkit.network.protocol.ContainerClosePacket;
import cn.nukkit.network.protocol.types.ContainerIds;
import cn.nukkit.scheduler.Task;
import it.unimi.dsi.fastutil.ints.Int2IntArrayMap;
import it.unimi.dsi.fastutil.ints.Int2IntMap;

import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -155,7 +159,7 @@ public boolean canExecute() {
if (inventory instanceof AnvilInventory) {
AnvilInventory anvil = (AnvilInventory) inventory;
addInventory(anvil);
if (this.primaryOutput.equals(anvil.getResult(), true, true)) {
if (equalsIgnoringEnchantmentOrder(this.primaryOutput, anvil.getResult(), true)) {
TakeLevelAction takeLevel = new TakeLevelAction(anvil.getLevelCost());
addAction(takeLevel);
if (takeLevel.isValid(source)) {
Expand Down Expand Up @@ -194,6 +198,56 @@ public boolean canExecute() {
return this.recipe != null && super.canExecute();
}

//TODO Move this to Item at 1.2.1.0-PN
@Deprecated
private boolean equalsIgnoringEnchantmentOrder(Item thisItem, Item item, boolean checkDamage) {
if (!thisItem.equals(item, checkDamage, false)) {
return false;
}

if (Arrays.equals(thisItem.getCompoundTag(), item.getCompoundTag())) {
return true;
}

if (!thisItem.hasCompoundTag() || !item.hasCompoundTag()) {
return false;
}

CompoundTag thisTags = thisItem.getNamedTag();
CompoundTag otherTags = item.getNamedTag();
if (thisTags.equals(otherTags)) {
return true;
}

if (!thisTags.contains("ench") || !otherTags.contains("ench")
|| !(thisTags.get("ench") instanceof ListTag)
|| !(otherTags.get("ench") instanceof ListTag)
|| thisTags.getList("ench").size() != otherTags.getList("ench").size()) {
return false;
}

ListTag<CompoundTag> thisEnchantmentTags = thisTags.getList("ench", CompoundTag.class);
ListTag<CompoundTag> otherEnchantmentTags = otherTags.getList("ench", CompoundTag.class);

int size = thisEnchantmentTags.size();
Int2IntMap enchantments = new Int2IntArrayMap(size);
enchantments.defaultReturnValue(Integer.MIN_VALUE);

for (int i = 0; i < size; i++) {
CompoundTag tag = thisEnchantmentTags.get(i);
enchantments.put(tag.getShort("id"), tag.getShort("lvl"));
}

for (int i = 0; i < size; i++) {
CompoundTag tag = otherEnchantmentTags.get(i);
if (enchantments.get(tag.getShort("id")) != tag.getShort("lvl")) {
return false;
}
}

return true;
}

protected boolean callExecuteEvent() {
CraftItemEvent ev;

Expand Down

0 comments on commit 0f058aa

Please sign in to comment.