Fix a weird issue where json deserializing didn't work out of the blue

This commit is contained in:
libraryaddict 2021-02-25 04:39:30 +13:00
parent 0228b3ecac
commit 3582d1ffed

View File

@ -21,8 +21,7 @@ import java.util.Optional;
/** /**
* Created by libraryaddict on 1/06/2017. * Created by libraryaddict on 1/06/2017.
*/ */
public class SerializerFlagWatcher public class SerializerFlagWatcher implements JsonDeserializer<FlagWatcher>, JsonSerializer<FlagWatcher>, InstanceCreator<FlagWatcher> {
implements JsonDeserializer<FlagWatcher>, JsonSerializer<FlagWatcher>, InstanceCreator<FlagWatcher> {
private Gson gson; private Gson gson;
public SerializerFlagWatcher(Gson gson) { public SerializerFlagWatcher(Gson gson) {
@ -30,11 +29,9 @@ public class SerializerFlagWatcher
} }
@Override @Override
public FlagWatcher deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) public FlagWatcher deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
throws JsonParseException {
try { try {
FlagWatcher watcher = FlagWatcher watcher = (FlagWatcher) gson.fromJson(json, Class.forName(((JsonObject) json).get("flagType").getAsString()));
(FlagWatcher) gson.fromJson(json, Class.forName(((JsonObject) json).get("flagType").getAsString()));
DisguiseType entity = DisguiseType.valueOf(((JsonObject) json).get("entityType").getAsString()); DisguiseType entity = DisguiseType.valueOf(((JsonObject) json).get("entityType").getAsString());
@ -75,8 +72,7 @@ public class SerializerFlagWatcher
} }
} else if (entry.getValue() instanceof String) { } else if (entry.getValue() instanceof String) {
if (index.getDefault() instanceof WrappedParticle) { if (index.getDefault() instanceof WrappedParticle) {
entry.setValue(((ParamInfoParticle) ParamInfoManager.getParamInfo(WrappedParticle.class)) entry.setValue(((ParamInfoParticle) ParamInfoManager.getParamInfo(WrappedParticle.class)).fromString((String) entry.getValue()));
.fromString((String) entry.getValue()));
} }
} else if (entry.getValue() instanceof LinkedTreeMap) { // If it's deserialized incorrectly as a map } else if (entry.getValue() instanceof LinkedTreeMap) { // If it's deserialized incorrectly as a map
// If the default value is not VillagerData // If the default value is not VillagerData
@ -103,9 +99,7 @@ public class SerializerFlagWatcher
if (((LinkedTreeMap) entry.getValue()).isEmpty()) { if (((LinkedTreeMap) entry.getValue()).isEmpty()) {
value = Optional.empty(); value = Optional.empty();
} else { } else {
value = Optional.of( value = Optional.of(gson.fromJson(gson.toJson(((LinkedTreeMap) entry.getValue()).get("value")), val));
gson.fromJson(gson.toJson(((LinkedTreeMap) entry.getValue()).get("value")),
val));
} }
entry.setValue(value); entry.setValue(value);
@ -131,7 +125,7 @@ public class SerializerFlagWatcher
@Override @Override
public FlagWatcher createInstance(Type type) { public FlagWatcher createInstance(Type type) {
try { try {
return (FlagWatcher) type.getClass().getConstructor(Disguise.class).newInstance(null); return (FlagWatcher) ((Class) type).getConstructor(Disguise.class).newInstance(new Object[]{null});
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }