Fixed changlings getting sickness effects before actually eating

This commit is contained in:
Sollace 2020-05-05 18:48:12 +02:00
parent 07d59d7f0b
commit 9f8194dc12
6 changed files with 41 additions and 47 deletions

View file

@ -8,7 +8,6 @@ import com.minelittlepony.unicopia.util.NbtSerialisable;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.projectile.ProjectileEntity; import net.minecraft.entity.projectile.ProjectileEntity;
import net.minecraft.item.ItemStack;
public interface Ponylike extends NbtSerialisable, Updatable { public interface Ponylike extends NbtSerialisable, Updatable {
Race getSpecies(); Race getSpecies();
@ -47,13 +46,6 @@ public interface Ponylike extends NbtSerialisable, Updatable {
return false; return false;
} }
/**
* Called when this player finishes eating food.
*/
default void onUse(ItemStack stack) {
}
/** /**
* Called when this entity jumps * Called when this entity jumps
*/ */

View file

@ -8,7 +8,6 @@ import com.minelittlepony.unicopia.ability.AbilityDispatcher;
import com.minelittlepony.unicopia.enchanting.PageOwner; import com.minelittlepony.unicopia.enchanting.PageOwner;
import com.minelittlepony.unicopia.entity.FlightControl; import com.minelittlepony.unicopia.entity.FlightControl;
import com.minelittlepony.unicopia.entity.Trap; import com.minelittlepony.unicopia.entity.Trap;
import com.minelittlepony.unicopia.item.UEffects;
import com.minelittlepony.unicopia.magic.Affinity; import com.minelittlepony.unicopia.magic.Affinity;
import com.minelittlepony.unicopia.magic.AttachedMagicEffect; import com.minelittlepony.unicopia.magic.AttachedMagicEffect;
import com.minelittlepony.unicopia.magic.HeldMagicEffect; import com.minelittlepony.unicopia.magic.HeldMagicEffect;
@ -18,6 +17,8 @@ import com.minelittlepony.unicopia.magic.spell.SpellRegistry;
import com.minelittlepony.unicopia.network.Channel; import com.minelittlepony.unicopia.network.Channel;
import com.minelittlepony.unicopia.network.EffectSync; import com.minelittlepony.unicopia.network.EffectSync;
import com.minelittlepony.unicopia.network.MsgPlayerCapabilities; import com.minelittlepony.unicopia.network.MsgPlayerCapabilities;
import com.minelittlepony.unicopia.toxin.Toxicity;
import com.minelittlepony.unicopia.toxin.Toxin;
import com.minelittlepony.util.BasicEasingInterpolator; import com.minelittlepony.util.BasicEasingInterpolator;
import com.minelittlepony.util.IInterpolator; import com.minelittlepony.util.IInterpolator;
import com.mojang.datafixers.util.Either; import com.mojang.datafixers.util.Either;
@ -28,12 +29,9 @@ import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.data.DataTracker; import net.minecraft.entity.data.DataTracker;
import net.minecraft.entity.data.TrackedData; import net.minecraft.entity.data.TrackedData;
import net.minecraft.entity.data.TrackedDataHandlerRegistry; import net.minecraft.entity.data.TrackedDataHandlerRegistry;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerEntity.SleepFailureReason; import net.minecraft.entity.player.PlayerEntity.SleepFailureReason;
import net.minecraft.entity.projectile.ProjectileEntity; import net.minecraft.entity.projectile.ProjectileEntity;
import net.minecraft.item.FoodComponent;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
@ -42,7 +40,6 @@ import net.minecraft.util.Hand;
import net.minecraft.util.Unit; import net.minecraft.util.Unit;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
import net.minecraft.world.Difficulty;
public class PlayerImpl implements Pony, MagicReserves { public class PlayerImpl implements Pony, MagicReserves {
@ -365,28 +362,9 @@ public class PlayerImpl implements Pony, MagicReserves {
} }
@Override @Override
public void onUse(ItemStack stack) { public void onEat(ItemStack stack) {
if (getSpecies() == Race.CHANGELING) { if (getSpecies() == Race.CHANGELING) {
PlayerEntity player = getOwner(); Toxin.POISON.afflict(getOwner(), Toxicity.SAFE, stack);
FoodComponent food = stack.getItem().getFoodComponent();
if (food != null) {
int health = food.getHunger();
float saturation = food.getSaturationModifier();
player.getHungerManager().add(-health/2, -saturation/2);
player.addStatusEffect(new StatusEffectInstance(StatusEffects.NAUSEA, 100, 3, true, true));
} else {
player.addStatusEffect(new StatusEffectInstance(StatusEffects.NAUSEA, 200, 3, true, true));
}
if (player.world.getDifficulty() != Difficulty.PEACEFUL && player.world.random.nextInt(20) == 0) {
player.addStatusEffect(new StatusEffectInstance(UEffects.FOOD_POISONING, 3, 2, true, true));
}
player.addStatusEffect(new StatusEffectInstance(StatusEffects.WEAKNESS, 2000, 2, true, true));
} }
} }

View file

@ -15,6 +15,7 @@ import com.mojang.authlib.GameProfile;
import com.mojang.datafixers.util.Either; import com.mojang.datafixers.util.Either;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Unit; import net.minecraft.util.Unit;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
@ -75,6 +76,11 @@ public interface Pony extends Caster<PlayerEntity>, RaceContainer<PlayerEntity>,
*/ */
float onImpact(float distance); float onImpact(float distance);
/**
* Called whenever the player eats something.
*/
void onEat(ItemStack food);
/** /**
* Attempts to sleep in a bed. * Attempts to sleep in a bed.
* *

View file

@ -41,15 +41,6 @@ abstract class MixinLivingEntity extends Entity implements PonyContainer<Ponylik
} }
} }
@Inject(method = "tickActiveItemStack()V", at = @At("HEAD"))
private void onFinishUsing(CallbackInfo info) {
LivingEntity self = (LivingEntity)(Object)this;
if (!self.getActiveItem().isEmpty() && self.isUsingItem()) {
get().onUse(self.getActiveItem());
}
}
@Inject(method = "jump()V", at = @At("RETURN")) @Inject(method = "jump()V", at = @At("RETURN"))
private void onJump(CallbackInfo info) { private void onJump(CallbackInfo info) {
get().onJump(); get().onJump();

View file

@ -22,6 +22,7 @@ import net.minecraft.item.ItemStack;
import net.minecraft.util.Unit; import net.minecraft.util.Unit;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.GameMode; import net.minecraft.world.GameMode;
import net.minecraft.world.World;
@Mixin(PlayerEntity.class) @Mixin(PlayerEntity.class)
abstract class MixinPlayerEntity extends LivingEntity implements PonyContainer<Pony> { abstract class MixinPlayerEntity extends LivingEntity implements PonyContainer<Pony> {
@ -40,6 +41,14 @@ abstract class MixinPlayerEntity extends LivingEntity implements PonyContainer<P
return get().onImpact(distance); return get().onImpact(distance);
} }
@Inject(method = "eatFood(Lnet/minecraft/world/World;Lnet/minecraft/item/ItemStack;)Lnet/minecraft/world/World;net/minecraft/item/ItemStack;",
at = @At("HEAD"))
public void onEatFood(World world, ItemStack stack, CallbackInfoReturnable<ItemStack> info) {
if (stack.isFood()) {
get().onEat(stack);
}
}
@Inject(method = "trySleep(Lnet/minecraft/util/math/BlockPos;)Lcom/mojang/datafixers/util/Either;", @Inject(method = "trySleep(Lnet/minecraft/util/math/BlockPos;)Lcom/mojang/datafixers/util/Either;",
at = @At("HEAD"), at = @At("HEAD"),
cancellable = true) cancellable = true)

View file

@ -1,22 +1,38 @@
package com.minelittlepony.unicopia.toxin; package com.minelittlepony.unicopia.toxin;
import com.minelittlepony.unicopia.item.UEffects;
import net.minecraft.entity.effect.StatusEffect; import net.minecraft.entity.effect.StatusEffect;
import net.minecraft.entity.effect.StatusEffectInstance; import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects; import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.FoodComponent;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.world.Difficulty;
@FunctionalInterface @FunctionalInterface
public interface Toxin { public interface Toxin {
Predicate ONE_EVERY_30_TICKS = (player, toxicity, stack) -> player.world.random.nextInt(30) == 0; Predicate ONE_EVERY_30_TICKS = (player, toxicity, stack) -> player.world.random.nextInt(30) == 0;
Toxin FOOD = (player, toxicity, stack) -> toxicity.afflict(player, toxicity, stack);
Toxin DAMAGE = ONE_EVERY_30_TICKS.then(of(StatusEffects.INSTANT_DAMAGE, 1, 1)); Toxin DAMAGE = ONE_EVERY_30_TICKS.then(of(StatusEffects.INSTANT_DAMAGE, 1, 1));
Toxin RADIOACTIVITY = ONE_EVERY_30_TICKS.then(of(StatusEffects.GLOWING, 10, 1)); Toxin RADIOACTIVITY = ONE_EVERY_30_TICKS.then(of(StatusEffects.GLOWING, 10, 1));
Toxin NAUSEA = of(StatusEffects.NAUSEA, 30, 1); Toxin NAUSEA = of(StatusEffects.NAUSEA, 30, 1);
Toxin WEAK_NAUSEA = of(StatusEffects.NAUSEA, 3, 1); Toxin WEAK_NAUSEA = of(StatusEffects.NAUSEA, 3, 1);
Toxin STRENGTH = of(StatusEffects.STRENGTH, 30, 1); Toxin STRENGTH = of(StatusEffects.STRENGTH, 30, 1);
Toxin BLINDNESS = of(StatusEffects.BLINDNESS, 30, 1); Toxin BLINDNESS = of(StatusEffects.BLINDNESS, 30, 1);
Toxin FOOD = (player, toxicity, stack) -> toxicity.afflict(player, toxicity, stack); Toxin POISON = (player, toxicity, stack) -> {
FoodComponent food = stack.getItem().getFoodComponent();
player.getHungerManager().add(-food.getHunger()/2, -food.getSaturationModifier()/2);
afflict(player, StatusEffects.NAUSEA, 1700, 10);
if (player.world.getDifficulty() != Difficulty.PEACEFUL && player.world.random.nextInt(20) == 0) {
afflict(player, UEffects.FOOD_POISONING, 150, 2);
}
afflict(player, StatusEffects.WEAKNESS, 2000, 20);
};
void afflict(PlayerEntity player, Toxicity toxicity, ItemStack stack); void afflict(PlayerEntity player, Toxicity toxicity, ItemStack stack);
@ -29,9 +45,11 @@ public interface Toxin {
} }
static Toxin of(StatusEffect effect, int duration, int amplifier) { static Toxin of(StatusEffect effect, int duration, int amplifier) {
return (player, toxicity, stack) -> { return (player, toxicity, stack) -> afflict(player, effect, duration, amplifier);
}
static void afflict(PlayerEntity player, StatusEffect effect, int duration, int amplifier) {
player.addStatusEffect(new StatusEffectInstance(effect, duration, amplifier, false, false)); player.addStatusEffect(new StatusEffectInstance(effect, duration, amplifier, false, false));
};
} }
interface Predicate { interface Predicate {