Guest User

CommonProxy

a guest
May 13th, 2015
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1. package com.ssw.Guns.common;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. import net.minecraft.entity.player.EntityPlayer;
  7. import net.minecraft.nbt.NBTTagCompound;
  8.  
  9. import com.ssw.Guns.event.ExtendedPlayer;
  10.  
  11.  
  12. public class CommonProxy {
  13.  
  14.    public void load() {}
  15.     /** Used to store IExtendedEntityProperties data temporarily between player death and respawn */
  16.     private static final Map<String, NBTTagCompound> extendedEntityData = new HashMap<String, NBTTagCompound>();
  17.  
  18.  
  19.     /**
  20.      * Adds an entity's custom data to the map for temporary storage
  21.      * @param compound An NBT Tag Compound that stores the IExtendedEntityProperties data only
  22.      */
  23.     public static void storeEntityData(String name, NBTTagCompound compound)
  24.     {
  25.         extendedEntityData.put(name, compound);
  26.     }
  27.  
  28.     /**
  29.      * Removes the compound from the map and returns the NBT tag stored for name or null if none exists
  30.      */
  31.     public static NBTTagCompound getEntityData(String name)
  32.     {
  33.         return extendedEntityData.remove(name);
  34.     }
  35.  
  36.  
  37.     /**
  38.      * Makes it look nicer in the methods save/loadProxyData
  39.      */
  40.     private static String getSaveKey(EntityPlayer player) {
  41.         return player.username + ":" + ExtendedPlayer.EXT_PROP_NAME;
  42.     }
  43.  
  44.     /**
  45.      * Does everything I did in onLivingDeathEvent and it's static,
  46.      * so you now only need to use the following in the above event:
  47.      * ExtendedPlayer.saveProxyData((EntityPlayer) event.entity));
  48.      */
  49.     public static void saveProxyData(EntityPlayer player) {
  50.         ExtendedPlayer playerData = ExtendedPlayer.get(player);
  51.         NBTTagCompound savedData = new NBTTagCompound();
  52.  
  53.         playerData.saveNBTData(savedData);
  54.         // Note that we made the CommonProxy method storeEntityData static,
  55.         // so now we don't need an instance of CommonProxy to use it! Great!
  56.         CommonProxy.storeEntityData(getSaveKey(player), savedData);
  57.     }
  58.  
  59.     /**
  60.      * This cleans up the onEntityJoinWorld event by replacing most of the code
  61.      * with a single line: ExtendedPlayer.loadProxyData((EntityPlayer) event.entity));
  62.      */
  63.     public static void loadProxyData(EntityPlayer player) {
  64.         ExtendedPlayer playerData = ExtendedPlayer.get(player);
  65.         NBTTagCompound savedData = CommonProxy.getEntityData(getSaveKey(player));
  66.  
  67.         if(savedData != null) {
  68.             playerData.loadNBTData(savedData);
  69.         }
  70.         // note we renamed 'syncExtendedProperties' to 'syncProperties' because yay, it's shorter
  71.         playerData.sync();
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment