Fix itemstack deserialization
This commit is contained in:
parent
251ca9b7e1
commit
322deb3d47
@ -4,6 +4,7 @@ import com.google.gson.*;
|
|||||||
import com.google.gson.internal.LinkedTreeMap;
|
import com.google.gson.internal.LinkedTreeMap;
|
||||||
import me.libraryaddict.disguise.disguisetypes.*;
|
import me.libraryaddict.disguise.disguisetypes.*;
|
||||||
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
|
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
@ -16,8 +17,8 @@ import java.util.Optional;
|
|||||||
/**
|
/**
|
||||||
* Created by libraryaddict on 1/06/2017.
|
* Created by libraryaddict on 1/06/2017.
|
||||||
*/
|
*/
|
||||||
public class SerializerFlagWatcher implements JsonDeserializer<FlagWatcher>, JsonSerializer<FlagWatcher>,
|
public class SerializerFlagWatcher
|
||||||
InstanceCreator<FlagWatcher> {
|
implements JsonDeserializer<FlagWatcher>, JsonSerializer<FlagWatcher>, InstanceCreator<FlagWatcher> {
|
||||||
private Gson gson;
|
private Gson gson;
|
||||||
|
|
||||||
public SerializerFlagWatcher(Gson gson) {
|
public SerializerFlagWatcher(Gson gson) {
|
||||||
@ -25,11 +26,11 @@ public class SerializerFlagWatcher implements JsonDeserializer<FlagWatcher>, Jso
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FlagWatcher deserialize(JsonElement json, Type typeOfT,
|
public FlagWatcher deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
|
||||||
JsonDeserializationContext context) throws JsonParseException {
|
throws JsonParseException {
|
||||||
try {
|
try {
|
||||||
FlagWatcher watcher = (FlagWatcher) gson
|
FlagWatcher watcher =
|
||||||
.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());
|
||||||
|
|
||||||
@ -37,16 +38,15 @@ public class SerializerFlagWatcher implements JsonDeserializer<FlagWatcher>, Jso
|
|||||||
correct(watcher, entity.getWatcherClass(), "backupEntityValues");
|
correct(watcher, entity.getWatcherClass(), "backupEntityValues");
|
||||||
|
|
||||||
return watcher;
|
return watcher;
|
||||||
}
|
} catch (Exception e) {
|
||||||
catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void correct(FlagWatcher watcher, Class<? extends FlagWatcher> flagWatcher,
|
private void correct(FlagWatcher watcher, Class<? extends FlagWatcher> flagWatcher, String name)
|
||||||
String name) throws NoSuchFieldException, IllegalAccessException {
|
throws NoSuchFieldException, IllegalAccessException {
|
||||||
Field field = FlagWatcher.class.getDeclaredField(name);
|
Field field = FlagWatcher.class.getDeclaredField(name);
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
HashMap<Integer, Object> map = (HashMap<Integer, Object>) field.get(watcher);
|
HashMap<Integer, Object> map = (HashMap<Integer, Object>) field.get(watcher);
|
||||||
@ -58,29 +58,28 @@ public class SerializerFlagWatcher implements JsonDeserializer<FlagWatcher>, Jso
|
|||||||
if (entry.getValue() instanceof Double) {
|
if (entry.getValue() instanceof Double) {
|
||||||
Object def = index.getDefault();
|
Object def = index.getDefault();
|
||||||
|
|
||||||
if (def instanceof Long)
|
if (def instanceof Long) {
|
||||||
entry.setValue(((Double) entry.getValue()).longValue());
|
entry.setValue(((Double) entry.getValue()).longValue());
|
||||||
else if (def instanceof Float)
|
} else if (def instanceof Float) {
|
||||||
entry.setValue(((Double) entry.getValue()).floatValue());
|
entry.setValue(((Double) entry.getValue()).floatValue());
|
||||||
else if (def instanceof Integer)
|
} else if (def instanceof Integer) {
|
||||||
entry.setValue(((Double) entry.getValue()).intValue());
|
entry.setValue(((Double) entry.getValue()).intValue());
|
||||||
else if (def instanceof Short)
|
} else if (def instanceof Short) {
|
||||||
entry.setValue(((Double) entry.getValue()).shortValue());
|
entry.setValue(((Double) entry.getValue()).shortValue());
|
||||||
else if (def instanceof Byte)
|
} else if (def instanceof Byte) {
|
||||||
entry.setValue(((Double) entry.getValue()).byteValue());
|
entry.setValue(((Double) entry.getValue()).byteValue());
|
||||||
|
}
|
||||||
} 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
|
||||||
if (index.getDefault() instanceof VillagerData) {
|
if (index.getDefault() instanceof VillagerData) {
|
||||||
entry.setValue(new Gson().fromJson(new Gson().toJson(entry.getValue()), VillagerData.class));
|
entry.setValue(new Gson().fromJson(new Gson().toJson(entry.getValue()), VillagerData.class));
|
||||||
} else if (index.getDefault() instanceof Optional) {
|
} else if (index.getDefault() instanceof Optional) {
|
||||||
|
|
||||||
for (Field f : MetaIndex.class.getFields()) {
|
for (Field f : MetaIndex.class.getFields()) {
|
||||||
try {
|
try {
|
||||||
if (f.get(null) != index) {
|
if (f.get(null) != index) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
} catch (IllegalAccessException e) {
|
||||||
catch (IllegalAccessException e) {
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,14 +94,16 @@ public class SerializerFlagWatcher implements JsonDeserializer<FlagWatcher>, Jso
|
|||||||
if (((LinkedTreeMap) entry.getValue()).isEmpty()) {
|
if (((LinkedTreeMap) entry.getValue()).isEmpty()) {
|
||||||
value = Optional.empty();
|
value = Optional.empty();
|
||||||
} else {
|
} else {
|
||||||
value = Optional
|
value = Optional.of(
|
||||||
.of(gson.fromJson(gson.toJson(((LinkedTreeMap) entry.getValue()).get("value")),
|
gson.fromJson(gson.toJson(((LinkedTreeMap) entry.getValue()).get("value")),
|
||||||
val));
|
val));
|
||||||
}
|
}
|
||||||
|
|
||||||
entry.setValue(value);
|
entry.setValue(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if (index.getDefault() instanceof ItemStack) {
|
||||||
|
entry.setValue(gson.fromJson(gson.toJson(entry.getValue()), ItemStack.class));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,8 +123,7 @@ public class SerializerFlagWatcher implements JsonDeserializer<FlagWatcher>, Jso
|
|||||||
public FlagWatcher createInstance(Type type) {
|
public FlagWatcher createInstance(Type type) {
|
||||||
try {
|
try {
|
||||||
return (FlagWatcher) type.getClass().getConstructor(Disguise.class).newInstance(null);
|
return (FlagWatcher) type.getClass().getConstructor(Disguise.class).newInstance(null);
|
||||||
}
|
} catch (Exception e) {
|
||||||
catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -141,8 +141,7 @@ public class SerializerFlagWatcher implements JsonDeserializer<FlagWatcher>, Jso
|
|||||||
method.setAccessible(true);
|
method.setAccessible(true);
|
||||||
Disguise disguise = (Disguise) method.invoke(src);
|
Disguise disguise = (Disguise) method.invoke(src);
|
||||||
obj.addProperty("entityType", disguise.getType().name());
|
obj.addProperty("entityType", disguise.getType().name());
|
||||||
}
|
} catch (Exception ex) {
|
||||||
catch (Exception ex) {
|
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user