2018-09-12 01:29:49 +02:00
|
|
|
package com.minelittlepony.unicopia.player;
|
|
|
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
2018-09-12 22:37:06 +02:00
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
|
|
|
|
2018-09-12 01:29:49 +02:00
|
|
|
import com.minelittlepony.unicopia.Race;
|
|
|
|
import com.minelittlepony.unicopia.UClient;
|
2018-09-12 22:37:06 +02:00
|
|
|
import com.minelittlepony.unicopia.Unicopia;
|
|
|
|
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.player.EntityPlayer;
|
|
|
|
import net.minecraft.init.MobEffects;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2018-09-12 22:37:06 +02:00
|
|
|
import net.minecraft.network.datasync.DataParameter;
|
|
|
|
import net.minecraft.network.datasync.DataSerializers;
|
|
|
|
import net.minecraft.network.datasync.EntityDataManager;
|
2018-09-12 01:29:49 +02:00
|
|
|
import net.minecraft.potion.PotionEffect;
|
2018-09-12 22:37:06 +02:00
|
|
|
import net.minecraft.stats.StatList;
|
2018-09-12 01:29:49 +02:00
|
|
|
|
|
|
|
class PlayerCapabilities implements IPlayer, ICaster<EntityPlayer> {
|
|
|
|
|
2018-09-12 22:37:06 +02:00
|
|
|
private static final Logger logger = LogManager.getLogger();
|
|
|
|
|
|
|
|
private static final DataParameter<Integer> PLAYER_RACE = EntityDataManager
|
|
|
|
.createKey(EntityPlayer.class, DataSerializers.VARINT);
|
2018-09-12 01:29:49 +02:00
|
|
|
|
|
|
|
private final PlayerAbilityDelegate powers = new PlayerAbilityDelegate(this);
|
|
|
|
|
|
|
|
private final PlayerGravityDelegate gravity = new PlayerGravityDelegate(this);
|
|
|
|
|
|
|
|
private IMagicEffect effect;
|
|
|
|
|
2018-09-12 02:26:35 +02:00
|
|
|
private EntityPlayer entity;
|
2018-09-12 22:37:06 +02:00
|
|
|
private UUID playerId;
|
2018-09-12 02:26:35 +02:00
|
|
|
|
2018-09-12 22:37:06 +02:00
|
|
|
PlayerCapabilities(EntityPlayer player) {
|
|
|
|
setOwner(player);
|
2018-09-12 02:26:35 +02:00
|
|
|
|
2018-09-12 22:37:06 +02:00
|
|
|
player.getDataManager().register(PLAYER_RACE, Race.HUMAN.ordinal());
|
2018-09-12 01:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Race getPlayerSpecies() {
|
2018-09-12 22:37:06 +02:00
|
|
|
if (getOwner() == null) {
|
|
|
|
return Race.HUMAN;
|
|
|
|
}
|
2018-09-13 12:45:24 +02:00
|
|
|
|
2018-09-12 22:37:06 +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 self = getOwner();
|
|
|
|
|
2018-09-13 12:45:24 +02:00
|
|
|
if (!PlayerSpeciesList.instance().speciesPermitted(race)) {
|
|
|
|
race = Race.HUMAN;
|
|
|
|
}
|
|
|
|
|
2018-09-12 22:37:06 +02:00
|
|
|
if (self != null) {
|
|
|
|
getOwner().getDataManager().set(PLAYER_RACE, race.ordinal());
|
|
|
|
|
|
|
|
self.capabilities.allowFlying = race.canFly();
|
|
|
|
gravity.updateFlightStat(self, self.capabilities.isFlying);
|
2018-09-12 01:29:49 +02:00
|
|
|
|
2018-09-12 22:37:06 +02:00
|
|
|
self.sendPlayerAbilities();
|
|
|
|
sendCapabilities(false);
|
|
|
|
}
|
2018-09-12 01:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-09-12 22:37:06 +02:00
|
|
|
public void sendCapabilities(boolean full) {
|
|
|
|
if (!getOwner().getEntityWorld().isRemote) {
|
|
|
|
if (full) {
|
|
|
|
Unicopia.channel.broadcast(new MsgPlayerCapabilities(this));
|
|
|
|
} else {
|
|
|
|
Unicopia.channel.broadcast(new MsgPlayerCapabilities(getPlayerSpecies(), getOwner().getGameProfile().getId()));
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 01:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IAbilityReceiver getAbilities() {
|
|
|
|
return powers;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isClientPlayer() {
|
|
|
|
return UClient.isClientSide() &&
|
2018-09-12 02:26:35 +02:00
|
|
|
Minecraft.getMinecraft().player.getGameProfile().getId().equals(getOwner().getGameProfile().getId());
|
2018-09-12 01:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-09-12 02:26:35 +02:00
|
|
|
public void onUpdate(EntityPlayer entity) {
|
|
|
|
powers.onUpdate(entity);
|
|
|
|
gravity.onUpdate(entity);
|
2018-09-12 01:29:49 +02:00
|
|
|
|
|
|
|
if (effect != null) {
|
2018-09-12 22:37:06 +02:00
|
|
|
if (!getPlayerSpecies().canCast()) {
|
|
|
|
setEffect(null);
|
|
|
|
} else {
|
|
|
|
if (entity.getEntityWorld().isRemote) { // && entity.getEntityWorld().getWorldTime() % 10 == 0
|
|
|
|
effect.render(entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!effect.update(entity)) {
|
|
|
|
setEffect(null);
|
|
|
|
}
|
2018-09-12 01:29:49 +02:00
|
|
|
}
|
2018-09-12 22:37:06 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-12 01:29:49 +02:00
|
|
|
|
2018-09-12 22:37:06 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2018-09-12 22:37:06 +02:00
|
|
|
compound.setString("playerSpecies", getPlayerSpecies().name());
|
2018-09-12 01:29:49 +02:00
|
|
|
compound.setTag("powers", powers.toNBT());
|
|
|
|
compound.setTag("gravity", gravity.toNBT());
|
|
|
|
|
|
|
|
if (effect != null) {
|
|
|
|
compound.setString("effect_id", effect.getName());
|
|
|
|
compound.setTag("effect", effect.toNBT());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readFromNBT(NBTTagCompound compound) {
|
2018-09-12 22:37:06 +02:00
|
|
|
setPlayerSpecies(Race.fromName(compound.getString("playerSpecies")));
|
2018-09-12 01:29:49 +02:00
|
|
|
powers.readFromNBT(compound.getCompoundTag("powers"));
|
|
|
|
gravity.readFromNBT(compound.getCompoundTag("gravity"));
|
|
|
|
|
2018-09-12 22:37:06 +02:00
|
|
|
effect = null;
|
|
|
|
|
2018-09-12 01:29:49 +02:00
|
|
|
if (compound.hasKey("effect_id") && compound.hasKey("effect")) {
|
|
|
|
SpellRegistry.instance().getSpellFromName(compound.getString("effect_id")).ifPresent(f -> {
|
|
|
|
effect = f;
|
|
|
|
effect.readFromNBT(compound.getCompoundTag("effect"));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 22:37:06 +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) {
|
|
|
|
this.effect = effect;
|
2018-09-12 22:37:06 +02:00
|
|
|
|
|
|
|
sendCapabilities(true);
|
2018-09-12 01:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IMagicEffect getEffect() {
|
|
|
|
return effect;
|
|
|
|
}
|
|
|
|
|
2018-09-12 22:37:06 +02:00
|
|
|
@Override
|
|
|
|
public void setOwner(EntityPlayer owner) {
|
|
|
|
entity = owner;
|
|
|
|
playerId = owner.getGameProfile().getId();
|
|
|
|
}
|
|
|
|
|
2018-09-12 01:29:49 +02:00
|
|
|
@Override
|
|
|
|
public EntityPlayer getOwner() {
|
2018-09-12 22:37:06 +02:00
|
|
|
if (entity == null) {
|
|
|
|
entity = IPlayer.getPlayerEntity(playerId);
|
|
|
|
if (entity == null) {
|
|
|
|
logger.error("Capabilities without player! Mismatched id was" + playerId);
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 02:26:35 +02:00
|
|
|
return entity;
|
2018-09-12 01:29:49 +02:00
|
|
|
}
|
|
|
|
}
|