Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the issue #53: crash in sorting the inventory #38

Open
wants to merge 3 commits into
base: nyan-work/dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mojang.blaze3d.platform.Window;
import com.plusls.ommc.OhMyMinecraftClientReference;
import com.plusls.ommc.api.sortInventory.IDyeBlock;
import com.plusls.ommc.config.Configs;
import com.plusls.ommc.mixin.accessor.AccessorAbstractContainerScreen;
Expand Down Expand Up @@ -302,7 +303,29 @@ private static int getItemId(@NotNull ItemStack itemStack) {
sortedItemStacks.add(itemStack);
}

sortedItemStacks.sort(new ItemStackComparator());
try {
sortedItemStacks.sort(new ItemStackComparator());
} catch (IllegalArgumentException e) {
OhMyMinecraftClientReference.getLogger().info("An IllegalArgumentException caught. Run comparator test.");
ItemStackComparator comparator = new ItemStackComparator();
for (int i = 0; i < endSlot; i++) {
for (int j = i + 1; j < endSlot; j++) {
ItemStack stack1 = sortedItemStacks.get(i);
ItemStack stack2 = sortedItemStacks.get(j);

int res1 = comparator.compare(stack1, stack2);
int res2 = comparator.compare(stack2, stack1);

if ( !( ( res1 > 0 && res2 < 0 ) || ( res1 < 0 && res2 > 0 ) || ( res1 == 0 && res2 == 0 && stack1 == stack2 ) ) ) {
OhMyMinecraftClientReference.getLogger().error("Conflict result when comparing slot {} (name={}) with slot {} (name={}), result={} and {}",
i, stack1.getItem().getName(stack1).toString(),
j, stack2.getItem().getName(stack2).toString(),
res1, res2);
}
}
}
throw e;
}

// 倒序遍历来确保少的方块放在后面,多的方块放在前面
for (int i = endSlot - 1; i >= startSlot; i--) {
Expand Down Expand Up @@ -415,7 +438,7 @@ public int compare(ItemStack a, ItemStack b) {
Block blockA = ((BlockItem) a.getItem()).getBlock();
Block blockB = ((BlockItem) b.getItem()).getBlock();

if (blockA instanceof IDyeBlock && blockB instanceof IDyeBlock) {
if (blockA instanceof IDyeBlock && blockB instanceof IDyeBlock && blockA.getClass() == blockB.getClass()) {
return SortInventoryUtil.DYE_COLOR_MAPPING.get(((IDyeBlock) blockA).ommc$getColor()) -
SortInventoryUtil.DYE_COLOR_MAPPING.get(((IDyeBlock) blockB).ommc$getColor());
}
Expand All @@ -429,6 +452,8 @@ public int compare(ItemStack a, ItemStack b) {
//#endif

if (SortInventoryUtil.bothContains("wool", ida, idb) ||
SortInventoryUtil.bothContains("carpet", ida, idb) ||
SortInventoryUtil.bothContains("bed", ida, idb) ||
SortInventoryUtil.bothContains("terracotta", ida, idb) ||
SortInventoryUtil.bothContains("concrete", ida, idb) ||
SortInventoryUtil.bothContains("candle", ida, idb)) {
Expand Down