PlayerPack - Serialize a player (kind of)

Discussion in 'Resources' started by LucasEmanuel, Jun 4, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    LucasEmanuel

    I created this when i needed to be able to save the most important aspects of a player with an objectstream. It should work, but i have never tested it since im over my head with other projects that needs to be finished.

    But i thought that i should let it out to the public and maybe they can test it out or upgrade it with missing features.

    ALOT of thanks to NuclearW for CardboardBox which is integrated into this little handy tool.

    This tool is merely intended for multiworld plugins, where the player has diffrent inventories etc in diffrent worlds.

    Features:
    Currently it saves the players food level, health and total experience.
    It also saves the players entire inventory with enchantments and item health. (thanks to NuclearW)

    Source:
    Code:java
    1. /**
    2. * Name: PlayerPack.java
    3. * Date: 20:29:38 - 120603
    4. *
    5. * Author: LucasEmanuel @ bukkit forums
    6. *
    7. *
    8. * Description:
    9. *
    10. * Serialize the most important aspects of a player so that we can save and
    11. * restore them when needed.
    12. *
    13. */
    14.  
    15. import java.io.Serializable;
    16. import java.util.HashMap;
    17. import java.util.Map;
    18.  
    19. import org.bukkit.enchantments.Enchantment;
    20. import org.bukkit.entity.Player;
    21. import org.bukkit.inventory.ItemStack;
    22. import org.bukkit.inventory.PlayerInventory;
    23.  
    24. public class PlayerPack implements Serializable {
    25. private static final long serialVersionUID = -7105874292045912110L;
    26.  
    27. // Player info
    28. private final int health;
    29. private final int foodLevel;
    30. private final int totalExperience;
    31.  
    32. private final HashMap<Integer, CardboardBox> inventory;
    33.  
    34. /**
    35. * Constructor
    36. *
    37. * @param player - Needs the player instance that its going to serialize
    38. */
    39. public PlayerPack(Player player) {
    40. this.health = player.getHealth();
    41. this.foodLevel = player.getFoodLevel();
    42. this.totalExperience = player.getTotalExperience();
    43.  
    44. this.inventory = this.serializeInventory(player.getInventory());
    45. }
    46.  
    47. private HashMap<Integer, CardboardBox> serializeInventory(
    48. PlayerInventory inventory) {
    49.  
    50. HashMap<Integer, CardboardBox> map = new HashMap<Integer, CardboardBox>();
    51.  
    52. for (int i = 0; i < 36; i++) {
    53. if (inventory.getItem(i) != null)
    54. map.put(i, new CardboardBox(inventory.getItem(i)));
    55. }
    56.  
    57. map.put(100, new CardboardBox(inventory.getBoots()));
    58. map.put(101, new CardboardBox(inventory.getLeggings()));
    59. map.put(102, new CardboardBox(inventory.getChestplate()));
    60. map.put(103, new CardboardBox(inventory.getHelmet()));
    61.  
    62. return map;
    63. }
    64.  
    65. /**
    66. * Unpacks the player and restore the saved data.
    67. *
    68. * @param player - It needs the player instance that is going to receive the saved data.
    69. */
    70. public void unpack(Player player) {
    71.  
    72. player.setHealth(this.health);
    73. player.setFoodLevel(this.foodLevel);
    74. player.setTotalExperience(this.totalExperience);
    75.  
    76. PlayerInventory inventory = player.getInventory();
    77.  
    78. // Clear the inventory so we have a clean one to work with.
    79. inventory.clear();
    80.  
    81. for (Integer i : this.inventory.keySet()) {
    82.  
    83. if (i < 100) {
    84. inventory.setItem(i, this.inventory.get(i).unbox());
    85. } else {
    86. if (i == 100)
    87. inventory.setBoots(this.inventory.get(100).unbox());
    88. else if (i == 101)
    89. inventory.setLeggings(this.inventory.get(101).unbox());
    90. else if (i == 102)
    91. inventory.setChestplate(this.inventory.get(102).unbox());
    92. else if (i == 103)
    93. inventory.setHelmet(this.inventory.get(103).unbox());
    94. }
    95. }
    96. }
    97. }
    98.  
    99. /**
    100. * Author: NuclearW @ Bukkit forums
    101. *
    102. * - Source URL: [url]http://forums.bukkit.org/threads/cardboard-serializable-itemstack-with-enchantments.75768/[/url]
    103. */
    104. class CardboardBox implements Serializable {
    105. private static final long serialVersionUID = 729890133797629668L;
    106.  
    107. private final int type, amount;
    108. private final short damage;
    109. private final byte data;
    110.  
    111. private final HashMap<CardboardEnchantment, Integer> enchants;
    112.  
    113. public CardboardBox(ItemStack item) {
    114. this.type = item.getTypeId();
    115. this.amount = item.getAmount();
    116. this.damage = item.getDurability();
    117. this.data = item.getData().getData();
    118.  
    119. HashMap<CardboardEnchantment, Integer> map = new HashMap<CardboardEnchantment, Integer>();
    120.  
    121. Map<Enchantment, Integer> enchantments = item.getEnchantments();
    122.  
    123. for (Enchantment enchantment : enchantments.keySet()) {
    124. map.put(new CardboardEnchantment(enchantment),
    125. enchantments.get(enchantment));
    126. }
    127.  
    128. this.enchants = map;
    129. }
    130.  
    131. public ItemStack unbox() {
    132. ItemStack item = new ItemStack(type, amount, damage, data);
    133.  
    134. HashMap<Enchantment, Integer> map = new HashMap<Enchantment, Integer>();
    135.  
    136. for (CardboardEnchantment cEnchantment : enchants.keySet()) {
    137. map.put(cEnchantment.unbox(), enchants.get(cEnchantment));
    138. }
    139.  
    140. item.addUnsafeEnchantments(map);
    141.  
    142. return item;
    143. }
    144. }
    145.  
    146. class CardboardEnchantment implements Serializable {
    147. private static final long serialVersionUID = 8973856768102665381L;
    148.  
    149. private final int id;
    150.  
    151. public CardboardEnchantment(Enchantment enchantment) {
    152. this.id = enchantment.getId();
    153. }
    154.  
    155. public Enchantment unbox() {
    156. return Enchantment.getById(this.id);
    157. }
    158. }
    159.  
     
  2. Offline

    SquidDevelops

    LucasEmanuel I know that this is an old post, but how do we retrieve the data to store it in lets say a mySQL database
     
  3. Offline

    LucasEmanuel

    SquidDevelops
    Oh, ignore this thread, it's rather bad :)
    If you want to save an inventory, take a look at this class. That class can be used to save inventories to a database and then retrieve it :)

    If you want to save a player's data in your database you could just create a column each for the information you want to store :)
     
Thread Status:
Not open for further replies.

Share This Page