Skip to content

Commit

Permalink
Fixes some bugs with sorting algorithms.
Browse files Browse the repository at this point in the history
- make all of them more equal(-ish)
- force gear to be sorted together.
  • Loading branch information
BONNe committed Nov 3, 2023
1 parent 1935821 commit 8f4f8b5
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 33 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ sophisticated_backpacks_version=4637292
# The version of Sophisticated Backpacks Mod
storage_network_version=3868680
# The mod version. See https://semver.org/
mod_version=2.3.0
mod_version=2.3.1
# The change Log
changelog=Initial Release
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,26 @@ public final void comparator(SortOrder sortOrder,
String leftName = leftWhat.getDisplayName().getString();
String rightName = rightWhat.getDisplayName().getString();

if (!leftWhat.getId().equals(rightWhat.getId()) ||
if (!leftWhat.getModId().equals(rightWhat.getModId()))
{
// some small cleanup. We want to sort only vault items.
return String.CASE_INSENSITIVE_ORDER.compare(
leftWhat.getModId(),
rightWhat.getModId());
}

int registryOrder = SortingHelper.compareRegistryNames(
leftWhat.getId(),
rightWhat.getId(),
ascending);

if (registryOrder != 0 ||
!MixinRepo.isSortable(leftWhat.getId()))
{
// Use default string comparing
return ascending ?
String.CASE_INSENSITIVE_ORDER.compare(leftName, rightName) :
String.CASE_INSENSITIVE_ORDER.compare(rightName, leftName);
return registryOrder;
}

if (leftWhat.getId() == ModItems.JEWEL.getRegistryName())
else if (leftWhat.getId() == ModItems.JEWEL.getRegistryName())
{
CompoundTag leftTag = leftWhat.toTag().getCompound("tag");
CompoundTag rightTag = rightWhat.toTag().getCompound("tag");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import java.util.Comparator;

import iskallia.vault.gear.data.AttributeGearData;
Expand Down Expand Up @@ -74,8 +73,25 @@ private Comparator<ISlotClickHandler.IScrollableSlot> redirectAscending(QIOItemV
ItemStack firstItem = stack1.getItem().getStack();
ItemStack secondItem = stack2.getItem().getStack();

// if shift is down, or sorting by mod, then skip everything.
if (firstItem.getItem() instanceof JewelItem &&
if (!stack1.getModID().equals(stack2.getModID()))
{
// some small cleanup. We want to sort only vault items.
return String.CASE_INSENSITIVE_ORDER.compare(
stack1.getModID(),
stack2.getModID());
}

int registryOrder = SortingHelper.compareRegistryNames(
firstItem.getItem().getRegistryName(),
secondItem.getItem().getRegistryName(),
true);

if (registryOrder != 0)
{
// Use default string comparing
return registryOrder;
}
else if (firstItem.getItem() instanceof JewelItem &&
secondItem.getItem() instanceof JewelItem)
{
String leftName = firstItem.getDisplayName().getString();
Expand Down Expand Up @@ -259,8 +275,25 @@ private Comparator<ISlotClickHandler.IScrollableSlot> redirectDescending(QIOItem
ItemStack firstItem = stack1.getItem().getStack();
ItemStack secondItem = stack2.getItem().getStack();

// if shift is down, or sorting by mod, then skip everything.
if (firstItem.getItem() instanceof JewelItem &&
if (!stack1.getModID().equals(stack2.getModID()))
{
// some small cleanup. We want to sort only vault items.
return String.CASE_INSENSITIVE_ORDER.compare(
stack2.getModID(),
stack1.getModID());
}

int registryOrder = SortingHelper.compareRegistryNames(
firstItem.getItem().getRegistryName(),
secondItem.getItem().getRegistryName(),
false);

if (registryOrder != 0)
{
// Use default string comparing
return registryOrder;
}
else if (firstItem.getItem() instanceof JewelItem &&
secondItem.getItem() instanceof JewelItem)
{
String leftName = firstItem.getDisplayName().getString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,17 @@ public void compare(IGridStack left,
if (left.getIngredient() instanceof ItemStack leftStack &&
right.getIngredient() instanceof ItemStack rightStack)
{
if (leftStack.getItem() instanceof JewelItem &&
int registryOrder = SortingHelper.compareRegistryNames(
leftStack.getItem().getRegistryName(),
rightStack.getItem().getRegistryName(),
sortingDirection == SortingDirection.ASCENDING);

if (registryOrder != 0)
{
// If registry order is not 0, then return it.
callbackInfoReturnable.setReturnValue(registryOrder);
}
else if (leftStack.getItem() instanceof JewelItem &&
rightStack.getItem() instanceof JewelItem)
{
if (!VaultJewelSorting.CONFIGURATION.getJewelSortingByMod().isEmpty())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,30 @@ public void compare(IGridStack left,
return;
}

if (!left.getModId().equals(right.getModId()))
{
// some small cleanup. We want to sort only vault items.
callbackInfoReturnable.setReturnValue(
String.CASE_INSENSITIVE_ORDER.compare(left.getModId(), right.getModId()));
return;
}

if (left.getIngredient() instanceof ItemStack leftStack &&
right.getIngredient() instanceof ItemStack rightStack)
{
if (leftStack.getItem() instanceof JewelItem &&
// Get item registry names.

int registryOrder = SortingHelper.compareRegistryNames(
leftStack.getItem().getRegistryName(),
rightStack.getItem().getRegistryName(),
sortingDirection == SortingDirection.ASCENDING);

if (registryOrder != 0)
{
// If registry order is not 0, then return it.
callbackInfoReturnable.setReturnValue(registryOrder);
}
else if (leftStack.getItem() instanceof JewelItem &&
rightStack.getItem() instanceof JewelItem)
{
if (!VaultJewelSorting.CONFIGURATION.getJewelSortingByAmount().isEmpty())
Expand All @@ -75,7 +95,6 @@ public void compare(IGridStack left,
GearDataCache.of(rightStack),
VaultJewelSorting.CONFIGURATION.getJewelSortingByAmount(),
sortingDirection == SortingDirection.ASCENDING));
callbackInfoReturnable.cancel();
}
}
else if (leftStack.getItem() instanceof ToolItem &&
Expand All @@ -100,7 +119,6 @@ else if (leftStack.getItem() instanceof VaultGearItem &&
VaultGearData.read(rightStack),
VaultJewelSorting.CONFIGURATION.getGearSortingByAmount(),
sortingDirection == SortingDirection.ASCENDING));
callbackInfoReturnable.cancel();
}
}
else if (leftStack.getItem() instanceof InscriptionItem &&
Expand All @@ -114,7 +132,6 @@ else if (leftStack.getItem() instanceof InscriptionItem &&
InscriptionData.from(rightStack),
VaultJewelSorting.CONFIGURATION.getInscriptionSortingByAmount(),
sortingDirection == SortingDirection.ASCENDING));
callbackInfoReturnable.cancel();
}
}
else if (leftStack.getItem() instanceof VaultCrystalItem &&
Expand All @@ -129,7 +146,6 @@ else if (leftStack.getItem() instanceof VaultCrystalItem &&
CrystalData.read(rightStack),
VaultJewelSorting.CONFIGURATION.getVaultCrystalSortingByAmount(),
sortingDirection == SortingDirection.ASCENDING));
callbackInfoReturnable.cancel();
}
}
else if (leftStack.getItem() instanceof TrinketItem &&
Expand All @@ -146,7 +162,6 @@ else if (leftStack.getItem() instanceof TrinketItem &&
rightStack.getTag(),
VaultJewelSorting.CONFIGURATION.getTrinketSortingByAmount(),
sortingDirection == SortingDirection.ASCENDING));
callbackInfoReturnable.cancel();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,17 @@ private static Comparator<Map.Entry<ItemStackKey, Integer>> compareJewels(Compar
{
return original.thenComparing((first, second) ->
{
if (first.getKey().getStack().getItem() instanceof JewelItem &&
int registryOrder = SortingHelper.compareRegistryNames(
first.getKey().getStack().getItem().getRegistryName(),
second.getKey().getStack().getItem().getRegistryName(),
true);

if (registryOrder != 0)
{
// Use default string comparing
return registryOrder;
}
else if (first.getKey().getStack().getItem() instanceof JewelItem &&
second.getKey().getStack().getItem() instanceof JewelItem)
{
return SortingHelper.compareJewels(
Expand Down Expand Up @@ -110,7 +120,17 @@ else if (first.getKey().getStack().getItem() instanceof TrinketItem &&
{
return original.thenComparing((first, second) ->
{
if (first.getKey().getStack().getItem() instanceof JewelItem &&
int registryOrder = SortingHelper.compareRegistryNames(
first.getKey().getStack().getItem().getRegistryName(),
second.getKey().getStack().getItem().getRegistryName(),
true);

if (registryOrder != 0)
{
// Use default string comparing
return registryOrder;
}
else if (first.getKey().getStack().getItem() instanceof JewelItem &&
second.getKey().getStack().getItem() instanceof JewelItem)
{
return SortingHelper.compareJewels(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,17 @@ private Comparator<ItemStack> sortStackWrappersJewelCompare(Comparator<ItemStack
return 0;
}

// Do not sort if shift is pressed
if (first.getItem() instanceof JewelItem &&
int registryOrder = SortingHelper.compareRegistryNames(
first.getItem().getRegistryName(),
second.getItem().getRegistryName(),
this.gui.getDownwards());

if (registryOrder != 0)
{
// Use default string comparing
return registryOrder;
}
else if (first.getItem() instanceof JewelItem &&
second.getItem() instanceof JewelItem)
{
String leftName = first.getDisplayName().getString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
package lv.id.bonne.vaulthunters.jewelsorting.utils;


import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.*;

import iskallia.vault.VaultMod;
import iskallia.vault.gear.VaultGearState;
import iskallia.vault.gear.attribute.VaultGearAttribute;
import iskallia.vault.gear.attribute.VaultGearModifier;
Expand All @@ -19,6 +18,7 @@
import iskallia.vault.gear.data.VaultGearData;
import iskallia.vault.gear.trinket.TrinketEffect;
import iskallia.vault.init.ModGearAttributes;
import iskallia.vault.init.ModItems;
import iskallia.vault.item.crystal.CrystalData;
import iskallia.vault.item.data.InscriptionData;
import net.minecraft.nbt.CompoundTag;
Expand All @@ -30,6 +30,29 @@
*/
public class SortingHelper
{
/**
* This method compares two given registry names.
* @param leftReg The left registry name.
* @param rightReg The right registry name.
* @param ascending The ascending or descending order.
* @return The comparison of two given registry names.
*/
public static int compareRegistryNames(ResourceLocation leftReg,
ResourceLocation rightReg,
boolean ascending)
{
String leftName = VAULT_GEAR_SET.contains(leftReg) ?
VaultMod.sId("gear") : leftReg.toString();

String rightName = VAULT_GEAR_SET.contains(rightReg) ?
VaultMod.sId("gear") : rightReg.toString();

return ascending ?
SortingHelper.compareString(leftName, rightName) :
SortingHelper.compareString(rightName, leftName);
}


/**
* This method compares two given jewels by their sorting order.
*
Expand Down Expand Up @@ -282,8 +305,9 @@ public static int compareVaultCrystals(String leftName,
returnValue = switch (sortOptions) {
case NAME -> SortingHelper.compareString(leftName, rightName);
case LEVEL -> Integer.compare(leftData.getLevel(), rightData.getLevel());
case TYPE -> leftData.getObjective().getClass().getName().
compareTo(rightData.getObjective().getClass().getName());
case TYPE -> SortingHelper.compareString(
leftData.getObjective().getClass().getName(),
rightData.getObjective().getClass().getName());
};
}

Expand Down Expand Up @@ -334,8 +358,9 @@ public static int compareTrinkets(String leftName,

if (leftValue.isPresent() && rightValue.isPresent())
{
yield leftValue.get().getTrinketConfig().getName().
compareTo(rightValue.get().getTrinketConfig().getName());
yield SortingHelper.compareString(
leftValue.get().getTrinketConfig().getName(),
rightValue.get().getTrinketConfig().getName());
}
else
{
Expand All @@ -351,8 +376,9 @@ public static int compareTrinkets(String leftName,
{
if (leftValue.get().getConfig().hasCuriosSlot() && rightValue.get().getConfig().hasCuriosSlot())
{
yield leftValue.get().getConfig().getCuriosSlot().
compareTo(rightValue.get().getConfig().getCuriosSlot());
yield SortingHelper.compareString(
leftValue.get().getConfig().getCuriosSlot(),
rightValue.get().getConfig().getCuriosSlot());
}
else
{
Expand Down Expand Up @@ -620,7 +646,7 @@ private static int compareRarity(VaultGearData leftData, VaultGearData rightData
if (leftIndex == rightIndex)
{
// sort by name
return leftRoll.compareTo(rightRoll);
return SortingHelper.compareString(leftRoll, rightRoll);
}
else
{
Expand Down Expand Up @@ -1049,4 +1075,28 @@ public enum TrinketOptions
* The name of the cache.
*/
public static final String EXTRA_GEAR_LEVEL = "extra_gear_level";

public static final Set<ResourceLocation> VAULT_GEAR_SET = new HashSet<>();

// Put all vault gear items into the set.
static
{
VAULT_GEAR_SET.add(ModItems.HELMET.getRegistryName());
VAULT_GEAR_SET.add(ModItems.CHESTPLATE.getRegistryName());
VAULT_GEAR_SET.add(ModItems.LEGGINGS.getRegistryName());
VAULT_GEAR_SET.add(ModItems.BOOTS.getRegistryName());

VAULT_GEAR_SET.add(ModItems.SWORD.getRegistryName());
VAULT_GEAR_SET.add(ModItems.AXE.getRegistryName());

VAULT_GEAR_SET.add(ModItems.WAND.getRegistryName());
VAULT_GEAR_SET.add(ModItems.SHIELD.getRegistryName());

VAULT_GEAR_SET.add(ModItems.MAGNET.getRegistryName());

VAULT_GEAR_SET.add(ModItems.IDOL_BENEVOLENT.getRegistryName());
VAULT_GEAR_SET.add(ModItems.IDOL_OMNISCIENT.getRegistryName());
VAULT_GEAR_SET.add(ModItems.IDOL_TIMEKEEPER.getRegistryName());
VAULT_GEAR_SET.add(ModItems.IDOL_MALEVOLENCE.getRegistryName());
}
}

0 comments on commit 8f4f8b5

Please sign in to comment.