pick 97420a31d The big reformat of 2020
This commit is contained in:
@@ -18,7 +18,6 @@
|
||||
|
||||
package appeng.util.helpers;
|
||||
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import net.minecraft.item.Item;
|
||||
@@ -27,81 +26,70 @@ import net.minecraft.nbt.CompoundNBT;
|
||||
|
||||
import appeng.api.config.FuzzyMode;
|
||||
|
||||
|
||||
/**
|
||||
* A helper class for comparing {@link Item}, {@link ItemStack} or NBT
|
||||
*
|
||||
*/
|
||||
public class ItemComparisonHelper
|
||||
{
|
||||
public class ItemComparisonHelper {
|
||||
|
||||
/**
|
||||
* Compare the two {@link ItemStack}s based on the same {@link Item} and damage value.
|
||||
*
|
||||
* In case of the item being damageable, only the {@link Item} will be considered.
|
||||
* If not it will also compare both damage values.
|
||||
*
|
||||
* Ignores NBT.
|
||||
*
|
||||
* @return true, if both are equal.
|
||||
*/
|
||||
public boolean isEqualItemType( @Nonnull final ItemStack that, @Nonnull final ItemStack other )
|
||||
{
|
||||
return !that.isEmpty() && !other.isEmpty() && that.getItem() == other.getItem();
|
||||
}
|
||||
/**
|
||||
* Compare the two {@link ItemStack}s based on the same {@link Item} and damage
|
||||
* value.
|
||||
*
|
||||
* In case of the item being damageable, only the {@link Item} will be
|
||||
* considered. If not it will also compare both damage values.
|
||||
*
|
||||
* Ignores NBT.
|
||||
*
|
||||
* @return true, if both are equal.
|
||||
*/
|
||||
public boolean isEqualItemType(@Nonnull final ItemStack that, @Nonnull final ItemStack other) {
|
||||
return !that.isEmpty() && !other.isEmpty() && that.getItem() == other.getItem();
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares two {@link ItemStack} and their NBT tag for equality.
|
||||
*
|
||||
* Use this when a precise check is required and the same item is required.
|
||||
* Not just something with different NBT tags.
|
||||
*
|
||||
* @return true, if both are identical.
|
||||
*/
|
||||
public boolean isSameItem( @Nonnull final ItemStack is, @Nonnull final ItemStack filter )
|
||||
{
|
||||
return ItemStack.areItemsEqual( is, filter ) && this.isNbtTagEqual( is.getTag(), filter.getTag() );
|
||||
}
|
||||
/**
|
||||
* Compares two {@link ItemStack} and their NBT tag for equality.
|
||||
*
|
||||
* Use this when a precise check is required and the same item is required. Not
|
||||
* just something with different NBT tags.
|
||||
*
|
||||
* @return true, if both are identical.
|
||||
*/
|
||||
public boolean isSameItem(@Nonnull final ItemStack is, @Nonnull final ItemStack filter) {
|
||||
return ItemStack.areItemsEqual(is, filter) && this.isNbtTagEqual(is.getTag(), filter.getTag());
|
||||
}
|
||||
|
||||
/**
|
||||
* Similar to {@link ItemComparisonHelper#isEqualItem(ItemStack, ItemStack)},
|
||||
* but it can further check, if both match the same {@link FuzzyMode}
|
||||
* or are considered equal by the {@link OreDictionary}
|
||||
*
|
||||
* @param mode how to compare the two {@link ItemStack}s
|
||||
* @return true, if both are matching the mode or considered equal by the {@link OreDictionary}
|
||||
*/
|
||||
public boolean isFuzzyEqualItem( final ItemStack a, final ItemStack b, final FuzzyMode mode )
|
||||
{
|
||||
if( a.isEmpty() && b.isEmpty() )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Similar to {@link ItemComparisonHelper#isEqualItem(ItemStack, ItemStack)},
|
||||
* but it can further check, if both match the same {@link FuzzyMode} or are
|
||||
* considered equal by the {@link OreDictionary}
|
||||
*
|
||||
* @param mode how to compare the two {@link ItemStack}s
|
||||
* @return true, if both are matching the mode or considered equal by the
|
||||
* {@link OreDictionary}
|
||||
*/
|
||||
public boolean isFuzzyEqualItem(final ItemStack a, final ItemStack b, final FuzzyMode mode) {
|
||||
if (a.isEmpty() && b.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if( a.isEmpty() || b.isEmpty() )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (a.isEmpty() || b.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// test damageable items..
|
||||
if( a.getItem() == b.getItem() && a.getItem().isDamageable() )
|
||||
{
|
||||
if( mode == FuzzyMode.IGNORE_ALL )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if( mode == FuzzyMode.PERCENT_99 )
|
||||
{
|
||||
return ( a.getDamage() > 1 ) == ( b.getDamage() > 1 );
|
||||
}
|
||||
else
|
||||
{
|
||||
final float percentDamagedOfA = (float) a.getDamage() / a.getMaxDamage();
|
||||
final float percentDamagedOfB = (float) b.getDamage() / b.getMaxDamage();
|
||||
// test damageable items..
|
||||
if (a.getItem() == b.getItem() && a.getItem().isDamageable()) {
|
||||
if (mode == FuzzyMode.IGNORE_ALL) {
|
||||
return true;
|
||||
} else if (mode == FuzzyMode.PERCENT_99) {
|
||||
return (a.getDamage() > 1) == (b.getDamage() > 1);
|
||||
} else {
|
||||
final float percentDamagedOfA = (float) a.getDamage() / a.getMaxDamage();
|
||||
final float percentDamagedOfB = (float) b.getDamage() / b.getMaxDamage();
|
||||
|
||||
return ( percentDamagedOfA > mode.breakPoint ) == ( percentDamagedOfB > mode.breakPoint );
|
||||
}
|
||||
}
|
||||
return (percentDamagedOfA > mode.breakPoint) == (percentDamagedOfB > mode.breakPoint);
|
||||
}
|
||||
}
|
||||
// FIXME
|
||||
// final OreReference aOR = OreHelper.INSTANCE.getOre( a ).orElse( null );
|
||||
// final OreReference bOR = OreHelper.INSTANCE.getOre( b ).orElse( null );
|
||||
@@ -111,39 +99,36 @@ public class ItemComparisonHelper
|
||||
// return true;
|
||||
// }
|
||||
|
||||
return a.isItemEqual( b );
|
||||
}
|
||||
return a.isItemEqual(b);
|
||||
}
|
||||
|
||||
/**
|
||||
* recursive test for NBT Equality, this was faster then trying to compare / generate hashes, its also more reliable
|
||||
* then the vanilla version which likes to fail when NBT Compound data changes order, it is pretty expensive
|
||||
* performance wise, so try an use shared tag compounds as long as the system remains in AE.
|
||||
*/
|
||||
public boolean isNbtTagEqual( final CompoundNBT left, final CompoundNBT right )
|
||||
{
|
||||
if( left == right )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* recursive test for NBT Equality, this was faster then trying to compare /
|
||||
* generate hashes, its also more reliable then the vanilla version which likes
|
||||
* to fail when NBT Compound data changes order, it is pretty expensive
|
||||
* performance wise, so try an use shared tag compounds as long as the system
|
||||
* remains in AE.
|
||||
*/
|
||||
public boolean isNbtTagEqual(final CompoundNBT left, final CompoundNBT right) {
|
||||
if (left == right) {
|
||||
return true;
|
||||
}
|
||||
|
||||
final boolean isLeftEmpty = left == null || left.isEmpty();
|
||||
final boolean isRightEmpty = right == null || right.isEmpty();
|
||||
final boolean isLeftEmpty = left == null || left.isEmpty();
|
||||
final boolean isRightEmpty = right == null || right.isEmpty();
|
||||
|
||||
if( isLeftEmpty && isRightEmpty )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (isLeftEmpty && isRightEmpty) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if( isLeftEmpty != isRightEmpty )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (isLeftEmpty != isRightEmpty) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if( left != null )
|
||||
{
|
||||
return left.equals( right );
|
||||
}
|
||||
if (left != null) {
|
||||
return left.equals(right);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,65 +18,48 @@
|
||||
|
||||
package appeng.util.helpers;
|
||||
|
||||
|
||||
import net.minecraft.inventory.CraftingInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
import net.minecraftforge.items.IItemHandlerModifiable;
|
||||
|
||||
public class ItemHandlerUtil {
|
||||
private ItemHandlerUtil() {
|
||||
}
|
||||
|
||||
public class ItemHandlerUtil
|
||||
{
|
||||
private ItemHandlerUtil()
|
||||
{
|
||||
}
|
||||
public static void setStackInSlot(final IItemHandler inv, final int slot, final ItemStack stack) {
|
||||
if (inv instanceof IItemHandlerModifiable) {
|
||||
((IItemHandlerModifiable) inv).setStackInSlot(slot, stack);
|
||||
} else {
|
||||
inv.extractItem(slot, Integer.MAX_VALUE, false);
|
||||
inv.insertItem(slot, stack, false);
|
||||
}
|
||||
}
|
||||
|
||||
public static void setStackInSlot( final IItemHandler inv, final int slot, final ItemStack stack )
|
||||
{
|
||||
if( inv instanceof IItemHandlerModifiable )
|
||||
{
|
||||
( (IItemHandlerModifiable) inv ).setStackInSlot( slot, stack );
|
||||
}
|
||||
else
|
||||
{
|
||||
inv.extractItem( slot, Integer.MAX_VALUE, false );
|
||||
inv.insertItem( slot, stack, false );
|
||||
}
|
||||
}
|
||||
public static void clear(final IItemHandler inv) {
|
||||
for (int x = 0; x < inv.getSlots(); x++) {
|
||||
setStackInSlot(inv, x, ItemStack.EMPTY);
|
||||
}
|
||||
}
|
||||
|
||||
public static void clear( final IItemHandler inv )
|
||||
{
|
||||
for( int x = 0; x < inv.getSlots(); x++ )
|
||||
{
|
||||
setStackInSlot( inv, x, ItemStack.EMPTY );
|
||||
}
|
||||
}
|
||||
public static boolean isEmpty(final IItemHandler inv) {
|
||||
for (int x = 0; x < inv.getSlots(); x++) {
|
||||
if (!inv.getStackInSlot(x).isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isEmpty( final IItemHandler inv )
|
||||
{
|
||||
for( int x = 0; x < inv.getSlots(); x++ )
|
||||
{
|
||||
if( !inv.getStackInSlot( x ).isEmpty() )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public static void copy(final IItemHandler from, final IItemHandler to, boolean deepCopy) {
|
||||
for (int i = 0; i < Math.min(from.getSlots(), to.getSlots()); ++i) {
|
||||
setStackInSlot(to, i, deepCopy ? from.getStackInSlot(i).copy() : from.getStackInSlot(i));
|
||||
}
|
||||
}
|
||||
|
||||
public static void copy( final IItemHandler from, final IItemHandler to, boolean deepCopy )
|
||||
{
|
||||
for( int i = 0; i < Math.min( from.getSlots(), to.getSlots() ); ++i )
|
||||
{
|
||||
setStackInSlot( to, i, deepCopy ? from.getStackInSlot( i ).copy() : from.getStackInSlot( i ) );
|
||||
}
|
||||
}
|
||||
|
||||
public static void copy( final CraftingInventory from, final IItemHandler to, boolean deepCopy )
|
||||
{
|
||||
for( int i = 0; i < Math.min( from.getSizeInventory(), to.getSlots() ); ++i )
|
||||
{
|
||||
setStackInSlot( to, i, deepCopy ? from.getStackInSlot( i ).copy() : from.getStackInSlot( i ) );
|
||||
}
|
||||
}
|
||||
public static void copy(final CraftingInventory from, final IItemHandler to, boolean deepCopy) {
|
||||
for (int i = 0; i < Math.min(from.getSizeInventory(), to.getSlots()); ++i) {
|
||||
setStackInSlot(to, i, deepCopy ? from.getStackInSlot(i).copy() : from.getStackInSlot(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,53 +18,44 @@
|
||||
|
||||
package appeng.util.helpers;
|
||||
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
import appeng.api.util.AEColor;
|
||||
|
||||
public class P2PHelper {
|
||||
|
||||
public class P2PHelper
|
||||
{
|
||||
public AEColor[] toColors(short frequency) {
|
||||
final AEColor[] colors = new AEColor[4];
|
||||
|
||||
public AEColor[] toColors( short frequency )
|
||||
{
|
||||
final AEColor[] colors = new AEColor[4];
|
||||
for (int i = 0; i < 4; i++) {
|
||||
int nibble = (frequency >> 4 * (3 - i)) & 0xF;
|
||||
|
||||
for( int i = 0; i < 4; i++ )
|
||||
{
|
||||
int nibble = ( frequency >> 4 * ( 3 - i ) ) & 0xF;
|
||||
colors[i] = AEColor.values()[nibble];
|
||||
}
|
||||
|
||||
colors[i] = AEColor.values()[nibble];
|
||||
}
|
||||
return colors;
|
||||
}
|
||||
|
||||
return colors;
|
||||
}
|
||||
public short fromColors(AEColor[] colors) {
|
||||
Preconditions.checkArgument(colors.length == 4);
|
||||
|
||||
public short fromColors( AEColor[] colors )
|
||||
{
|
||||
Preconditions.checkArgument( colors.length == 4 );
|
||||
int t = 0;
|
||||
|
||||
int t = 0;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
int code = colors[3 - i].ordinal() << 4 * i;
|
||||
|
||||
for( int i = 0; i < 4; i++ )
|
||||
{
|
||||
int code = colors[3 - i].ordinal() << 4 * i;
|
||||
t |= code;
|
||||
}
|
||||
|
||||
t |= code;
|
||||
}
|
||||
return (short) (t & 0xFFFF);
|
||||
}
|
||||
|
||||
return (short) ( t & 0xFFFF );
|
||||
}
|
||||
public String toHexDigit(AEColor color) {
|
||||
return String.format("%01X", color.ordinal());
|
||||
}
|
||||
|
||||
public String toHexDigit( AEColor color )
|
||||
{
|
||||
return String.format( "%01X", color.ordinal() );
|
||||
}
|
||||
|
||||
public String toHexString( short frequency )
|
||||
{
|
||||
return String.format( "%04X", frequency );
|
||||
}
|
||||
public String toHexString(short frequency) {
|
||||
return String.format("%04X", frequency);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user