This repository has been archived by the owner on Jan 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'eat-event' into 1.18-forge
- Loading branch information
Showing
5 changed files
with
92 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/org/auioc/mods/ahutils/mixin/server/MixinLivingEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.auioc.mods.ahutils.mixin.server; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import com.mojang.datafixers.util.Pair; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Redirect; | ||
import net.minecraft.world.effect.MobEffectInstance; | ||
import net.minecraft.world.entity.LivingEntity; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraft.world.level.Level; | ||
|
||
@Mixin(value = LivingEntity.class) | ||
public class MixinLivingEntity { | ||
|
||
// @org.spongepowered.asm.mixin.Debug(export = true, print = true) | ||
@Redirect( | ||
method = "Lnet/minecraft/world/entity/LivingEntity;eat(Lnet/minecraft/world/level/Level;Lnet/minecraft/world/item/ItemStack;)Lnet/minecraft/world/item/ItemStack;", | ||
at = @At( | ||
value = "INVOKE", | ||
target = "Lnet/minecraft/world/entity/LivingEntity;addEatEffect(Lnet/minecraft/world/item/ItemStack;Lnet/minecraft/world/level/Level;Lnet/minecraft/world/entity/LivingEntity;)V", | ||
ordinal = 0 | ||
), | ||
require = 1, | ||
allow = 1 | ||
) | ||
private void onEatAddEffect(LivingEntity thisClass, ItemStack food, Level level, LivingEntity entity) { | ||
if (!level.isClientSide) { | ||
List<MobEffectInstance> effects = new ArrayList<>(); | ||
for (Pair<MobEffectInstance, Float> pair : food.getItem().getFoodProperties().getEffects()) { | ||
if (pair.getFirst() != null && level.random.nextFloat() < pair.getSecond()) { | ||
effects.add(new MobEffectInstance(pair.getFirst())); | ||
} | ||
} | ||
effects = org.auioc.mods.ahutils.server.event.ServerEventRegistry.postLivingEatAddEffectEvent(entity, food, effects); | ||
for (MobEffectInstance instance : effects) { | ||
entity.addEffect(instance); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/org/auioc/mods/ahutils/server/event/impl/LivingEatAddEffectEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.auioc.mods.ahutils.server.event.impl; | ||
|
||
import java.util.List; | ||
import net.minecraft.world.effect.MobEffectInstance; | ||
import net.minecraft.world.entity.LivingEntity; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraftforge.event.entity.living.LivingEvent; | ||
import net.minecraftforge.eventbus.api.Cancelable; | ||
|
||
@Cancelable | ||
public class LivingEatAddEffectEvent extends LivingEvent { | ||
|
||
private final ItemStack food; | ||
private final List<MobEffectInstance> effects; | ||
|
||
public LivingEatAddEffectEvent(LivingEntity entity, ItemStack food, List<MobEffectInstance> effects) { | ||
super(entity); | ||
this.food = food; | ||
this.effects = effects; | ||
} | ||
|
||
public ItemStack getFood() { | ||
return food; | ||
} | ||
|
||
public List<MobEffectInstance> getEffects() { | ||
return effects; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters