Jump to content

Recommended Posts

Posted (edited)

I'm trying to add an on hit effect to a "Sword" and remove it's damage. How and where would I do such a thing?

 

Edit: I actually hope to add more than just one effect to the strike.

Edited by set9753
Posted
7 hours ago, set9753 said:

I'm trying to add an on hit effect to a "Sword" and remove it's damage. How and where would I do such a thing?

 

Edit: I actually hope to add more than just one effect to the strike.

Vanilla ItemSword

image.png

Posted
10 hours ago, poopoodice said:

You can add on-hit effect here

image.png

Where does this code go? In the main file for the mod items? Or in some kind of new class for the modded item's effects? I should note, I've only recently started modding. I've only managed to spaghetti code the item into the game as is. (I used TechnoVision's youtube series to get started.) If it helps, I could share the source file? It's not like I'm worried about the idea being stolen

Posted
13 hours ago, poopoodice said:

Vanilla ItemSword

Dont post source code that doesnt belong to you. Yes even the vanilla code.

 

2 hours ago, set9753 said:

Where does this code go?

The Items class file.

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.

Posted
3 minutes ago, set9753 said:

Three, to be specific. Speed 1, Strength 1, and Fire Resistance 1. For 8 seconds.

So potion effects?
LivingEntityBase#addPotionEffect

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.

Posted

When I try to use that, I get an error reading "Syntax error on token "Invalid Character", delete this token.

The code looks like this, I know it's wrong, but I'm really stuck here.

 

 

public class ItemSword {
    
    PotionEffect effect;
    
    public boolean hitEntity(ItemStack stack, EntityLivingBase target, EntityLivingBase attacker, EntityLivingBase#addPotionEffect)
    {
        stack.damageItem(0, attacker);
        return true;
    }

}

Posted
30 minutes ago, set9753 said:

I know it's wrong, but I'm really stuck here.

You need to learn Java. What you have there isnt a Forge error. You'd be able to fix it if you knew Java.

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.

Posted

You'll want to override hitEntity inside your sword class, and apply the potion effect inside the overridden method.

At the "target.addPotionEffect", you may want to give the effect to the attacker, rather than the target, I'm not sure.

 

	@Override
	public boolean hitEntity(ItemStack stack, EntityLivingBase target, EntityLivingBase attacker)
	{
		target.addPotionEffect(new PotionEffect(MobEffects.STRENGTH, 8 * 20, 0));
		stack.damageItem(1, attacker);
		return true;
	}

 

As for the illegal character...

Quote

public boolean hitEntity(ItemStack stack, EntityLivingBase target, EntityLivingBase attacker, EntityLivingBase#addPotionEffect)

What you were trying to do there was call a method inside the parameters taken in by hitEntity. The parameters are variables fed into hitEntity for it to do things with. When it's called, the stuff int the {} happens. When you hit an entity with your sword, hitEntity is called. The parameters are fed into the method, and the {} happens.

 

Also, EntityLivingBase#addPotionEffect means "the addPotionEffect within instances of EntityLivingBase", rather than

being an actual line of executable code. That's why I call it here on the target object, since target is an instance of EntityLivingBase.

Fancy 3D Graphing Calculator mod, with many different coordinate systems.

Lightweight 3D/2D position/vector transformations library, also with support for different coordinate systems.

Posted

Thank you, so much for the explanations! I just have a few more questions to tie this together, if you don't mind...

 

I have the class ItemSword extending Item, and the item I want to use to boost on hit is made as an Item... Does the extension mean this will work?

If I wanted to add more than one potion effect, how would I go about that?

Posted
Quote

I have the class ItemSword extending Item, and the item I want to use to boost on hit is made as an Item

So you have two items here, one is a miscellaneous sword, and the other is a sword/other with a hit effect?

You can get some extra sword-related methods and functionality by extending ItemSword (which in turn extends Item) instead of simply Item, but you can easily make the basic functionalities you're asking about in either, as hitEntity is a method for Items, not specifically ItemSwords. If you want some, but not all of the ItemSword features, you could just re-write some of it in your class.

In other words, the extensions are fine, but you may want more functionalities, which could be gotten from the ItemSword class. I'd suggest you take a look at its methods to get a better idea of what it can do.

 

Quote

If I wanted to add more than one potion effect, how would I go about that?

Quote

target.addPotionEffect(new PotionEffect(MobEffects.STRENGTH, 8 * 20, 0));

You can just add another of these. E.G: this inside the hitEntity method.

target.addPotionEffect(new PotionEffect(MobEffects.STRENGTH, 8 * 20, 0));
target.addPotionEffect(new PotionEffect(MobEffects.SPEED, 8 * 20, 0));

 

If you wanted a flexible, easily expandable set of potion effects, you could also do something like this.

Potion[] effectList = new Potion[]
{
		MobEffects.STRENGTH,
		MobEffects.SPEED
};

Iterating through the list in hitEntity.

	@Override
	public boolean hitEntity(ItemStack stack, EntityLivingBase target, EntityLivingBase attacker)
	{
		for (Potion current : effectList) { target.addPotionEffect(new PotionEffect(current, 8 * 20, 0)); }
		stack.damageItem(1, attacker);
		return true;
	}

Fancy 3D Graphing Calculator mod, with many different coordinate systems.

Lightweight 3D/2D position/vector transformations library, also with support for different coordinate systems.

Posted

I should clarify, I've been spaghetti coding my way through this mod. And at one point thought I had to make a SwordItem to make the on hit effect work. I've since deleted the sword item, but kept the class and all. So I just made the SwordItem class extend the Item class. I only have the item, and assumed by extending Item via SwordItem's class I could achieve the effect I wanted (an item that could boost others by whacking them with it)

 

Would it be worth my time to just rewrite all the code into the Item class, and remove the Sworditem class? Or would it work the way I have it now? Everything seems to be turning out, I pretty much just want to know if the hitEntity effect will work out. (Once I finish this, the mod is done. I wont be adding onto it, so don't worry about my odd coding being a bother for later additions)

Posted

If your class extends Item, and you override hitEntity and add the potion effect methods, then the potion effect methods should run when an entity is hit with the item.

 

If you want the extra sword functionalities that the base Item class doesn't have, you should extend ItemSword and do nothing different with hitEntity. If not, you can just keep it as a simple extension of Item.

Fancy 3D Graphing Calculator mod, with many different coordinate systems.

Lightweight 3D/2D position/vector transformations library, also with support for different coordinate systems.

Posted
10 hours ago, set9753 said:

Where does this code go? In the main file for the mod items? Or in some kind of new class for the modded item's effects? I should note, I've only recently started modding. I've only managed to spaghetti code the item into the game as is. (I used TechnoVision's youtube series to get started.) If it helps, I could share the source file? It's not like I'm worried about the idea being stolen

you can override them once your iteminit extends ItemSword

Posted
8 hours ago, Animefan8888 said:

Dont post source code that doesnt belong to you. Yes even the vanilla code.

 

The Items class file.

Mb I thought that images can explain more clear rather than words. I wont do that again

Posted
2 minutes ago, poopoodice said:

you can override them once your iteminit extends ItemSword

It doesn't have to extend ItemSword specifically, just Item.

Fancy 3D Graphing Calculator mod, with many different coordinate systems.

Lightweight 3D/2D position/vector transformations library, also with support for different coordinate systems.

Posted (edited)
3 minutes ago, SerpentDagger said:

It doesn't have to extend ItemSword specifically, just Item.

Yes, but since he/she wants an item that has the attribute of a sword but without attack damage, it will be an easier way to do it

Edited by poopoodice
Posted
8 minutes ago, poopoodice said:

 without attack damage

True, though an even easier way of getting rid of attack damage would be to override onLeftClickEntity and return true after the potion effect methods.

	@Override
	public boolean onLeftClickEntity(ItemStack stack, EntityPlayer player, Entity entity)
	{
		if (entity instanceof EntityLivingBase) ((EntityLivingBase) entity).addPotionEffect(new PotionEffect(MobEffects.STRENGTH, 8 * 20, 0));
		return true;
	}

That doesn't deal any damage at all, not even fist damage. It doesn't apply knockback, though, so extending ItemSword would still be the way to go if they wanted that.

Come to think of it, would the attackDamage correspond to total damage dealt, or additional damage dealt? Meaning, would giving it a value of 0 result in 0 damage + fist damage, or 0 overall, negating the fist damage?

Fancy 3D Graphing Calculator mod, with many different coordinate systems.

Lightweight 3D/2D position/vector transformations library, also with support for different coordinate systems.

Posted
21 minutes ago, SerpentDagger said:

True, though an even easier way of getting rid of attack damage would be to override onLeftClickEntity and return true after the potion effect methods.


	@Override
	public boolean onLeftClickEntity(ItemStack stack, EntityPlayer player, Entity entity)
	{
		if (entity instanceof EntityLivingBase) ((EntityLivingBase) entity).addPotionEffect(new PotionEffect(MobEffects.STRENGTH, 8 * 20, 0));
		return true;
	}

That doesn't deal any damage at all, not even fist damage. It doesn't apply knockback, though, so extending ItemSword would still be the way to go if they wanted that.

Come to think of it, would the attackDamage correspond to total damage dealt, or additional damage dealt? Meaning, would giving it a value of 0 result in 0 damage + fist damage, or 0 overall, negating the fist damage?

Good point. Now he/she can make a item that without everything but the attributes he wants such as potion effect while leftclick on an entity. Nice.

Posted
3 hours ago, set9753 said:

I should clarify, I've been spaghetti coding my way through this mod. And at one point thought I had to make a SwordItem to make the on hit effect work. I've since deleted the sword item, but kept the class and all. So I just made the SwordItem class extend the Item class. I only have the item, and assumed by extending Item via SwordItem's class I could achieve the effect I wanted (an item that could boost others by whacking them with it)

 

Would it be worth my time to just rewrite all the code into the Item class, and remove the Sworditem class? Or would it work the way I have it now? Everything seems to be turning out, I pretty much just want to know if the hitEntity effect will work out. (Once I finish this, the mod is done. I wont be adding onto it, so don't worry about my odd coding being a bother for later additions)

SwordItem has a lot more sword-specific functions than an Item. Only extends sword when you need to actually use it to attack another entity like a vanilla sword. In your case extending Item should be sufficient.

 

@SerpentDagger Please note that posting copy-pastable code for others is discouraged, as others will not learn and might blindly copy the code.

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

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.