Work
This commit is contained in:
@@ -7,24 +7,26 @@ import com.comphenix.protocol.ProtocolManager;
|
||||
import com.comphenix.protocol.events.PacketContainer;
|
||||
import com.comphenix.protocol.reflect.StructureModifier;
|
||||
import com.comphenix.protocol.wrappers.BlockPosition;
|
||||
import com.comphenix.protocol.wrappers.WrappedBlockData;
|
||||
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
|
||||
import com.comphenix.protocol.wrappers.WrappedGameProfile;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.mojang.authlib.GameProfile;
|
||||
import com.mojang.authlib.properties.PropertyMap;
|
||||
import me.libraryaddict.disguise.DisguiseAPI;
|
||||
import me.libraryaddict.disguise.DisguiseConfig;
|
||||
import me.libraryaddict.disguise.DisguiseConfig.DisguisePushing;
|
||||
import me.libraryaddict.disguise.LibsDisguises;
|
||||
import me.libraryaddict.disguise.disguisetypes.Disguise;
|
||||
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
|
||||
import me.libraryaddict.disguise.disguisetypes.PlayerDisguise;
|
||||
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise;
|
||||
import me.libraryaddict.disguise.disguisetypes.*;
|
||||
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise.TargetType;
|
||||
import me.libraryaddict.disguise.disguisetypes.watchers.AgeableWatcher;
|
||||
import me.libraryaddict.disguise.disguisetypes.watchers.PlayerWatcher;
|
||||
import me.libraryaddict.disguise.disguisetypes.watchers.ZombieWatcher;
|
||||
import me.libraryaddict.disguise.utilities.PacketsManager.LibsPackets;
|
||||
import me.libraryaddict.disguise.utilities.json.*;
|
||||
import org.bukkit.*;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Ageable;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -39,11 +41,13 @@ import org.bukkit.scoreboard.Team.Option;
|
||||
import org.bukkit.scoreboard.Team.OptionStatus;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.lang.reflect.*;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -69,62 +73,22 @@ public class DisguiseUtilities {
|
||||
private static HashMap<UUID, String> preDisguiseTeam = new HashMap<>();
|
||||
private static File profileCache = new File("plugins/LibsDisguises/GameProfiles"), savedDisguises = new File(
|
||||
"plugins/LibsDisguises/SavedDisguises");
|
||||
private static Gson gson;
|
||||
|
||||
static {
|
||||
try {
|
||||
Object server = ReflectionManager.getNmsMethod("MinecraftServer", "getServer").invoke(null);
|
||||
Object world = ((List) server.getClass().getField("worlds").get(server)).get(0);
|
||||
public static void saveDisguises() {
|
||||
Iterator<HashSet<TargetedDisguise>> itel = disguisesInUse.values().iterator();
|
||||
|
||||
Object bedChunk = ReflectionManager.getNmsClass("Chunk").getConstructor(
|
||||
ReflectionManager.getNmsClass("World"), int.class, int.class).newInstance(world, 0, 0);
|
||||
while (itel.hasNext()) {
|
||||
HashSet<TargetedDisguise> list = itel.next();
|
||||
|
||||
Field cSection = bedChunk.getClass().getDeclaredField("sections");
|
||||
cSection.setAccessible(true);
|
||||
if (list.isEmpty())
|
||||
continue;
|
||||
|
||||
Object chunkSection = ReflectionManager.getNmsClass("ChunkSection").getConstructor(int.class,
|
||||
boolean.class).newInstance(0, true);
|
||||
|
||||
Object block = ReflectionManager.getNmsClass("Block").getMethod("getById", int.class).invoke(null,
|
||||
Material.BED_BLOCK.getId());
|
||||
|
||||
Method fromLegacyData = block.getClass().getMethod("fromLegacyData", int.class);
|
||||
Method setType = chunkSection.getClass().getMethod("setType", int.class, int.class, int.class,
|
||||
ReflectionManager.getNmsClass("IBlockData"));
|
||||
Method setSky = chunkSection.getClass().getMethod("a", int.class, int.class, int.class, int.class);
|
||||
Method setEmitted = chunkSection.getClass().getMethod("b", int.class, int.class, int.class, int.class);
|
||||
|
||||
for (BlockFace face : new BlockFace[]{BlockFace.EAST, BlockFace.WEST, BlockFace.NORTH, BlockFace.SOUTH}) {
|
||||
int x = 1 + face.getModX();
|
||||
|
||||
int z = 1 + face.getModZ();
|
||||
|
||||
setType.invoke(chunkSection, x, 0, z, fromLegacyData.invoke(block, face.ordinal()));
|
||||
|
||||
setSky.invoke(chunkSection, x, 0, z, 0);
|
||||
|
||||
setEmitted.invoke(chunkSection, x, 0, z, 0);
|
||||
}
|
||||
|
||||
Object[] array = (Object[]) Array.newInstance(chunkSection.getClass(), 16);
|
||||
|
||||
array[0] = chunkSection;
|
||||
|
||||
cSection.set(bedChunk, array);
|
||||
|
||||
spawnChunk = ProtocolLibrary.getProtocolManager().createPacketConstructor(PacketType.Play.Server.MAP_CHUNK,
|
||||
bedChunk, 65535).createPacket(bedChunk, 65535);
|
||||
|
||||
Field threadField = ReflectionManager.getNmsField("MinecraftServer", "primaryThread");
|
||||
threadField.setAccessible(true);
|
||||
|
||||
mainThread = (Thread) threadField.get(ReflectionManager.getMinecraftServer());
|
||||
}
|
||||
catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
saveDisguises(list.iterator().next().getEntity().getUniqueId(), list.toArray(new Disguise[0]));
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean hasCacheEntry(String playername) {
|
||||
public static boolean hasGameProfile(String playername) {
|
||||
return cachedNames.contains(playername.toLowerCase());
|
||||
}
|
||||
|
||||
@@ -163,14 +127,14 @@ public class DisguiseUtilities {
|
||||
player.sendMessage(ChatColor.RED + "Example usage: /disguise " + reference);
|
||||
} else {
|
||||
player.sendMessage(
|
||||
ChatColor.RED + "Failed to store the reference, too many cloned disguises. Please raise the " +
|
||||
"maximum cloned disguises, or lower the time they last");
|
||||
ChatColor.RED + "Failed to store the reference, too many cloned disguises. Please raise the " + "maximum cloned disguises, or lower the time they last");
|
||||
}
|
||||
}
|
||||
|
||||
private static void saveDisguiseToFile
|
||||
|
||||
public static void saveDisguises(UUID owningEntity, Disguise[] disguise) {
|
||||
if (!LibVersion.isPremium())
|
||||
return;
|
||||
|
||||
try {
|
||||
File disguiseFile = new File(savedDisguises, owningEntity.toString());
|
||||
|
||||
@@ -190,18 +154,12 @@ public class DisguiseUtilities {
|
||||
disguises[i] = dis;
|
||||
}
|
||||
|
||||
FileOutputStream files = new FileOutputStream(disguiseFile);
|
||||
ObjectOutputStream obj = new ObjectOutputStream(files);
|
||||
|
||||
obj.writeObject(disguises);
|
||||
PrintWriter writer = new PrintWriter(disguiseFile);
|
||||
writer.write(gson.toJson(disguises));
|
||||
writer.close();
|
||||
|
||||
savedDisguiseList.add(owningEntity);
|
||||
|
||||
obj.close();
|
||||
files.close();
|
||||
}
|
||||
|
||||
savedDisguises.save(new File(libsDisguises.getDataFolder(), "saveddisguises.yml"));
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
@@ -213,29 +171,29 @@ public class DisguiseUtilities {
|
||||
}
|
||||
|
||||
public static Disguise[] getSavedDisguises(UUID entityUUID, boolean remove) {
|
||||
if (isSavedDisguise(entityUUID))
|
||||
if (!isSavedDisguise(entityUUID) || !LibVersion.isPremium())
|
||||
return new Disguise[0];
|
||||
|
||||
String cached = savedDisguises.getString(entityUUID.toString());
|
||||
File disguiseFile = new File(savedDisguises, entityUUID.toString());
|
||||
|
||||
if (cached == null) {
|
||||
cachedNames.remove(entityUUID.toString());
|
||||
if (!disguiseFile.exists()) {
|
||||
savedDisguiseList.remove(entityUUID);
|
||||
return new Disguise[0];
|
||||
}
|
||||
|
||||
try {
|
||||
ObjectInputStream outputStream = new ObjectInputStream(new ByteArrayInputStream(cached.getBytes()));
|
||||
|
||||
Disguise[] toReturn = (Disguise[]) outputStream.readObject();
|
||||
BufferedReader reader = new BufferedReader(new FileReader(disguiseFile));
|
||||
String cached = reader.readLine();
|
||||
reader.close();
|
||||
|
||||
if (remove) {
|
||||
removeSavedDisguise(entityUUID);
|
||||
}
|
||||
|
||||
return toReturn;
|
||||
return gson.fromJson(cached, Disguise[].class);
|
||||
}
|
||||
catch (Exception e) {
|
||||
System.out.println("Error while loading Entity Disguises, malformed config?");
|
||||
System.out.println("Malformed disguise for " + entityUUID);
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -246,14 +204,9 @@ public class DisguiseUtilities {
|
||||
if (!savedDisguiseList.remove(entityUUID))
|
||||
return;
|
||||
|
||||
savedDisguises.set(entityUUID.toString(), null);
|
||||
File disguiseFile = new File(savedDisguises, entityUUID.toString());
|
||||
|
||||
try {
|
||||
savedDisguises.save(new File(libsDisguises.getDataFolder(), "saveddisguises.yml"));
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
disguiseFile.delete();
|
||||
}
|
||||
|
||||
public static boolean isSavedDisguise(UUID entityUUID) {
|
||||
@@ -330,16 +283,12 @@ public class DisguiseUtilities {
|
||||
|
||||
public static void addGameProfile(String string, WrappedGameProfile gameProfile) {
|
||||
try {
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
ObjectOutputStream obj = new ObjectOutputStream(bytes);
|
||||
obj.writeObject(new WrappedProfile(gameProfile));
|
||||
File file = new File(profileCache, string.toLowerCase());
|
||||
PrintWriter writer = new PrintWriter(file);
|
||||
writer.write(gson.toJson(gameProfile));
|
||||
writer.close();
|
||||
|
||||
gameProfileCache.set(string.toLowerCase(), new String(bytes.toByteArray()));
|
||||
cachedNames.add(string.toLowerCase());
|
||||
|
||||
if (DisguiseConfig.isSaveCache()) {
|
||||
gameProfileCache.save(new File(libsDisguises.getDataFolder(), "cache.yml"));
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
@@ -602,17 +551,19 @@ public class DisguiseUtilities {
|
||||
if (!cachedNames.contains(playerName.toLowerCase()))
|
||||
return null;
|
||||
|
||||
String cached = gameProfileCache.getString(playerName.toLowerCase());
|
||||
File file = new File(profileCache, playerName.toLowerCase());
|
||||
|
||||
if (cached == null) {
|
||||
if (!file.exists()) {
|
||||
cachedNames.remove(playerName.toLowerCase());
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
ObjectInputStream outputStream = new ObjectInputStream(new ByteArrayInputStream(cached.getBytes()));
|
||||
BufferedReader reader = new BufferedReader(new FileReader(file));
|
||||
String cached = reader.readLine();
|
||||
reader.close();
|
||||
|
||||
return ((WrappedProfile) outputStream.readObject()).getProfile();
|
||||
return gson.fromJson(cached, WrappedGameProfile.class);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
@@ -820,22 +771,84 @@ public class DisguiseUtilities {
|
||||
return selfDisguised;
|
||||
}
|
||||
|
||||
public static boolean hasGameProfile(String playerName) {
|
||||
return getGameProfile(playerName) != null;
|
||||
}
|
||||
|
||||
public static void init(LibsDisguises disguises) {
|
||||
libsDisguises = disguises;
|
||||
|
||||
gameProfileCache = YamlConfiguration.loadConfiguration(new File(disguises.getDataFolder(), "cache.yml"));
|
||||
GsonBuilder gsonBuilder = new GsonBuilder();
|
||||
gsonBuilder.registerTypeAdapter(MetaIndex.class, new SerializerMetaIndex());
|
||||
gsonBuilder.registerTypeAdapter(WrappedGameProfile.class, new SerializerGameProfile());
|
||||
gsonBuilder.registerTypeAdapter(WrappedBlockData.class, new SerializerWrappedBlockData());
|
||||
gsonBuilder.registerTypeAdapter(Disguise.class, new SerializerDisguise());
|
||||
gsonBuilder.registerTypeAdapter(FlagWatcher.class, new SerializerFlagWatcher());
|
||||
gsonBuilder.registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer());
|
||||
|
||||
cachedNames.addAll(gameProfileCache.getKeys(false));
|
||||
gson = gsonBuilder.create();
|
||||
|
||||
savedDisguises = YamlConfiguration.loadConfiguration(new File(disguises.getDataFolder(), "saveddisguises.yml"));
|
||||
if (!profileCache.exists())
|
||||
profileCache.mkdirs();
|
||||
|
||||
for (String key : savedDisguises.getKeys(false)) {
|
||||
if (!savedDisguises.exists())
|
||||
savedDisguises.mkdirs();
|
||||
|
||||
try {
|
||||
Object server = ReflectionManager.getNmsMethod("MinecraftServer", "getServer").invoke(null);
|
||||
Object world = ((List) server.getClass().getField("worlds").get(server)).get(0);
|
||||
|
||||
Object bedChunk = ReflectionManager.getNmsClass("Chunk").getConstructor(
|
||||
ReflectionManager.getNmsClass("World"), int.class, int.class).newInstance(world, 0, 0);
|
||||
|
||||
Field cSection = bedChunk.getClass().getDeclaredField("sections");
|
||||
cSection.setAccessible(true);
|
||||
|
||||
Object chunkSection = ReflectionManager.getNmsClass("ChunkSection").getConstructor(int.class,
|
||||
boolean.class).newInstance(0, true);
|
||||
|
||||
Object block = ReflectionManager.getNmsClass("Block").getMethod("getById", int.class).invoke(null,
|
||||
Material.BED_BLOCK.getId());
|
||||
|
||||
Method fromLegacyData = block.getClass().getMethod("fromLegacyData", int.class);
|
||||
Method setType = chunkSection.getClass().getMethod("setType", int.class, int.class, int.class,
|
||||
ReflectionManager.getNmsClass("IBlockData"));
|
||||
Method setSky = chunkSection.getClass().getMethod("a", int.class, int.class, int.class, int.class);
|
||||
Method setEmitted = chunkSection.getClass().getMethod("b", int.class, int.class, int.class, int.class);
|
||||
|
||||
for (BlockFace face : new BlockFace[]{BlockFace.EAST, BlockFace.WEST, BlockFace.NORTH, BlockFace.SOUTH}) {
|
||||
int x = 1 + face.getModX();
|
||||
|
||||
int z = 1 + face.getModZ();
|
||||
|
||||
setType.invoke(chunkSection, x, 0, z, fromLegacyData.invoke(block, face.ordinal()));
|
||||
|
||||
setSky.invoke(chunkSection, x, 0, z, 0);
|
||||
|
||||
setEmitted.invoke(chunkSection, x, 0, z, 0);
|
||||
}
|
||||
|
||||
Object[] array = (Object[]) Array.newInstance(chunkSection.getClass(), 16);
|
||||
|
||||
array[0] = chunkSection;
|
||||
|
||||
cSection.set(bedChunk, array);
|
||||
|
||||
spawnChunk = ProtocolLibrary.getProtocolManager().createPacketConstructor(PacketType.Play.Server.MAP_CHUNK,
|
||||
bedChunk, 65535).createPacket(bedChunk, 65535);
|
||||
|
||||
Field threadField = ReflectionManager.getNmsField("MinecraftServer", "primaryThread");
|
||||
threadField.setAccessible(true);
|
||||
|
||||
mainThread = (Thread) threadField.get(ReflectionManager.getMinecraftServer());
|
||||
}
|
||||
catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
cachedNames.addAll(Arrays.asList(profileCache.list()));
|
||||
|
||||
for (String key : savedDisguises.list()) {
|
||||
savedDisguiseList.add(UUID.fromString(key));
|
||||
}
|
||||
|
||||
LibVersion.check(libsDisguises);
|
||||
}
|
||||
|
||||
public static boolean isDisguiseInUse(Disguise disguise) {
|
||||
@@ -1088,16 +1101,10 @@ public class DisguiseUtilities {
|
||||
|
||||
public static void removeGameProfile(String string) {
|
||||
cachedNames.remove(string.toLowerCase());
|
||||
gameProfileCache.set(string.toLowerCase(), null);
|
||||
|
||||
if (DisguiseConfig.isSaveCache()) {
|
||||
try {
|
||||
gameProfileCache.save(new File(libsDisguises.getDataFolder(), "cache.yml"));
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
File file = new File(profileCache, string.toLowerCase());
|
||||
|
||||
file.delete();
|
||||
}
|
||||
|
||||
public static void removeSelfDisguise(Player player) {
|
||||
|
65
src/me/libraryaddict/disguise/utilities/LibVersion.java
Normal file
65
src/me/libraryaddict/disguise/utilities/LibVersion.java
Normal file
@@ -0,0 +1,65 @@
|
||||
package me.libraryaddict.disguise.utilities;
|
||||
|
||||
import me.libraryaddict.disguise.LibsDisguises;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
|
||||
/**
|
||||
* Created by libraryaddict on 2/06/2017.
|
||||
*/
|
||||
public class LibVersion {
|
||||
/**
|
||||
* If you're seriously going to modify this to get the premium stuff for free, can you at least not
|
||||
* distribute it? You didn't pay for it despite how cheap it is. You spend $8 on a trip to McDonalds
|
||||
* but you balk at the idea of actually supporting someone when you can just steal it for free.
|
||||
* Is the only reason you don't rob McDonalds because they can catch you? Is the only reason you don't rob your
|
||||
* Grandma being that she knows who was in her house? If you see someone's credit card drop out their pocket,
|
||||
* you planning on taking it and going shopping?
|
||||
* Do you really have the right to give someones work away for free?
|
||||
* You know enough to start coding, but you resist the idea of contributing to this plugin. Its even
|
||||
* open-source, no one is stopping you. You're the guy who files a bug report because the hacked version has
|
||||
* malware installed.
|
||||
* I'd hate to work with you.
|
||||
*/
|
||||
private static Boolean thisPluginIsPaidFor;
|
||||
|
||||
/**
|
||||
* Don't even think about disabling this unless you purchased the premium plugin. It will uh, corrupt your server
|
||||
* and stuff. Also my dog will cry because I can't afford to feed him. And my sister will be beaten by my dad
|
||||
* again because I'm not bringing enough money in.
|
||||
*/
|
||||
public static Boolean isPremium() {
|
||||
return thisPluginIsPaidFor == null ? !"%%__USER__%%".contains("__USER__") : thisPluginIsPaidFor;
|
||||
}
|
||||
|
||||
public static void check(LibsDisguises disguises) {
|
||||
thisPluginIsPaidFor = isPremium();
|
||||
|
||||
if (!isPremium() && disguises.getDescription().getVersion().contains("SNAPSHOT")) {
|
||||
for (File file : new File("plugins/LibsDisguises/").listFiles()) {
|
||||
if (!file.isFile())
|
||||
continue;
|
||||
|
||||
if (!file.getName().endsWith(".jar"))
|
||||
continue;
|
||||
|
||||
try {
|
||||
ClassLoader cl = new URLClassLoader(new URL[]{file.toURI().toURL()});
|
||||
Class c = cl.loadClass(LibVersion.class.getName());
|
||||
|
||||
Method m = c.getMethod("isPremium");
|
||||
thisPluginIsPaidFor = (Boolean) m.invoke(null);
|
||||
|
||||
if (isPremium())
|
||||
break;
|
||||
}
|
||||
catch (Exception ex) {
|
||||
// Don't print off errors
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -861,7 +861,8 @@ public class ReflectionManager {
|
||||
}
|
||||
} else if (value instanceof ItemStack) {
|
||||
return getNmsItem((ItemStack) value);
|
||||
}
|
||||
} else if (value instanceof Double)
|
||||
return ((Double) value).floatValue();
|
||||
|
||||
return value;
|
||||
}
|
||||
|
@@ -1,48 +0,0 @@
|
||||
package me.libraryaddict.disguise.utilities;
|
||||
|
||||
import com.comphenix.protocol.wrappers.WrappedGameProfile;
|
||||
import com.comphenix.protocol.wrappers.WrappedSignedProperty;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Created by libraryaddict on 15/05/2017.
|
||||
*/
|
||||
public class WrappedProfile implements Serializable {
|
||||
private WrappedGameProfile profile;
|
||||
|
||||
public WrappedProfile(WrappedGameProfile profile) {
|
||||
this.profile = profile;
|
||||
}
|
||||
|
||||
public WrappedGameProfile getProfile() {
|
||||
return profile;
|
||||
}
|
||||
|
||||
private void writeObject(java.io.ObjectOutputStream out) throws IOException {
|
||||
out.writeObject(profile.getUUID());
|
||||
out.writeObject(profile.getName());
|
||||
out.writeByte(profile.getProperties().size());
|
||||
|
||||
for (Map.Entry<String, WrappedSignedProperty> entry : profile.getProperties().entries()) {
|
||||
WrappedSignedProperty property = entry.getValue();
|
||||
|
||||
out.writeUTF(entry.getKey());
|
||||
out.writeUTF(property.getName());
|
||||
out.writeUTF(property.getSignature());
|
||||
out.writeUTF(property.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
|
||||
profile = new WrappedGameProfile((UUID) in.readObject(), in.readUTF());
|
||||
|
||||
for (int i = in.readByte(); i > 0; i--) {
|
||||
profile.getProperties().put(in.readUTF(),
|
||||
new WrappedSignedProperty(in.readUTF(), in.readUTF(), in.readUTF()));
|
||||
}
|
||||
}
|
||||
}
|
@@ -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,25 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
System.out.println(src.getHandle().toString());
|
||||
return context.serialize(src.getHandle(), GameProfile.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WrappedGameProfile deserialize(JsonElement json, Type typeOfT,
|
||||
JsonDeserializationContext context) throws JsonParseException {
|
||||
return WrappedGameProfile.fromHandle(context.deserialize(json, GameProfile.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