Unicopia/src/main/java/com/minelittlepony/unicopia/player/PlayerGravityDelegate.java

135 lines
4.5 KiB
Java
Raw Normal View History

2018-09-12 01:29:49 +02:00
package com.minelittlepony.unicopia.player;
import com.minelittlepony.unicopia.InbtSerialisable;
import net.minecraft.block.Block;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
2018-09-12 01:29:49 +02:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.MobEffects;
import net.minecraft.init.SoundEvents;
2018-09-12 01:29:49 +02:00
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.DamageSource;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
2018-09-12 01:29:49 +02:00
class PlayerGravityDelegate implements IUpdatable<EntityPlayer>, InbtSerialisable {
2018-09-12 01:29:49 +02:00
private final IPlayer player;
private static final float MAXIMUM_FLIGHT_EXPERIENCE = 100;
private int ticksInAir = 0;
private float flightExperience = 0;
2018-09-12 01:29:49 +02:00
public boolean isFlying = false;
public PlayerGravityDelegate(IPlayer player) {
this.player = player;
}
@Override
public void onUpdate(EntityPlayer entity) {
entity.capabilities.allowFlying = entity.capabilities.isCreativeMode || player.getPlayerSpecies().canFly();
entity.capabilities.isFlying |= entity.capabilities.allowFlying && isFlying;
if (!entity.capabilities.isCreativeMode && !entity.isElytraFlying()) {
if (entity.capabilities.isFlying && !entity.isRiding()) {
entity.fallDistance = 0;
float exhaustion = (0.2F * ticksInAir++) / 100;
if (entity.isSprinting()) {
exhaustion *= 3.11F;
2018-09-12 01:29:49 +02:00
}
entity.addExhaustion(exhaustion * (1 - flightExperience));
if (entity.ticksExisted % 20000 == 0) {
addFlightExperience(entity);
}
} else {
ticksInAir = 0;
2018-09-12 01:29:49 +02:00
}
}
}
2018-09-12 01:29:49 +02:00
public void landHard(EntityPlayer player, float distance, float damageMultiplier) {
if (distance <= 0) {
return;
2018-09-12 01:29:49 +02:00
}
PotionEffect potioneffect = player.getActivePotionEffect(MobEffects.JUMP_BOOST);
float potion = potioneffect != null ? potioneffect.getAmplifier() + 1 : 0;
int i = MathHelper.ceil((distance - 8.0F - potion) * damageMultiplier);
if (i > 0) {
int j = MathHelper.floor(player.posX);
int k = MathHelper.floor(player.posY - 0.20000000298023224D);
int l = MathHelper.floor(player.posZ);
BlockPos pos = new BlockPos(j, k, l);
IBlockState state = player.world.getBlockState(pos);
Block block = state.getBlock();
if (state.getMaterial() != Material.AIR) {
player.playSound(getFallSound(i), 1, 1);
player.attackEntityFrom(DamageSource.FALL, i);
SoundType soundtype = block.getSoundType(state, player.getEntityWorld(), pos, player);
player.playSound(soundtype.getStepSound(), soundtype.getVolume() * 0.5f, soundtype.getPitch() * 0.75f);
}
}
}
protected SoundEvent getFallSound(int distance) {
return distance > 4 ? SoundEvents.ENTITY_PLAYER_BIG_FALL : SoundEvents.ENTITY_PLAYER_SMALL_FALL;
}
private void addFlightExperience(EntityPlayer entity) {
entity.addExperience(1);
flightExperience += (flightExperience - MAXIMUM_FLIGHT_EXPERIENCE) / 20;
2018-09-12 01:29:49 +02:00
}
public void updateFlightStat(EntityPlayer entity, boolean flying) {
if (!entity.capabilities.isCreativeMode) {
entity.capabilities.allowFlying = player.getPlayerSpecies().canFly();
if (entity.capabilities.allowFlying) {
entity.capabilities.isFlying |= flying;
isFlying = entity.capabilities.isFlying;
if (isFlying) {
ticksInAir = 0;
2018-09-12 01:29:49 +02:00
}
} else {
entity.capabilities.isFlying = false;
isFlying = false;
}
}
}
@Override
public void writeToNBT(NBTTagCompound compound) {
compound.setInteger("flightDuration", ticksInAir);
compound.setFloat("flightExperience", flightExperience);
2018-09-12 01:29:49 +02:00
compound.setBoolean("isFlying", isFlying);
}
@Override
public void readFromNBT(NBTTagCompound compound) {
ticksInAir = compound.getInteger("flightDuration");
flightExperience = compound.getFloat("flightExperience");
2018-09-12 01:29:49 +02:00
isFlying = compound.getBoolean("isFlying");
}
}