From f4212a86c3668952ef6b2e26268feebe965c9adf Mon Sep 17 00:00:00 2001 From: Kyle VanderBeek Date: Sat, 2 Jun 2018 02:05:44 -0700 Subject: [PATCH] Fixes #3520: Correct order for sort by mod and general code cleanup (#3517) Comparable/Comparator implementation now use fast Java built-ins for primitive types rather than hand-coded versions. Also DRY some code and simplify logic. This maintains the "biggest stack first" notion of "ascending", but also fixes the inconsistent/accidental reversal of sort by mod (the mod name should sort the same direction as the item name). --- .../appeng/client/me/ClientDCInternalInv.java | 2 +- .../implementations/CraftingCPURecord.java | 2 +- src/main/java/appeng/util/ItemSorters.java | 50 ++++++++----------- 3 files changed, 24 insertions(+), 30 deletions(-) diff --git a/src/main/java/appeng/client/me/ClientDCInternalInv.java b/src/main/java/appeng/client/me/ClientDCInternalInv.java index a42561ff4..4cb99673b 100644 --- a/src/main/java/appeng/client/me/ClientDCInternalInv.java +++ b/src/main/java/appeng/client/me/ClientDCInternalInv.java @@ -68,4 +68,4 @@ public class ClientDCInternalInv implements Comparable { return this.id; } -} \ No newline at end of file +} diff --git a/src/main/java/appeng/container/implementations/CraftingCPURecord.java b/src/main/java/appeng/container/implementations/CraftingCPURecord.java index 900045e78..538210287 100644 --- a/src/main/java/appeng/container/implementations/CraftingCPURecord.java +++ b/src/main/java/appeng/container/implementations/CraftingCPURecord.java @@ -70,4 +70,4 @@ public class CraftingCPURecord implements Comparable { return this.size; } -} \ No newline at end of file +} diff --git a/src/main/java/appeng/util/ItemSorters.java b/src/main/java/appeng/util/ItemSorters.java index c683e8e9f..7fc4eb92d 100644 --- a/src/main/java/appeng/util/ItemSorters.java +++ b/src/main/java/appeng/util/ItemSorters.java @@ -39,13 +39,11 @@ public class ItemSorters @Override public int compare( final IAEItemStack o1, final IAEItemStack o2 ) { - if( getDirection() == SortDir.ASCENDING ) - { - return Platform.getItemDisplayName( o1 ).compareToIgnoreCase( Platform.getItemDisplayName( o2 ) ); - } - return Platform.getItemDisplayName( o2 ).compareToIgnoreCase( Platform.getItemDisplayName( o1 ) ); + final int cmp = Platform.getItemDisplayName( o1 ).compareToIgnoreCase( Platform.getItemDisplayName( o2 ) ); + return applyDirection( cmp ); } }; + public static final Comparator CONFIG_BASED_SORT_BY_MOD = new Comparator() { @@ -54,38 +52,30 @@ public class ItemSorters { final AEItemStack op1 = (AEItemStack) o1; final AEItemStack op2 = (AEItemStack) o2; + int cmp = op1.getModID().compareToIgnoreCase( op2.getModID() ); - if( getDirection() == SortDir.ASCENDING ) + if( cmp == 0 ) { - return this.secondarySort( op2.getModID().compareToIgnoreCase( op1.getModID() ), o1, o2 ); - } - return this.secondarySort( op1.getModID().compareToIgnoreCase( op2.getModID() ), o2, o1 ); - } - - private int secondarySort( final int compareToIgnoreCase, final IAEItemStack o1, final IAEItemStack o2 ) - { - if( compareToIgnoreCase == 0 ) - { - return Platform.getItemDisplayName( o2 ).compareToIgnoreCase( Platform.getItemDisplayName( o1 ) ); + cmp = Platform.getItemDisplayName( o1 ).compareToIgnoreCase( Platform.getItemDisplayName( o2 ) ); } - return compareToIgnoreCase; + return applyDirection( cmp ); } }; + public static final Comparator CONFIG_BASED_SORT_BY_SIZE = new Comparator() { @Override public int compare( final IAEItemStack o1, final IAEItemStack o2 ) { - if( getDirection() == SortDir.ASCENDING ) - { - return Long.compare( o2.getStackSize(), o1.getStackSize() ); - } - return Long.compare( o1.getStackSize(), o2.getStackSize() ); + final int cmp = Long.compare( o2.getStackSize(), o1.getStackSize() ); + return applyDirection( cmp ); } }; + private static IInvTweaks api; + public static final Comparator CONFIG_BASED_SORT_BY_INV_TWEAKS = new Comparator() { @@ -98,12 +88,7 @@ public class ItemSorters } final int cmp = api.compareItems( o1.asItemStackRepresentation(), o2.asItemStackRepresentation() ); - - if( getDirection() == SortDir.ASCENDING ) - { - return cmp; - } - return -cmp; + return applyDirection( cmp ); } }; @@ -133,4 +118,13 @@ public class ItemSorters { Direction = direction; } + + private static int applyDirection( int cmp ) + { + if ( getDirection() == SortDir.ASCENDING ) + { + return cmp; + } + return -cmp; + } }