If you are familiar with gson

Discussion in 'Plugin Development' started by DoggyCode™, Apr 30, 2017.

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

    DoggyCode™

    I am getting the gson error: java.lang.IllegalArgumentException: class java.util.concurrent.atomic.AtomicInteger declares multiple JSON fields named serialVersionUID.

    The error occurs once trying to use Points.getInstance().forceSave(). I have yet to be able to test the loading, but I am assuming this won't work either. Anyone have any alternative solutions?

    I am guessing this is an error with Java generics. However I can't quite seem to grasp why.

    Here are my classes:
    public abstract class Points:
    Code:java
    1. package me.expdev.gkitpvp;
    2.  
    3. import me.expdev.gkitpvp.persist.json.JSONPoints;
    4.  
    5. public abstract class Points {
    6.  
    7. private static Points instance = getBPointsImpl();
    8.  
    9. public static Points getInstance() {
    10. return instance;
    11. }
    12.  
    13. private static Points getBPointsImpl() {
    14. return new JSONPoints();
    15. }
    16.  
    17. public abstract void clean();
    18.  
    19. public abstract void forceSave();
    20.  
    21. public abstract void forceSave(boolean sync);
    22.  
    23. public abstract GLocation getSpawn();
    24.  
    25. public abstract void setSpawn(GLocation loc);
    26.  
    27. public abstract GLocation getPoint(String id);
    28.  
    29. public abstract void addPoint(String id, GLocation loc);
    30.  
    31. public abstract void removePoint(String id);
    32.  
    33. public abstract void load();
    34.  
    35. }


    public abstract class MemoryPoints extending the abstract class Points:
    Code:java
    1. package me.expdev.gkitpvp.persist;
    2.  
    3. import me.expdev.gkitpvp.GLocation;
    4. import me.expdev.gkitpvp.Points;
    5. import java.util.Map;
    6. import java.util.concurrent.ConcurrentSkipListMap;
    7.  
    8. public abstract class MemoryPoints extends Points {
    9.  
    10. public GLocation spawn;
    11.  
    12. public Map<String, GLocation> points = new ConcurrentSkipListMap<String, GLocation>();
    13.  
    14. @Override
    15. public void clean() {
    16.  
    17. }
    18.  
    19. @Override
    20. public GLocation getSpawn() {
    21. return spawn;
    22. }
    23.  
    24. @Override
    25. public void setSpawn(GLocation loc) {
    26. this.spawn = loc;
    27. }
    28.  
    29. @Override
    30. public GLocation getPoint(String id) {
    31. return points.get(id);
    32. }
    33.  
    34. @Override
    35. public void addPoint(String id, GLocation loc) {
    36. points.put(id, loc);
    37. }
    38.  
    39. @Override
    40. public void removePoint(String id) {
    41. points.remove(id);
    42. }
    43.  
    44. @Override
    45. public abstract void forceSave();
    46.  
    47. @Override
    48. public abstract void load();
    49.  
    50. }


    class extending MemoryPoints:
    Code:java
    1. package me.expdev.gkitpvp.persist.json;
    2.  
    3. import com.google.gson.Gson;
    4. import com.google.gson.reflect.TypeToken;
    5. import me.expdev.gkitpvp.GKitPvPPlugin;
    6. import me.expdev.gkitpvp.persist.MemoryPoints;
    7. import me.expdev.gkitpvp.utils.DiscUtil;
    8.  
    9. import java.io.File;
    10.  
    11. public class JSONPoints extends MemoryPoints {
    12.  
    13. private Gson gson;
    14. private File file;
    15.  
    16. public JSONPoints() {
    17. file = new File(GKitPvPPlugin.p.getDataFolder(), "points.json");
    18. gson = GKitPvPPlugin.gson;
    19. }
    20.  
    21. public Gson getGson() {
    22. return gson;
    23. }
    24.  
    25. public void setGson(Gson gson) {
    26. this.gson = gson;
    27. }
    28.  
    29. public void forceSave() {
    30. forceSave(true);
    31. }
    32.  
    33. public void forceSave(boolean sync) {
    34. saveCore(file, this, sync);
    35. }
    36.  
    37. private boolean saveCore(File target, MemoryPoints data, boolean sync) {
    38. return DiscUtil.writeCatch(target, this.gson.toJson(data), sync);
    39. }
    40.  
    41. public void load() {
    42. MemoryPoints memoryPoints = this.loadCore();
    43. if (memoryPoints == null) {
    44. return;
    45. }
    46. this.points.clear();
    47. this.points.putAll(memoryPoints.points);
    48. }
    49.  
    50. private MemoryPoints loadCore() {
    51. if (!this.file.exists()) {
    52. return this;
    53. }
    54.  
    55. String content = DiscUtil.readCatch(this.file);
    56. if (content == null) {
    57. return null;
    58. }
    59.  
    60. MemoryPoints data = this.gson.fromJson(
    61. content,
    62. new TypeToken<MemoryPoints>() {
    63. }.getType());
    64.  
    65. saveCore(this.file, data, true); // Update the flatfile
    66.  
    67. return data;
    68. }
    69.  
    70. }
    71.  


    GLocation implementing Serializable, also containg serialVersionUID if relevant:
    Code:java
    1. package me.expdev.gkitpvp;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Location;
    5. import org.bukkit.World;
    6.  
    7. import java.io.Serializable;
    8.  
    9. public class GLocation implements Serializable {
    10.  
    11. private transient static final long serialVersionUID = -6049901271320963314L;
    12. private transient Location location = null;
    13.  
    14. private String worldName;
    15. private double x;
    16. private double y;
    17. private double z;
    18. private float pitch;
    19. private float yaw;
    20.  
    21. public GLocation(Location loc) {
    22. setLocation(loc);
    23. }
    24.  
    25. public GLocation(String worldName, double x, double y, double z, float yaw, float pitch) {
    26. this.worldName = worldName;
    27. this.x = x;
    28. this.y = y;
    29. this.z = z;
    30. this.yaw = yaw;
    31. this.pitch = pitch;
    32. }
    33.  
    34. public GLocation(String worldName, double x, double y, double z) {
    35. this(worldName, x, y, z, 0, 0);
    36. }
    37.  
    38. // This returns the actual Location
    39. public final Location getLocation() {
    40. // Make sure Location is initialized before returning it
    41. initLocation();
    42. return location;
    43. }
    44.  
    45. // Change the Location
    46. public void setLocation(Location loc) {
    47. this.location = loc;
    48. this.worldName = loc.getWorld().getName();
    49. this.x = loc.getX();
    50. this.y = loc.getY();
    51. this.z = loc.getZ();
    52. this.yaw = loc.getYaw();
    53. this.pitch = loc.getPitch();
    54. }
    55.  
    56.  
    57. // This initializes the Location
    58. private void initLocation() {
    59. // if location is already initialized, simply return
    60. if (location != null) {
    61. return;
    62. }
    63.  
    64. // get World; hopefully it's initialized at this point
    65. World world = Bukkit.getWorld(worldName);
    66. if (world == null) {
    67. return;
    68. }
    69.  
    70. // store the Location for future calls, and pass it on
    71. location = new Location(world, x, y, z, yaw, pitch);
    72. }
    73.  
    74.  
    75. public String getWorldName() {
    76. return worldName;
    77. }
    78.  
    79. public double getX() {
    80. return x;
    81. }
    82.  
    83. public double getY() {
    84. return y;
    85. }
    86.  
    87. public double getZ() {
    88. return z;
    89. }
    90.  
    91. public double getPitch() {
    92. return pitch;
    93. }
    94.  
    95. public double getYaw() {
    96. return yaw;
    97. }
    98. }


    Thanks in advance.

    I'd be happy if anyone could just take a good look at this and try to spot anything with return or something that makes this fail. :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Apr 30, 2017
Thread Status:
Not open for further replies.

Share This Page