jokekid

Untitled

Aug 30th, 2015
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.82 KB | None | 0 0
  1. package com.jokekid.terraria.extendentity;
  2.  
  3. import com.jokekid.terraria.inventory.AccesoryInventory;
  4.  
  5. import net.minecraft.entity.DataWatcher;
  6. import net.minecraft.entity.Entity;
  7. import net.minecraft.entity.player.EntityPlayer;
  8. import net.minecraft.nbt.NBTTagCompound;
  9. import net.minecraft.world.World;
  10. import net.minecraftforge.common.IExtendedEntityProperties;
  11.  
  12. public class ExtendedPlayer implements IExtendedEntityProperties {
  13.  
  14.     public final AccesoryInventory inventory = new AccesoryInventory();
  15.    
  16.     public final static String PROP_NAME = "ExtendedPlayer";
  17.     public final EntityPlayer player;
  18.     //public int currentMana;
  19.     public boolean canCloudJump;
  20.     public final int MAXMANA = 20;
  21.     public static final int MANA_WATCHER = 15;
  22.     //DataWatcher dw;
  23.    
  24.    
  25.     public ExtendedPlayer(EntityPlayer player) {
  26.         this.player = player;
  27.        
  28.         this.canCloudJump = true;
  29.        
  30.         this.player.getDataWatcher().addObject(MANA_WATCHER, this.MAXMANA);
  31.     }
  32.  
  33.     @Override
  34.     public void saveNBTData(NBTTagCompound compound) {
  35.         NBTTagCompound properties = new NBTTagCompound();
  36.         properties.setInteger("CurrentMana", this.player.getDataWatcher().getWatchableObjectInt(MANA_WATCHER));
  37.         this.inventory.writeToNBT(properties);     
  38.         compound.setTag(PROP_NAME, properties);
  39.         System.out.println("saved extendedPlayer NBTData");
  40.     }
  41.  
  42.     @Override
  43.     public void loadNBTData(NBTTagCompound compound) {
  44.         NBTTagCompound properties = (NBTTagCompound) compound.getTag(PROP_NAME);
  45.         this.inventory.readFromNBT(properties);
  46.         this.player.getDataWatcher().updateObject(MANA_WATCHER, properties.getInteger("CurrentMana"));
  47.         System.out.println("Mana from NBT: " + this.player.getDataWatcher().getWatchableObjectInt(MANA_WATCHER));
  48.     }
  49.  
  50.     public boolean consumeMana(int amount) {
  51.         int currentMana = this.player.getDataWatcher().getWatchableObjectInt(MANA_WATCHER);
  52.         boolean success = amount <= currentMana;
  53.         if(success){
  54.             this.player.getDataWatcher().updateObject(MANA_WATCHER, currentMana - amount);
  55.         }
  56.         // Return TRUE if the player had enough mana
  57.         return success;
  58.     }
  59.    
  60.     public int replenishMana(int amount){
  61.         int replenish;
  62.         int oldMana = player.getDataWatcher().getWatchableObjectInt(MANA_WATCHER);
  63.         int newMana = oldMana + amount;
  64.         if (newMana > this.MAXMANA){
  65.             newMana = this.MAXMANA;
  66.         }
  67.         replenish = newMana - oldMana;
  68.         player.getDataWatcher().updateObject(MANA_WATCHER, newMana);
  69.         return replenish;
  70.     }
  71.  
  72.     @Override
  73.     public void init(Entity entity, World world) {
  74.  
  75.     }
  76.  
  77.     // CONVENIENCE METHODS:
  78.     public static final void register(EntityPlayer player) {
  79.         player.registerExtendedProperties(ExtendedPlayer.PROP_NAME, new ExtendedPlayer(player));
  80.         System.out.println("extended player registered");
  81.     }
  82.  
  83.     public static final ExtendedPlayer get(EntityPlayer player) {
  84.         return (ExtendedPlayer) player.getExtendedProperties(PROP_NAME);
  85.     }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment