Skip to content

Instantly share code, notes, and snippets.

@dazsim
Created July 5, 2016 07:09
Show Gist options
  • Save dazsim/ce57a27400490248cfdf1119ce82f83a to your computer and use it in GitHub Desktop.
Save dazsim/ce57a27400490248cfdf1119ce82f83a to your computer and use it in GitHub Desktop.
package com.workshopcraft.simplebarrels.handlers;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper;
public class BarrelItemHandler implements IItemHandler{
public ItemStack[] barrelContents = new ItemStack[1];
public int count = 0;
public int size = 4096;
@Override
public int getSlots()
{
int a = size;
if (barrelContents[0]!=null)
{
a =(int) (a / this.barrelContents[0].getMaxStackSize());
}
else
{
a = a / 64;
}
return a;
}
@Override
public ItemStack getStackInSlot(int slot)
{
if (slot < (int)(count/this.barrelContents[0].getMaxStackSize()))
{
ItemStack i = new ItemStack(this.barrelContents[0].getItem(),64,this.barrelContents[0].getItemDamage());
i.setTagCompound(barrelContents[0].getTagCompound());
return i;
} else if (slot == (int)(count/this.barrelContents[0].getMaxStackSize()))
{
int j = (int)(count%this.barrelContents[0].getMaxStackSize());
ItemStack i = new ItemStack(this.barrelContents[0].getItem(),j,this.barrelContents[0].getItemDamage());
i.setTagCompound(barrelContents[0].getTagCompound());
return i;
} else
{
return null;
}
}
@Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate)
{
if (count == 0)
{
if (!simulate)
{
this.barrelContents[0]=stack;
count = stack.stackSize;
}
return null;
}
if (count == size)
{
//barrel is full
return stack;
}
if (this.barrelContents[0].isItemEqual(stack))
{
if (stack.stackSize+count<=size)
{
//input stack will not fill barrel
if (!simulate)
{
count+=stack.stackSize;
}
return null;
} else if (stack.stackSize+count>size)
{
//input stack will fill barrel
if (!simulate)
{
int a = size - count;
count = size;
stack.stackSize -= a;
}
return stack;
}
}
return stack;
}
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate) {
// we ignore slot as internal to the barrel we dont actually care
int a = 0;
if (count == 0)
{
return null;
}
else
{
if (simulate)
{
if (count >= amount)
{
a = amount;
} else
{
a = count;
}
ItemStack i = new ItemStack(this.barrelContents[0].getItem(),a,this.barrelContents[0].getItemDamage());
i.setTagCompound(barrelContents[0].getTagCompound());
return i;
} else
{
if (count >= amount)
{
a = amount;
} else
{
a = count;
}
ItemStack i = new ItemStack(this.barrelContents[0].getItem(),a,this.barrelContents[0].getItemDamage());
i.setTagCompound(barrelContents[0].getTagCompound());
count -= a;
if (count == 0)
{
this.barrelContents[0]=null;
}
return i;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment