Unicopia/src/main/java/com/minelittlepony/unicopia/mixin/MixinPlayerEntity.java

141 lines
6.2 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.mixin;
2020-01-16 12:35:46 +01:00
2023-08-08 02:35:05 +02:00
import java.util.Map;
2020-01-16 12:35:46 +01:00
import org.spongepowered.asm.mixin.Mixin;
2021-08-18 17:30:59 +02:00
import org.spongepowered.asm.mixin.gen.Invoker;
2020-01-16 12:35:46 +01:00
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
2023-08-08 02:35:05 +02:00
import org.spongepowered.asm.mixin.injection.Redirect;
2020-01-16 12:35:46 +01:00
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import com.minelittlepony.unicopia.entity.duck.PlayerEntityDuck;
2023-11-08 13:58:31 +01:00
import com.minelittlepony.unicopia.EquinePredicates;
import com.minelittlepony.unicopia.entity.Equine;
import com.minelittlepony.unicopia.entity.player.Pony;
2020-01-16 12:35:46 +01:00
import com.mojang.datafixers.util.Either;
import net.minecraft.block.BlockState;
import net.minecraft.entity.EntityDimensions;
import net.minecraft.entity.EntityPose;
2020-01-16 12:35:46 +01:00
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.LivingEntity;
2020-06-26 11:44:47 +02:00
import net.minecraft.entity.attribute.DefaultAttributeContainer;
2021-08-04 15:38:03 +02:00
import net.minecraft.entity.damage.DamageSource;
2020-01-16 12:35:46 +01:00
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.stat.Stats;
2020-01-16 12:35:46 +01:00
import net.minecraft.util.Unit;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
2020-01-16 12:35:46 +01:00
@Mixin(PlayerEntity.class)
abstract class MixinPlayerEntity extends LivingEntity implements Equine.Container<Pony>, PlayerEntityDuck {
2020-01-16 12:35:46 +01:00
private MixinPlayerEntity() { super(null, null); }
2021-08-18 17:30:59 +02:00
@Override
@Invoker("updateCapeAngles")
public abstract void callUpdateCapeAngles();
2020-01-16 12:35:46 +01:00
@Override
public Equine<?> create() {
return new Pony((PlayerEntity)(Object)this);
2020-01-16 12:35:46 +01:00
}
@Inject(method = "createPlayerAttributes()Lnet/minecraft/entity/attribute/DefaultAttributeContainer$Builder;", at = @At("RETURN"))
private static void onCreateAttributes(CallbackInfoReturnable<DefaultAttributeContainer.Builder> info) {
Pony.registerAttributes(info.getReturnValue());
}
2021-12-22 10:15:09 +01:00
@ModifyVariable(method = "applyDamage(Lnet/minecraft/entity/damage/DamageSource;F)V", at = @At("HEAD"), ordinal = 0)
protected float modifyDamageAmount(float amount, DamageSource source) {
return get().modifyDamage(source, amount).orElse(amount);
}
2021-08-04 15:38:03 +02:00
@Inject(method = "handleFallDamage(FFLnet/minecraft/entity/damage/DamageSource;)Z", at = @At("HEAD"), cancellable = true)
private void onHandleFallDamage(float distance, float damageMultiplier, DamageSource cause, CallbackInfoReturnable<Boolean> info) {
get().onImpact(fallDistance, damageMultiplier, cause).ifPresent(newDistance -> {
PlayerEntity self = (PlayerEntity)(Object)this;
if (distance >= 2) {
self.increaseStat(Stats.FALL_ONE_CM, Math.round(distance * 100));
}
2021-08-04 15:38:03 +02:00
info.setReturnValue(super.handleFallDamage(newDistance, damageMultiplier, cause));
});
2020-01-16 12:35:46 +01:00
}
@Inject(method = "eatFood(Lnet/minecraft/world/World;Lnet/minecraft/item/ItemStack;)Lnet/minecraft/item/ItemStack;", at = @At("HEAD"))
private void onEatFood(World world, ItemStack stack, CallbackInfoReturnable<ItemStack> info) {
get().onEat(stack);
2020-06-26 11:44:47 +02:00
}
2020-01-16 12:35:46 +01:00
@Inject(method = "trySleep(Lnet/minecraft/util/math/BlockPos;)Lcom/mojang/datafixers/util/Either;",
at = @At("HEAD"),
cancellable = true)
private void onTrySleep(BlockPos pos, CallbackInfoReturnable<Either<PlayerEntity.SleepFailureReason, Unit>> info) {
2023-06-03 13:40:54 +02:00
if (!getWorld().isClient) {
get().trySleep(pos).ifPresent(reason -> {
((PlayerEntity)(Object)this).sendMessage(reason, true);
2020-01-16 12:35:46 +01:00
info.setReturnValue(Either.left(ServerPlayerEntity.SleepFailureReason.OTHER_PROBLEM));
});
2020-01-16 12:35:46 +01:00
}
}
@Inject(method = "dropItem(Lnet/minecraft/item/ItemStack;ZZ)Lnet/minecraft/entity/ItemEntity;",
at = @At("RETURN"))
private void onDropItem(ItemStack itemStack_1, boolean scatter, boolean retainOwnership, CallbackInfoReturnable<ItemEntity> info) {
get().onDropItem(info.getReturnValue());
2020-01-16 12:35:46 +01:00
}
2020-01-27 11:05:22 +01:00
@Inject(method = "getActiveEyeHeight(Lnet/minecraft/entity/EntityPose;Lnet/minecraft/entity/EntityDimensions;)F",
at = @At("RETURN"),
cancellable = true)
private void onGetActiveEyeHeight(EntityPose pose, EntityDimensions dimensions, CallbackInfoReturnable<Float> info) {
get().getMotion().getDimensions().calculateActiveEyeHeight(dimensions).ifPresent(info::setReturnValue);
}
2023-08-08 02:35:05 +02:00
/*
@Inject(method = "getDimensions(Lnet/minecraft/entity/EntityPose;)Lnet/minecraft/entity/EntityDimensions;",
at = @At("RETURN"),
cancellable = true)
private void onGetDimensions(EntityPose pose, CallbackInfoReturnable<EntityDimensions> info) {
get().getMotion().getDimensions().calculateDimensions().ifPresent(info::setReturnValue);
2023-08-08 02:35:05 +02:00
}*/
@Redirect(method = "getDimensions(Lnet/minecraft/entity/EntityPose;)Lnet/minecraft/entity/EntityDimensions;",
at = @At(
value = "INVOKE",
target = "Ljava/util/Map;getOrDefault(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;",
remap = false
))
private Object redirect_onGetDimensions(Map<EntityPose, EntityDimensions> self, Object key, Object def) {
return get().getMotion().getDimensions().calculateDimensions().orElse(self.getOrDefault((EntityPose)key, (EntityDimensions)def));
}
@Inject(method = "getBlockBreakingSpeed(Lnet/minecraft/block/BlockState;)F",
at = @At("RETURN"),
cancellable = true)
private void onGetBlockBreakingSpeed(BlockState state, CallbackInfoReturnable<Float> info) {
info.setReturnValue(info.getReturnValue() * get().getBlockBreakingSpeed());
}
2023-11-08 13:58:31 +01:00
@Override
protected int getNextAirUnderwater(int air) {
if (EquinePredicates.PLAYER_SEAPONY.test(this)) {
return super.getNextAirOnLand(air);
}
return super.getNextAirUnderwater(air);
}
@Override
protected int getNextAirOnLand(int air) {
if (EquinePredicates.PLAYER_SEAPONY.test(this)) {
return super.getNextAirUnderwater(air);
}
return super.getNextAirOnLand(air);
}
2020-01-16 12:35:46 +01:00
}