Solved Gson reporting

Discussion in 'Spigot Plugin Development' started by DecisionsYT, Aug 11, 2018.

  1. Hey!

    I am using the following code to convert a custom object called SmelterObject into Json using Gson.

    My SmelterObject class is:
    Code (Text):
    package com.idleappsinc.furnacespro;

    import org.bukkit.Location;

    public class SmelterObject {

        Location location;
        String ownerUUID;
        int efficiencyLevel, durabilityLevel, fortuneLevel;
        boolean isLocked;

        public Location getLocation() {
            return location;
        }

        public void setLocation(Location location) {
            this.location = location;
        }

        public String getOwnerUUID() {
            return ownerUUID;
        }

        public void setOwnerUUID(String ownerUUID) {
            this.ownerUUID = ownerUUID;
        }

        public int getEfficiencyLevel() {
            return efficiencyLevel;
        }

        public void setEfficiencyLevel(int efficiencyLevel) {
            this.efficiencyLevel = efficiencyLevel;
        }

        public int getDurabilityLevel() {
            return durabilityLevel;
        }

        public void setDurabilityLevel(int durabilityLevel) {
            this.durabilityLevel = durabilityLevel;
        }

        public int getFortuneLevel() {
            return fortuneLevel;
        }

        public void setFortuneLevel(int fortuneLevel) {
            this.fortuneLevel = fortuneLevel;
        }

        public boolean isLocked() {
            return isLocked;
        }

        public void setLocked(boolean locked) {
            isLocked = locked;
        }
    }
     
    My code to convert it to Json using Gson is:
    Code (Text):
    SmelterObject smelterObject = new SmelterObject();
                smelterObject.setLocation(placedBlock.getLocation());
                smelterObject.setDurabilityLevel(1);
                smelterObject.setEfficiencyLevel(1);
                smelterObject.setFortuneLevel(1);
                smelterObject.setLocked(false);
                smelterObject.setOwnerUUID(player.getUniqueId().toString());


               

                Gson json = new Gson();
                String response = json.toJson(smelterObject);
    The error being reported is:
    Code (Text):
    [09:49:48 ERROR]: Could not pass event BlockPlaceEvent to FurnacesPro v1.0
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:308) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:485) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at org.bukkit.craftbukkit.v1_13_R1.event.CraftEventFactory.callBlockPlaceEvent(CraftEventFactory.java:153) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at net.minecraft.server.v1_13_R1.ItemStack.placeItem(ItemStack.java:204) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at net.minecraft.server.v1_13_R1.PlayerInteractManager.a(PlayerInteractManager.java:500) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at net.minecraft.server.v1_13_R1.PlayerConnection.a(PlayerConnection.java:1227) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at net.minecraft.server.v1_13_R1.PacketPlayInUseItem.a(PacketPlayInUseItem.java:37) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at net.minecraft.server.v1_13_R1.PacketPlayInUseItem.a(PacketPlayInUseItem.java:1) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at net.minecraft.server.v1_13_R1.PlayerConnectionUtils.a(SourceFile:10) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:?]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:?]
            at net.minecraft.server.v1_13_R1.SystemUtils.a(SourceFile:198) [spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at net.minecraft.server.v1_13_R1.MinecraftServer.w(MinecraftServer.java:884) [spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at net.minecraft.server.v1_13_R1.DedicatedServer.w(DedicatedServer.java:411) [spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at net.minecraft.server.v1_13_R1.MinecraftServer.v(MinecraftServer.java:819) [spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at net.minecraft.server.v1_13_R1.MinecraftServer.run(MinecraftServer.java:717) [spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at java.lang.Thread.run(Unknown Source) [?:?]
    Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make private java.lang.module.Configuration() accessible: module java.base does not "opens java.lang.module" to unnamed module @23d23d98
            at java.lang.reflect.AccessibleObject.checkCanSetAccessible(Unknown Source) ~[?:?]
            at java.lang.reflect.AccessibleObject.checkCanSetAccessible(Unknown Source) ~[?:?]
            at java.lang.reflect.Constructor.checkCanSetAccessible(Unknown Source) ~[?:?]
            at java.lang.reflect.Constructor.setAccessible(Unknown Source) ~[?:?]
            at com.google.gson.internal.ConstructorConstructor.newDefaultConstructor(ConstructorConstructor.java:101) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.ConstructorConstructor.get(ConstructorConstructor.java:83) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:99) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.Gson.getAdapter(Gson.java:423) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.Gson.getAdapter(Gson.java:423) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.Gson.getAdapter(Gson.java:423) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.Gson.getAdapter(Gson.java:423) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.Gson.getAdapter(Gson.java:423) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.Gson.getAdapter(Gson.java:423) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.Gson.getAdapter(Gson.java:423) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.Gson.getAdapter(Gson.java:423) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.Gson.getAdapter(Gson.java:423) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.Gson.getAdapter(Gson.java:423) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.Gson.getAdapter(Gson.java:423) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:56) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.Gson.toJson(Gson.java:669) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.Gson.toJson(Gson.java:648) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.Gson.toJson(Gson.java:603) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.google.gson.Gson.toJson(Gson.java:583) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            at com.idleappsinc.furnacespro.listeners.PlaceSmelterListener.onPlaceSmelter(PlaceSmelterListener.java:61) ~[?:?]
            at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
            at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:?]
            at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:?]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:?]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:304) ~[spigot-1.13.jar:git-Spigot-1503de9-200b239]
            ... 18 more
    Line 61 where the error is being reported is:
    Code (Text):
    String response = json.toJson(smelterObject);
    I have looked around online and found some people saying that adding
    Code (Text):
    <properties>
            <argLine>java --add-opens java.base/java.lang=ALL-UNNAMED</argLine>
        </properties>
    Should fix it, but I've had no luck.

    In case it's needed, here is my pom.xml file:
    Code (Text):
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>

        <groupId>com.idleappsinc</groupId>
        <artifactId>FurnacesPro</artifactId>
        <version>1.0-SNAPSHOT</version>
        <properties>
            <argLine>java --add-opens java.base/java.lang=ALL-UNNAMED</argLine>
        </properties>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.3</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>

        <repositories>
            <repository>
                <id>spigotmc-repo</id>
                <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
            </repository>
        </repositories>

        <dependencies>
            <dependency>
                <groupId>org.spigotmc</groupId>
                <artifactId>spigot-api</artifactId>
                <version>1.13-R0.1-SNAPSHOT</version>
                <scope>provided</scope>
            </dependency>

            <dependency>
                <groupId>com.google.code.gson</groupId>
                <artifactId>gson</artifactId>
                <version>2.8.5</version>
            </dependency>

        </dependencies>


     
    Any help is appreciated.
     
  2. I'm not a GSON expert. I use org.json.simple and write my own serialize & deserialize methods. This is what i would suggest to you.
    You got 6 fields in your class. Just write a serializor, which sets these 6 fields in an empty JSON and a deserializor, which reads the same out of the JSON again.
    This would be done in 5 to 10 minutes ans saves a lot of trouble with the strange and magical - never needed toJson & fromJson methods.
    Edit: furthermore this got the advantage, that you got the full control over the JSON structure and don't need to take whatever #toJson presents you.
     
  3. MiniDigger

    Supporter

    if you use java 9+, you need to use the module system properly. my guess is that the gson version minecraft includes doesn't properly support java 9+. the fix for your issue is here, got merged into gson in jan https://github.com/google/gson/pull/1218
    what I suggest you to do is to use the maven shade plugin and relocate your gson version so that you can use an up to date version.

    that temp fix you found needs to be applied on runtime, not in your maven pom but in your start file.

    oh, what could work too is not including the location, thats where it seems to choke on. not entirely sure, haven't really worked with java 9+ :shrug:
    after looking closer, I would recommend writing a custom serializer for location, else it will save the whole world object, which is really not what you want, you want only the location

    gson isn't the issue here. gson is fine and as a user you normally don't need to worry about any of its magic. the issue here is that minecraft includes an old gson version that is not compatible with javas new module system and OP runs java 9+.
    gson is a godsend, manually serializing is error prone, boring and doesn't allow fancy stuff like versioning your files.
     
  4. Hey, thanks. I've looked online as to how you create a serializor in an obejct class (I'm a noob on that) I've added implements serializable into my SmelterObject class and now I'm not sure what to do from here.
     
  5. I'm not talking about java.io.Serializable.
    I‘m talking of writing your own "public Json serialize()", in which you set your 6 fields into a JSON and return it. That simple.

    That's good to know, but i probably still won’t use it in the future.

    Let OP decide, what he / she wants to do. There are dis- / advantages on both sides.
     
  6. So, do you think it would be better if I didn't include the lcoation in my object at all, then I wouldn't need to do any of this serialization / deserialization? But rather to store the location XYZ and world as strings?

    EDIT: I think I'm going to do that instead. Thanks to everyone who suggested their ideas.
     
    #7 DecisionsYT, Aug 11, 2018
    Last edited: Aug 11, 2018
  7. I know this is solved and may be a little offtopic, but may I suggest you use lombok? Instead of manually creating getters and setters like you are doing, you can just use an annotation. It really helps :)
     
  8. Thanks! Looks very useful. I might use that.