Skip to content

Instantly share code, notes, and snippets.

@dazsim
Created July 2, 2016 13:03
Show Gist options
  • Save dazsim/79db172ab8183ac331e0209c166a4dfd to your computer and use it in GitHub Desktop.
Save dazsim/79db172ab8183ac331e0209c166a4dfd to your computer and use it in GitHub Desktop.
complete class for the BarrelItemHandler.java
package com.workshopcraft.simplebarrels.handlers;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandler;
public class BarrelItemHandler implements IItemHandler{
public ItemStack[] barrelContents = new ItemStack[1];
int count = 0;
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)
{
return null;
}
@Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate)
{
if (count == 0)
{
this.barrelContents[0]=stack;
if (!simulate)
{
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