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
base: 1.19.x
Are you sure you want to change the base?
Conversation
- public void m_38707_(int p_38708_, float p_38709_) { | ||
+ public void eatInternal(int p_38708_, float p_38709_) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
- | ||
+ @Deprecated | ||
+ public void m_38707_(int p_38708_, float p_38709_) { | ||
+ this.eat(p_38708_, p_38709_, null); | ||
+ } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
+ 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]); | ||
+ } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
src/test/java/net/minecraftforge/debug/entity/living/LivingFoodEventTest.java
Show resolved
Hide resolved
src/test/java/net/minecraftforge/debug/entity/living/LivingFoodEventTest.java
Outdated
Show resolved
Hide resolved
src/test/java/net/minecraftforge/debug/entity/living/LivingFoodEventTest.java
Outdated
Show resolved
Hide resolved
- 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())); | ||
+ } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
patches/minecraft/net/minecraft/world/entity/LivingEntity.java.patch
Outdated
Show resolved
Hide resolved
src/main/java/net/minecraftforge/event/entity/living/LivingFoodEffectEvent.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this 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.
- 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())); | ||
+ } |
There was a problem hiding this comment.
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.
- public void m_38707_(int p_38708_, float p_38709_) { | ||
+ public void eatInternal(int p_38708_, float p_38709_) { |
There was a problem hiding this comment.
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.
- | ||
+ @Deprecated | ||
+ public void m_38707_(int p_38708_, float p_38709_) { | ||
+ this.eat(p_38708_, p_38709_, null); | ||
+ } |
There was a problem hiding this comment.
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.
+ 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]); | ||
+ } |
There was a problem hiding this comment.
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> |
There was a problem hiding this comment.
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> |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
public ItemStack getFoodItem() { | ||
return foodItem; | ||
} | ||
|
||
public List<Pair<MobEffectInstance, Float>> getEffects() { | ||
return effectPairs; | ||
} |
There was a problem hiding this comment.
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.
- 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]); |
There was a problem hiding this comment.
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> |
There was a problem hiding this comment.
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.
The way the code is written makes it hard to combine into one event. Because One solution I can think of is when Another solution I came up with is to overload to get I've implemented the second option into my code as of now. |
@James103 |
Parallel's this PR with the same features just in 1.19.x