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

248 lines
7.5 KiB
Java
Raw Normal View History

2018-09-12 01:29:49 +02:00
package com.minelittlepony.unicopia.player;
import com.minelittlepony.unicopia.Race;
import com.minelittlepony.unicopia.UClient;
import com.minelittlepony.unicopia.Unicopia;
import com.minelittlepony.unicopia.network.EffectSync;
import com.minelittlepony.unicopia.network.MsgPlayerCapabilities;
2018-09-12 01:29:49 +02:00
import com.minelittlepony.unicopia.spell.ICaster;
import com.minelittlepony.unicopia.spell.IMagicEffect;
import com.minelittlepony.unicopia.spell.SpellRegistry;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
2018-09-12 01:29:49 +02:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
2018-09-12 01:29:49 +02:00
import net.minecraft.init.MobEffects;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.datasync.DataParameter;
import net.minecraft.network.datasync.DataSerializers;
import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.network.play.server.SPacketSetPassengers;
2018-09-12 01:29:49 +02:00
import net.minecraft.potion.PotionEffect;
import net.minecraft.stats.StatList;
2018-09-12 01:29:49 +02:00
class PlayerCapabilities implements IPlayer, ICaster<EntityPlayer> {
private static final DataParameter<Integer> PLAYER_RACE = EntityDataManager
.createKey(EntityPlayer.class, DataSerializers.VARINT);
private static final DataParameter<Float> EXERTION = EntityDataManager
.createKey(EntityPlayer.class, DataSerializers.FLOAT);
2018-09-12 01:29:49 +02:00
private static final DataParameter<NBTTagCompound> EFFECT = EntityDataManager
.createKey(EntityPlayer.class, DataSerializers.COMPOUND_TAG);
2018-09-12 01:29:49 +02:00
private final PlayerAbilityDelegate powers = new PlayerAbilityDelegate(this);
private final PlayerGravityDelegate gravity = new PlayerGravityDelegate(this);
2018-09-19 09:09:30 +02:00
private final PlayerAttributes attributes = new PlayerAttributes();
private final EffectSync<EntityPlayer> effectDelegate = new EffectSync<>(this, EFFECT);
private float nextStepDistance = 1;
2018-09-12 01:29:49 +02:00
private EntityPlayer entity;
PlayerCapabilities(EntityPlayer player) {
setOwner(player);
player.getDataManager().register(PLAYER_RACE, Race.HUMAN.ordinal());
player.getDataManager().register(EXERTION, 0F);
player.getDataManager().register(EFFECT, new NBTTagCompound());
2018-09-12 01:29:49 +02:00
}
@Override
public Race getPlayerSpecies() {
if (getOwner() == null) {
return Race.HUMAN;
}
2018-09-13 12:45:24 +02:00
return Race.fromId(getOwner().getDataManager().get(PLAYER_RACE));
2018-09-12 01:29:49 +02:00
}
@Override
public void setPlayerSpecies(Race race) {
EntityPlayer player = getOwner();
2018-09-12 01:29:49 +02:00
if (!PlayerSpeciesList.instance().speciesPermitted(race, player)) {
2018-09-13 12:45:24 +02:00
race = Race.HUMAN;
}
player.getDataManager().set(PLAYER_RACE, race.ordinal());
player.capabilities.allowFlying = race.canFly();
gravity.updateFlightStat(player, player.capabilities.isFlying);
2018-09-12 01:29:49 +02:00
player.sendPlayerAbilities();
sendCapabilities(false);
2018-09-12 01:29:49 +02:00
}
@Override
public float getExertion() {
return getOwner().getDataManager().get(EXERTION);
}
@Override
public void setExertion(float exertion) {
getOwner().getDataManager().set(EXERTION, Math.max(0, exertion));
}
2018-09-12 01:29:49 +02:00
@Override
public void sendCapabilities(boolean full) {
if (!getOwner().getEntityWorld().isRemote) {
System.out.println("[SERVER] Sending player capabilities.");
if (full) {
Unicopia.channel.broadcast(new MsgPlayerCapabilities(this));
} else {
Unicopia.channel.broadcast(new MsgPlayerCapabilities(getPlayerSpecies(), getOwner()));
}
}
2018-09-12 01:29:49 +02:00
}
@Override
public IAbilityReceiver getAbilities() {
return powers;
}
@Override
public boolean isClientPlayer() {
return UClient.isClientSide() &&
Minecraft.getMinecraft().player.getGameProfile().getId().equals(getOwner().getGameProfile().getId());
2018-09-12 01:29:49 +02:00
}
@Override
public void beforeUpdate(EntityPlayer entity) {
if (entity.world.isRemote) {
if (entity.isRiding() && entity.isSneaking()) {
Entity ridee = entity.getRidingEntity();
entity.dismountRidingEntity();
if (ridee instanceof EntityPlayerMP) {
((EntityPlayerMP)ridee).getServerWorld().getEntityTracker().sendToTrackingAndSelf(ridee, new SPacketSetPassengers(ridee));
}
}
}
}
2018-09-12 01:29:49 +02:00
@Override
public void onUpdate(EntityPlayer entity) {
powers.onUpdate(entity);
gravity.onUpdate(entity);
2018-09-12 01:29:49 +02:00
if (hasEffect()) {
if (!getPlayerSpecies().canCast()) {
setEffect(null);
} else {
if (entity.getEntityWorld().isRemote) { // && entity.getEntityWorld().getWorldTime() % 10 == 0
2018-09-24 21:37:16 +02:00
getEffect().render(this);
}
2018-09-24 21:37:16 +02:00
if (!getEffect().update(this)) {
setEffect(null);
}
2018-09-12 01:29:49 +02:00
}
}
addExertion(-1);
2018-09-19 09:09:30 +02:00
attributes.applyAttributes(entity, getPlayerSpecies());
}
2018-09-12 01:29:49 +02:00
@Override
public void onFall(float distance, float damageMultiplier) {
if (!entity.getEntityWorld().isRemote) {
if (getPlayerSpecies().canFly()) {
if (entity.fallDistance > 2) {
entity.addStat(StatList.FALL_ONE_CM, (int)Math.round(distance * 100));
}
gravity.landHard(entity, distance, damageMultiplier);
2018-09-12 01:29:49 +02:00
}
}
}
@Override
public boolean stepOnCloud() {
EntityPlayer player = getOwner();
if ((player.fallDistance > 1) || player.distanceWalkedOnStepModified > nextStepDistance) {
nextStepDistance = player.distanceWalkedOnStepModified + 2;
player.fallDistance = 0;
return true;
}
return false;
}
@Override
2018-09-12 01:29:49 +02:00
public void onEntityEat() {
if (getPlayerSpecies() == Race.CHANGELING) {
EntityPlayer player = getOwner();
player.addPotionEffect(new PotionEffect(MobEffects.WEAKNESS, 2000, 2));
player.addPotionEffect(new PotionEffect(MobEffects.NAUSEA, 2000, 2));
}
}
@Override
public void writeToNBT(NBTTagCompound compound) {
compound.setString("playerSpecies", getPlayerSpecies().name());
2018-09-12 01:29:49 +02:00
compound.setTag("powers", powers.toNBT());
compound.setTag("gravity", gravity.toNBT());
IMagicEffect effect = getEffect();
2018-09-12 01:29:49 +02:00
if (effect != null) {
compound.setTag("effect", SpellRegistry.instance().serializeEffectToNBT(effect));
2018-09-12 01:29:49 +02:00
}
}
@Override
public void readFromNBT(NBTTagCompound compound) {
setPlayerSpecies(Race.fromName(compound.getString("playerSpecies"), Race.HUMAN));
2018-09-12 01:29:49 +02:00
powers.readFromNBT(compound.getCompoundTag("powers"));
gravity.readFromNBT(compound.getCompoundTag("gravity"));
if (compound.hasKey("effect")) {
setEffect(SpellRegistry.instance().createEffectFromNBT(compound.getCompoundTag("effect")));
}
2018-09-12 01:29:49 +02:00
}
@Override
public void copyFrom(IPlayer oldPlayer) {
setEffect(oldPlayer.getEffect());
setPlayerSpecies(oldPlayer.getPlayerSpecies());
}
2018-09-12 01:29:49 +02:00
@Override
public void setEffect(IMagicEffect effect) {
effectDelegate.set(effect);
sendCapabilities(true);
2018-09-12 01:29:49 +02:00
}
@Override
public IMagicEffect getEffect() {
return effectDelegate.get();
2018-09-12 01:29:49 +02:00
}
@Override
public void setOwner(EntityPlayer owner) {
entity = owner;
}
2018-09-12 01:29:49 +02:00
@Override
public EntityPlayer getOwner() {
return entity;
2018-09-12 01:29:49 +02:00
}
}