Skip to content

Commit

Permalink
add itemUseDuration
Browse files Browse the repository at this point in the history
  • Loading branch information
friendlyhj committed May 2, 2022
1 parent 5d9bbe3 commit c2b59ac
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.blamejared.contenttweaker.api.functions;

import com.blamejared.crafttweaker.api.annotations.ZenRegister;
import com.blamejared.crafttweaker.api.item.IItemStack;
import com.blamejared.crafttweaker_annotations.annotations.Document;
import org.openzen.zencode.java.ZenCodeType;

/**
* @author youyihj
*/
@FunctionalInterface
@ZenRegister
@ZenCodeType.Name("mods.contenttweaker.functions.IItemUseDurationSupplier")
@Document("mods/contenttweaker/API/functions/IItemUseDurationSupplier")
public interface IItemUseDurationSupplier extends ICotFunction {
@ZenCodeType.Method
int apply(IItemStack stack);
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public CoTItemAdvanced(Properties properties, ResourceLocation location) {
private IItemInventoryTick itemInventoryTick;
private IItemUseActionSupplier itemUseActionSupplier = stack -> stack.getInternal().getItem().isFood() ? UseAction.EAT : UseAction.NONE;
private IItemUseFinish itemUseFinish = IItemUseFinish.DEFAULT;
private IItemUseDurationSupplier itemUseDurationSupplier = null;


/**
Expand Down Expand Up @@ -165,6 +166,19 @@ public CoTItemAdvanced setItemUseAction(IItemUseActionSupplier func) {
return this;
}

/**
* How long it takes to use or consume an item.
*
* By default, if the use finish function is defined or the item is a food, it will be 32.
* @param func an IItemUseDurationSupplier function
* @return the CoTItemAdvanced, used for method chaining
*/
@ZenCodeType.Method
public CoTItemAdvanced setItemUseDuration(IItemUseDurationSupplier func) {
ActionSetFunction.applyNewAction("itemUseDuration", this, func, (item, fun) -> item.itemUseDurationSupplier = fun);
return this;
}

@Override
public ActionResultType onItemUse(ItemUseContext context) {
if (itemUse != null) {
Expand All @@ -184,6 +198,17 @@ public UseAction getUseAction(ItemStack stack) {
return itemUseActionSupplier.apply(new MCItemStack(stack));
}

@Override
public int getUseDuration(ItemStack stack) {
if (itemUseDurationSupplier != null) {
return itemUseDurationSupplier.apply(new MCItemStackMutable(stack));
}
if (itemUseFinish != IItemUseFinish.DEFAULT) {
return 32;
}
return super.getUseDuration(stack);
}

@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
ItemStack stack = playerIn.getHeldItem(handIn);
Expand Down

0 comments on commit c2b59ac

Please sign in to comment.