Update to V1.2

MC 1.8 support
This commit is contained in:
Georg 2015-03-13 17:22:36 +01:00
parent c09b291f65
commit 50a2936b8d
10 changed files with 369 additions and 44 deletions

1
.gitignore vendored
View File

@ -46,3 +46,4 @@ Temporary Items
*.project *.project
*.prefs *.prefs
/target/ /target/
jar_libs/bukkit.jar

14
pom.xml
View File

@ -2,8 +2,13 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>at.pcgamingfreaks</groupId> <groupId>at.pcgamingfreaks</groupId>
<artifactId>MinePacks</artifactId> <artifactId>MinePacks</artifactId>
<version>1.1.3</version> <version>1.2</version>
<repositories> <repositories>
<repository>
<id>in-project</id>
<name>In Project Repo</name>
<url>file://${project.basedir}/jar_libs</url>
</repository>
<repository> <repository>
<id>spigot-repo</id> <id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url> <url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
@ -66,5 +71,12 @@
<version>LATEST</version> <version>LATEST</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>net.minecraft</groupId>
<artifactId>Minecraft</artifactId>
<version>1</version>
<scope>system</scope>
<systemPath>${project.basedir}/jar_libs/bukkit.jar</systemPath>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -17,7 +17,6 @@
package at.pcgamingfreaks.georgh.MinePacks; package at.pcgamingfreaks.georgh.MinePacks;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -28,7 +27,6 @@
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.util.io.BukkitObjectOutputStream;
public class Backpack public class Backpack
{ {
@ -183,21 +181,4 @@ public String getTitle()
{ {
return title; 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;
}
}
} }

View File

@ -24,12 +24,14 @@
import at.pcgamingfreaks.georgh.MinePacks.Backpack; import at.pcgamingfreaks.georgh.MinePacks.Backpack;
import at.pcgamingfreaks.georgh.MinePacks.MinePacks; import at.pcgamingfreaks.georgh.MinePacks.MinePacks;
import at.pcgamingfreaks.georgh.MinePacks.Database.Serializer.ItemStackSerializer;
public class Database public class Database
{ {
protected MinePacks plugin; protected MinePacks plugin;
public HashSet<Backpack> backpacks = new HashSet<Backpack>(); public HashSet<Backpack> backpacks = new HashSet<Backpack>();
public ItemStackSerializer itsSerializer = new ItemStackSerializer();
public Database(MinePacks mp) public Database(MinePacks mp)
{ {

View File

@ -17,7 +17,6 @@
package at.pcgamingfreaks.georgh.MinePacks.Database; package at.pcgamingfreaks.georgh.MinePacks.Database;
import java.io.ByteArrayInputStream;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -30,7 +29,6 @@
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.util.io.BukkitObjectInputStream;
import at.pcgamingfreaks.georgh.MinePacks.Backpack; import at.pcgamingfreaks.georgh.MinePacks.Backpack;
import at.pcgamingfreaks.georgh.MinePacks.MinePacks; 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`));"); 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(); stmt.close();
} }
catch (SQLException e) catch (SQLException e)
@ -224,15 +233,23 @@ public void SaveBackpack(Backpack backpack)
} }
rs.close(); rs.close();
ps.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.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 else
{ {
ps = GetConnection().prepareStatement("UPDATE `" + Table_Backpacks + "` SET `itemstacks`=? WHERE `owner`=?"); ps = GetConnection().prepareStatement("UPDATE `" + Table_Backpacks + "` SET `itemstacks`=?,`version`=? WHERE `owner`=?");
ps.setBinaryStream(1, new ByteArrayInputStream(backpack.getBackpackByteArray())); ps.setBytes(1, itsSerializer.Serialize(backpack.getBackpack()));
ps.setInt(2, backpack.getID()); ps.setInt(2, itsSerializer.getUsedVersion());
ps.setInt(3, backpack.getID());
} }
ps.execute(); ps.execute();
ps.close(); ps.close();
@ -248,7 +265,7 @@ public Backpack LoadBackpack(OfflinePlayer player)
try try
{ {
PreparedStatement ps = null; // Statement Variable 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) if(plugin.UseUUIDs)
{ {
ps.setString(1, player.getUniqueId().toString().replace("-", "")); ps.setString(1, player.getUniqueId().toString().replace("-", ""));
@ -263,9 +280,7 @@ public Backpack LoadBackpack(OfflinePlayer player)
return null; return null;
} }
int bpid = rs.getInt(1); int bpid = rs.getInt(1);
BukkitObjectInputStream bois = new BukkitObjectInputStream(rs.getBinaryStream(2)); ItemStack[] its = itsSerializer.Deserialize(rs.getBytes(2), rs.getInt(3));
ItemStack[] its = (ItemStack[]) bois.readObject();
bois.close();
rs.close(); rs.close();
ps.close(); ps.close();
return new Backpack(player, its, bpid); return new Backpack(player, its, bpid);

View File

@ -17,7 +17,6 @@
package at.pcgamingfreaks.georgh.MinePacks.Database; package at.pcgamingfreaks.georgh.MinePacks.Database;
import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
@ -31,7 +30,6 @@
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.util.io.BukkitObjectInputStream;
import at.pcgamingfreaks.georgh.MinePacks.Backpack; import at.pcgamingfreaks.georgh.MinePacks.Backpack;
import at.pcgamingfreaks.georgh.MinePacks.MinePacks; import at.pcgamingfreaks.georgh.MinePacks.MinePacks;
@ -101,7 +99,6 @@ private Connection GetConnection()
{ {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
catch (SQLException e) 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);"); 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(); stmt.close();
} }
catch (SQLException e) catch (SQLException e)
@ -218,15 +226,23 @@ public void SaveBackpack(Backpack backpack)
} }
rs.close(); rs.close();
ps.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.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 else
{ {
ps = GetConnection().prepareStatement("UPDATE `" + Table_Backpacks + "` SET `itemstacks`=? WHERE `owner`=?"); ps = GetConnection().prepareStatement("UPDATE `" + Table_Backpacks + "` SET `itemstacks`=?,`version`=? WHERE `owner`=?");
ps.setBytes(1, backpack.getBackpackByteArray()); ps.setBytes(1, itsSerializer.Serialize(backpack.getBackpack()));
ps.setInt(2, backpack.getID()); ps.setInt(2, itsSerializer.getUsedVersion());
ps.setInt(3, backpack.getID());
} }
ps.execute(); ps.execute();
ps.close(); ps.close();
@ -242,7 +258,7 @@ public Backpack LoadBackpack(OfflinePlayer player)
try try
{ {
PreparedStatement ps = null; // Statement Variable 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) if(plugin.UseUUIDs)
{ {
ps.setString(1, player.getUniqueId().toString().replace("-", "")); ps.setString(1, player.getUniqueId().toString().replace("-", ""));
@ -257,9 +273,7 @@ public Backpack LoadBackpack(OfflinePlayer player)
return null; return null;
} }
int bpid = rs.getInt(1); int bpid = rs.getInt(1);
BukkitObjectInputStream bois = new BukkitObjectInputStream(new ByteArrayInputStream(rs.getBytes(2))); ItemStack[] its = itsSerializer.Deserialize(rs.getBytes(2), rs.getInt(3));
ItemStack[] its = (ItemStack[]) bois.readObject();
bois.close();
rs.close(); rs.close();
ps.close(); ps.close();
return new Backpack(player, its, bpid); return new Backpack(player, its, bpid);

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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;
}
}

View File

@ -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;
}
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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;
}
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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;
}
}