Guest User

ExtendedEntityProps

a guest
Apr 21st, 2015
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.63 KB | None | 0 0
  1. package fr.sosoh.hogsmod.common.entity.props;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import net.minecraft.entity.Entity;
  7. import net.minecraft.entity.player.EntityPlayer;
  8. import net.minecraft.entity.player.EntityPlayerMP;
  9. import net.minecraft.nbt.NBTTagCompound;
  10. import net.minecraft.nbt.NBTTagList;
  11. import net.minecraft.nbt.NBTTagString;
  12. import net.minecraft.world.World;
  13. import net.minecraftforge.common.IExtendedEntityProperties;
  14. import net.minecraftforge.common.util.Constants;
  15. import fr.sosoh.hogsmod.common.Hogsmod;
  16. import fr.sosoh.hogsmod.common.packet.PacketMana;
  17. import fr.sosoh.hogsmod.common.packet.PacketSpellLeft;
  18. import fr.sosoh.hogsmod.common.packet.PacketSpellRight;
  19. import fr.sosoh.hogsmod.common.packet.PacketSpellsList;
  20. import fr.sosoh.hogsmod.proxy.CommonProxy;
  21.  
  22. public class ExtendedEntityProps implements IExtendedEntityProperties
  23. {
  24.  
  25.     public final static String EXT_PROP_NAME = "ExtHogs";
  26.  
  27.     private final EntityPlayer player;
  28.  
  29.     public double mana;
  30.     public double maxMana;
  31.     public List<String> spellsList = new ArrayList<String>();
  32.     public String spellRight, spellLeft;
  33.    
  34.     public ExtendedEntityProps(EntityPlayer player)
  35.     {
  36.         this.player = player;
  37.         this.mana = 1000;
  38.         this.maxMana = 1000;
  39.        
  40.         this.spellRight = "";
  41.         this.spellLeft = "";
  42.     }
  43.    
  44.  
  45.     @Override
  46.     public void init(Entity entity, World world)
  47.     {
  48.        
  49.     }
  50.    
  51.     public static final void register(EntityPlayer player)
  52.     {
  53.         player.registerExtendedProperties(ExtendedEntityProps.EXT_PROP_NAME,
  54.         new ExtendedEntityProps(player));
  55.     }
  56.  
  57.     public static final ExtendedEntityProps get(EntityPlayer player)
  58.     {
  59.         return (ExtendedEntityProps) player.getExtendedProperties(EXT_PROP_NAME);
  60.     }
  61.    
  62.     @Override
  63.     public void saveNBTData(NBTTagCompound compound)
  64.     {
  65.         NBTTagCompound properties = new NBTTagCompound();
  66.         properties.setDouble("Mana", this.mana);
  67.         properties.setDouble("MaxMana", this.maxMana);
  68.         properties.setString("spellRight", this.spellRight);
  69.         properties.setString("spellLeft", this.spellLeft);
  70.        
  71.         if (this.spellsList != null) {
  72.             NBTTagList list = new NBTTagList();
  73.             for (String spell : this.spellsList) {
  74.                 list.appendTag(new NBTTagString(spell));
  75.             }
  76.             properties.setTag("Spells", list);
  77.         }
  78.         compound.setTag(EXT_PROP_NAME, properties);
  79.     }
  80.  
  81.     @Override
  82.     public void loadNBTData(NBTTagCompound compound)
  83.     {
  84.         NBTTagCompound properties = (NBTTagCompound) compound.getTag(EXT_PROP_NAME);
  85.         this.mana = properties.getDouble("Mana");
  86.         this.maxMana = properties.getDouble("MaxMana");
  87.         this.spellRight = properties.getString("spellRight");
  88.         this.spellLeft = properties.getString("spellLeft");
  89.         NBTTagList list = compound.getTagList("Spells", Constants.NBT.TAG_STRING);
  90.         for (int i = 0; i < list.tagCount(); i++) {
  91.             this.spellsList.add(list.getStringTagAt(i));
  92.         }
  93.     }
  94.  
  95.    
  96.     public final void sync()
  97.     {
  98.         PacketMana packetMana = new PacketMana(this.mana, this.maxMana);
  99.         PacketSpellRight packetSpellRight = new PacketSpellRight(this.spellRight);
  100.         PacketSpellLeft packetSpellLeft = new PacketSpellLeft(this.spellLeft);
  101.         PacketSpellsList packetSpellsList = new PacketSpellsList(this.spellsList);
  102.  
  103.         if (!player.worldObj.isRemote)
  104.         {
  105.             EntityPlayerMP player1 = (EntityPlayerMP) player;
  106.             Hogsmod.network.sendTo(packetMana, player1);
  107.             Hogsmod.network.sendTo(packetSpellRight, player1);
  108.             Hogsmod.network.sendTo(packetSpellLeft, player1);
  109.             Hogsmod.network.sendTo(packetSpellsList, player1);
  110.         }
  111.     }
  112.    
  113.     private static String getSaveKey(EntityPlayer player)
  114.     {
  115.         return player.getDisplayName() + ":" + EXT_PROP_NAME;
  116.     }
  117.    
  118.     public static void saveProxyData(EntityPlayer player) {
  119.         ExtendedEntityProps playerData = ExtendedEntityProps.get(player);
  120.         NBTTagCompound savedData = new NBTTagCompound();
  121.         playerData.saveNBTData(savedData);
  122.         CommonProxy.storeEntityData(getSaveKey(player), savedData);
  123.     }
  124.  
  125.     public static void loadProxyData(EntityPlayer player) {
  126.         ExtendedEntityProps playerData = ExtendedEntityProps.get(player);
  127.         NBTTagCompound savedData = CommonProxy.getEntityData(getSaveKey(player));
  128.  
  129.         if (savedData != null)
  130.         {
  131.             playerData.loadNBTData(savedData);
  132.         }
  133.         playerData.sync();
  134.     }
  135.    
  136.     public boolean removeMana(double amount)
  137.     {
  138.         boolean sufficient = amount <= this.mana;
  139.  
  140.         if (sufficient)
  141.         {
  142.             this.mana -= amount;
  143.             this.sync();
  144.         } else
  145.         {
  146.             return false;
  147.         }
  148.  
  149.         return sufficient;
  150.     }
  151.  
  152.     public void addMana(double amount)
  153.     {
  154.         this.mana += amount;
  155.         this.sync();
  156.     }
  157.  
  158.     public double getMana()
  159.     {
  160.         return this.mana;
  161.     }
  162.  
  163.     public void setMana(double newMana)
  164.     {
  165.         this.mana = newMana;
  166.         this.sync();
  167.     }
  168.  
  169.  
  170.     public double getMaxMana() {
  171.         return this.maxMana;
  172.     }
  173.    
  174.     public boolean setSpellRight(String spell){
  175.        
  176.         if(this.spellsList.contains(spell)){
  177.             this.spellRight = spell;
  178.             this.sync();
  179.             return true;
  180.         }
  181.        
  182.         return false;
  183.     }
  184.    
  185.     public boolean setSpellLeft(String spell){
  186.        
  187.         if(this.spellsList.contains(spell)){
  188.             this.spellLeft = spell;
  189.             this.sync();
  190.             return true;
  191.         }
  192.        
  193.         return false;
  194.     }
  195.    
  196.     public String getSpellRight(){
  197.         return this.spellRight;
  198.     }
  199.    
  200.     public String getSpellLeft(){
  201.         return this.spellLeft;
  202.     }
  203.    
  204.     public boolean addSpellsList(String spell){
  205.         if(this.getSpellsList().contains(spell) == false){
  206.             this.spellsList.add(spell);
  207.             this.sync();
  208.             return true;
  209.         }
  210.         return false;
  211.     }
  212.    
  213.     public boolean removeSpellsList(String spell){
  214.         if(this.getSpellsList().contains(spell) == true){
  215.             this.spellsList.remove(spell);
  216.             this.sync();
  217.             return true;
  218.         }
  219.         return false;
  220.     }
  221.    
  222.     public List<String> getSpellsList(){
  223.         return this.spellsList;
  224.     }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment