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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello , when I try to launch the forge installer it just crash with a message for 0,5 secondes. I'm using java 17 to launch it. Here's the link of the error :https://cdn.corenexis.com/view/?img=d/ma24/qs7u4U.jpg  
    • You will find the crash-report or log in your minecraft directory (crash-report or logs folder)
    • Use a modpack which is using these 2 mods as working base:   https://www.curseforge.com/minecraft/modpacks/life-in-the-village-3
    • inicie un mundo donde instale Croptopia y Farmer's Delight, entonces instale el addon Croptopia Delight pero no funciona. es la version 1.18.2
    • Hello all. I'm currently grappling with the updateShape method in a custom class extending Block.  My code currently looks like this: The conditionals in CheckState are there to switch blockstate properties, which is working fine, as it functions correctly every time in getStateForPlacement.  The problem I'm running into is that when I update a state, the blocks seem to call CheckState with the position of the block which was changed updated last.  If I build a wall I can see the same change propagate across. My question thus is this: is updateShape sending its return to the neighbouring block?  Is each block not independently executing the updateShape method, thus inserting its own current position?  The first statement appears to be true, and the second false (each block is not independently executing the method). I have tried to fix this by saving the block's own position to a variable myPos at inception, and then feeding this in as CheckState(myPos) but this causes a worse outcome, where all blocks take the update of the first modified block, rather than just their neighbour.  This raises more questions than it answers, obviously: how is a different instance's variable propagating here?  I also tried changing it so that CheckState did not take a BlockPos, but had myPos built into the body - same problem. I have previously looked at neighbourUpdate and onNeighbourUpdate, but could not find a way to get this to work at all.  One post on here about updatePostPlacement and other methods has proven itself long superceded.  All other sources on the net seem to be out of date. Many thanks in advance for any help you might offer me, it's been several days now of trying to get this work and several weeks of generally trying to get round this roadblock.  - Sandermall
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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