From 1f7d4eaf21d31f5016021fab316b1753b218108f Mon Sep 17 00:00:00 2001 From: shedaniel Date: Thu, 11 Jun 2020 14:56:39 +0800 Subject: Fix tags in favorites, fix #349 Signed-off-by: shedaniel --- .../me/shedaniel/rei/gui/widget/EntryWidget.java | 2 +- .../java/me/shedaniel/rei/impl/ItemEntryStack.java | 38 +++++++++++++++------- 2 files changed, 27 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/main/java/me/shedaniel/rei/gui/widget/EntryWidget.java b/src/main/java/me/shedaniel/rei/gui/widget/EntryWidget.java index 1f5bc6f8d..46e2649d1 100644 --- a/src/main/java/me/shedaniel/rei/gui/widget/EntryWidget.java +++ b/src/main/java/me/shedaniel/rei/gui/widget/EntryWidget.java @@ -275,7 +275,7 @@ public class EntryWidget extends WidgetWithBounds { entry.setAmount(127); if (keyCode.matchesKey(int_1, int_2)) { if (reverseFavoritesAction()) - ConfigObject.getInstance().getFavorites().remove(entry); + ConfigObject.getInstance().getFavorites().removeIf(entry::equalsIgnoreAmount); else if (!CollectionUtils.anyMatchEqualsEntryIgnoreAmount(ConfigObject.getInstance().getFavorites(), entry)) ConfigObject.getInstance().getFavorites().add(entry); ConfigManager.getInstance().saveConfig(); diff --git a/src/main/java/me/shedaniel/rei/impl/ItemEntryStack.java b/src/main/java/me/shedaniel/rei/impl/ItemEntryStack.java index 0bb10bd7f..42cb8a344 100644 --- a/src/main/java/me/shedaniel/rei/impl/ItemEntryStack.java +++ b/src/main/java/me/shedaniel/rei/impl/ItemEntryStack.java @@ -41,6 +41,7 @@ import net.minecraft.client.texture.SpriteAtlasTexture; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.item.ItemStack; import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.ListTag; import net.minecraft.nbt.Tag; import net.minecraft.util.Identifier; import net.minecraft.util.registry.Registry; @@ -140,10 +141,10 @@ public class ItemEntryStack extends AbstractEntryStack implements OptimalEntrySt ItemStack otherStack = stack.getItemStack(); CompoundTag o1 = itemStack.getTag(); CompoundTag o2 = otherStack.getTag(); - return o1 == o2 || ((o1 != null && o2 != null) && equals(o1, o2)); + return o1 == o2 || ((o1 != null && o2 != null) && equalsTagWithoutCount(o1, o2)); } - public boolean equals(CompoundTag o1, CompoundTag o2) { + private boolean equalsTagWithoutCount(CompoundTag o1, CompoundTag o2) { int o1Size = 0; int o2Size = 0; for (String key : o1.getKeys()) { @@ -167,16 +168,8 @@ public class ItemEntryStack extends AbstractEntryStack implements OptimalEntrySt continue; Tag value = o1.get(key); Tag otherValue = o2.get(key); - if (value == null) { - if (!(otherValue == null && o2.contains(key))) - return false; - } else if (value instanceof CompoundTag && otherValue instanceof CompoundTag) { - if (!(value == otherValue || (value != null && otherValue != null) && equals((CompoundTag) value, (CompoundTag) otherValue))) - return false; - } else { - if (!value.asString().equals(otherValue.asString())) - return false; - } + if (!equalsTag(value, otherValue)) + return false; } } catch (ClassCastException | NullPointerException unused) { return false; @@ -185,6 +178,27 @@ public class ItemEntryStack extends AbstractEntryStack implements OptimalEntrySt return true; } + private boolean equalsTag(Tag tag, Tag otherTag) { + if (tag == null || otherTag == null) { + return tag == otherTag; + } + if (tag instanceof ListTag && otherTag instanceof ListTag) + return equalsList((ListTag) tag, (ListTag) otherTag); + return tag.equals(otherTag); + } + + private boolean equalsList(ListTag listTag, ListTag otherListTag) { + if (listTag.size() != otherListTag.size()) + return false; + for (int i = 0; i < listTag.size(); i++) { + Tag value = listTag.get(i); + Tag otherValue = otherListTag.get(i); + if (!equalsTag(value, otherValue)) + return false; + } + return true; + } + @Override public boolean equalsIgnoreTags(EntryStack stack) { if (stack.getType() != Type.ITEM) -- cgit