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

190 lines
6.5 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.mixin;
2020-01-16 12:35:46 +01:00
import java.util.Optional;
2020-01-16 12:35:46 +01:00
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
2021-02-17 16:05:37 +01:00
import org.spongepowered.asm.mixin.gen.Accessor;
2020-01-16 12:35:46 +01:00
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Constant;
2020-01-16 12:35:46 +01:00
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyConstant;
2020-01-16 12:35:46 +01:00
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import com.minelittlepony.unicopia.ability.magic.Caster;
import com.minelittlepony.unicopia.ability.magic.spell.DisguiseSpell;
2021-03-03 10:33:23 +01:00
import com.minelittlepony.unicopia.ability.magic.spell.SpellType;
import com.minelittlepony.unicopia.entity.Creature;
import com.minelittlepony.unicopia.entity.PonyContainer;
import com.minelittlepony.unicopia.entity.behaviour.Disguise;
import com.minelittlepony.unicopia.entity.player.Pony;
import com.minelittlepony.unicopia.entity.Equine;
2021-02-17 16:05:37 +01:00
import com.minelittlepony.unicopia.entity.Jumper;
import com.minelittlepony.unicopia.entity.Leaner;
2020-09-29 19:18:25 +02:00
import com.minelittlepony.unicopia.entity.ItemWielder;
2020-01-16 12:35:46 +01:00
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.attribute.DefaultAttributeContainer;
import net.minecraft.entity.damage.DamageSource;
2020-09-29 19:18:25 +02:00
import net.minecraft.item.ItemStack;
2021-08-04 15:38:03 +02:00
import net.minecraft.nbt.NbtCompound;
2020-09-29 19:18:25 +02:00
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
2020-01-16 12:35:46 +01:00
@Mixin(LivingEntity.class)
abstract class MixinLivingEntity extends Entity implements PonyContainer<Equine<?>>, ItemWielder, Jumper, Leaner {
2020-09-29 19:18:25 +02:00
@Shadow
protected ItemStack activeItemStack;
@Shadow
protected int itemUseTimeLeft;
2020-01-16 12:35:46 +01:00
@Shadow
private Optional<BlockPos> climbingPos;
private Equine<?> caster;
2020-01-16 12:35:46 +01:00
private MixinLivingEntity() { super(null, null); }
2020-09-29 19:18:25 +02:00
@Shadow
protected abstract void setLivingFlag(int mask, boolean value);
2020-01-16 12:35:46 +01:00
@Override
public Equine<?> create() {
return new Creature((LivingEntity)(Object)this);
2020-01-16 12:35:46 +01:00
}
@Override
public Equine<?> get() {
if (caster == null) {
caster = create();
}
2020-01-16 12:35:46 +01:00
return caster;
}
2021-02-17 16:05:37 +01:00
@Override
@Accessor("jumping")
public abstract boolean isJumping();
@Override
@Accessor("leaningPitch")
public abstract float getLeaningPitch();
@Override
@Accessor("leaningPitch")
public abstract void setLeaningPitch(float pitch);
@Inject(method = "createLivingAttributes()Lnet/minecraft/entity/attribute/DefaultAttributeContainer$Builder;", at = @At("RETURN"))
private static void onCreateAttributes(CallbackInfoReturnable<DefaultAttributeContainer.Builder> info) {
Creature.registerAttributes(info.getReturnValue());
}
@Inject(method = "isClimbing()Z", at = @At("HEAD"), cancellable = true)
public void onIsClimbing(CallbackInfoReturnable<Boolean> info) {
if (get() instanceof Pony && horizontalCollision) {
2021-03-03 10:33:23 +01:00
((Pony)get()).getSpellSlot().get(SpellType.DISGUISE, false)
.map(DisguiseSpell::getDisguise)
.filter(Disguise::canClimbWalls)
.ifPresent(v -> {
climbingPos = Optional.of(getBlockPos());
info.setReturnValue(true);
});
}
}
@Inject(method = "isPushable()Z", at = @At("HEAD"), cancellable = true)
private void onIsPushable(CallbackInfoReturnable<Boolean> info) {
Caster.of(this)
2021-03-03 10:33:23 +01:00
.flatMap(c -> c.getSpellSlot().get(SpellType.DISGUISE, false))
.map(DisguiseSpell::getDisguise)
.map(Disguise::getAppearance)
.filter(Entity::isPushable)
.ifPresent(v -> {
info.setReturnValue(false);
});
}
2020-05-05 16:42:14 +02:00
@Inject(method = "canSee(Lnet/minecraft/entity/Entity;)Z", at = @At("HEAD"), cancellable = true)
2020-01-16 12:35:46 +01:00
private void onCanSee(Entity other, CallbackInfoReturnable<Boolean> info) {
if (get().isInvisible()) {
2020-01-16 12:35:46 +01:00
info.setReturnValue(false);
}
}
@Inject(method = "jump()V", at = @At("RETURN"))
private void onJump(CallbackInfo info) {
get().onJump();
2020-01-16 12:35:46 +01:00
}
@Inject(method = "tick()V", at = @At("HEAD"), cancellable = true)
2020-01-16 12:35:46 +01:00
private void beforeTick(CallbackInfo info) {
if (get().beforeUpdate()) {
info.cancel();
}
2020-01-16 12:35:46 +01:00
}
@Inject(method = "tick()V", at = @At("RETURN"))
private void afterTick(CallbackInfo info) {
get().tick();
2020-01-16 12:35:46 +01:00
}
@Inject(method = "<clinit>()V", at = @At("RETURN"), remap = false)
2020-01-16 12:35:46 +01:00
private static void clinit(CallbackInfo info) {
Creature.boostrap();
2020-01-16 12:35:46 +01:00
}
2020-04-22 20:23:54 +02:00
@Inject(method = "damage(Lnet/minecraft/entity/damage/DamageSource;F)Z", at = @At("HEAD"), cancellable = true)
private void onDamage(DamageSource source, float amount, CallbackInfoReturnable<Boolean> info) {
get().onDamage(source, amount).ifPresent(info::setReturnValue);
}
2021-08-04 15:38:03 +02:00
@Inject(method = "writeCustomDataToNbt(Lnet/minecraft/nbt/NbtCompound;)V", at = @At("HEAD"))
private void onWriteCustomDataToTag(NbtCompound tag, CallbackInfo info) {
tag.put("unicopia_caster", get().toNBT());
}
2021-08-04 15:38:03 +02:00
@Inject(method = "readCustomDataFromNbt(Lnet/minecraft/nbt/NbtCompound;)V", at = @At("HEAD"))
private void onReadCustomDataFromTag(NbtCompound tag, CallbackInfo info) {
if (tag.contains("unicopia_caster")) {
get().fromNBT(tag.getCompound("unicopia_caster"));
}
}
@ModifyConstant(method = "travel(Lnet/minecraft/util/math/Vec3d;)V", constant = {
@Constant(doubleValue = 0.08D),
@Constant(doubleValue = 0.01D)
})
private double modifyGravity(double initial) {
return get().getPhysics().calcGravity(initial);
}
2020-09-29 19:18:25 +02:00
@Override
public void updateItemUsage(Hand hand, ItemStack stack, int time) {
activeItemStack = stack;
itemUseTimeLeft = time;
if (!world.isClient) {
setLivingFlag(1, !stack.isEmpty());
setLivingFlag(2, hand == Hand.OFF_HAND);
}
}
@Override
2021-08-04 15:38:03 +02:00
public BlockPos getBlockPos() {
if (get().getPhysics().isGravityNegative()) {
return get().getPhysics().getHeadPosition();
}
2021-08-04 15:38:03 +02:00
return super.getBlockPos();
}
@Override
protected void spawnSprintingParticles() {
if (get().getPhysics().isGravityNegative()) {
get().getPhysics().spawnSprintingParticles();
} else {
super.spawnSprintingParticles();
}
}
2020-01-16 12:35:46 +01:00
}