buildcraft/api/buildcraft/api/core/StackKey.java

135 lines
3.5 KiB
Java
Raw Normal View History

2014-02-15 09:21:40 +01:00
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
2013-07-14 21:09:54 +02:00
* http://www.mod-buildcraft.com
2014-02-15 09:21:40 +01:00
*
* The BuildCraft API is distributed under the terms of the MIT License.
* Please check the contents of the license, which should be located
* as "LICENSE.API" in the BuildCraft source code distribution.
2013-07-14 21:09:54 +02:00
*/
package buildcraft.api.core;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
2013-07-14 21:09:54 +02:00
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidStack;
2013-07-14 21:09:54 +02:00
/**
* This class is used whenever stacks needs to be stored as keys.
*/
public final class StackKey {
2013-07-14 21:09:54 +02:00
public final ItemStack stack;
public final FluidStack fluidStack;
public StackKey(FluidStack fluidStack) {
this(null, fluidStack);
}
2013-07-14 21:09:54 +02:00
public StackKey(ItemStack stack) {
this(stack, null);
}
public StackKey(ItemStack stack, FluidStack fluidStack) {
2013-07-14 21:09:54 +02:00
this.stack = stack;
this.fluidStack = fluidStack;
2013-07-14 21:09:54 +02:00
}
public static StackKey stack(Item item, int amount, int damage) {
return new StackKey(new ItemStack(item, amount, damage));
}
public static StackKey stack(Block block, int amount, int damage) {
return new StackKey(new ItemStack(block, amount, damage));
}
public static StackKey stack(Item item) {
return new StackKey(new ItemStack(item, 1, 0));
}
public static StackKey stack(Block block) {
return new StackKey(new ItemStack(block, 1, 0));
}
public static StackKey stack(ItemStack itemStack) {
return new StackKey(itemStack);
}
public static StackKey fluid(Fluid fluid, int amount) {
return new StackKey(new FluidStack(fluid, amount));
}
public static StackKey fluid(Fluid fluid) {
return new StackKey(new FluidStack(fluid, FluidContainerRegistry.BUCKET_VOLUME));
}
public static StackKey fluid(FluidStack fluidStack) {
return new StackKey(fluidStack);
2013-07-14 21:09:54 +02:00
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || o.getClass() != StackKey.class) {
2013-07-14 21:09:54 +02:00
return false;
}
StackKey k = (StackKey) o;
if ((stack == null ^ k.stack == null) || (fluidStack == null ^ k.fluidStack == null)) {
2013-07-14 21:09:54 +02:00
return false;
}
if (stack != null) {
if (stack.getItem() != k.stack.getItem() ||
stack.getHasSubtypes() && stack.getItemDamage() != k.stack.getItemDamage() ||
2014-10-24 20:07:14 +02:00
!objectsEqual(stack.getTagCompound(), k.stack.getTagCompound())) {
return false;
}
}
if (fluidStack != null) {
if (fluidStack.fluidID != k.fluidStack.fluidID ||
fluidStack.amount != k.fluidStack.amount ||
2014-10-24 20:07:14 +02:00
!objectsEqual(fluidStack.tag, k.fluidStack.tag)) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
int result = 7;
if (stack != null) {
result = 31 * result + stack.getItem().hashCode();
result = 31 * result + stack.getItemDamage();
2014-10-24 20:07:14 +02:00
result = 31 * result + objectHashCode(stack.getTagCompound());
}
result = 31 * result + 7;
if (fluidStack != null) {
result = 31 * result + fluidStack.fluidID;
result = 31 * result + fluidStack.amount;
2014-10-24 20:07:14 +02:00
result = 31 * result + objectHashCode(fluidStack.tag);
}
return result;
}
2014-10-24 20:07:14 +02:00
private boolean objectsEqual(Object o1, Object o2) {
if (o1 == null && o2 == null) {
return true;
} else if (o1 == null || o2 == null) {
return false;
} else {
return o1.equals(o2);
}
}
private int objectHashCode(Object o) {
return o != null ? o.hashCode() : 0;
}
public StackKey copy() {
return new StackKey(stack != null ? stack.copy() : null,
fluidStack != null ? fluidStack.copy() : null);
2013-07-14 21:09:54 +02:00
}
}