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

[1.19.x] Add Cancelable LivingEatEvent & LivingFoodEffectEvent #9134

Open
wants to merge 6 commits into
base: 1.19.x
Choose a base branch
from

Conversation

Adneths
Copy link
Contributor

@Adneths Adneths commented Nov 5, 2022

Parallel's this PR with the same features just in 1.19.x

@Matyrobbrt Matyrobbrt requested a review from a team November 5, 2022 11:07
@Matyrobbrt Matyrobbrt added Feature This request implements a new feature. Triage This request requires the active attention of the Triage Team. Requires labelling or reviews. 1.19 New Event This request implements a new event. labels Nov 5, 2022
Comment on lines 7 to 8
- public void m_38707_(int p_38708_, float p_38709_) {
+ public void eatInternal(int p_38708_, float p_38709_) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't remove and change the name of methods. Add a deprecation message to the new method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the deprecation be on the old method to encourage use of the new method that has a LivingEntity input. Also the method is not removed as m_38707_(int, float) still exists just calling eat(int, float, LivingEntity) which then calls eatInternal(int, float). So it effectively does the same thing just that it also fires the LivingEatEvent.
So I'm not sure why this is an issue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are replacing all calls to the basic #eat method, so this can be deprecated in favor of the LivingEntity counterpart. This can just be private in that case.

Comment on lines 12 to 16
-
+ @Deprecated
+ public void m_38707_(int p_38708_, float p_38709_) {
+ this.eat(p_38708_, p_38709_, null);
+ }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this unnecessary, it is still an eat event and should cause the LivingEatEvent to fire.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that's the case, this should be moved on top of your #eatInternal method to reduce the patch size.

Comment on lines 17 to 21
+ public void eat(int p_38708_, float p_38709_, @javax.annotation.Nullable net.minecraft.world.entity.LivingEntity entity) {
+ float[] ret = net.minecraftforge.common.ForgeHooks.onLivingEat(entity, p_38708_, p_38709_);
+ if(ret != null)
+ this.eatInternal((int)ret[0], ret[1]);
+ }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be an extension method to reduce patch size.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what you mean with extension

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scratch this, the private change I mentioned above would cause this to fail.

Comment on lines 666 to 674
- if (!p_21065_.f_46443_ && pair.getFirst() != null && p_21065_.f_46441_.m_188501_() < pair.getSecond()) {
- p_21066_.m_7292_(new MobEffectInstance(pair.getFirst()));
+ List<Pair<MobEffectInstance, Float>> ret = net.minecraftforge.common.ForgeHooks.onLivingFoodEffect(p_21066_, p_21064_);
+ if(ret != null)
+ {
+ for(Pair<MobEffectInstance, Float> pair : ret) {
+ if (!p_21065_.f_46443_ && pair.getFirst() != null && p_21065_.f_46441_.m_188501_() < pair.getSecond()) {
+ p_21066_.m_7292_(new MobEffectInstance(pair.getFirst()));
+ }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make patch smaller. Can unindent everything and have the if statement without braces.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the rest of the patch uses indent? Do I column align the code?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't indent more than previous lines have. If column align is matching the previous code indent with the new one, then yes.

Copy link
Contributor

@ChampionAsh5357 ChampionAsh5357 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LivingEatEvent and LivingFoodEffectEvent is essentially a redundancy issue. The only cases that is not covered in the latter event are cakes and effects. So, there is a massive overlap between the two events otherwise, which should be addressed in some capacity as otherwise we are just firing the same event twice. Though I do not know the best way to go about fixing this.

Comment on lines 666 to 674
- if (!p_21065_.f_46443_ && pair.getFirst() != null && p_21065_.f_46441_.m_188501_() < pair.getSecond()) {
- p_21066_.m_7292_(new MobEffectInstance(pair.getFirst()));
+ List<Pair<MobEffectInstance, Float>> ret = net.minecraftforge.common.ForgeHooks.onLivingFoodEffect(p_21066_, p_21064_);
+ if(ret != null)
+ {
+ for(Pair<MobEffectInstance, Float> pair : ret) {
+ if (!p_21065_.f_46443_ && pair.getFirst() != null && p_21065_.f_46441_.m_188501_() < pair.getSecond()) {
+ p_21066_.m_7292_(new MobEffectInstance(pair.getFirst()));
+ }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't indent more than previous lines have. If column align is matching the previous code indent with the new one, then yes.

Comment on lines 7 to 8
- public void m_38707_(int p_38708_, float p_38709_) {
+ public void eatInternal(int p_38708_, float p_38709_) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are replacing all calls to the basic #eat method, so this can be deprecated in favor of the LivingEntity counterpart. This can just be private in that case.

Comment on lines 12 to 16
-
+ @Deprecated
+ public void m_38707_(int p_38708_, float p_38709_) {
+ this.eat(p_38708_, p_38709_, null);
+ }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that's the case, this should be moved on top of your #eatInternal method to reduce the patch size.

Comment on lines 17 to 21
+ public void eat(int p_38708_, float p_38709_, @javax.annotation.Nullable net.minecraft.world.entity.LivingEntity entity) {
+ float[] ret = net.minecraftforge.common.ForgeHooks.onLivingEat(entity, p_38708_, p_38709_);
+ if(ret != null)
+ this.eatInternal((int)ret[0], ret[1]);
+ }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scratch this, the private change I mentioned above would cause this to fail.

* LivingEatEvent is fire when an LivingEntity eats food (item, cake, etc.).<br>
* Specifically it is fired for the method call {@link FoodData#eat()}.<br>
* <br>
* This event is fired via the {@link ForgeHooks#onLivingEat()} .<br>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would it matter? People have the ability to backtrace through the code, so they could just easily look it up where the event is initialized from.

* LivingFoodEffectEvent is fire when an LivingEntity eats food item.<br>
* Specifically it is fired for the method call {@link LivingEntity#addEatEffect()}.<br>
* <br>
* This event is fired via the {@link ForgeHooks#onLivingFoodEffect()} .<br>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would it matter? People have the ability to backtrace through the code, so they could just easily look it up where the event is initialized from.

this.saturaionAmount = saturationAmount;
}

public LivingEatEvent(@javax.annotation.Nullable LivingEntity entity, ItemStack foodItem, FoodProperties prop) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only needs to be one constructor which takes in the nullable entity, food and saturation, and the nullable item stack. FoodProperties is outside the scope as the event does not take it in.

Comment on lines 41 to 47
public ItemStack getFoodItem() {
return foodItem;
}

public List<Pair<MobEffectInstance, Float>> getEffects() {
return effectPairs;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Document things on a public API level. Class documents are never good enough.

Comment on lines 29 to 33
- FoodProperties foodproperties = p_38713_.m_41473_();
+ FoodProperties foodproperties = p_38714_.getFoodProperties(entity);
this.m_38707_(foodproperties.m_38744_(), foodproperties.m_38745_());
- this.m_38707_(foodproperties.m_38744_(), foodproperties.m_38745_());
+ float[] ret = net.minecraftforge.common.ForgeHooks.onLivingEat(entity, p_38714_, p_38714_.getFoodProperties(entity));
+ if(ret != null)
+ this.eatInternal((int)ret[0], ret[1]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be two removal two add now, but essentially it boils down to putting the condition and the execution statement on the same line (if and #eatInternal call)

import net.minecraftforge.eventbus.api.Cancelable;

/**
* LivingFoodEffectEvent is fire when an LivingEntity eats food item.<br>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LivingApplyEffectOnEatEvent or probably LivingApplyFoodEffectEvent? It's just a thought that describes the action being taken.

As for your sentence, 'the event is fired whenever a LivingEntity receives an effect from food regardless of if the food applies any effects'. Though this brings a redundancy issue.

@Adneths
Copy link
Contributor Author

Adneths commented Nov 19, 2022

LivingEatEvent and LivingFoodEffectEvent is essentially a redundancy issue. The only cases that is not covered in the latter event are cakes and effects. So, there is a massive overlap between the two events otherwise, which should be addressed in some capacity as otherwise we are just firing the same event twice. Though I do not know the best way to go about fixing this.

The way the code is written makes it hard to combine into one event. Because FoodData is changed at the start of Player#eat() while MobEffectInstance is applied in LivingEntity#eat() which is called at the end of Player#eat()

One solution I can think of is when FoodData#eat() with ItemStack is called it returns List<Pair<MobEffectInstance, Float>> after being modified. Then replace super.eat() with code copied from LivingEntity#eat() and change the way it processes food item effects. This choice does not change the method signatures but does mean Player#eat() does not really extend upon LivingEntity#eat() any more.

Another solution I came up with is to overload to get LivingEntity#eat(Level, ItemStack, List<Pair<MobEffectInstance, Float>>) so the list of effects can be specified outside of the method. This would allow the inheritance connection to remain.

I've implemented the second option into my code as of now.

@Adneths
Copy link
Contributor Author

Adneths commented Nov 30, 2022

@James103
So what do I do now, I'm unfamiliar with the process as a whole.

@sciwhiz12 sciwhiz12 added the Needs Rebase This PR requires a rebase to implement upstream changes (usually to fix merge conflicts). label Dec 16, 2022
@CLAassistant
Copy link

CLAassistant commented Jul 17, 2023

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.19 Feature This request implements a new feature. Needs Rebase This PR requires a rebase to implement upstream changes (usually to fix merge conflicts). New Event This request implements a new event. Triage This request requires the active attention of the Triage Team. Requires labelling or reviews.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants