Tweaked the batpony ability to work better with the new flying mechanics

This commit is contained in:
Sollace 2020-10-02 14:04:52 +02:00
parent 92bf11b87c
commit 4f9787a7b0
2 changed files with 45 additions and 26 deletions

View file

@ -1,17 +1,21 @@
package com.minelittlepony.unicopia.ability; package com.minelittlepony.unicopia.ability;
import com.minelittlepony.unicopia.Race; 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.PlayerAttributes;
import com.minelittlepony.unicopia.entity.player.Pony; import com.minelittlepony.unicopia.entity.player.Pony;
import com.minelittlepony.unicopia.util.RayTraceHelper;
import net.minecraft.entity.attribute.EntityAttributeInstance; 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. * A magic casting ability for unicorns.
* (only shields for now) * (only shields for now)
*/ */
public class BatPonyHangAbility implements Ability<Numeric> { public class BatPonyHangAbility implements Ability<Multi> {
@Override @Override
public int getWarmupTime(Pony player) { public int getWarmupTime(Pony player) {
@ -29,34 +33,45 @@ public class BatPonyHangAbility implements Ability<Numeric> {
} }
@Override @Override
public Numeric tryActivate(Pony player) { public Multi tryActivate(Pony player) {
EntityAttributeInstance attr = player.getOwner().getAttributeInstance(PlayerAttributes.ENTITY_GRAVTY_MODIFIER); if (player.isHanging()) {
return new Multi(BlockPos.ZERO, 0);
if (attr.hasModifier(PlayerAttributes.BAT_HANGING)) {
return new Numeric(0);
} else if (player.canHangAt()) {
return new Numeric(1);
} }
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 @Override
public Numeric.Serializer<Numeric> getSerializer() { public Multi.Serializer<Multi> getSerializer() {
return Numeric.SERIALIZER; return Multi.SERIALIZER;
} }
@Override @Override
public void apply(Pony player, Numeric data) { public void apply(Pony player, Multi data) {
EntityAttributeInstance attr = player.getOwner().getAttributeInstance(PlayerAttributes.ENTITY_GRAVTY_MODIFIER); 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); attr.removeModifier(PlayerAttributes.BAT_HANGING);
return; 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); attr.addPersistentModifier(PlayerAttributes.BAT_HANGING);
} }
} }

View file

@ -34,7 +34,6 @@ import com.mojang.authlib.GameProfile;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.attribute.DefaultAttributeContainer; import net.minecraft.entity.attribute.DefaultAttributeContainer;
import net.minecraft.entity.attribute.EntityAttributeInstance;
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;
@ -244,26 +243,31 @@ public class Pony implements Caster<PlayerEntity>, Equine<PlayerEntity>, Transmi
return false; return false;
} }
public boolean canHangAt() { public boolean isHanging() {
BlockPos above = getOrigin(); return entity.getAttributeInstance(PlayerAttributes.ENTITY_GRAVTY_MODIFIER).hasModifier(PlayerAttributes.BAT_HANGING);
above = new BlockPos(above.getX(), getOwner().getEyeY() + 1, above.getZ()); }
BlockState state = getWorld().getBlockState(above);
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 @Override
public void tick() { public void tick() {
EntityAttributeInstance attr = entity.getAttributes().getCustomInstance(PlayerAttributes.ENTITY_GRAVTY_MODIFIER); if (isHanging()) {
if (attr.hasModifier(PlayerAttributes.BAT_HANGING)) {
gravity.isFlyingSurvival = false; gravity.isFlyingSurvival = false;
gravity.isFlyingEither = false; gravity.isFlyingEither = false;
entity.abilities.flying = false; entity.abilities.flying = false;
if (Entity.squaredHorizontalLength(entity.getVelocity()) > 0.01 || entity.isSneaking() || !canHangAt()) { if (Entity.squaredHorizontalLength(entity.getVelocity()) > 0.01 || entity.isSneaking() || !canHangAt(getHangingPos())) {
attr.removeModifier(PlayerAttributes.BAT_HANGING); entity.getAttributes().getCustomInstance(PlayerAttributes.ENTITY_GRAVTY_MODIFIER).removeModifier(PlayerAttributes.BAT_HANGING);
entity.calculateDimensions(); entity.calculateDimensions();
} }
} }