Jump to content

Recommended Posts

Posted

Is it possible to change a weapon's attack speed like 2 attacks per second

Or the weapon cools down for 2 seconds after attacking once?

If yes, how could I do it?

 

*Sorry for my bad English

Posted

Create class Cooldownable.class:

public class Cooldownable{

public static final String COOLDOWN_NBT = "agravaine:cooldown";

/**
 * Call this onUpdate() in your Item WARNING: you should check for
 * !world.isRemote
 * 
 * @param iStack
 *            stack to update
 * @param maxCooldown
 *            maximum cooldown
 */
public static void onUpdate(ItemStack iStack, int maxCooldown){
	AgravaineNBTHelper.checkNBT(iStack);
	if(!AgravaineNBTHelper.verifyKey(iStack, COOLDOWN_NBT)){
		AgravaineNBTHelper.setInteger(iStack, COOLDOWN_NBT, maxCooldown);
	}
	if(AgravaineNBTHelper.getInteger(iStack, COOLDOWN_NBT) > 0){
		AgravaineNBTHelper.decreaseInteger(iStack, COOLDOWN_NBT, 1);
	}
}

/**
 * Checks for cooldown. Call it when your item needs to be used
 * 
 * @param iStack
 *            Stack to check
 * @param maxCooldown
 *            Maximum cooldown
 * @return True if item's cooldown is zero, returns true and sets it to
 *         maximum, otherwise false
 */
public static boolean canUse(ItemStack iStack, int maxCooldown){
	AgravaineNBTHelper.checkNBT(iStack);
	if(AgravaineNBTHelper.getInteger(iStack, COOLDOWN_NBT) > 0){
		return false;
	}else{
		AgravaineNBTHelper.setInteger(iStack, COOLDOWN_NBT, maxCooldown);
		return true;
	}
}

/**
 * Call this addInformation() in your Item
 * 
 * @param iStack
 *            stack to update
 * @param info
 *            cooldown text
 */
public static void displayCooldown(ItemStack iStack, List info){
	AgravaineNBTHelper.checkNBT(iStack);
	if(AgravaineNBTHelper.getInteger(iStack, COOLDOWN_NBT) > 0){
		info.add("Cooldown: " + AgravaineNBTHelper.getInteger(iStack, COOLDOWN_NBT));
	}else{
		info.add("Cooldown: ready");
	}
}
}

Example weapons with cooldown:

public class ItemBase extends Item{

public static final int COOLDOWN = 200; //200 - cooldown in ticks. 1 sec - 20 ticks.

public ItemBase(){
	super();
}

public void onUpdate(ItemStack iStack, World world, Entity entity, int esotericInt, boolean agravaineBoolean){
	if(!world.isRemote){
                    Cooldownable.onUpdate(iStack, COOLDOWN);
               }
}

public ItemStack onItemRightClick(ItemStack iStack, World world, EntityPlayer player){
	if(Cooldownable.canUse(iStack, COOLDOWN)){
		player.motionY++;
	}
	return iStack;
}

public void addInformation(ItemStack iStack, EntityPlayer player, List info, boolean esotericBoolean){
	Cooldownable.displayCooldown(iStack, info); //display current cooldown
}
}

Util:

public class AgravaineNBTHelper{

/**
 * Used for null-safety
 * 
 * @param stack
 */
public static NBTTagCompound checkNBT(ItemStack stack){
	if(stack.stackTagCompound == null){
		stack.stackTagCompound = new NBTTagCompound();
	}
	return stack.stackTagCompound;
}

/**
 * Shortcut for NBTTagCompound.hasKey()
 */
public static boolean verifyKey(ItemStack stack, String name){
	return stack.stackTagCompound.hasKey(name);
}

public static void setInteger(ItemStack stack, String name, int value){
	stack.stackTagCompound.setInteger(name, value);
}

public static int getInteger(ItemStack stack, String name){
	return stack.stackTagCompound.getInteger(name);
}

public static void decreaseInteger(ItemStack stack, String name, int value){
	if(getInteger(stack, name) > 0){
		setInteger(stack, name, getInteger(stack, name) - value);
	}
}

public static void decreaseIntegerIgnoreZero(ItemStack stack, String name, int value){
	setInteger(stack, name, getInteger(stack, name) - value);
}

public static void setString(ItemStack stack, String name, String value){
	stack.stackTagCompound.setString(name, value);
}

public static String getString(ItemStack stack, String name){
	return stack.stackTagCompound.getString(name);
}

public static void setBoolean(ItemStack stack, String name, boolean value){
	stack.stackTagCompound.setBoolean(name, value);
}

public static boolean getBoolean(ItemStack stack, String name){
	return stack.stackTagCompound.getBoolean(name);
}

public static void invertBoolean(ItemStack stack, String name){
	setBoolean(stack, name, !getBoolean(stack, name));
}

public static void setByte(ItemStack stack, String name, byte value){
	stack.stackTagCompound.setByte(name, value);
}

public static byte getByte(ItemStack stack, String name){
	if(!verifyKey(stack, name)){
		setByte(stack, name, (byte)0);
	}
	return stack.stackTagCompound.getByte(name);
}
}

Good luck!

Posted

package tutorial.basic;

import java.awt.List;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumToolMaterial;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword;
import net.minecraft.world.World;

public class GenericSword extends ItemSword {

public GenericSword(int id, EnumToolMaterial par2EnumToolMaterial) {
	super(id, par2EnumToolMaterial);
	setCreativeTab(CreativeTabs.tabCombat);
	setUnlocalizedName("generic_Sword");
	setTextureName("chiyin:generic_sword");
}

    public static final int COOLDOWN = 2000; //200 - cooldown in ticks. 1 sec - 20 ticks.

public void onUpdate(ItemStack iStack, World world, Entity entity, int esotericInt, boolean agravaineBoolean){
	if(!world.isRemote){
                    Cooldownable.onUpdate(iStack, COOLDOWN);
               }
}

public ItemStack onItemRightClick(ItemStack iStack, World world, EntityPlayer player){
	if(Cooldownable.canUse(iStack, COOLDOWN)){
		player.motionY++;
	}
	return iStack;
}

public void addInformation(ItemStack iStack, EntityPlayer player, List info, boolean esotericBoolean){
	Cooldownable.displayCooldown(iStack, info); //display current cooldown
}


}

 

I've created the 2 class : Cooldownable.class & AgravaineNBTHelper.class

Posted

What's still not working? Also, use onItemUse, not onItemRightClick.

We all stuff up sometimes... But I seem to be at the bottom of that pot.

  • 4 years later...
Posted

Don't necropost. If you have an issue create a new thread instead of digging this 4 year old one.

Things changed between versions and ItemStack.stackTagCompound can now be accessed via ItemStack#getTagCompound.

  • Guest locked this topic
Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.