diff --git a/.gitignore b/.gitignore index ea7188e..bb1de4c 100644 --- a/.gitignore +++ b/.gitignore @@ -45,4 +45,5 @@ Temporary Items *.classpath *.project *.prefs -/target/ \ No newline at end of file +/target/ +jar_libs/bukkit.jar \ No newline at end of file diff --git a/pom.xml b/pom.xml index 32b5a52..5fb9509 100644 --- a/pom.xml +++ b/pom.xml @@ -2,8 +2,13 @@ 4.0.0 at.pcgamingfreaks MinePacks - 1.1.3 + 1.2 + + in-project + In Project Repo + file://${project.basedir}/jar_libs + spigot-repo https://hub.spigotmc.org/nexus/content/groups/public/ @@ -66,5 +71,12 @@ LATEST provided + + net.minecraft + Minecraft + 1 + system + ${project.basedir}/jar_libs/bukkit.jar + \ No newline at end of file diff --git a/src/at/pcgamingfreaks/georgh/MinePacks/Backpack.java b/src/at/pcgamingfreaks/georgh/MinePacks/Backpack.java index 704fb35..b3e7fe4 100644 --- a/src/at/pcgamingfreaks/georgh/MinePacks/Backpack.java +++ b/src/at/pcgamingfreaks/georgh/MinePacks/Backpack.java @@ -17,7 +17,6 @@ package at.pcgamingfreaks.georgh.MinePacks; -import java.io.ByteArrayOutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -28,7 +27,6 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; -import org.bukkit.util.io.BukkitObjectOutputStream; public class Backpack { @@ -183,21 +181,4 @@ public String getTitle() { return title; } - - public byte[] getBackpackByteArray() - { - try - { - ByteArrayOutputStream b = new ByteArrayOutputStream(); - BukkitObjectOutputStream output = new BukkitObjectOutputStream(b); - output.writeObject(getBackpack().getContents()); - byte[] ba = b.toByteArray(); - output.close(); - return ba; - } - catch(Exception e) - { - return null; - } - } } \ No newline at end of file diff --git a/src/at/pcgamingfreaks/georgh/MinePacks/Database/Database.java b/src/at/pcgamingfreaks/georgh/MinePacks/Database/Database.java index ec8e206..a034717 100644 --- a/src/at/pcgamingfreaks/georgh/MinePacks/Database/Database.java +++ b/src/at/pcgamingfreaks/georgh/MinePacks/Database/Database.java @@ -24,12 +24,14 @@ import at.pcgamingfreaks.georgh.MinePacks.Backpack; import at.pcgamingfreaks.georgh.MinePacks.MinePacks; +import at.pcgamingfreaks.georgh.MinePacks.Database.Serializer.ItemStackSerializer; public class Database { protected MinePacks plugin; public HashSet backpacks = new HashSet(); + public ItemStackSerializer itsSerializer = new ItemStackSerializer(); public Database(MinePacks mp) { diff --git a/src/at/pcgamingfreaks/georgh/MinePacks/Database/MySQL.java b/src/at/pcgamingfreaks/georgh/MinePacks/Database/MySQL.java index 7d7b28f..b760651 100644 --- a/src/at/pcgamingfreaks/georgh/MinePacks/Database/MySQL.java +++ b/src/at/pcgamingfreaks/georgh/MinePacks/Database/MySQL.java @@ -17,7 +17,6 @@ package at.pcgamingfreaks.georgh.MinePacks.Database; -import java.io.ByteArrayInputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; @@ -30,7 +29,6 @@ import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -import org.bukkit.util.io.BukkitObjectInputStream; import at.pcgamingfreaks.georgh.MinePacks.Backpack; import at.pcgamingfreaks.georgh.MinePacks.MinePacks; @@ -125,6 +123,17 @@ private void CheckDB() } } stmt.execute("CREATE TABLE IF NOT EXISTS `" + Table_Backpacks + "` (`owner` INT UNSIGNED NOT NULL, `itemstacks` BLOB, PRIMARY KEY (`owner`));"); + try + { + stmt.execute("ALTER TABLE `" + Table_Backpacks + "` ADD COLUMN `version` INT DEFAULT 0;"); + } + catch(SQLException e) + { + if(e.getErrorCode() != 1060) + { + e.printStackTrace(); + } + } stmt.close(); } catch (SQLException e) @@ -224,15 +233,23 @@ public void SaveBackpack(Backpack backpack) } rs.close(); ps.close(); - ps = GetConnection().prepareStatement("INSERT INTO `" + Table_Backpacks + "` (`owner`, `itemstacks`) VALUES (?,?);"); + ps = GetConnection().prepareStatement("INSERT INTO `" + Table_Backpacks + "` (`owner`, `itemstacks`, `version`) VALUES (?,?,?);", Statement.RETURN_GENERATED_KEYS); ps.setInt(1, backpack.getID()); - ps.setBinaryStream(2, new ByteArrayInputStream(backpack.getBackpackByteArray())); + ps.setBytes(2, itsSerializer.Serialize(backpack.getBackpack())); + ps.setInt(3, itsSerializer.getUsedVersion()); + ps.executeUpdate(); + rs = ps.getGeneratedKeys(); + backpack.setID(rs.getInt(1)); + ps.close(); + rs.close(); + return; } else { - ps = GetConnection().prepareStatement("UPDATE `" + Table_Backpacks + "` SET `itemstacks`=? WHERE `owner`=?"); - ps.setBinaryStream(1, new ByteArrayInputStream(backpack.getBackpackByteArray())); - ps.setInt(2, backpack.getID()); + ps = GetConnection().prepareStatement("UPDATE `" + Table_Backpacks + "` SET `itemstacks`=?,`version`=? WHERE `owner`=?"); + ps.setBytes(1, itsSerializer.Serialize(backpack.getBackpack())); + ps.setInt(2, itsSerializer.getUsedVersion()); + ps.setInt(3, backpack.getID()); } ps.execute(); ps.close(); @@ -248,7 +265,7 @@ public Backpack LoadBackpack(OfflinePlayer player) try { PreparedStatement ps = null; // Statement Variable - ps = GetConnection().prepareStatement("SELECT `owner`,`itemstacks` FROM `" + Table_Backpacks + "` INNER JOIN `" + Table_Players + "` ON `owner`=`player_id` WHERE " + ((plugin.UseUUIDs) ? "`uuid`" : "`name`")+ "=?;"); + ps = GetConnection().prepareStatement("SELECT `owner`,`itemstacks`,`version` FROM `" + Table_Backpacks + "` INNER JOIN `" + Table_Players + "` ON `owner`=`player_id` WHERE " + ((plugin.UseUUIDs) ? "`uuid`" : "`name`")+ "=?;"); if(plugin.UseUUIDs) { ps.setString(1, player.getUniqueId().toString().replace("-", "")); @@ -263,9 +280,7 @@ public Backpack LoadBackpack(OfflinePlayer player) return null; } int bpid = rs.getInt(1); - BukkitObjectInputStream bois = new BukkitObjectInputStream(rs.getBinaryStream(2)); - ItemStack[] its = (ItemStack[]) bois.readObject(); - bois.close(); + ItemStack[] its = itsSerializer.Deserialize(rs.getBytes(2), rs.getInt(3)); rs.close(); ps.close(); return new Backpack(player, its, bpid); diff --git a/src/at/pcgamingfreaks/georgh/MinePacks/Database/SQLite.java b/src/at/pcgamingfreaks/georgh/MinePacks/Database/SQLite.java index d6746c2..df5cbd8 100644 --- a/src/at/pcgamingfreaks/georgh/MinePacks/Database/SQLite.java +++ b/src/at/pcgamingfreaks/georgh/MinePacks/Database/SQLite.java @@ -17,7 +17,6 @@ package at.pcgamingfreaks.georgh.MinePacks.Database; -import java.io.ByteArrayInputStream; import java.io.File; import java.sql.Connection; import java.sql.DriverManager; @@ -31,7 +30,6 @@ import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -import org.bukkit.util.io.BukkitObjectInputStream; import at.pcgamingfreaks.georgh.MinePacks.Backpack; import at.pcgamingfreaks.georgh.MinePacks.MinePacks; @@ -101,7 +99,6 @@ private Connection GetConnection() { e.printStackTrace(); } - } } catch (SQLException e) @@ -132,6 +129,17 @@ private void CheckDB() } } stmt.execute("CREATE TABLE IF NOT EXISTS `" + Table_Backpacks + "` (`owner` INT UNSIGNED PRIMARY KEY, `itemstacks` BLOB);"); + try + { + stmt.execute("ALTER TABLE `" + Table_Backpacks + "` ADD COLUMN `version` INT DEFAULT 0;"); + } + catch(SQLException e) + { + if(e.getErrorCode() != 1060) + { + e.printStackTrace(); + } + } stmt.close(); } catch (SQLException e) @@ -218,15 +226,23 @@ public void SaveBackpack(Backpack backpack) } rs.close(); ps.close(); - ps = GetConnection().prepareStatement("INSERT INTO `" + Table_Backpacks + "` (`owner`, `itemstacks`) VALUES (?,?);"); + ps = GetConnection().prepareStatement("INSERT INTO `" + Table_Backpacks + "` (`owner`, `itemstacks`, `version`) VALUES (?,?,?);", Statement.RETURN_GENERATED_KEYS); ps.setInt(1, backpack.getID()); - ps.setBytes(2, backpack.getBackpackByteArray()); + ps.setBytes(2, itsSerializer.Serialize(backpack.getBackpack())); + ps.setInt(3, itsSerializer.getUsedVersion()); + ps.executeUpdate(); + rs = ps.getGeneratedKeys(); + backpack.setID(rs.getInt(1)); + ps.close(); + rs.close(); + return; } else { - ps = GetConnection().prepareStatement("UPDATE `" + Table_Backpacks + "` SET `itemstacks`=? WHERE `owner`=?"); - ps.setBytes(1, backpack.getBackpackByteArray()); - ps.setInt(2, backpack.getID()); + ps = GetConnection().prepareStatement("UPDATE `" + Table_Backpacks + "` SET `itemstacks`=?,`version`=? WHERE `owner`=?"); + ps.setBytes(1, itsSerializer.Serialize(backpack.getBackpack())); + ps.setInt(2, itsSerializer.getUsedVersion()); + ps.setInt(3, backpack.getID()); } ps.execute(); ps.close(); @@ -242,7 +258,7 @@ public Backpack LoadBackpack(OfflinePlayer player) try { PreparedStatement ps = null; // Statement Variable - ps = GetConnection().prepareStatement("SELECT `owner`,`itemstacks` FROM `" + Table_Backpacks + "` INNER JOIN `" + Table_Players + "` ON `owner`=`player_id` WHERE " + ((plugin.UseUUIDs) ? "`uuid`" : "`name`")+ "=?;"); + ps = GetConnection().prepareStatement("SELECT `owner`,`itemstacks`,`version` FROM `" + Table_Backpacks + "` INNER JOIN `" + Table_Players + "` ON `owner`=`player_id` WHERE " + ((plugin.UseUUIDs) ? "`uuid`" : "`name`")+ "=?;"); if(plugin.UseUUIDs) { ps.setString(1, player.getUniqueId().toString().replace("-", "")); @@ -257,9 +273,7 @@ public Backpack LoadBackpack(OfflinePlayer player) return null; } int bpid = rs.getInt(1); - BukkitObjectInputStream bois = new BukkitObjectInputStream(new ByteArrayInputStream(rs.getBytes(2))); - ItemStack[] its = (ItemStack[]) bois.readObject(); - bois.close(); + ItemStack[] its = itsSerializer.Deserialize(rs.getBytes(2), rs.getInt(3)); rs.close(); ps.close(); return new Backpack(player, its, bpid); diff --git a/src/at/pcgamingfreaks/georgh/MinePacks/Database/Serializer/Base.java b/src/at/pcgamingfreaks/georgh/MinePacks/Database/Serializer/Base.java new file mode 100644 index 0000000..3c58ad5 --- /dev/null +++ b/src/at/pcgamingfreaks/georgh/MinePacks/Database/Serializer/Base.java @@ -0,0 +1,62 @@ +/* + * 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.Serializer; + +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((Object)inv.getContents()); + output.close(); + ba = b.toByteArray(); + output.close(); + return ba; + } + catch(Exception e) {} + return ba; + } + + public ItemStack[] toItemStack(byte[] data) + { + try + { + BukkitObjectInputStream bois = new BukkitObjectInputStream(new ByteArrayInputStream(data)); + ItemStack[] its = (ItemStack[]) bois.readObject(); + bois.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/Serializer/ItemStackSerializer.java b/src/at/pcgamingfreaks/georgh/MinePacks/Database/Serializer/ItemStackSerializer.java new file mode 100644 index 0000000..cd76625 --- /dev/null +++ b/src/at/pcgamingfreaks/georgh/MinePacks/Database/Serializer/ItemStackSerializer.java @@ -0,0 +1,64 @@ +package at.pcgamingfreaks.georgh.MinePacks.Database.Serializer; + +import org.bukkit.Bukkit; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; + +public class ItemStackSerializer +{ + Base serializer; + 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")) + { + if(version[2].equals("R1")) + { + serializer = new MC_1_8_R1(); + } + else if(version[2].equals("R2")) + { + serializer = new MC_1_8_R2(); + } + } + } + } + catch(Exception e){} + if(serializer == null) + { + usedVersion = 0; + serializer = new Base(); + } + } + + public byte[] Serialize(Inventory inv) + { + return serializer.toByteArray(inv); + } + + public ItemStack[] Deserialize(byte[] data) + { + return serializer.toItemStack(data); + } + + public ItemStack[] Deserialize(byte[] data, int version) + { + if(version == 0) + { + (new Base()).toItemStack(data); + } + return serializer.toItemStack(data); + } + + public int getUsedVersion() + { + return usedVersion; + } +} \ No newline at end of file diff --git a/src/at/pcgamingfreaks/georgh/MinePacks/Database/Serializer/MC_1_8_R1.java b/src/at/pcgamingfreaks/georgh/MinePacks/Database/Serializer/MC_1_8_R1.java new file mode 100644 index 0000000..d0924e1 --- /dev/null +++ b/src/at/pcgamingfreaks/georgh/MinePacks/Database/Serializer/MC_1_8_R1.java @@ -0,0 +1,87 @@ +/* + * 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.Serializer; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.OutputStream; + +import net.minecraft.server.v1_8_R1.NBTCompressedStreamTools; +import net.minecraft.server.v1_8_R1.NBTTagCompound; + +import org.bukkit.craftbukkit.v1_8_R1.inventory.CraftItemStack; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; + +public class MC_1_8_R1 extends Base +{ + public byte[] toByteArray(Inventory inv) + { + byte[] ba = null; + NBTTagCompound localNBTTagCompound = new NBTTagCompound(); + localNBTTagCompound.setInt("size", inv.getSize()); + for (int i = 0; i < inv.getSize(); i++) + { + if (inv.getItem(i) != null) + { + localNBTTagCompound.set(String.valueOf(i), CraftItemStack.asNMSCopy(inv.getItem(i)).save(new NBTTagCompound())); + } + } + try + { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DataOutputStream w = new DataOutputStream(baos); + NBTCompressedStreamTools.a(localNBTTagCompound, (OutputStream)w); + w.flush(); + ba = baos.toByteArray(); + w.close(); + } + catch (Exception e) + { + e.printStackTrace(); + } + return ba; + } + + public ItemStack[] toItemStack(byte[] data) + { + try + { + if (data != null) + { + NBTTagCompound localNBTTagCompound = NBTCompressedStreamTools.a(new ByteArrayInputStream(data)); + int i = localNBTTagCompound.getInt("size"); + ItemStack[] its = new ItemStack[i]; + for (int k = 0; k < i; k++) + { + if (localNBTTagCompound.hasKeyOfType(String.valueOf(k), 10)) + { + its[k] = CraftItemStack.asBukkitCopy(net.minecraft.server.v1_8_R1.ItemStack.createStack(localNBTTagCompound.getCompound(String.valueOf(k)))); + } + } + return its; + } + } + catch (Exception e) + { + e.printStackTrace(); + } + return null; + } +} \ No newline at end of file diff --git a/src/at/pcgamingfreaks/georgh/MinePacks/Database/Serializer/MC_1_8_R2.java b/src/at/pcgamingfreaks/georgh/MinePacks/Database/Serializer/MC_1_8_R2.java new file mode 100644 index 0000000..b7b9e8a --- /dev/null +++ b/src/at/pcgamingfreaks/georgh/MinePacks/Database/Serializer/MC_1_8_R2.java @@ -0,0 +1,87 @@ +/* + * 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.Serializer; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.OutputStream; + +import net.minecraft.server.v1_8_R2.NBTCompressedStreamTools; +import net.minecraft.server.v1_8_R2.NBTTagCompound; + +import org.bukkit.craftbukkit.v1_8_R2.inventory.CraftItemStack; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; + +public class MC_1_8_R2 extends Base +{ + public byte[] toByteArray(Inventory inv) + { + byte[] ba = null; + NBTTagCompound localNBTTagCompound = new NBTTagCompound(); + localNBTTagCompound.setInt("size", inv.getSize()); + for (int i = 0; i < inv.getSize(); i++) + { + if (inv.getItem(i) != null) + { + localNBTTagCompound.set(String.valueOf(i), CraftItemStack.asNMSCopy(inv.getItem(i)).save(new NBTTagCompound())); + } + } + try + { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DataOutputStream w = new DataOutputStream(baos); + NBTCompressedStreamTools.a(localNBTTagCompound, (OutputStream)w); + w.flush(); + ba = baos.toByteArray(); + w.close(); + } + catch (Exception e) + { + e.printStackTrace(); + } + return ba; + } + + public ItemStack[] toItemStack(byte[] data) + { + try + { + if (data != null) + { + NBTTagCompound localNBTTagCompound = NBTCompressedStreamTools.a(new ByteArrayInputStream(data)); + int i = localNBTTagCompound.getInt("size"); + ItemStack[] its = new ItemStack[i]; + for (int k = 0; k < i; k++) + { + if (localNBTTagCompound.hasKeyOfType(String.valueOf(k), 10)) + { + its[k] = CraftItemStack.asBukkitCopy(net.minecraft.server.v1_8_R2.ItemStack.createStack(localNBTTagCompound.getCompound(String.valueOf(k)))); + } + } + return its; + } + } + catch (Exception e) + { + e.printStackTrace(); + } + return null; + } +} \ No newline at end of file