Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fr.sosoh.hogsmod.common.entity.props;
- import java.util.ArrayList;
- 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.nbt.NBTTagList;
- import net.minecraft.nbt.NBTTagString;
- import net.minecraft.world.World;
- import net.minecraftforge.common.IExtendedEntityProperties;
- import net.minecraftforge.common.util.Constants;
- 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();
- properties.setDouble("Mana", this.mana);
- properties.setDouble("MaxMana", this.maxMana);
- properties.setString("spellRight", this.spellRight);
- properties.setString("spellLeft", this.spellLeft);
- if (this.spellsList != null) {
- NBTTagList list = new NBTTagList();
- for (String spell : this.spellsList) {
- list.appendTag(new NBTTagString(spell));
- }
- properties.setTag("Spells", list);
- }
- compound.setTag(EXT_PROP_NAME, properties);
- }
- @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");
- NBTTagList list = compound.getTagList("Spells", Constants.NBT.TAG_STRING);
- for (int i = 0; i < list.tagCount(); i++) {
- this.spellsList.add(list.getStringTagAt(i));
- }
- }
- 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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment