Jump to content

[1.12.2] Random damage for sword every time it's crafted


Recommended Posts

Posted

Hello, so I'm trying to make a new material with tools that get random bonus attributes when crafted. I'm starting with the sword, which can get a bonus 1-3 damage when crafted. My problem is that, whenever I craft them, the damage of all the previous swords changes too. This is my code:

 

public class ToolSword extends ItemSword implements IHasModel{
	
	private Random rand;
	private int bonusDamage;

	public ToolSword(String name, ToolMaterial material){
		super(material);
		setUnlocalizedName(name);
		setRegistryName(name);
		setCreativeTab(CreativeTabs.COMBAT);
		rand = new Random();
		bonusDamage = 0;
		
		ModItems.ITEMS.add(this);
	}
	
	@Override
	public void registerModels() {
		
		Main.proxy.registerItemRenderer(this, 0, "inventory");
		
	}
	
	@Override
	public void onCreated(ItemStack stack, World worldIn, EntityPlayer playerIn){
		bonusDamage = rand.nextInt(3) + 1;
    }
	
	@Override
	public Multimap<String, AttributeModifier> getAttributeModifiers(EntityEquipmentSlot slot, ItemStack stack) {
		final Multimap<String, AttributeModifier> modifiers = super.getAttributeModifiers(slot, stack);

		if (slot == EntityEquipmentSlot.MAINHAND) {
			replaceModifier(modifiers, SharedMonsterAttributes.ATTACK_DAMAGE, ATTACK_DAMAGE_MODIFIER, bonusDamage);
		}

		return modifiers;
	}
	
	private void replaceModifier(Multimap<String, AttributeModifier> modifierMultimap, IAttribute attribute, UUID id, double amount) {
		// Get the modifiers for the specified attribute
		final Collection<AttributeModifier> modifiers = modifierMultimap.get(attribute.getName());

		// Find the modifier with the specified ID, if any
		final Optional<AttributeModifier> modifierOptional = modifiers.stream().filter(attributeModifier -> attributeModifier.getID().equals(id)).findFirst();

		if (modifierOptional.isPresent()) { // If it exists,
			final AttributeModifier modifier = modifierOptional.get();
			modifiers.remove(modifier); // Remove it
			modifiers.add(new AttributeModifier(modifier.getID(), modifier.getName(), modifier.getAmount() + amount, modifier.getOperation())); // Add the new modifier
		}
	}
	
}

 

I did try using 

stack.addAttributeModifier

which kept each item with its unique value. However, it completely replaces the old damage and attack speed values and the Sharpness enchantment no longer has effect on it, so I don't like using this command because I still want Sharpness to increase its damage even more.

 

Any help?

Posted

Items are singletons (there is only 1 of the ever) if you want damage etc. you have to apply that stuff to the ItemStacks

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Posted
6 minutes ago, Cadiboo said:

Items are singletons (there is only 1 of the ever) if you want damage etc. you have to apply that stuff to the ItemStacks

I also tried that, but the original attributes all get removed, but I just want to add additional damage, while keeping the original one and the original attack speed.

 

This is what I tried:

 

@Override
	public void onCreated(ItemStack stack, World worldIn, EntityPlayer playerIn){
		bonusDamage = rand.nextInt(3) + 9;
		stack.addAttributeModifier(SharedMonsterAttributes.ATTACK_DAMAGE.getName(), new AttributeModifier("Weapon modifier", bonusDamage, 0), EntityEquipmentSlot.MAINHAND);
	}

 

It says "add" but it doesn't really add, it replaces the old ones, which I don't want.

Posted

there is a ItemStack sensitive variant of addAttributeModifier. not sure if this fixes that but at least it's what I used for something similar

My Projects:

Cruelars Triforcemod (1.12 release; 1.14 alpha soon coming)

 

Important:

As my mod is on at least 10 different third party sites without my permission, I want to warn you about that with a link to StopModReposts

Posted

You should probably be using a capability on the ItemStack and subscribe to an attack entity event, and add extra damage based on that capability

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Posted (edited)

You need to store the additional value to your ItemStack's NBT data or a capability, then override Item#getAttributeModifiers and make the damage attribute add the itemDamage + yourDamage as the modifier. See how ItemSword does that.

 

Edit: Thanks for @Animefan8888 for correcting my mistake, whoops, had ItemStack written as the base class instead of Item.

Edited by V0idWa1k3r
Posted (edited)
1 hour ago, V0idWa1k3r said:

Edit: Thanks for @Animefan8888 for correcting my mistake, whoops

No problem.?

Edited by Animefan8888
  • Like 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.

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.

Announcements



×
×
  • Create New...

Important Information

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