Corrected maven structure
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package me.libraryaddict.disguise.utilities.json;
|
||||
|
||||
import com.google.gson.*;
|
||||
import me.libraryaddict.disguise.disguisetypes.*;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* Created by libraryaddict on 1/06/2017.
|
||||
*/
|
||||
public class SerializerDisguise implements JsonDeserializer<Disguise>, JsonSerializer<Disguise>, InstanceCreator<Disguise> {
|
||||
|
||||
@Override
|
||||
public Disguise deserialize(JsonElement json, Type typeOfT,
|
||||
JsonDeserializationContext context) throws JsonParseException {
|
||||
JsonObject obj = (JsonObject) json;
|
||||
DisguiseType type = DisguiseType.valueOf(obj.get("disguiseType").getAsString());
|
||||
TargetedDisguise disg;
|
||||
|
||||
if (type.isPlayer()) {
|
||||
disg = context.deserialize(json, PlayerDisguise.class);
|
||||
} else if (type.isMob()) {
|
||||
disg = context.deserialize(json, MobDisguise.class);
|
||||
} else if (type.isMisc()) {
|
||||
disg = context.deserialize(json, MiscDisguise.class);
|
||||
} else
|
||||
return null;
|
||||
|
||||
try {
|
||||
Method method = FlagWatcher.class.getDeclaredMethod("setDisguise", TargetedDisguise.class);
|
||||
method.setAccessible(true);
|
||||
method.invoke(disg.getWatcher(), disg);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return disg;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Disguise createInstance(Type type) {
|
||||
if (type == PlayerDisguise.class)
|
||||
return new PlayerDisguise("SaveDisgError");
|
||||
else if (type == MobDisguise.class)
|
||||
return new MobDisguise(DisguiseType.SHEEP);
|
||||
else if (type == MiscDisguise.class)
|
||||
return new MiscDisguise(DisguiseType.BOAT);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(Disguise src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
if (src.isPlayerDisguise())
|
||||
return context.serialize(src, PlayerDisguise.class);
|
||||
else if (src.isMobDisguise())
|
||||
return context.serialize(src, MobDisguise.class);
|
||||
else if (src.isMiscDisguise())
|
||||
return context.serialize(src, MiscDisguise.class);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
@@ -0,0 +1,97 @@
|
||||
package me.libraryaddict.disguise.utilities.json;
|
||||
|
||||
import com.google.gson.*;
|
||||
import me.libraryaddict.disguise.disguisetypes.Disguise;
|
||||
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
|
||||
import me.libraryaddict.disguise.disguisetypes.FlagWatcher;
|
||||
import me.libraryaddict.disguise.disguisetypes.MetaIndex;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by libraryaddict on 1/06/2017.
|
||||
*/
|
||||
public class SerializerFlagWatcher implements JsonDeserializer<FlagWatcher>, JsonSerializer<FlagWatcher>, InstanceCreator<FlagWatcher> {
|
||||
|
||||
@Override
|
||||
public FlagWatcher deserialize(JsonElement json, Type typeOfT,
|
||||
JsonDeserializationContext context) throws JsonParseException {
|
||||
try {
|
||||
FlagWatcher watcher = context.deserialize(json,
|
||||
Class.forName(((JsonObject) json).get("flagType").getAsString()));
|
||||
|
||||
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);
|
||||
|
||||
for (Map.Entry<Integer, Object> entry : map.entrySet()) {
|
||||
if (!(entry.getValue() instanceof Double))
|
||||
continue;
|
||||
|
||||
MetaIndex index = MetaIndex.getFlag(flagWatcher, entry.getKey());
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
@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) {
|
||||
JsonObject obj = (JsonObject) context.serialize(src);
|
||||
|
||||
obj.addProperty("flagType", src.getClass().getName());
|
||||
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;
|
||||
}
|
||||
}
|
@@ -0,0 +1,34 @@
|
||||
package me.libraryaddict.disguise.utilities.json;
|
||||
|
||||
import com.comphenix.protocol.wrappers.WrappedGameProfile;
|
||||
import com.google.gson.*;
|
||||
import com.mojang.authlib.GameProfile;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.HashMap;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Created by libraryaddict on 1/06/2017.
|
||||
*/
|
||||
public class SerializerGameProfile implements JsonSerializer<WrappedGameProfile>, JsonDeserializer<WrappedGameProfile> {
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(WrappedGameProfile src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
return context.serialize(src.getHandle(), GameProfile.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WrappedGameProfile deserialize(JsonElement json, Type typeOfT,
|
||||
JsonDeserializationContext context) throws JsonParseException {
|
||||
JsonObject obj = json.getAsJsonObject();
|
||||
|
||||
if (obj.has("id") && !obj.get("id").getAsString().contains("-")) {
|
||||
obj.addProperty("id",
|
||||
Pattern.compile("([0-9a-fA-F]{8})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]+)")
|
||||
.matcher(obj.get("id").getAsString()).replaceFirst("$1-$2-$3-$4-$5"));
|
||||
}
|
||||
|
||||
return WrappedGameProfile.fromHandle(context.deserialize(json, GameProfile.class));
|
||||
}
|
||||
}
|
@@ -0,0 +1,28 @@
|
||||
package me.libraryaddict.disguise.utilities.json;
|
||||
|
||||
import com.comphenix.protocol.wrappers.WrappedGameProfile;
|
||||
import com.google.gson.*;
|
||||
import com.mojang.authlib.GameProfile;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Created by libraryaddict on 1/06/2017.
|
||||
*/
|
||||
public class SerializerItemStack implements JsonSerializer<ItemStack>, JsonDeserializer<ItemStack> {
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(ItemStack src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
return context.serialize(src.serialize());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack deserialize(JsonElement json, Type typeOfT,
|
||||
JsonDeserializationContext context) throws JsonParseException {
|
||||
return ItemStack.deserialize((Map<String, Object>) context.deserialize(json, HashMap.class));
|
||||
}
|
||||
}
|
@@ -0,0 +1,42 @@
|
||||
package me.libraryaddict.disguise.utilities.json;
|
||||
|
||||
import com.comphenix.protocol.wrappers.WrappedGameProfile;
|
||||
import com.google.gson.*;
|
||||
import com.mojang.authlib.GameProfile;
|
||||
import me.libraryaddict.disguise.disguisetypes.MetaIndex;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* Created by libraryaddict on 1/06/2017.
|
||||
*/
|
||||
public class SerializerMetaIndex implements JsonSerializer<MetaIndex>, JsonDeserializer<MetaIndex> {
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(MetaIndex src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
JsonObject obj = new JsonObject();
|
||||
obj.addProperty("index", src.getIndex());
|
||||
obj.addProperty("flagwatcher", src.getFlagWatcher().getSimpleName());
|
||||
return obj;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MetaIndex deserialize(JsonElement json, Type typeOfT,
|
||||
JsonDeserializationContext context) throws JsonParseException {
|
||||
JsonObject obj = json.getAsJsonObject();
|
||||
String name = obj.get("flagwatcher").getAsString();
|
||||
int index = obj.get("index").getAsInt();
|
||||
|
||||
for (MetaIndex i : MetaIndex.values()) {
|
||||
if (i.getIndex() != index)
|
||||
continue;
|
||||
|
||||
if (!i.getFlagWatcher().getSimpleName().equals(name))
|
||||
continue;
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
package me.libraryaddict.disguise.utilities.json;
|
||||
|
||||
import com.comphenix.protocol.wrappers.WrappedBlockData;
|
||||
import com.comphenix.protocol.wrappers.WrappedGameProfile;
|
||||
import com.google.gson.*;
|
||||
import com.mojang.authlib.GameProfile;
|
||||
import org.bukkit.Material;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* Created by libraryaddict on 1/06/2017.
|
||||
*/
|
||||
public class SerializerWrappedBlockData implements JsonSerializer<WrappedBlockData>, JsonDeserializer<WrappedBlockData> {
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(WrappedBlockData src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
JsonObject obj = new JsonObject();
|
||||
obj.addProperty("type", src.getType().name());
|
||||
obj.addProperty("data", src.getData());
|
||||
return obj;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WrappedBlockData deserialize(JsonElement json, Type typeOfT,
|
||||
JsonDeserializationContext context) throws JsonParseException {
|
||||
JsonObject obj = json.getAsJsonObject();
|
||||
|
||||
return WrappedBlockData.createData(Material.valueOf(obj.get("type").getAsString()), obj.get("data").getAsInt());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user