| package fr.sosoh.hogsmod.common.entity.props; |
| |
| import java.util.ArrayList; |
| import java.util.Arrays; |
| import java.util.List; |
| |
| import net.minecraft.entity.Entity; |
| import net.minecraft.entity.player.EntityPlayer; |
| import net.minecraft.entity.player.EntityPlayerMP; |
| import net.minecraft.nbt.NBTTagCompound; |
| import net.minecraft.world.World; |
| import net.minecraftforge.common.IExtendedEntityProperties; |
| import fr.sosoh.hogsmod.common.Hogsmod; |
| import fr.sosoh.hogsmod.common.packet.PacketMana; |
| import fr.sosoh.hogsmod.common.packet.PacketSpellLeft; |
| import fr.sosoh.hogsmod.common.packet.PacketSpellRight; |
| import fr.sosoh.hogsmod.common.packet.PacketSpellsList; |
| import fr.sosoh.hogsmod.proxy.CommonProxy; |
| |
| public class ExtendedEntityProps implements IExtendedEntityProperties |
| { |
| |
| public final static String EXT_PROP_NAME = "ExtHogs"; |
| |
| private final EntityPlayer player; |
| |
| public double mana; |
| public double maxMana; |
| public List <string>spellsList = new ArrayList<string>(); |
| public String spellRight, spellLeft; |
| |
| public ExtendedEntityProps(EntityPlayer player) |
| { |
| this.player = player; |
| this.mana = 1000; |
| this.maxMana = 1000; |
| |
| this.spellRight = ""; |
| this.spellLeft = ""; |
| } |
| |
| @Override |
| public void init(Entity entity, World world) |
| { |
| |
| } |
| |
| public static final void register(EntityPlayer player) |
| { |
| player.registerExtendedProperties(ExtendedEntityProps.EXT_PROP_NAME, |
| new ExtendedEntityProps(player)); |
| } |
| |
| public static final ExtendedEntityProps get(EntityPlayer player) |
| { |
| return (ExtendedEntityProps) player.getExtendedProperties(EXT_PROP_NAME); |
| } |
| |
| @Override |
| public void saveNBTData(NBTTagCompound compound) |
| { |
| |
| NBTTagCompound properties = new NBTTagCompound(); |
| compound.setTag(EXT_PROP_NAME, properties); |
| properties.setDouble("Mana", this.mana); |
| properties.setDouble("MaxMana", this.maxMana); |
| properties.setString("spellRight", this.spellRight); |
| properties.setString("spellLeft", this.spellLeft); |
| properties.setInteger("spellsListSize", this.spellsList.size()); |
| if(this.spellsList != null){ |
| for(String spell : this.spellsList) |
| { |
| properties.setString("spellsList", spell); |
| } |
| } |
| } |
| |
| @Override |
| public void loadNBTData(NBTTagCompound compound) |
| { |
| NBTTagCompound properties = (NBTTagCompound) compound.getTag(EXT_PROP_NAME); |
| this.mana = properties.getDouble("Mana"); |
| this.maxMana = properties.getDouble("MaxMana"); |
| this.spellRight = properties.getString("spellRight"); |
| this.spellLeft = properties.getString("spellLeft"); |
| int size = properties.getInteger("spellsListSize"); |
| if(size < 0){ |
| for(int i = 0; i < size; i++){ |
| String stringI = this.spellsList.get(i); |
| this.spellsList.add(i, stringI); |
| } |
| } |
| } |
| |
| public final void sync() |
| { |
| PacketMana packetMana = new PacketMana(this.mana, this.maxMana); |
| PacketSpellRight packetSpellRight = new PacketSpellRight(this.spellRight); |
| PacketSpellLeft packetSpellLeft = new PacketSpellLeft(this.spellLeft); |
| PacketSpellsList packetSpellsList = new PacketSpellsList(this.spellsList); |
| |
| if (!player.worldObj.isRemote) |
| { |
| EntityPlayerMP player1 = (EntityPlayerMP) player; |
| Hogsmod.network.sendTo(packetMana, player1); |
| Hogsmod.network.sendTo(packetSpellRight, player1); |
| Hogsmod.network.sendTo(packetSpellLeft, player1); |
| Hogsmod.network.sendTo(packetSpellsList, player1); |
| } |
| } |
| |
| private static String getSaveKey(EntityPlayer player) |
| { |
| return player.getDisplayName() + ":" + EXT_PROP_NAME; |
| } |
| |
| public static void saveProxyData(EntityPlayer player) { |
| ExtendedEntityProps playerData = ExtendedEntityProps.get(player); |
| NBTTagCompound savedData = new NBTTagCompound(); |
| playerData.saveNBTData(savedData); |
| CommonProxy.storeEntityData(getSaveKey(player), savedData); |
| } |
| |
| public static void loadProxyData(EntityPlayer player) |
| { |
| ExtendedEntityProps playerData = ExtendedEntityProps.get(player); |
| NBTTagCompound savedData = CommonProxy.getEntityData(getSaveKey(player)); |
| |
| if (savedData != null) |
| { |
| playerData.loadNBTData(savedData); |
| } |
| playerData.sync(); |
| } |
| |
| public boolean removeMana(double amount) |
| { |
| boolean sufficient = amount <= this.mana; |
| |
| if (sufficient) |
| { |
| this.mana -= amount; |
| this.sync(); |
| } else |
| { |
| return false; |
| } |
| |
| return sufficient; |
| } |
| |
| public void addMana(double amount) |
| { |
| this.mana += amount; |
| this.sync(); |
| } |
| |
| public double getMana() |
| { |
| return this.mana; |
| } |
| |
| public void setMana(double newMana) |
| { |
| this.mana = newMana; |
| this.sync(); |
| } |
| |
| public double getMaxMana() { |
| return this.maxMana; |
| } |
| |
| public boolean setSpellRight(String spell){ |
| |
| if(this.spellsList.contains(spell)){ |
| this.spellRight = spell; |
| this.sync(); |
| return true; |
| } |
| |
| return false; |
| } |
| |
| public boolean setSpellLeft(String spell){ |
| |
| if(this.spellsList.contains(spell)){ |
| this.spellLeft = spell; |
| this.sync(); |
| return true; |
| } |
| |
| return false; |
| } |
| |
| public String getSpellRight(){ |
| return this.spellRight; |
| } |
| |
| public String getSpellLeft(){ |
| return this.spellLeft; |
| } |
| |
| public boolean addSpellsList(String spell){ |
| if(this.getSpellsList().contains(spell) == false){ |
| this.spellsList.add(spell); |
| this.sync(); |
| return true; |
| } |
| return false; |
| } |
| |
| public boolean removeSpellsList(String spell){ |
| if(this.getSpellsList().contains(spell) == true){ |
| this.spellsList.remove(spell); |
| this.sync(); |
| return true; |
| } |
| return false; |
| } |
| |
| public List <string>getSpellsList(){ |
| return this.spellsList; |
| } |
| } |