Skip to content

Instantly share code, notes, and snippets.

@Daomephsta
Last active September 18, 2016 01:18
Show Gist options
  • Save Daomephsta/8a4478c7d99584111e0015aa9b0e5831 to your computer and use it in GitHub Desktop.
Save Daomephsta/8a4478c7d99584111e0015aa9b0e5831 to your computer and use it in GitHub Desktop.
ItemTickThingy
public class ItemTimer extends Item
{
//This is the identifier for the NBT tag that will store the time
private static String TIMER_TAG = "Timer";
//This is the delay we want to have between actions. It is in game ticks, 1 game tick is equal to 1/20 of asecond.
private static int DELAY = 5;
//Standard item init stuff
public ItemTimer()
{
this.setUnlocalizedName("mod.timer");
this.setTextureName("mod:timer")
}
@Override
onUpdate(ItemStack stack, World world, Entity entity, int invSlot, boolean isInHand)
{
//if the timer has reached DELAY, perform the action and set the timer to 0...
if(ItemTimer.getTimer(stack) == DELAY)
{
//Do stuff here
ItemTimer.setTimer(stack, 0);
}
//...otherwise, increment it
else
{
ItemTimer.incrementTimer(stack);
}
}
//This method returns the current value of the timer
private static int getTimer(ItemStack stack)
{
//if the stack doesn't have an NBT tag yet, give it one
if (stack.stackTagCompound == null) stack.stackTagCompound = new NBTTagCompound();
return stack.stackTagCompound.getInteger(TIMER_TAG);
}
//This method sets the current value of the timer
private static void setTimer(ItemStack stack, int value)
{
//if the stack doesn't have an NBT tag yet, give it one
if (stack.stackTagCompound == null) stack.stackTagCompound = new NBTTagCompound();
stack.stackTagCompound.setInteger(TIMER_TAG, value);
}
//This method increments the current value of the timer by 1
private static void incrementTimer(ItemStack stack)
{
ItemTimer.setTimer(stack, ItemTimer.getTimer(stack) + 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment