2017-06-02 15:51:03 +02:00
|
|
|
package me.libraryaddict.disguise.utilities.json;
|
|
|
|
|
2019-04-19 06:52:56 +02:00
|
|
|
import com.comphenix.protocol.wrappers.WrappedBlockData;
|
|
|
|
import com.comphenix.protocol.wrappers.WrappedChatComponent;
|
2019-09-15 01:04:28 +02:00
|
|
|
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
|
2019-04-19 06:52:56 +02:00
|
|
|
import com.comphenix.protocol.wrappers.WrappedGameProfile;
|
2017-06-02 15:51:03 +02:00
|
|
|
import com.google.gson.*;
|
2019-05-24 09:20:55 +02:00
|
|
|
import com.google.gson.internal.LinkedTreeMap;
|
2019-04-19 06:52:56 +02:00
|
|
|
import com.mojang.authlib.properties.PropertyMap;
|
2019-05-18 08:54:51 +02:00
|
|
|
import me.libraryaddict.disguise.disguisetypes.*;
|
2019-05-24 09:20:55 +02:00
|
|
|
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
|
2019-09-15 01:04:28 +02:00
|
|
|
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
|
2019-04-19 06:52:56 +02:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
2017-06-02 15:51:03 +02:00
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.lang.reflect.Method;
|
2019-09-15 01:04:28 +02:00
|
|
|
import java.lang.reflect.ParameterizedType;
|
2017-06-02 15:51:03 +02:00
|
|
|
import java.lang.reflect.Type;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2019-09-15 01:04:28 +02:00
|
|
|
import java.util.Optional;
|
2017-06-02 15:51:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by libraryaddict on 1/06/2017.
|
|
|
|
*/
|
2019-04-19 06:52:56 +02:00
|
|
|
public class SerializerFlagWatcher implements JsonDeserializer<FlagWatcher>, JsonSerializer<FlagWatcher>,
|
|
|
|
InstanceCreator<FlagWatcher> {
|
|
|
|
private Gson gson;
|
|
|
|
|
|
|
|
public SerializerFlagWatcher() {
|
|
|
|
GsonBuilder gsonBuilder = new GsonBuilder();
|
|
|
|
gsonBuilder.registerTypeAdapter(MetaIndex.class, new SerializerMetaIndex());
|
|
|
|
gsonBuilder.registerTypeAdapter(WrappedGameProfile.class, new SerializerGameProfile());
|
|
|
|
gsonBuilder.registerTypeAdapter(WrappedBlockData.class, new SerializerWrappedBlockData());
|
|
|
|
gsonBuilder.registerTypeAdapter(WrappedChatComponent.class, new SerializerChatComponent());
|
|
|
|
gsonBuilder.registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer());
|
|
|
|
gsonBuilder.registerTypeAdapter(ItemStack.class, new SerializerItemStack());
|
|
|
|
|
|
|
|
gson = gsonBuilder.create();
|
|
|
|
}
|
2017-06-02 15:51:03 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public FlagWatcher deserialize(JsonElement json, Type typeOfT,
|
|
|
|
JsonDeserializationContext context) throws JsonParseException {
|
|
|
|
try {
|
2019-04-19 06:52:56 +02:00
|
|
|
FlagWatcher watcher = (FlagWatcher) gson
|
|
|
|
.fromJson(json, Class.forName(((JsonObject) json).get("flagType").getAsString()));
|
2017-06-02 15:51:03 +02:00
|
|
|
|
|
|
|
DisguiseType entity = DisguiseType.valueOf(((JsonObject) json).get("entityType").getAsString());
|
|
|
|
|
|
|
|
correct(watcher, watcher.getClass(), "entityValues");
|
|
|
|
correct(watcher, entity.getWatcherClass(), "backupEntityValues");
|
|
|
|
|
|
|
|
return watcher;
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void correct(FlagWatcher watcher, Class<? extends FlagWatcher> flagWatcher,
|
|
|
|
String name) throws NoSuchFieldException, IllegalAccessException {
|
|
|
|
Field field = FlagWatcher.class.getDeclaredField(name);
|
|
|
|
field.setAccessible(true);
|
|
|
|
HashMap<Integer, Object> map = (HashMap<Integer, Object>) field.get(watcher);
|
2019-05-24 09:20:55 +02:00
|
|
|
int count = 0;
|
2017-06-02 15:51:03 +02:00
|
|
|
|
|
|
|
for (Map.Entry<Integer, Object> entry : map.entrySet()) {
|
2019-05-24 09:20:55 +02:00
|
|
|
MetaIndex index = MetaIndex.getMetaIndex(flagWatcher, entry.getKey());
|
2019-05-18 08:54:51 +02:00
|
|
|
|
2019-05-24 09:20:55 +02:00
|
|
|
if (entry.getValue() instanceof Double) {
|
2019-05-18 08:54:51 +02:00
|
|
|
Object def = index.getDefault();
|
|
|
|
|
|
|
|
if (def instanceof Long)
|
|
|
|
entry.setValue(((Double) entry.getValue()).longValue());
|
|
|
|
else if (def instanceof Float)
|
|
|
|
entry.setValue(((Double) entry.getValue()).floatValue());
|
|
|
|
else if (def instanceof Integer)
|
|
|
|
entry.setValue(((Double) entry.getValue()).intValue());
|
|
|
|
else if (def instanceof Short)
|
|
|
|
entry.setValue(((Double) entry.getValue()).shortValue());
|
|
|
|
else if (def instanceof Byte)
|
|
|
|
entry.setValue(((Double) entry.getValue()).byteValue());
|
2019-05-24 09:20:55 +02:00
|
|
|
} else if (entry.getValue() instanceof LinkedTreeMap) { // If it's deserialized incorrectly as a map
|
|
|
|
// If the default value is not VillagerData
|
2019-09-15 01:04:28 +02:00
|
|
|
if (index.getDefault() instanceof VillagerData) {
|
|
|
|
entry.setValue(new Gson().fromJson(new Gson().toJson(entry.getValue()), VillagerData.class));
|
|
|
|
} else if (index.getDefault() instanceof Optional) {
|
|
|
|
|
|
|
|
for (Field f : MetaIndex.class.getFields()) {
|
|
|
|
try {
|
|
|
|
if (f.get(null) != index) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (IllegalAccessException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
Type type = f.getGenericType();
|
|
|
|
Type opt = ((ParameterizedType) type).getActualTypeArguments()[0];
|
|
|
|
|
|
|
|
if (opt instanceof ParameterizedType) {
|
|
|
|
Type val = ((ParameterizedType) opt).getActualTypeArguments()[0];
|
|
|
|
|
2019-09-15 02:26:29 +02:00
|
|
|
Optional value;
|
|
|
|
|
|
|
|
if (((LinkedTreeMap) entry.getValue()).isEmpty()) {
|
|
|
|
value = Optional.empty();
|
|
|
|
} else {
|
|
|
|
value = Optional
|
|
|
|
.of(gson.fromJson(gson.toJson(((LinkedTreeMap) entry.getValue()).get("value")),
|
|
|
|
val));
|
|
|
|
}
|
|
|
|
|
|
|
|
entry.setValue(value);
|
2019-09-15 01:04:28 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-18 08:54:51 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-24 09:20:55 +02:00
|
|
|
|
|
|
|
// If the deserialized class is not the same class type as the default
|
|
|
|
if (!index.getDefault().getClass().isInstance(entry.getValue())) {
|
|
|
|
entry.setValue(index.getDefault());
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count > 0) {
|
|
|
|
DisguiseUtilities.getLogger().info("Fixed " + count + " incorrect disguise flags on saved disguise");
|
2017-06-02 15:51:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public FlagWatcher createInstance(Type type) {
|
|
|
|
try {
|
|
|
|
return (FlagWatcher) type.getClass().getConstructor(Disguise.class).newInstance(null);
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public JsonElement serialize(FlagWatcher src, Type typeOfSrc, JsonSerializationContext context) {
|
2019-04-19 06:52:56 +02:00
|
|
|
JsonObject obj = (JsonObject) gson.toJsonTree(src);
|
2017-06-02 15:51:03 +02:00
|
|
|
|
|
|
|
obj.addProperty("flagType", src.getClass().getName());
|
2018-12-12 23:28:02 +01:00
|
|
|
|
2017-06-02 15:51:03 +02:00
|
|
|
try {
|
|
|
|
Method method = FlagWatcher.class.getDeclaredMethod("getDisguise");
|
|
|
|
method.setAccessible(true);
|
|
|
|
Disguise disguise = (Disguise) method.invoke(src);
|
|
|
|
obj.addProperty("entityType", disguise.getType().name());
|
|
|
|
}
|
|
|
|
catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
}
|