This commit is contained in:
Raoul Van den Berge
2015-12-22 12:39:03 +01:00
parent da89f6778e
commit 3b143310e3
76 changed files with 1730 additions and 847 deletions

View File

@@ -9,7 +9,8 @@ import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;
import net.minecraftforge.common.util.Constants;
public class InventoryUtils {
public class InventoryUtils
{
public static final String NBT_INVENTORY = "Inventory";
public static final String NBT_SLOT = "Slot";
@@ -17,11 +18,14 @@ public class InventoryUtils {
public static final int COMPARE_NBT = 2;
public static final int COMPARE_QUANTITY = 4;
public static void saveInventory(IInventory inventory, NBTTagCompound nbt) {
public static void saveInventory(IInventory inventory, NBTTagCompound nbt)
{
NBTTagList tagList = new NBTTagList();
for (int i = 0; i < inventory.getSizeInventory(); i++) {
if (inventory.getStackInSlot(i) != null) {
for (int i = 0; i < inventory.getSizeInventory(); i++)
{
if (inventory.getStackInSlot(i) != null)
{
NBTTagCompound compoundTag = new NBTTagCompound();
compoundTag.setInteger(NBT_SLOT, i);
@@ -35,11 +39,14 @@ public class InventoryUtils {
nbt.setTag(NBT_INVENTORY, tagList);
}
public static void restoreInventory(IInventory inventory, NBTTagCompound nbt) {
if (nbt.hasKey(NBT_INVENTORY)) {
public static void restoreInventory(IInventory inventory, NBTTagCompound nbt)
{
if (nbt.hasKey(NBT_INVENTORY))
{
NBTTagList tagList = nbt.getTagList(NBT_INVENTORY, Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < tagList.tagCount(); i++) {
for (int i = 0; i < tagList.tagCount(); i++)
{
int slot = tagList.getCompoundTagAt(i).getInteger(NBT_SLOT);
ItemStack stack = ItemStack.loadItemStackFromNBT(tagList.getCompoundTagAt(i));
@@ -50,13 +57,16 @@ public class InventoryUtils {
}
// https://github.com/cpw/ironchest/blob/master/src/main/java/cpw/mods/ironchest/BlockIronChest.java#L200
public static void dropInventory(World world, IInventory inventory, int x, int y, int z) {
public static void dropInventory(World world, IInventory inventory, int x, int y, int z)
{
Random random = world.rand;
for (int i = 0; i < inventory.getSizeInventory(); ++i) {
for (int i = 0; i < inventory.getSizeInventory(); ++i)
{
ItemStack stack = inventory.getStackInSlot(i);
if (stack == null) {
if (stack == null)
{
continue;
}
@@ -64,10 +74,12 @@ public class InventoryUtils {
float yo = random.nextFloat() * 0.8F + 0.1F;
float zo = random.nextFloat() * 0.8F + 0.1F;
while (stack.stackSize > 0) {
while (stack.stackSize > 0)
{
int amount = random.nextInt(21) + 10;
if (amount > stack.stackSize) {
if (amount > stack.stackSize)
{
amount = stack.stackSize;
}
@@ -79,7 +91,8 @@ public class InventoryUtils {
entity.motionY = (float) random.nextGaussian() * 0.05F + 0.2F;
entity.motionZ = (float) random.nextGaussian() * 0.05F;
if (stack.hasTagCompound()) {
if (stack.hasTagCompound())
{
entity.getEntityItem().setTagCompound((NBTTagCompound) stack.getTagCompound().copy());
}
@@ -88,44 +101,57 @@ public class InventoryUtils {
}
}
public static void pushToInventorySlot(IInventory inventory, int i, ItemStack stack) {
public static void pushToInventorySlot(IInventory inventory, int i, ItemStack stack)
{
ItemStack slot = inventory.getStackInSlot(i);
if (slot == null) {
if (slot == null)
{
inventory.setInventorySlotContents(i, stack);
} else if (compareStackNoQuantity(slot, stack)) {
}
else if (compareStackNoQuantity(slot, stack))
{
slot.stackSize += stack.stackSize;
}
}
public static boolean canPushToInventorySlot(IInventory inventory, int i, ItemStack stack) {
public static boolean canPushToInventorySlot(IInventory inventory, int i, ItemStack stack)
{
ItemStack slot = inventory.getStackInSlot(i);
if (slot == null) {
if (slot == null)
{
return true;
}
if (!compareStackNoQuantity(slot, stack)) {
if (!compareStackNoQuantity(slot, stack))
{
return false;
}
return slot.stackSize + stack.stackSize < slot.getMaxStackSize();
}
public static void pushToInventory(IInventory inventory, ItemStack stack) {
public static void pushToInventory(IInventory inventory, ItemStack stack)
{
int toGo = stack.stackSize;
for (int i = 0; i < inventory.getSizeInventory(); ++i) {
for (int i = 0; i < inventory.getSizeInventory(); ++i)
{
ItemStack slot = inventory.getStackInSlot(i);
if (slot == null) {
if (slot == null)
{
inventory.setInventorySlotContents(i, stack);
return;
} else if (compareStackNoQuantity(slot, stack)) {
}
else if (compareStackNoQuantity(slot, stack))
{
int toAdd = toGo;
if (slot.stackSize + toAdd > slot.getMaxStackSize()) {
if (slot.stackSize + toAdd > slot.getMaxStackSize())
{
toAdd = slot.getMaxStackSize() - slot.stackSize;
}
@@ -133,31 +159,39 @@ public class InventoryUtils {
toGo -= toAdd;
if (toGo == 0) {
if (toGo == 0)
{
return;
}
}
}
}
public static boolean canPushToInventory(IInventory inventory, ItemStack stack) {
public static boolean canPushToInventory(IInventory inventory, ItemStack stack)
{
int toGo = stack.stackSize;
for (int i = 0; i < inventory.getSizeInventory(); ++i) {
for (int i = 0; i < inventory.getSizeInventory(); ++i)
{
ItemStack slot = inventory.getStackInSlot(i);
if (slot == null) {
if (slot == null)
{
return true;
} else if (compareStackNoQuantity(slot, stack)) {
}
else if (compareStackNoQuantity(slot, stack))
{
int toAdd = toGo;
if (slot.stackSize + toAdd > slot.getMaxStackSize()) {
if (slot.stackSize + toAdd > slot.getMaxStackSize())
{
toAdd = slot.getMaxStackSize() - slot.stackSize;
}
toGo -= toAdd;
if (toGo == 0) {
if (toGo == 0)
{
break;
}
}
@@ -166,25 +200,33 @@ public class InventoryUtils {
return toGo == 0;
}
public static boolean compareStack(ItemStack first, ItemStack second) {
public static boolean compareStack(ItemStack first, ItemStack second)
{
return compareStack(first, second, COMPARE_NBT | COMPARE_DAMAGE | COMPARE_QUANTITY);
}
public static boolean compareStack(ItemStack first, ItemStack second, int flags) {
if ((flags & COMPARE_DAMAGE) == COMPARE_DAMAGE) {
if (first.getItemDamage() != second.getItemDamage()) {
public static boolean compareStack(ItemStack first, ItemStack second, int flags)
{
if ((flags & COMPARE_DAMAGE) == COMPARE_DAMAGE)
{
if (first.getItemDamage() != second.getItemDamage())
{
return false;
}
}
if ((flags & COMPARE_NBT) == COMPARE_NBT) {
if (first.stackTagCompound != null && !first.stackTagCompound.equals(second.stackTagCompound)) {
if ((flags & COMPARE_NBT) == COMPARE_NBT)
{
if (first.stackTagCompound != null && !first.stackTagCompound.equals(second.stackTagCompound))
{
return false;
}
}
if ((flags & COMPARE_QUANTITY) == COMPARE_QUANTITY) {
if (first.stackSize != second.stackSize) {
if ((flags & COMPARE_QUANTITY) == COMPARE_QUANTITY)
{
if (first.stackSize != second.stackSize)
{
return false;
}
}
@@ -192,7 +234,8 @@ public class InventoryUtils {
return first.getItem() == second.getItem();
}
public static boolean compareStackNoQuantity(ItemStack first, ItemStack second) {
public static boolean compareStackNoQuantity(ItemStack first, ItemStack second)
{
return compareStack(first, second, COMPARE_NBT | COMPARE_DAMAGE);
}
}