Jump to content

Minecraft attack speed or attack cooldown


Szymeon

Recommended Posts

Hey, i found 4 years old post where some guy showed how to increase attack speed, but code is old, and one thing isnt working. My code:

Tool:

public class ToolSpeed extends ItemSword implements IHasModel {

	public ToolSpeed(String name, ToolMaterial material)
	{
		super(material);
		setUnlocalizedName(name);
		setRegistryName(name);
		setCreativeTab(CreativeTabs.COMBAT);
		
		
		ModItems.ITEMS.add(this);
	}

	@Override
	public void registerModels() 
	{
		Main.proxy.registerItemRenderer(this, 0, "inventory");
	}
	
	@Override
	public Multimap<String, AttributeModifier> getItemAttributeModifiers(EntityEquipmentSlot equipmentSlot)
    {
	     Multimap<String, AttributeModifier> multimap = super.getItemAttributeModifiers(equipmentSlot);

	        if (equipmentSlot == EntityEquipmentSlot.MAINHAND)
	        {
	            multimap.put(SharedMonsterAttributes.ATTACK_SPEED.getName(),
	            	new AttributeModifier(ATTACK_SPEED_MODIFIER, "Weapon modifier", -1.8D, 0));
        }

        return multimap;
    }
}


Cooldownable:

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");
	}
}
}


and util AgravaineNBTHelper, here i have an error:

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);
}
}

"The field ItemStack.stackTagCompound is not visible" i dont know what should be instead
*Sorry for bad english if i said something wrong xd*

Edited by Szymeon
Link to comment
Share on other sites

10 minutes ago, Szymeon said:

IHasModel

Stop using IHasModel, it is stupid. All items have models. All of them, no exceptions. Nothing that is required by model registering is private to your item class. Model registration needs to happen in ModelRegistryEvent so do it there directly. 

 

Al this stack tag compound nonesense is no longer needed. It was a solution to a 1.7 problem. Nowdays the attack speed is controlled by the attack speed attribute modifier.

10 minutes ago, Szymeon said:

multimap.put(SharedMonsterAttributes.ATTACK_SPEED.getName(), new AttributeModifier(ATTACK_SPEED_MODIFIER, "Weapon modifier", -1.8D, 0));

^This line. The -1.8D is the modifier. Change it to something else.

 

10 minutes ago, Szymeon said:

ModItems.ITEMS.add(this);

This line screams "I was initialized in a static initializer!". Don't use static initializers. Instantinate your stuff directly in the RegistryEvent.Register. Like this. Not only is this the correct way of registering and instantinating stuff it also shortens your code to a 33% of what it used to be.

Link to comment
Share on other sites

1 minute ago, Szymeon said:

I dont know how to stop using IHasModel, and how to register in a different way. I'm totally new and everything what I know is from youtube and forums like this one. xd

Step one delete the class step to iterate through your items step three call ModelLoader.setCustomModelResourceLocation on all the items in your ModelRegistryEvent.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

7 minutes ago, Szymeon said:

I dont know how to stop using IHasModel, and how to register in a different way

I just told you how. Ditch it entirely and register your models in the ModelRegistryEvent directly. Like I do here if you need an example.

7 minutes ago, Szymeon said:

everything what I know is from youtube

I have yet to see a "good" modding tutorial on youtube. All of them make mistakes like this, or static initializers, or CommonProxy or something else posts on this forum are plagued by. The truth is they don't know much more than you. They've just learned how to make their code not explode in their faces and they are eager to share that knowledge with the world even if their code makes no sense(CommonProxy, IHasModel) or is plain incorrect(static initializers)

Edited by V0idWa1k3r
  • Like 1
Link to comment
Share on other sites

1 minute ago, V0idWa1k3r said:

I have yet to see a "good" modding tutorial on youtube.

Don't worry as soon as 1.13 is out I will attempt a good modding tutorial for it, and hopefully the plague will end.

  • Thanks 1

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

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