From 4f9787a7b01ed073253bba4b401e8ea7df628785 Mon Sep 17 00:00:00 2001 From: Sollace Date: Fri, 2 Oct 2020 14:04:52 +0200 Subject: [PATCH] Tweaked the batpony ability to work better with the new flying mechanics --- .../unicopia/ability/BatPonyHangAbility.java | 45 ++++++++++++------- .../unicopia/entity/player/Pony.java | 26 ++++++----- 2 files changed, 45 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/ability/BatPonyHangAbility.java b/src/main/java/com/minelittlepony/unicopia/ability/BatPonyHangAbility.java index ee14e350..722d8c53 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/BatPonyHangAbility.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/BatPonyHangAbility.java @@ -1,17 +1,21 @@ package com.minelittlepony.unicopia.ability; import com.minelittlepony.unicopia.Race; -import com.minelittlepony.unicopia.ability.data.Numeric; +import com.minelittlepony.unicopia.ability.data.Multi; import com.minelittlepony.unicopia.entity.player.PlayerAttributes; import com.minelittlepony.unicopia.entity.player.Pony; +import com.minelittlepony.unicopia.util.RayTraceHelper; import net.minecraft.entity.attribute.EntityAttributeInstance; +import net.minecraft.predicate.entity.EntityPredicates; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Vec3d; /** * A magic casting ability for unicorns. * (only shields for now) */ -public class BatPonyHangAbility implements Ability { +public class BatPonyHangAbility implements Ability { @Override public int getWarmupTime(Pony player) { @@ -29,34 +33,45 @@ public class BatPonyHangAbility implements Ability { } @Override - public Numeric tryActivate(Pony player) { + public Multi tryActivate(Pony player) { - EntityAttributeInstance attr = player.getOwner().getAttributeInstance(PlayerAttributes.ENTITY_GRAVTY_MODIFIER); - - if (attr.hasModifier(PlayerAttributes.BAT_HANGING)) { - return new Numeric(0); - } else if (player.canHangAt()) { - return new Numeric(1); + if (player.isHanging()) { + return new Multi(BlockPos.ZERO, 0); } - return null; + BlockPos poss = RayTraceHelper.doTrace(player.getOwner(), 5, 1, EntityPredicates.EXCEPT_SPECTATOR).getBlockPos().orElse(null); + if (poss != null) { + boolean air = player.getWorld().isAir(poss.down()) && player.getWorld().isAir(poss.down(2)); + + if (air && player.canHangAt(poss)) { + return new Multi(poss, 1); + } + } + + return RayTraceHelper.doTrace(player.getOwner(), 5, 1, EntityPredicates.EXCEPT_SPECTATOR).getBlockPos() + .map(BlockPos::down) + .filter(pos -> player.getWorld().isAir(pos) && player.getWorld().isAir(pos.down()) && player.canHangAt(pos)) + .map(pos -> new Multi(pos, 1)) + .orElse(null); } @Override - public Numeric.Serializer getSerializer() { - return Numeric.SERIALIZER; + public Multi.Serializer getSerializer() { + return Multi.SERIALIZER; } @Override - public void apply(Pony player, Numeric data) { + public void apply(Pony player, Multi data) { EntityAttributeInstance attr = player.getOwner().getAttributeInstance(PlayerAttributes.ENTITY_GRAVTY_MODIFIER); - if (data.type == 0 && attr.hasModifier(PlayerAttributes.BAT_HANGING)) { + if (data.hitType == 0 && attr.hasModifier(PlayerAttributes.BAT_HANGING)) { attr.removeModifier(PlayerAttributes.BAT_HANGING); return; } - if (data.type == 1 && player.canHangAt()) { + if (data.hitType == 1 && player.canHangAt(data.pos())) { + player.getOwner().teleport(data.x + 0.5, data.y - 2, data.z + 0.5); + player.getOwner().setVelocity(Vec3d.ZERO); attr.addPersistentModifier(PlayerAttributes.BAT_HANGING); } } diff --git a/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java b/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java index a8c09812..eb4eae7f 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java @@ -34,7 +34,6 @@ import com.mojang.authlib.GameProfile; import net.minecraft.block.BlockState; import net.minecraft.entity.Entity; import net.minecraft.entity.attribute.DefaultAttributeContainer; -import net.minecraft.entity.attribute.EntityAttributeInstance; import net.minecraft.entity.data.DataTracker; import net.minecraft.entity.data.TrackedData; import net.minecraft.entity.data.TrackedDataHandlerRegistry; @@ -244,26 +243,31 @@ public class Pony implements Caster, Equine, Transmi return false; } - public boolean canHangAt() { - BlockPos above = getOrigin(); - above = new BlockPos(above.getX(), getOwner().getEyeY() + 1, above.getZ()); - BlockState state = getWorld().getBlockState(above); + public boolean isHanging() { + return entity.getAttributeInstance(PlayerAttributes.ENTITY_GRAVTY_MODIFIER).hasModifier(PlayerAttributes.BAT_HANGING); + } - return state.hasSolidTopSurface(getWorld(), above, getEntity(), Direction.DOWN); + public boolean canHangAt(BlockPos pos) { + BlockState state = getWorld().getBlockState(pos); + + return state.hasSolidTopSurface(getWorld(), pos, getEntity(), Direction.DOWN); + } + + private BlockPos getHangingPos() { + BlockPos pos = getOrigin(); + return new BlockPos(pos.getX(), getOwner().getEyeY() + 1, pos.getZ()); } @Override public void tick() { - EntityAttributeInstance attr = entity.getAttributes().getCustomInstance(PlayerAttributes.ENTITY_GRAVTY_MODIFIER); - - if (attr.hasModifier(PlayerAttributes.BAT_HANGING)) { + if (isHanging()) { gravity.isFlyingSurvival = false; gravity.isFlyingEither = false; entity.abilities.flying = false; - if (Entity.squaredHorizontalLength(entity.getVelocity()) > 0.01 || entity.isSneaking() || !canHangAt()) { - attr.removeModifier(PlayerAttributes.BAT_HANGING); + if (Entity.squaredHorizontalLength(entity.getVelocity()) > 0.01 || entity.isSneaking() || !canHangAt(getHangingPos())) { + entity.getAttributes().getCustomInstance(PlayerAttributes.ENTITY_GRAVTY_MODIFIER).removeModifier(PlayerAttributes.BAT_HANGING); entity.calculateDimensions(); } }