Jump to content

Checking if a player swung with full attack power


Javisel

Recommended Posts

I'm overriding the HityEntity method in Item to do something for the player. But for design choices/balancing I need to gate it behind weapon charge time. I'm trying to figure out how I can detect if an attack was done with full power to prevent spam clicking. I tried looking into EntityPlayer's and how it handles sweeping edge but I don't really understand it.

Project Source Code 

Link to comment
Share on other sites

EntityPlayer#getCooledAttackStrength.

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.

 

 

Link to comment
Share on other sites

6 hours ago, Javisel said:

I tried that, but it didn't work.

Provide which item you are working with you gave us no direction to the actual file(s) we are working with.How did you use EntityPlayer$getCooledAttackStrength?

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

@Animefan8888 You're back!

 

@Javisel Post your code. That method gives the percentage of the attack cool down.

Edited by DavidM

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.

 

 

Link to comment
Share on other sites

14 hours ago, Animefan8888 said:

Provide which item you are working with you gave us no direction to the actual file(s) we are working with.How did you use EntityPlayer$getCooledAttackStrength?

 

11 hours ago, DavidM said:

@Animefan8888 You're back!

 

@Javisel Post your code. That method gives the percentage of the attack cool down.

 

Sorry my bad. Here's a link to the item: Link

Link to comment
Share on other sites

14 hours ago, DavidM said:

You're back!

Yeah college and other more important stuff started taking all of my time, now it's time to learn all this new forge code base. ?

  • 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.

Link to comment
Share on other sites

3 hours ago, Javisel said:

 

 

Sorry my bad. Here's a link to the item: Link

Your problem is here.
 

if (player.getCooledAttackStrength(0) == 0.0F) {

0.0F would be it hasn't recharged at all 1.0F would be fully recharged and 0.5F would be half of full

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

27 minutes ago, Animefan8888 said:

Your problem is here.
 


if (player.getCooledAttackStrength(0) == 0.0F) {

0.0F would be it hasn't recharged at all 1.0F would be fully recharged and 0.5F would be half of full

I've tried both numbers and it won't work. I'm trying to figure out if it was swung with 1.0F power, not if it is currently at 0.5f or 1.0F power.

Link to comment
Share on other sites

27 minutes ago, Javisel said:

I've tried both numbers and it won't work. I'm trying to figure out if it was swung with 1.0F power, not if it is currently at 0.5f or 1.0F power.

I see the problem after a little time debugging. Item#hitEntity is called after the LivingEntity#ticksSinceLastSwing is reset to 0. So using PlayerEntity#getCooledAttackStrength won't work. Your  best bet is to either create a capability to store an integer in either your Item/ItemStack or the player. I suggest the player if you are going to use the cooldown more than once. And just count the ticks since a swing.

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

3 hours ago, Animefan8888 said:

I see the problem after a little time debugging. Item#hitEntity is called after the LivingEntity#ticksSinceLastSwing is reset to 0. So using PlayerEntity#getCooledAttackStrength won't work. Your  best bet is to either create a capability to store an integer in either your Item/ItemStack or the player. I suggest the player if you are going to use the cooldown more than once. And just count the ticks since a swing.

Do you think it'd be possible to use getCooldownPeriod() somehow? I've tried but that hasn't worked. So i'm wondering if my implementation is incorrect and if there's a better way

 

Link to comment
Share on other sites

Just now, Javisel said:

getCooldownPeriod()

This tells you how long the cooldown should be not how long has passed.

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

On 7/24/2019 at 4:45 PM, Animefan8888 said:

This tells you how long the cooldown should be not how long has passed.

Oh, my bad.

 

What about Sweeping Edge? The sweep effect is only activated when the swing was done with full strength. I took a look at the code for it in EntityPlayer and couldn't really understand it or replicate it's effects. Is it impossible to do so?

Link to comment
Share on other sites

3 hours ago, Javisel said:

Oh, my bad.

 

What about Sweeping Edge? The sweep effect is only activated when the swing was done with full strength. I took a look at the code for it in EntityPlayer and couldn't really understand it or replicate it's effects. Is it impossible to do so?

Sweeping edge happens earlier in the execution order. Now maybe you can use LivingDamageEvent(I think that is the name) which might be early enough in the execution, maybe it even has a field that tells you about the cool down I'm not sure, but worth a try.

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

LivingDamageEvent is triggered before the attack deals damage.

LivingDamageEvent#getSource returns the damage source, which, if is an instance of EntityDamageSource (when an entity inflicts the damage), will record the entity that dealt the damage, and can be obtained with EntityDamageSource#getTrueSource.

If the return entity is an instance of EntityPlayer, you can get the attack cool down with EntityPlayer#getCooldownPeriod or EntityPlayer#getCooledAttackStrength.

Edited by DavidM

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.

 

 

Link to comment
Share on other sites

1 minute ago, DavidM said:

If the return entity is an instance of EntityPlayer, you can get the attack cool down with EntityPlayer#getCooldownPeriod or EntityPlayer#getCooledAttackStrength.

Yes I understand that, my idea was maybe they added a Field to the LivingDamageEvent that contained the value or something related, its unlikely. Also do you know if this event is pushed before the EntityLiving#ticksSinceLastSwing is reset to 0. Because if it isnt then EntityPlayer#getCooledAttackStrength wont work.

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

3 minutes ago, Animefan8888 said:

if this event is pushed before the EntityLiving#ticksSinceLastSwing is reset to 0

My bad. Just checked and realized LivingDamageEvent is triggered after calculations, but before the damage applies.

It is probably triggered after the ticksSinceLastSwing is reset to 0.

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.

 

 

Link to comment
Share on other sites

Try subscribing to AttackEntityEvent.

AttackEntityEvent fires before the cool down reset.

Overriding IForgeItem#onLeftClickEntity should also work.

Edited by DavidM
  • Thanks 1

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.

 

 

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.

Announcements



×
×
  • Create New...

Important Information

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