From daa328cd1f1222e57e5c1fe2fe227f7e0a7cedd8 Mon Sep 17 00:00:00 2001 From: GeorgH93 Date: Wed, 23 Sep 2015 00:33:34 +0200 Subject: [PATCH] Using the Bukkit/Bungee Plugin Lib --- .../Database/ItemStackSerializer/Base.java | 68 -------------- .../InventorySerializer.java | 64 +++++++++++++ .../ItemStackSerializer.java | 61 ------------ .../Database/ItemStackSerializer/MC_1_8.java | 93 ------------------- 4 files changed, 64 insertions(+), 222 deletions(-) delete mode 100644 src/at/pcgamingfreaks/georgh/MinePacks/Database/ItemStackSerializer/Base.java create mode 100644 src/at/pcgamingfreaks/georgh/MinePacks/Database/ItemStackSerializer/InventorySerializer.java delete mode 100644 src/at/pcgamingfreaks/georgh/MinePacks/Database/ItemStackSerializer/ItemStackSerializer.java delete mode 100644 src/at/pcgamingfreaks/georgh/MinePacks/Database/ItemStackSerializer/MC_1_8.java diff --git a/src/at/pcgamingfreaks/georgh/MinePacks/Database/ItemStackSerializer/Base.java b/src/at/pcgamingfreaks/georgh/MinePacks/Database/ItemStackSerializer/Base.java deleted file mode 100644 index e01a891..0000000 --- a/src/at/pcgamingfreaks/georgh/MinePacks/Database/ItemStackSerializer/Base.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (C) 2014-2015 GeorgH93 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package at.pcgamingfreaks.georgh.MinePacks.Database.ItemStackSerializer; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; - -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.ItemStack; -import org.bukkit.util.io.BukkitObjectInputStream; -import org.bukkit.util.io.BukkitObjectOutputStream; - -public class Base -{ - public byte[] toByteArray(Inventory inv) - { - byte[] ba = null; - try - { - ByteArrayOutputStream b = new ByteArrayOutputStream(); - BukkitObjectOutputStream output = new BukkitObjectOutputStream(b); - output.writeObject(inv.getContents()); - output.close(); - ba = b.toByteArray(); - output.close(); - return ba; - } - catch(Exception e) - { - e.printStackTrace(); - } - return ba; - } - - public ItemStack[] toItemStack(byte[] data) - { - if(data != null) - { - try - { - BukkitObjectInputStream bukkitObjectInputStream = new BukkitObjectInputStream(new ByteArrayInputStream(data)); - ItemStack[] its = (ItemStack[]) bukkitObjectInputStream.readObject(); - bukkitObjectInputStream.close(); - return its; - } - catch (Exception e) - { - e.printStackTrace(); - } - } - return null; - } -} \ No newline at end of file diff --git a/src/at/pcgamingfreaks/georgh/MinePacks/Database/ItemStackSerializer/InventorySerializer.java b/src/at/pcgamingfreaks/georgh/MinePacks/Database/ItemStackSerializer/InventorySerializer.java new file mode 100644 index 0000000..54fa145 --- /dev/null +++ b/src/at/pcgamingfreaks/georgh/MinePacks/Database/ItemStackSerializer/InventorySerializer.java @@ -0,0 +1,64 @@ +package at.pcgamingfreaks.georgh.MinePacks.Database.ItemStackSerializer; + +import at.pcgamingfreaks.Bukkit.ItemStackSerializer.BukkitItemStackSerializer; +import at.pcgamingfreaks.Bukkit.ItemStackSerializer.ItemStackSerializer; +import at.pcgamingfreaks.Bukkit.ItemStackSerializer.NBTItemStackSerializer; +import org.bukkit.Bukkit; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; + +public class InventorySerializer +{ + ItemStackSerializer serializer, baseItemStackSerializer = new BukkitItemStackSerializer(); + int usedSerializer = 1; + + public InventorySerializer() + { + String name = Bukkit.getServer().getClass().getPackage().getName(); + String[] version = name.substring(name.lastIndexOf('.') + 2).split("_"); + try + { + if(version[0].equals("1")) + { + if(version[1].equals("8")) + { + serializer = new NBTItemStackSerializer(); + } + } + } + catch(Exception e) + { + e.printStackTrace(); + } + if(serializer == null) + { + usedSerializer = 0; + serializer = baseItemStackSerializer; + } + } + + public byte[] serialize(Inventory inv) + { + return serializer.serialize(inv.getContents()); + } + + @SuppressWarnings("unused") + public ItemStack[] deserialize(byte[] data) + { + return deserialize(data, usedSerializer); + } + + public ItemStack[] deserialize(byte[] data, int usedSerializer) + { + switch(usedSerializer) + { + case 0: return baseItemStackSerializer.deserialize(data); + default: return serializer.deserialize(data); + } + } + + public int getUsedSerializer() + { + return usedSerializer; + } +} \ No newline at end of file diff --git a/src/at/pcgamingfreaks/georgh/MinePacks/Database/ItemStackSerializer/ItemStackSerializer.java b/src/at/pcgamingfreaks/georgh/MinePacks/Database/ItemStackSerializer/ItemStackSerializer.java deleted file mode 100644 index 86c321e..0000000 --- a/src/at/pcgamingfreaks/georgh/MinePacks/Database/ItemStackSerializer/ItemStackSerializer.java +++ /dev/null @@ -1,61 +0,0 @@ -package at.pcgamingfreaks.georgh.MinePacks.Database.ItemStackSerializer; - -import org.bukkit.Bukkit; -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.ItemStack; - -public class ItemStackSerializer -{ - Base serializer, base = new Base(); - int usedVersion = 1; - - public ItemStackSerializer() - { - String name = Bukkit.getServer().getClass().getPackage().getName(); - String[] version = name.substring(name.lastIndexOf('.') + 2).split("_"); - try - { - if(version[0].equals("1")) - { - if(version[1].equals("8")) - { - serializer = new MC_1_8(); - } - } - } - catch(Exception e) - { - e.printStackTrace(); - } - if(serializer == null) - { - usedVersion = 0; - serializer = base; - } - } - - public byte[] serialize(Inventory inv) - { - return serializer.toByteArray(inv); - } - - @SuppressWarnings("unused") - public ItemStack[] deserialize(byte[] data) - { - return deserialize(data, usedVersion); - } - - public ItemStack[] deserialize(byte[] data, int version) - { - switch(version) - { - case 0: return base.toItemStack(data); - default: return serializer.toItemStack(data); - } - } - - public int getUsedVersion() - { - return usedVersion; - } -} \ No newline at end of file diff --git a/src/at/pcgamingfreaks/georgh/MinePacks/Database/ItemStackSerializer/MC_1_8.java b/src/at/pcgamingfreaks/georgh/MinePacks/Database/ItemStackSerializer/MC_1_8.java deleted file mode 100644 index ac68387..0000000 --- a/src/at/pcgamingfreaks/georgh/MinePacks/Database/ItemStackSerializer/MC_1_8.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright (C) 2014-2015 GeorgH93 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package at.pcgamingfreaks.georgh.MinePacks.Database.ItemStackSerializer; - -import at.pcgamingfreaks.Bukkit.Reflection; -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.ItemStack; - -import java.io.*; -import java.lang.Object; -import java.lang.reflect.Method; - -public class MC_1_8 extends Base -{ - Class NBTTagCompound = Reflection.getNMSClass("NBTTagCompound"), NBTCompressedStreamTools = Reflection.getNMSClass("NBTCompressedStreamTools"); - Class CraftItemStack = Reflection.getOBCClass("inventory.CraftItemStack"), NMSItemStack = Reflection.getNMSClass("ItemStack"); - - Method setInt = Reflection.getMethod(NBTTagCompound, "setInt", String.class, int.class), a = Reflection.getMethod(NBTCompressedStreamTools, "a", NBTTagCompound, OutputStream.class); - Method set = Reflection.getMethod(NBTTagCompound, "set", String.class, Reflection.getNMSClass("NBTBase")), save = Reflection.getMethod(NMSItemStack, "save", NBTTagCompound); - Method asNMSCopy = Reflection.getMethod(CraftItemStack, "asNMSCopy", ItemStack.class), getInt = Reflection.getMethod(NBTTagCompound, "getInt", String.class); - Method hasKeyOfType = Reflection.getMethod(NBTTagCompound, "hasKeyOfType", String.class, int.class), getCompound = Reflection.getMethod(NBTTagCompound, "getCompound", String.class); - Method createStack = Reflection.getMethod(NMSItemStack, "createStack", NBTTagCompound), asBukkitCopy = Reflection.getMethod(CraftItemStack, "asBukkitCopy", NMSItemStack); - Method ain = Reflection.getMethod(NBTCompressedStreamTools, "a", InputStream.class); - - public byte[] toByteArray(Inventory inv) - { - byte[] ba = null; - try - { - Object localNBTTagCompound = NBTTagCompound.newInstance(); - setInt.invoke(localNBTTagCompound, "size", inv.getSize()); - for (int i = 0; i < inv.getSize(); i++) - { - if (inv.getItem(i) != null) - { - set.invoke(localNBTTagCompound, String.valueOf(i), save.invoke(asNMSCopy.invoke(null, inv.getItem(i)), NBTTagCompound.newInstance())); - } - } - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - DataOutputStream w = new DataOutputStream(baos); - a.invoke(null, localNBTTagCompound, w); - w.flush(); - ba = baos.toByteArray(); - w.close(); - } - catch (Exception e) - { - e.printStackTrace(); - } - return ba; - } - - public ItemStack[] toItemStack(byte[] data) - { - try - { - if (data != null) - { - Object localNBTTagCompound = ain.invoke(null, new ByteArrayInputStream(data)); - int i = (int)getInt.invoke(localNBTTagCompound, "size"); - ItemStack[] its = new ItemStack[i]; - for (int k = 0; k < i; k++) - { - if ((boolean)hasKeyOfType.invoke(localNBTTagCompound, String.valueOf(k), 10)) - { - its[k] = (ItemStack)asBukkitCopy.invoke(null, createStack.invoke(null, getCompound.invoke(localNBTTagCompound, String.valueOf(k)))); - } - } - return its; - } - } - catch (Exception e) - { - e.printStackTrace(); - } - return null; - } -} \ No newline at end of file