Fix particle serialization

This commit is contained in:
libraryaddict 2020-12-05 08:10:53 +13:00
parent d2decad8d5
commit b5e41f3144
3 changed files with 31 additions and 1 deletions

View File

@ -1238,11 +1238,13 @@ public class DisguiseUtilities {
GsonBuilder gsonBuilder = new GsonBuilder(); GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.disableHtmlEscaping(); gsonBuilder.disableHtmlEscaping();
gsonBuilder.setPrettyPrinting();
gsonBuilder.registerTypeAdapter(MetaIndex.class, new SerializerMetaIndex()); gsonBuilder.registerTypeAdapter(MetaIndex.class, new SerializerMetaIndex());
gsonBuilder.registerTypeAdapter(WrappedGameProfile.class, new SerializerGameProfile()); gsonBuilder.registerTypeAdapter(WrappedGameProfile.class, new SerializerGameProfile());
gsonBuilder.registerTypeAdapter(WrappedBlockData.class, new SerializerWrappedBlockData()); gsonBuilder.registerTypeAdapter(WrappedBlockData.class, new SerializerWrappedBlockData());
gsonBuilder.registerTypeAdapter(WrappedChatComponent.class, new SerializerChatComponent()); gsonBuilder.registerTypeAdapter(WrappedChatComponent.class, new SerializerChatComponent());
gsonBuilder.registerTypeAdapter(WrappedParticle.class, new SerializerParticle());
gsonBuilder.registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer()); gsonBuilder.registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer());
gsonBuilder.registerTypeHierarchyAdapter(ItemStack.class, new SerializerItemStack()); gsonBuilder.registerTypeHierarchyAdapter(ItemStack.class, new SerializerItemStack());

View File

@ -1,9 +1,13 @@
package me.libraryaddict.disguise.utilities.json; package me.libraryaddict.disguise.utilities.json;
import com.comphenix.protocol.wrappers.WrappedParticle;
import com.google.gson.*; 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 me.libraryaddict.disguise.utilities.params.ParamInfoManager;
import me.libraryaddict.disguise.utilities.params.types.custom.ParamInfoParticle;
import me.libraryaddict.disguise.utilities.parser.DisguiseParseException;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@ -46,7 +50,7 @@ public class SerializerFlagWatcher
} }
private void correct(FlagWatcher watcher, Class<? extends FlagWatcher> flagWatcher, String name) private void correct(FlagWatcher watcher, Class<? extends FlagWatcher> flagWatcher, String name)
throws NoSuchFieldException, IllegalAccessException { throws NoSuchFieldException, IllegalAccessException, DisguiseParseException {
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);
@ -69,6 +73,11 @@ public class SerializerFlagWatcher
} 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 String) {
if (index.getDefault() instanceof WrappedParticle) {
entry.setValue(((ParamInfoParticle) ParamInfoManager.getParamInfo(WrappedParticle.class))
.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
if (index.getDefault() instanceof VillagerData) { if (index.getDefault() instanceof VillagerData) {

View File

@ -0,0 +1,19 @@
package me.libraryaddict.disguise.utilities.json;
import com.comphenix.protocol.wrappers.WrappedParticle;
import com.google.gson.*;
import me.libraryaddict.disguise.utilities.params.ParamInfoManager;
import org.bukkit.Bukkit;
import org.bukkit.block.data.BlockData;
import java.lang.reflect.Type;
/**
* Created by libraryaddict on 5/12/2020.
*/
public class SerializerParticle implements JsonSerializer<WrappedParticle> {
@Override
public JsonElement serialize(WrappedParticle src, Type typeOfSrc, JsonSerializationContext context) {
return context.serialize(ParamInfoManager.getParamInfo(WrappedParticle.class).toString(src));
}
}