2014-11-14 12:02:52 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Applied Energistics 2.
|
2015-05-16 20:48:32 +02:00
|
|
|
* Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
|
2014-11-14 12:02:52 +01:00
|
|
|
*
|
|
|
|
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
|
|
|
*/
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
package appeng.util;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.config.SortDir;
|
|
|
|
import appeng.api.storage.data.IAEItemStack;
|
2015-05-16 20:48:32 +02:00
|
|
|
import appeng.integration.IntegrationRegistry;
|
2014-07-24 00:26:23 +02:00
|
|
|
import appeng.integration.IntegrationType;
|
2014-02-27 08:10:11 +01:00
|
|
|
import appeng.integration.abstraction.IInvTweaks;
|
2014-08-06 06:30:59 +02:00
|
|
|
import appeng.util.item.AEItemStack;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2017-04-12 16:10:13 +02:00
|
|
|
import java.util.Comparator;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
public class ItemSorters
|
|
|
|
{
|
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
private static SortDir Direction = SortDir.ASCENDING;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
public static final Comparator<IAEItemStack> CONFIG_BASED_SORT_BY_NAME = new Comparator<IAEItemStack>()
|
2014-02-21 07:10:38 +01:00
|
|
|
{
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public int compare( final IAEItemStack o1, final IAEItemStack o2 )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-10-08 15:42:42 +02:00
|
|
|
if( getDirection() == SortDir.ASCENDING )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-02-06 05:47:52 +01:00
|
|
|
return Platform.getItemDisplayName( o1 ).compareToIgnoreCase( Platform.getItemDisplayName( o2 ) );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-02-06 05:47:52 +01:00
|
|
|
return Platform.getItemDisplayName( o2 ).compareToIgnoreCase( Platform.getItemDisplayName( o1 ) );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
};
|
2015-04-03 08:54:31 +02:00
|
|
|
public static final Comparator<IAEItemStack> CONFIG_BASED_SORT_BY_MOD = new Comparator<IAEItemStack>()
|
|
|
|
{
|
2014-08-06 06:30:59 +02:00
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public int compare( final IAEItemStack o1, final IAEItemStack o2 )
|
2014-08-06 06:30:59 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final AEItemStack op1 = (AEItemStack) o1;
|
|
|
|
final AEItemStack op2 = (AEItemStack) o2;
|
2014-08-06 06:30:59 +02:00
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
if( getDirection() == SortDir.ASCENDING )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.secondarySort( op2.getModID().compareToIgnoreCase( op1.getModID() ), o1, o2 );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.secondarySort( op1.getModID().compareToIgnoreCase( op2.getModID() ), o2, o1 );
|
2014-08-06 06:30:59 +02:00
|
|
|
}
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
private int secondarySort( final int compareToIgnoreCase, final IAEItemStack o1, final IAEItemStack o2 )
|
2014-08-06 06:30:59 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( compareToIgnoreCase == 0 )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-08-06 06:30:59 +02:00
|
|
|
return Platform.getItemDisplayName( o2 ).compareToIgnoreCase( Platform.getItemDisplayName( o1 ) );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-08-06 06:30:59 +02:00
|
|
|
|
|
|
|
return compareToIgnoreCase;
|
|
|
|
}
|
|
|
|
};
|
2015-04-03 08:54:31 +02:00
|
|
|
public static final Comparator<IAEItemStack> CONFIG_BASED_SORT_BY_SIZE = new Comparator<IAEItemStack>()
|
|
|
|
{
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public int compare( final IAEItemStack o1, final IAEItemStack o2 )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-10-08 15:42:42 +02:00
|
|
|
if( getDirection() == SortDir.ASCENDING )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2013-12-27 23:59:59 +01:00
|
|
|
return compareLong( o2.getStackSize(), o1.getStackSize() );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
return compareLong( o1.getStackSize(), o2.getStackSize() );
|
|
|
|
}
|
|
|
|
};
|
2015-04-03 08:54:31 +02:00
|
|
|
private static IInvTweaks api;
|
|
|
|
public static final Comparator<IAEItemStack> CONFIG_BASED_SORT_BY_INV_TWEAKS = new Comparator<IAEItemStack>()
|
|
|
|
{
|
2014-02-27 08:10:11 +01:00
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public int compare( final IAEItemStack o1, final IAEItemStack o2 )
|
2014-02-27 08:10:11 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( api == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-01-01 22:13:10 +01:00
|
|
|
return CONFIG_BASED_SORT_BY_NAME.compare( o1, o2 );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-02-27 08:10:11 +01:00
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
final int cmp = api.compareItems( o1.getItemStack(), o2.getItemStack() );
|
2014-02-27 08:10:11 +01:00
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
if( getDirection() == SortDir.ASCENDING )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-02-27 08:10:11 +01:00
|
|
|
return cmp;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-02-27 08:10:11 +01:00
|
|
|
return -cmp;
|
|
|
|
}
|
|
|
|
};
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
public static void init()
|
|
|
|
{
|
|
|
|
if( api != null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2015-05-16 20:48:32 +02:00
|
|
|
if( IntegrationRegistry.INSTANCE.isEnabled( IntegrationType.InvTweaks ) )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-05-16 20:48:32 +02:00
|
|
|
api = (IInvTweaks) IntegrationRegistry.INSTANCE.getInstance( IntegrationType.InvTweaks );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
else
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
api = null;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
}
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
public static int compareInt( final int a, final int b )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
|
|
|
if( a == b )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return 0;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
if( a < b )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return -1;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
public static int compareLong( final long a, final long b )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
|
|
|
if( a == b )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return 0;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
if( a < b )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return -1;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
public static int compareDouble( final double a, final double b )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
|
|
|
if( a == b )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return 0;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
if( a < b )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return -1;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2015-10-08 15:42:42 +02:00
|
|
|
|
|
|
|
private static SortDir getDirection()
|
|
|
|
{
|
|
|
|
return Direction;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setDirection( final SortDir direction )
|
|
|
|
{
|
|
|
|
Direction = direction;
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|