Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up entity damage handling #2040

Merged
merged 34 commits into from
Jun 19, 2023

Conversation

Jikoo
Copy link
Member

@Jikoo Jikoo commented Apr 15, 2023

The entity damage handling is a huge mess. It's a behemoth of logic containing a ton of outdated checks and redundancies.

Going to open this as a draft and update as I go. The majority of this will be reorganization, but certain things like the nonfunctional Firework handling (Firework extends Projectile, so it can't be hit) I'll need to test more thoroughly.

When the time does come to review, I recommend checking individual commits, potentially ignoring whitespace diffs. I expect that as this is a major chunk of housekeeping it will be very hard to follow otherwise.

@Jikoo
Copy link
Member Author

Jikoo commented Apr 15, 2023

Re: flat removal of unreachable Firework handling: Spigot has been assigning fireworks' shooter correctly since somewhere in 1.15-ish I believe.

Firework testing

image
image

@SrBedrock
Copy link

Would it be possible to block the ender's crystal damage inside the protection?

@Jikoo
Copy link
Member Author

Jikoo commented Apr 15, 2023

Would it be possible to block the ender's crystal damage inside the protection?

I'm not looking to change features or fix bugs within this PR, I'm trying to make it less of a nightmare to work with. If you think GP is missing a case, please open an issue instead, I'll probably forget otherwise.

In general end crystal explosions originate inside the claim and as a result should damage things also inside the claim. The only source of damage to end crystals we have an issue tracking is specifically for TNT minecarts in #540, are there other sources?

@SrBedrock
Copy link

If the player is inside the protection and someone breaks an ender crystal outside the protection but within the explosion range the player takes damage, I will test it further and open an issue.

@Jikoo
Copy link
Member Author

Jikoo commented Apr 15, 2023

I agonized a bit about the most recent commit. The for loop reducing duplicate code is pretty gross looking, but the other easy option is to create another helper method that takes like 8 parameters, which is also gross. Not sure what's best. Guess the cleanest way to go would be a little record for data storage, but that's still kind of a nuisance. Either way, done working on this for now, probably will pick it back up tomorrow or later this evening.
Turns out the code was duplicated in another in-claim check, so helper method it is.

@RoboMWM if there's anything you'd like me to do differently with this (move helpers to separate pulls or something if possible, Idk) please let me know. I'm doing this the way that I am because I think the other options will be just as bad to review and even slower to process, but if there's something that works better for you I'm all ears.

@Jikoo
Copy link
Member Author

Jikoo commented Apr 17, 2023

Re: 4fe0098 - I specifically set out to not fix bugs here, so I've moved comments and TODO to the appropriate locations to actually enable damage to villagers from the correct sources.

I think this is approaching readiness. I'm still torn about creating a claim check helper, but for some cases it might aid with consistency, i.e. attacker last claim is inconsistently set to defender entity claim for non-pvp attacks. May be good to just always set it as expected.

@Jikoo
Copy link
Member Author

Jikoo commented Apr 19, 2023

I was concerned about replacing the entityType == EntityType.XYZ comparisons with instanceof calls because that's been a long-standing Bukkit community optimization.

Running benchmarks in JMH yields weird results for the invocation and equality operator, i.e. entity.getType() == EntityType.XYZ; seems like there was some JIT de-optimization regarding it when it becomes a hot spot, which doesn't make sense to me. I was able to avoid it by manually setting up compiler black holes, but it makes me uncomfortable about the reliability of the results for that test. The other tests were completely consistent across the board.

As far as the rest goes, the loss is probably negligible due to call frequency (or lack thereof - not a super heavy load method) in the event of future weird NMS implementations that claim to be an EntityType but don't extend the corresponding interface.

@Jikoo Jikoo marked this pull request as ready for review April 21, 2023 16:42
@Jikoo
Copy link
Member Author

Jikoo commented Apr 21, 2023

Okay, there are definitely more changes I'd like to include in the other listeners, but this already is truly disgustingly large (57 commits and +/-920ish lines is offensive) so I've marked it as ready.

Note that I believe the line endings of the PlayerEventHandler are correct now, I think it still had Unix line endings when we specify Windows. IntelliJ does not allow mixed line endings by design, so any edit either must use existing line endings or convert all of them. I had thought it was my git installation messing up, but nope, "feature" of the IDE. Wonderful.

@RoboMWM, if you'd prefer I can split this up into several PRs:

  1. Split all relevant handlers into a separate combat handling listener
  2. 8 PRs for helper methods (2 commits each; the split itself and then the changes made to the split code)
  3. Additional polishing PRs/bugfixing for noted areas

If you don't mind it as-is but don't want the whitespace changes I can convert the one file to Unix line endings, but we probably should renormalize again at some point - I know I hit the same issue in a couple previous PRs.

@RoboMWM
Copy link

RoboMWM commented May 7, 2023

tbh, since you're consistently active here, I don't mind it all being in one PR, since I know you'll be looking to fix things that arise. It's when I get big PRs from people who aren't so reliably active and also change behavior too (like the two I pulled for wilderness-tp) that I'm never ever gonna do again.

@RoboMWM
Copy link

RoboMWM commented May 7, 2023

One way or another this stuff does need to be cleaned up, v20 break or not. Idea would be to break this into modules or something to separate out regular block/non-player entity protection vs pvp protection handling and such for v20 at least.

So I guess in your last comment's question, I presume that means going with 1?

@Jikoo
Copy link
Member Author

Jikoo commented May 7, 2023

So I guess in your last comment's question, I presume that means going with 1?

Ah, that was a 3 step process. Basically, 9 PRs in total that would ideally be more digestable: the initial split, then one per major code segment broken out into a helper method. If you're good with it as-is, all of the damage/combat-type handlers and helpers are in the EntityCombatHandler as of 17588a7

Idea would be to break this into modules or something to separate out regular block/non-player entity protection vs pvp protection handling and such for v20 at least.

This is less modularization and more of a prep/cleanup that would allow readable edits to logic here. All of the PVP-specific state handling should (barring the one handler on Monitor that is exclusively for PVP) now be inside #handlePvpXYZ, so when we do reach the modularization point it should be significantly easier to move to a PVP module.

@RoboMWM
Copy link

RoboMWM commented May 17, 2023

ah, re-read. In that case, option 3 is probably best. Or just put it in here. Significant refactor stuff would take anyone a long time to review - if anything, probably would be best to pair on such - but I trust your revisions since you have history of remaining active and fixing anything that's discovered along the way.

Jikoo added 2 commits May 18, 2023 09:00
Fireworks have become real projectiles and now actually have a shooter.
@Jikoo
Copy link
Member Author

Jikoo commented May 19, 2023

Okay, working on squashing into a more readable form. I will be including bugfixes too, but I will document them as I come across them, and they will receive their own commits so they can be more easily dropped if undesirable.

First up is #538 - GP did its best job to prevent broken vanilla behavior, but vanilla has since fixed that broken behavior. GP's fix silently disables firework PVP damage when its own PVP system is not enabled. If vanilla's anti-PVP is capable of handling something (which it is - I verified that disabling PVP in server.properties does prevent firework damage on 1.19.4), GP does not need to handle it.
Per my previous comment, GP handles fireworks just fine via its normal projectile handling code.

I had accidentally included this in the previous cleanup attempt and only realized that it was a behavioral change when I went to squash it all.

Jikoo added 14 commits May 19, 2023 10:37
Switched to instanceof with pattern variables instead of educated guess casting. Previously we've run into issues with custom pet plugins creating NMS-based entities that return an "incorrect" EntityType for the Bukkit interfaces they implement. The instanceof calls are marginally slower, but are much safer for the subsequent checks.
Use quick returns, pattern variable, and new switch for readability
Pattern variables, remove redundant check
Remove redundant PVP check, add quick returns
Use quick returns, extract duplicate code into a helper method
Use quick returns, pattern variables, previously extracted helper method for duplicate code
Use quick returns
Remove unreachable code
Note potential issues/missed cases
Jikoo added 8 commits May 21, 2023 12:29
Use quick returns
Remove unreachable villager handling
Make last claim caching more consistent
Use quick returns
Previous patch to this area for display names of owners cleaned up most of the smells that were in the original cleanup PR
Use quick returns, pattern variables
Listen to more specific event (no need to duplicate event system's handling)
Don't fetch player data for self-inflicted damage
Use existing local instances instead of static access - GriefPrevention is still a god object, but this is a step towards testability
Annotate handlers
Clean up a few comments
Also don't load pet owner name from disk if not sending messages. Theoretically doesn't cause issues because player-induced ignition should not occur if original damage was cancelled, but is an error.
Use quick returns and pattern variables, remove unnecessary guards
This does not fully address potion splashing issues (namely, it still follows a completely different rule set from regular damage) but it is progress. I'm also taking this opportunity to correct effected -> affected, because as a grammar nerd that has bothered me for ages.

Add some missing positive and negative effects - only slow fall is a vanilla-obtainable effect, but supporting other plugins is never a bad thing.
Use pattern variables
Reduce duplicate code with claim PVP handler
@Jikoo Jikoo force-pushed the dev/damage-cleanup-v2 branch from ee8a68c to 8641abe Compare May 22, 2023 17:06
Jikoo added 2 commits May 22, 2023 17:15
More cases were previously "allowed" in an unreachable segment of code, but even that was missing several cases.
Causing projectiles to ignore and pass through entities isn't easy to do "correctly" via the API without potentially becoming subtly incompatible with other plugins due to team manipulation.
@Jikoo
Copy link
Member Author

Jikoo commented May 22, 2023

Following the force push, the partial squashing is caught up with where I was in the previous iteration. Missing raider types doesn't have an issue attached to it, but projectiles spamming shooter is #850
Still could definitely merge a lot of logic between vehicles and entities, so I'll likely do that later, but a quick fix for now.

Jikoo added 2 commits June 12, 2023 12:06
This is (technically) a behavioral change.
Pets are prevented from engaging in PVP attacks if the defender is PVP immune. Theoretically this state is unable to occur in vanilla, but plugins could add additional triggers for pets attacking.
Pets are no longer checked with PVP rules if PVP rules are not enabled. The assumption is that either vanilla PVP protection is in place or another PVP plugin is in place.
- In vanilla, pets will not engage in PVP unprompted. Disabling PVP disables other players causing pets to attack, so an attack must be retaliatory.
- If another PVP plugin is in place, it should handle all of PVP.
@RoboMWM
Copy link

RoboMWM commented Jun 18, 2023

The only feasible way to know if this works well is to test all the stuff you've touched at the very least, which would be quite an effort. Since there seems to be interest in beta releases, I might go ahead and just pull and release anyways. Do you think it's ready for release?

Jikoo added 5 commits June 18, 2023 08:55
This will ideally make the code easier to follow and easier to move to modularize later.
All messages are error messages, and the documentation exists.
@Jikoo
Copy link
Member Author

Jikoo commented Jun 18, 2023

Had a bit more stuff to push out locally to resolve some notes. I think this should be release-quality, yes. The vast majority of it is tool-modified existing code.

@RoboMWM
Copy link

RoboMWM commented Jun 19, 2023

Ok, imma pull this and #1218, and make a beta release. Will create a discussion for that release.

@RoboMWM RoboMWM added this to the 16.18.2 milestone Jun 19, 2023
@RoboMWM RoboMWM merged commit 38c4cdb into GriefPrevention:master Jun 19, 2023
@Jikoo Jikoo deleted the dev/damage-cleanup-v2 branch June 19, 2023 22:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants