optimized sql inv serialising

This commit is contained in:
Georg 2015-01-12 21:07:11 +01:00
parent 1752fcc5be
commit d80b58079a
4 changed files with 24 additions and 19 deletions

View File

@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>at.pcgamingfreaks</groupId>
<artifactId>MinePacks</artifactId>
<version>1.1</version>
<version>1.1.1</version>
<repositories>
<repository>
<id>bukkit-repo</id>

View File

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

View File

@ -18,7 +18,6 @@
package at.pcgamingfreaks.georgh.MinePacks.Database;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
@ -32,7 +31,6 @@
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.io.BukkitObjectInputStream;
import org.bukkit.util.io.BukkitObjectOutputStream;
import at.pcgamingfreaks.georgh.MinePacks.Backpack;
import at.pcgamingfreaks.georgh.MinePacks.MinePacks;
@ -201,10 +199,6 @@ public void SaveBackpack(Backpack backpack)
{
try
{
// Serialising the backpack
ByteArrayOutputStream b = new ByteArrayOutputStream();
BukkitObjectOutputStream output = new BukkitObjectOutputStream(b);
output.writeObject(backpack.getBackpack().getContents());
PreparedStatement ps = null; // Statement Variable
// Building the mysql statement
if(backpack.getID() <= 0)
@ -232,15 +226,14 @@ public void SaveBackpack(Backpack backpack)
ps.close();
ps = GetConnection().prepareStatement("INSERT INTO `" + Table_Backpacks + "` (`owner`, `itemstacks`) VALUES (?,?);");
ps.setInt(1, backpack.getID());
ps.setBinaryStream(2, new ByteArrayInputStream(b.toByteArray()));
ps.setBinaryStream(2, new ByteArrayInputStream(backpack.getBackpackByteArray()));
}
else
{
ps = GetConnection().prepareStatement("UPDATE `" + Table_Backpacks + "` SET `itemstacks`=? WHERE `owner`=?");
ps.setBinaryStream(1, new ByteArrayInputStream(b.toByteArray()));
ps.setBinaryStream(1, new ByteArrayInputStream(backpack.getBackpackByteArray()));
ps.setInt(2, backpack.getID());
}
output.close();
ps.execute();
ps.close();
}

View File

@ -18,7 +18,6 @@
package at.pcgamingfreaks.georgh.MinePacks.Database;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
@ -33,7 +32,6 @@
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.io.BukkitObjectInputStream;
import org.bukkit.util.io.BukkitObjectOutputStream;
import at.pcgamingfreaks.georgh.MinePacks.Backpack;
import at.pcgamingfreaks.georgh.MinePacks.MinePacks;
@ -195,10 +193,6 @@ public void SaveBackpack(Backpack backpack)
{
try
{
// Serialising the backpack
ByteArrayOutputStream b = new ByteArrayOutputStream();
BukkitObjectOutputStream output = new BukkitObjectOutputStream(b);
output.writeObject(backpack.getBackpack().getContents());
PreparedStatement ps = null; // Statement Variable
// Building the mysql statement
if(backpack.getID() <= 0)
@ -226,15 +220,14 @@ public void SaveBackpack(Backpack backpack)
ps.close();
ps = GetConnection().prepareStatement("INSERT INTO `" + Table_Backpacks + "` (`owner`, `itemstacks`) VALUES (?,?);");
ps.setInt(1, backpack.getID());
ps.setBytes(2, b.toByteArray());
ps.setBytes(2, backpack.getBackpackByteArray());
}
else
{
ps = GetConnection().prepareStatement("UPDATE `" + Table_Backpacks + "` SET `itemstacks`=? WHERE `owner`=?");
ps.setBytes(1, b.toByteArray());
ps.setBytes(1, backpack.getBackpackByteArray());
ps.setInt(2, backpack.getID());
}
output.close();
ps.execute();
ps.close();
}