Solved ConfigurationSerialization

Discussion in 'Plugin Development' started by Chew, Jul 18, 2014.

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

    Chew

    I'd like to learn about configuration serialization for a list in which have created.

    Code:java
    1. public static List<Zone> zones = new ArrayList<Zone>();


    Code:java
    1. package com.relocatedgaming;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5. import java.util.HashSet;
    6. import java.util.List;
    7. import java.util.Map;
    8.  
    9. public class Zone{
    10.  
    11. private int id;
    12.  
    13. private String name;
    14.  
    15. private ArrayList<String> allowList = new ArrayList<String>();
    16.  
    17. private int x1;
    18. private int y1;
    19. private int z1;
    20.  
    21. private int x2;
    22. private int y2;
    23. private int z2;
    24.  
    25. private double x3;
    26. private double y3;
    27. private double z3;
    28. private float yaw;
    29. private float pitch;
    30.  
    31. private int price;
    32.  
    33. private String world;
    34.  
    35. private boolean pvp;
    36. private boolean doors;
    37. private boolean noTP;
    38. private boolean mobs;
    39. private boolean homeZone;
    40. private boolean TNT;
    41.  
    42. private String owner;
    43.  
    44. public String getOwner() {
    45. return owner;
    46. }
    47.  
    48. public void setOwner(String owner) {
    49. this.owner = owner;
    50. }
    51.  
    52. public ArrayList<String> getAllowList() {
    53. return allowList;
    54. }
    55.  
    56. public void addAllowList(ArrayList<String> allowList, String allowedPlayer) {
    57. if (!(allowList.contains(allowedPlayer))) {
    58. allowList.add(allowedPlayer);
    59. }
    60. this.allowList = allowList;
    61. }
    62.  
    63. public void removeAllowList(ArrayList<String> allowList, String unallowedPlayer) {
    64. if (allowList.contains(unallowedPlayer)) {
    65. allowList.remove(unallowedPlayer);
    66. }
    67. this.allowList = allowList;
    68. }
    69.  
    70. public void setAllowList(ArrayList<String> allowList) {
    71. this.allowList = allowList;
    72. }
    73.  
    74. public int getId() {
    75. return id;
    76. }
    77.  
    78. public void setId(int id) {
    79. this.id = id;
    80. }
    81.  
    82. public int getPrice() {
    83. return price;
    84. }
    85.  
    86. public void setPrice(int price) {
    87. this.price = price;
    88. }
    89.  
    90. public String getName() {
    91. return name;
    92. }
    93.  
    94. public void setName(String name) {
    95. this.name = name;
    96. }
    97.  
    98. public int getX1() {
    99. return x1;
    100. }
    101.  
    102. public void setX1(int x1) {
    103. this.x1 = x1;
    104. }
    105.  
    106. public int getY1() {
    107. return y1;
    108. }
    109.  
    110. public void setY1(int y1) {
    111. this.y1 = y1;
    112. }
    113.  
    114. public int getZ1() {
    115. return z1;
    116. }
    117.  
    118. public void setZ1(int z1) {
    119. this.z1 = z1;
    120. }
    121.  
    122. public int getX2() {
    123. return x2;
    124. }
    125.  
    126. public void setX2(int x2) {
    127. this.x2 = x2;
    128. }
    129.  
    130. public int getY2() {
    131. return y2;
    132. }
    133.  
    134. public void setY2(int y2) {
    135. this.y2 = y2;
    136. }
    137.  
    138. public int getZ2() {
    139. return z2;
    140. }
    141.  
    142. public void setZ2(int z2) {
    143. this.z2 = z2;
    144. }
    145.  
    146. public double getX3() {
    147. return x3;
    148. }
    149.  
    150. public void setX3(double x3) {
    151. this.x3 = x3;
    152. }
    153.  
    154. public double getY3() {
    155. return y3;
    156. }
    157.  
    158. public void setY3(double y3) {
    159. this.y3 = y3;
    160. }
    161.  
    162. public double getZ3() {
    163. return z3;
    164. }
    165.  
    166. public void setZ3(double z3) {
    167. this.z3 = z3;
    168. }
    169.  
    170. public float getYaw() {
    171. return yaw;
    172. }
    173.  
    174. public void setYaw(float yaw) {
    175. this.yaw = yaw;
    176. }
    177.  
    178. public float getPitch() {
    179. return pitch;
    180. }
    181.  
    182. public void setPitch(float pitch) {
    183. this.pitch = pitch;
    184. }
    185.  
    186. public String getWorld() {
    187. return world;
    188. }
    189.  
    190. public void setWorld(String world) {
    191. this.world = world;
    192. }
    193.  
    194. public boolean isPvp() {
    195. return pvp;
    196. }
    197.  
    198. public void setPvp(boolean pvp) {
    199. this.pvp = pvp;
    200. }
    201.  
    202. public boolean isMobs() {
    203. return mobs;
    204. }
    205.  
    206. public void setMobs(boolean mobs) {
    207. this.mobs = mobs;
    208. }
    209.  
    210. public boolean isDoors() {
    211. return doors;
    212. }
    213.  
    214. public void setDoors(boolean doors) {
    215. this.doors = doors;
    216. }
    217. public boolean isnoTP() {
    218. return noTP;
    219. }
    220.  
    221. public void setnoTP(boolean noTP) {
    222. this.noTP = noTP;
    223. }
    224. public boolean ishomeZone() {
    225. return homeZone;
    226. }
    227.  
    228. public void sethomeZone(boolean homeZone) {
    229. this.homeZone = homeZone;
    230. }
    231. public boolean isTNT() {
    232. return TNT;
    233. }
    234.  
    235. public void setTNT(boolean TNT) {
    236. this.TNT = TNT;
    237. }
    238. }
    239.  


    Much thanks to everyone who can help me.

    What I was trying beforehand was.

    Code:java
    1. public void onDisable() {
    2. plugin = null;
    3. if (zones != null) {
    4. for (Zone obj : zones) {
    5. int zoneInt = zones.indexOf(obj);
    6. String sInt = Integer.toString(zoneInt);
    7. zConfig.set("zones." + sInt + "", obj);
    8. saveConfig(zConfig);
    9. }
    10. }
    11.  
    12. }


    Which gave me this config.

    Code:
    zones:
      '0': !!com.relocatedgaming.Zone
        TNT: false
        allowList: []
        doors: false
        homeZone: false
        id: 0
        list: []
        map: {}
        mobs: false
        name: (None)
        noTP: false
        owner: 7abfc826-af20-4c79-b725-e0d96344cc6e
        pitch: 0.0
        price: 0
        pvp: false
        world: RG
        x1: 323
        x2: 329
        x3: 326.5
        y1: 1
        y2: 256
        y3: 0.0
        yaw: 0.0
        z1: 25
        z2: 29
        z3: 27.5
      '1': !!com.relocatedgaming.Zone
        TNT: false
        allowList: []
        doors: false
        homeZone: false
        id: 0
        list: []
        map: {}
        mobs: false
        name: (None)
        noTP: false
        owner: 7abfc826-af20-4c79-b725-e0d96344cc6e
        pitch: 0.0
        price: 0
        pvp: false
        world: RG
        x1: 321
        x2: 327
        x3: 324.5
        y1: 1
        y2: 256
        y3: 0.0
        yaw: 0.0
        z1: 40
        z2: 50
        z3: 45.5
    
     
  2. Offline

    _LB

  3. Offline

    Chew

    _LB Once I change my class to this below, how do I proceed?
    Code:java
    1. package com.relocatedgaming;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5. import java.util.HashSet;
    6. import java.util.List;
    7. import java.util.Map;
    8.  
    9. import org.bukkit.configuration.ConfigurationSection;
    10. import org.bukkit.configuration.serialization.ConfigurationSerializable;
    11. import org.bukkit.configuration.serialization.ConfigurationSerialization;
    12.  
    13. public class Zone implements ConfigurationSerializable{
    14.  
    15. { ConfigurationSerialization.registerClass(Zone.class); }
    16.  
    17. private int id;
    18.  
    19. private String name;
    20.  
    21. private HashSet<Zone> subZones = new HashSet<Zone>();
    22. private ArrayList<String> allowList = new ArrayList<String>();
    23.  
    24.  
    25. private int x1;
    26. private int y1;
    27. private int z1;
    28.  
    29. private int x2;
    30. private int y2;
    31. private int z2;
    32.  
    33. private double x3;
    34. private double y3;
    35. private double z3;
    36. private float yaw;
    37. private float pitch;
    38.  
    39. private int price;
    40.  
    41. private String world;
    42.  
    43. private boolean pvp;
    44. private boolean doors;
    45. private boolean noTP;
    46. private boolean mobs;
    47. private boolean homeZone;
    48. private boolean TNT;
    49.  
    50. public Map<String, Object> map = new HashMap<String, Object>();
    51. public List<Object> list = new ArrayList<Object>();
    52.  
    53. @Override
    54. public Map<String, Object> serialize() {
    55. Map<String, Object> data = new HashMap<String, Object>();
    56. if (Main.zones != null) {
    57. for (Zone obj : Main.zones) {
    58. int zoneInt = Main.zones.indexOf(obj);
    59. String sInt = Integer.toString(zoneInt);
    60. data.put("zones." + sInt + ".owner", owner);
    61. data.put("zones." + sInt + ".allowList", allowList);
    62. data.put("zones." + sInt + ".name", name);
    63. data.put("zones." + sInt + ".x1", x1);
    64. data.put("zones." + sInt + ".y1", y1);
    65. data.put("zones." + sInt + ".z1", z1);
    66. data.put("zones." + sInt + ".x2", x2);
    67. data.put("zones." + sInt + ".y2", y2);
    68. data.put("zones." + sInt + ".z2", z2);
    69. data.put("zones." + sInt + ".x3", x3);
    70. data.put("zones." + sInt + ".y3", y3);
    71. data.put("zones." + sInt + ".z3", z3);
    72. data.put("zones." + sInt + ".pitch", pitch);
    73. data.put("zones." + sInt + ".yaw", yaw);
    74. data.put("zones." + sInt + ".price", price);
    75. data.put("zones." + sInt + ".world", world);
    76. data.put("zones." + sInt + ".pvp", pvp);
    77. data.put("zones." + sInt + ".doors", doors);
    78. data.put("zones." + sInt + ".noTP", noTP);
    79. data.put("zones." + sInt + ".mobs", mobs);
    80. data.put("zones." + sInt + ".homeZone", homeZone);
    81. data.put("zones." + sInt + ".TNT", TNT);
    82. }
    83. }
    84. return data;
    85. }
    86.  
    87. public Zone() {
    88.  
    89. }
    90.  
    91. public Zone(Map<String, Object> map) {
    92. Map<String, Object> zoneMap = (Map<String, Object>) map.get("zones");
    93. ConfigurationSection sect = Main.zConfig.getConfigurationSection("zones");
    94. List<String> drinks = new ArrayList<>(sect.getKeys(false));
    95. for (int i = 0; i < drinks.size(); i++) {
    96. owner = (String) zoneMap.get("zones."+ i +".owner");
    97. allowList = (ArrayList) zoneMap.get("zones."+ i +".allowList");
    98. x1 = (Integer) zoneMap.get("zones."+ i +".x1");
    99. y1 = (Integer) zoneMap.get("zones."+ i +".y1");
    100. z1 = (Integer) zoneMap.get("zones."+ i +".z1");
    101. x2 = (Integer) zoneMap.get("zones."+ i +".x2");
    102. y2 = (Integer) zoneMap.get("zones."+ i +".y2");
    103. z2 = (Integer) zoneMap.get("zones."+ i +".z2");
    104. x3 = (Integer) zoneMap.get("zones."+ i +".x3");
    105. y3 = (Integer) zoneMap.get("zones."+ i +".y3");
    106. z3 = (Integer) zoneMap.get("zones."+ i +".z3");
    107. pitch = (float) zoneMap.get("zones."+ i +".pitch");
    108. yaw = (float) zoneMap.get("zones."+ i +".yaw");
    109. price = (Integer) zoneMap.get("zones."+ i +".price");
    110. world = (String) zoneMap.get("zones."+ i +".world");
    111. pvp = (Boolean) zoneMap.get("zones."+ i +".pvp");
    112. doors = (Boolean) zoneMap.get("zones."+ i +".doors");
    113. noTP = (Boolean) zoneMap.get("zones."+ i +".noTP");
    114. mobs = (Boolean) zoneMap.get("zones."+ i +".mobs");
    115. homeZone = (Boolean) zoneMap.get("zones."+ i +".homeZone");
    116. TNT = (Boolean) zoneMap.get("zones."+ i +".TNT");
    117.  
    118. }
    119.  
    120.  
    121.  
    122.  
    123. }
    124.  
    125. private String owner;
    126.  
    127. public HashSet<Zone> getsubZones() {
    128. return subZones;
    129. }
    130.  
    131. public void setsubZone(HashSet<Zone> subZones) {
    132. this.subZones = subZones;
    133. }
    134. public void addsubZone(HashSet<Zone> subZones) {
    135. this.subZones = subZones;
    136. }
    137. public void removesubZone(HashSet<Zone> subZones) {
    138. this.subZones = subZones;
    139. }
    140.  
    141. public String getOwner() {
    142. return owner;
    143. }
    144.  
    145. public void setOwner(String owner) {
    146. this.owner = owner;
    147. }
    148.  
    149. public ArrayList<String> getAllowList() {
    150. return allowList;
    151. }
    152.  
    153. public void addAllowList(ArrayList<String> allowList, String allowedPlayer) {
    154. if (!(allowList.contains(allowedPlayer))) {
    155. allowList.add(allowedPlayer);
    156. }
    157. this.allowList = allowList;
    158. }
    159.  
    160. public void removeAllowList(ArrayList<String> allowList, String unallowedPlayer) {
    161. if (allowList.contains(unallowedPlayer)) {
    162. allowList.remove(unallowedPlayer);
    163. }
    164. this.allowList = allowList;
    165. }
    166.  
    167. public void setAllowList(ArrayList<String> allowList) {
    168. this.allowList = allowList;
    169. }
    170.  
    171. public int getId() {
    172. return id;
    173. }
    174.  
    175. public void setId(int id) {
    176. this.id = id;
    177. }
    178.  
    179. public int getPrice() {
    180. return price;
    181. }
    182.  
    183. public void setPrice(int price) {
    184. this.price = price;
    185. }
    186.  
    187. public String getName() {
    188. return name;
    189. }
    190.  
    191. public void setName(String name) {
    192. this.name = name;
    193. }
    194.  
    195. public int getX1() {
    196. return x1;
    197. }
    198.  
    199. public void setX1(int x1) {
    200. this.x1 = x1;
    201. }
    202.  
    203. public int getY1() {
    204. return y1;
    205. }
    206.  
    207. public void setY1(int y1) {
    208. this.y1 = y1;
    209. }
    210.  
    211. public int getZ1() {
    212. return z1;
    213. }
    214.  
    215. public void setZ1(int z1) {
    216. this.z1 = z1;
    217. }
    218.  
    219. public int getX2() {
    220. return x2;
    221. }
    222.  
    223. public void setX2(int x2) {
    224. this.x2 = x2;
    225. }
    226.  
    227. public int getY2() {
    228. return y2;
    229. }
    230.  
    231. public void setY2(int y2) {
    232. this.y2 = y2;
    233. }
    234.  
    235. public int getZ2() {
    236. return z2;
    237. }
    238.  
    239. public void setZ2(int z2) {
    240. this.z2 = z2;
    241. }
    242.  
    243. public double getX3() {
    244. return x3;
    245. }
    246.  
    247. public void setX3(double x3) {
    248. this.x3 = x3;
    249. }
    250.  
    251. public double getY3() {
    252. return y3;
    253. }
    254.  
    255. public void setY3(double y3) {
    256. this.y3 = y3;
    257. }
    258.  
    259. public double getZ3() {
    260. return z3;
    261. }
    262.  
    263. public void setZ3(double z3) {
    264. this.z3 = z3;
    265. }
    266.  
    267. public float getYaw() {
    268. return yaw;
    269. }
    270.  
    271. public void setYaw(float yaw) {
    272. this.yaw = yaw;
    273. }
    274.  
    275. public float getPitch() {
    276. return pitch;
    277. }
    278.  
    279. public void setPitch(float pitch) {
    280. this.pitch = pitch;
    281. }
    282.  
    283. public String getWorld() {
    284. return world;
    285. }
    286.  
    287. public void setWorld(String world) {
    288. this.world = world;
    289. }
    290.  
    291. public boolean isPvp() {
    292. return pvp;
    293. }
    294.  
    295. public void setPvp(boolean pvp) {
    296. this.pvp = pvp;
    297. }
    298.  
    299. public boolean isMobs() {
    300. return mobs;
    301. }
    302.  
    303. public void setMobs(boolean mobs) {
    304. this.mobs = mobs;
    305. }
    306.  
    307. public boolean isDoors() {
    308. return doors;
    309. }
    310.  
    311. public void setDoors(boolean doors) {
    312. this.doors = doors;
    313. }
    314. public boolean isnoTP() {
    315. return noTP;
    316. }
    317.  
    318. public void setnoTP(boolean noTP) {
    319. this.noTP = noTP;
    320. }
    321. public boolean ishomeZone() {
    322. return homeZone;
    323. }
    324.  
    325. public void sethomeZone(boolean homeZone) {
    326. this.homeZone = homeZone;
    327. }
    328. public boolean isTNT() {
    329. return TNT;
    330. }
    331.  
    332. public void setTNT(boolean TNT) {
    333. this.TNT = TNT;
    334. }
    335. }
    336.  


    What would I have to write into the serialization constructors and methods for this class?
    Then how would I in my Main class load and save this into a config?
     
  4. Offline

    _LB

    Did you read the documentation at the link I provided? It explains what you have to do.
     
  5. Offline

    Chew

    Yes I did several times for the past four days along with other bukkit forum posts, and other documentation, it would be even more helpful if I could put that into a config file "zConfig.set("zones", ???);"
     
  6. Offline

    mythbusterma

    Chew

    You can, it is literally

    Code:java
    1. ConfigurationSection#set(String key, Class<? extends ConfigurationSerializable> object);


    Do you understand what serialization entails? It should be obvious what goes in those classes if you do.
     
  7. Offline

    Smerfa

    Maybe that some kind of offtopic but... after may playing with deserialization i never do:
    Code:
     someInt = (Integer) map.get("someInt");
    but always
    Code:java
    1. someInt = ((Number) map.get("someInt")).intValue();

    for int maybe that isn't very needed, but for double/float:
    Code:java
    1. someDouble = ((Number) map.get("someDouble")).doubleValue();

    it's good idea... someone can change something in config. and set it to "2" instead of "2.0", and if you just cast it to double you can get error :p

    Casting to Number and then getting value that you want, will work with every number ;) even is someone use "4.45" in int field
     
Thread Status:
Not open for further replies.

Share This Page