我正在开发一个minecraft-forge模块,它可以从游戏中收集数据,并将所有数据转换为JSON字符串。这个模块工作正常,而我的类只有这些字段:
public class MyInfo {
public int ID;
public float Health;
public double X;
public double Y;
public double Z;
public MyInfo(Player play) {
ID = play.getId();
Health = play.getHealth();
X = play.getX();
Y = play.getY();
Z = play.getZ();
}
Player
是net.minecraft.world.entity.player.Player
的一个示例,我可以在其中检索一些所需的数据。
它工作得很好,我制作JSON字符串的那一行没有遇到任何问题。
Gson _Json = new GsonBuilder().setPrettyPrinting().create();
MyInfo pers = new MyInfo(player);
String Data = _Json.toJson(pers);
问题:
只要我在MyInfo
类中添加了一个ArrayList
字段,就像我现在要展示的那样:
public class MyInfo {
public int ID;
public float Health;
public double X;
public double Y;
public double Z;
public ArrayList<MobEffectInstance> ActivePotionEffects;
public MyInfo(Player play) {
ID = play.getId();
Health = play.getHealth();
X = play.getX();
Y = play.getY();
Z = play.getZ();
ActivePotionEffects = new ArrayList<>(play.getActiveEffects()); //the method .getActiveEffects returns a Collection<MobEffectIstance>
}
线程一到达我使用_Json.toJson(pers)
的行就遇到一个异常,该异常表示:
Exception in thread "Thread-6" java.lang.reflect.InaccessibleObjectException:
Unable to make field private final java.util.concurrent.atomic.AtomicLong java.util.Random.seed accessible: module java.base does not "opens java.util" to module com.google.gson
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:178)
at java.base/java.lang.reflect.Field.setAccessible(Field.java:172)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.reflect.UnsafeReflectionAccessor.makeAccessible(UnsafeReflectionAccessor.java:44)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:159)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.Gson.getAdapter(Gson.java:489)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.Gson.getAdapter(Gson.java:489)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(CollectionTypeAdapterFactory.java:53)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.Gson.getAdapter(Gson.java:489)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.Gson.getAdapter(Gson.java:489)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.Gson.getAdapter(Gson.java:489)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.Gson.getAdapter(Gson.java:489)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(CollectionTypeAdapterFactory.java:53)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.Gson.getAdapter(Gson.java:489)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.Gson.getAdapter(Gson.java:489)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.Gson.getAdapter(Gson.java:489)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.Gson.getAdapter(Gson.java:489)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.Gson.toJson(Gson.java:727)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.Gson.toJsonTree(Gson.java:628)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.Gson.toJsonTree(Gson.java:607)
如果我在示例化_Json
的同一个类中示例化一个ArrayList<Integer>
,并通过FOR循环填充它,然后使用toJson()
,则不会出现访问java.lang
以获得Gson
的问题
即:
ArrayList<Integer> numbers = new ArrayList<>();
for (int i = 0; i < 10; i++) {
numbers.add(i);
}
Gson _Json = new GsonBuilder().setPrettyPrinting().create();
MyInfo playr = new MyInfo(player);
String Data = _Json.toJson(numbers); //<-- not passing *playr* anymore, but *numbers*
编辑:
我找到了一个变通方法,它主要是为我的目的工作的,是有类型MobEffectInstance
分解为基本数据类型(即String
或Integer
),并把这些值 在集合中
public class MyInfo {
public int ID;
public float Health;
public double X;
public double Y;
public double Z;
public HashMap<String,Integer> ActivePotionEffects;
public MyInfo(Player play) {
ID = play.getId();
Health = play.getHealth();
X = play.getX();
Y = play.getY();
Z = play.getZ();
ActivePotionEffects = new HashMap<>();
ArrayList<MobEffectInstance> Temp = new ArrayList<>(play.getActiveEffects());
for (MobEffectInstance mobEffectInstance : Temp) {
ActivePotionEffects.put(mobEffectInstance.getDescriptionId(), mobEffectInstance.getDuration());
}
}
1条答案
按热度按时间lb3vh1jj1#
当你初始化你的数组列表时,你有空的〈〉括号,它们应该有MobEffectInstance在里面。