mirror of
https://github.com/GeorgH93/Minepacks.git
synced 2024-11-15 10:55:20 +01:00
Using the Bukkit/Bungee Plugin Lib
This commit is contained in:
parent
2209b02feb
commit
daa328cd1f
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user