Advertisement
Guest User

Your IPlayerTracker

a guest
Jun 18th, 2013
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | None | 0 0
  1. package your_package;
  2.  
  3. import java.util.HashMap;
  4.  
  5. import net.minecraft.entity.player.EntityPlayer;
  6. import net.minecraft.nbt.NBTTagCompound;
  7. import cpw.mods.fml.common.IPlayerTracker;
  8.  
  9. import path.to.a_class_that_can_store_your_values;
  10.  
  11. class HelperHelper implements IPlayerTracker{
  12.     public static final instance = new HelperHelper(); //You register this instance in your @Mod-file
  13.                                //by typing
  14.                              //GameRegistry.registerPlayerTracker(HelperHelper.instance);
  15.     private static HashMap<EntityPlayer, a_class_that_can_store_your_values> playerData = new
  16.     HashMap<EntityPlayer, a_class_that_can_store_your_values>(); //This HashMap stores the playerData while the
  17.                                      //player is logged in.
  18.  
  19.     public static a_class_that_can_store_your_values getValuesFor(EntityPlayer player)
  20.     //With this method you can access the player's data while he is on the server
  21.     {
  22.         return playerData.get(player);
  23.     }
  24.  
  25.     @Override
  26.     public void onPlayerLogin(EntityPlayer player) //This is the earliest point where you would need the data
  27.     {
  28.         if(player.getEntityData().hasKey("what_so_ever") //Replace what_so_ever with any String you want
  29.         {
  30.             playerData.put(player, readFromNBTStream(player.getEntityData().getCompoundTag("what_so_ever"); //Here the same String as before
  31.         }else{
  32.             playerData.put(player, new a_class_that_can_store_your_values()); //If the player has no
  33.                                               //saved data
  34.                                             //You create new data for him
  35.         }
  36.     }
  37.  
  38.     @Override
  39.     public void onPlayerLogout(EntityPlayer player) //Here you can save the data for the player
  40.     {
  41.         playerData.remove(player); //You can now "unload" the player as you won't need the data anymore
  42.     }
  43.  
  44.     private a_class_that_can_store_your_values readFromNBTStream(NBTTagCompound playerCompound) //This returns
  45.                                         //a new instance of your player-data
  46.     {
  47.         a_class_that_can_store_your_values toReturn = new a_class_that_can_store_your_values();
  48.         toReturn.readFromNBTTagCompound(playerCompound);
  49.         return toReturn;
  50.     }
  51.  
  52.  
  53.     @Override
  54.     public void onPlayerChangedDimension(EntityPlayer player) {}
  55.     @Override
  56.     public void onPlayerRespawn(EntityPlayer player) {}
  57. }
  58.  
  59. public class a_class_that_can_store_your_values { //You can put it anywhere you want. Also in other files
  60.     //your values
  61.  
  62.     public a_class_that_can_store_your_values()
  63.     {
  64.         //set your default values
  65.     }
  66.  
  67.     public void readFromNBTTagCompound(NBTTagCompound toReadFrom) {
  68.         //read the values you need
  69.     }
  70.  
  71.     public void writeToNBTTagCompound(NBTTagCompound toWriteTo) {
  72.         //write the values
  73.     }
  74.  
  75.     //Feature methods to access the player data and all the variables you need
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement