Shortened SQLite/Database class

The method to get data of the database has changed completely so the
uses also had to be changed. The IDE also did some code reformatting,
but these are just very small changes.
This commit is contained in:
Eric 2016-05-29 15:32:54 +02:00
parent e00f30bf0b
commit 654fa260f4
13 changed files with 173 additions and 439 deletions

View File

@ -10,7 +10,7 @@ import de.epiceric.shopchest.interfaces.jsonbuilder.*;
import de.epiceric.shopchest.interfaces.utils.*; import de.epiceric.shopchest.interfaces.utils.*;
import de.epiceric.shopchest.shop.Shop; import de.epiceric.shopchest.shop.Shop;
import de.epiceric.shopchest.shop.Shop.ShopType; import de.epiceric.shopchest.shop.Shop.ShopType;
import de.epiceric.shopchest.sql.SQLite; import de.epiceric.shopchest.sql.Database;
import de.epiceric.shopchest.utils.Metrics; import de.epiceric.shopchest.utils.Metrics;
import de.epiceric.shopchest.utils.Metrics.Graph; import de.epiceric.shopchest.utils.Metrics.Graph;
import de.epiceric.shopchest.utils.Metrics.Plotter; import de.epiceric.shopchest.utils.Metrics.Plotter;
@ -25,7 +25,7 @@ import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.io.*; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -36,7 +36,7 @@ public class ShopChest extends JavaPlugin {
public static Permission perm = null; public static Permission perm = null;
public static LWC lwc = null; public static LWC lwc = null;
public static boolean lockette = false; public static boolean lockette = false;
public static SQLite sqlite; public static Database database;
public static boolean isUpdateNeeded = false; public static boolean isUpdateNeeded = false;
public static String latestVersion = ""; public static String latestVersion = "";
public static String downloadLink = ""; public static String downloadLink = "";
@ -121,8 +121,7 @@ public class ShopChest extends JavaPlugin {
reloadConfig(); reloadConfig();
saveDefaultConfig(); saveDefaultConfig();
sqlite = new SQLite(this); database = new Database(this);
sqlite.load();
switch (Utils.getVersion(getServer())) { switch (Utils.getVersion(getServer())) {
@ -257,10 +256,10 @@ public class ShopChest extends JavaPlugin {
private void initializeShops() { private void initializeShops() {
int count = 0; int count = 0;
for (int id = 1; id < sqlite.getHighestID() + 1; id++) { for (int id = 1; id < database.getHighestID() + 1; id++) {
try { try {
Shop shop = sqlite.getShop(id); Shop shop = (Shop) database.get(id, Database.ShopInfo.SHOP);
shop.createHologram(); shop.createHologram();
shop.createItem(); shop.createItem();
ShopUtils.addShop(shop); ShopUtils.addShop(shop);

View File

@ -6,7 +6,7 @@ import de.epiceric.shopchest.config.Config;
import de.epiceric.shopchest.interfaces.Utils; import de.epiceric.shopchest.interfaces.Utils;
import de.epiceric.shopchest.shop.Shop; import de.epiceric.shopchest.shop.Shop;
import de.epiceric.shopchest.shop.Shop.ShopType; import de.epiceric.shopchest.shop.Shop.ShopType;
import de.epiceric.shopchest.sql.SQLite; import de.epiceric.shopchest.sql.Database;
import de.epiceric.shopchest.utils.*; import de.epiceric.shopchest.utils.*;
import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse; import net.milkbowl.vault.economy.EconomyResponse;
@ -36,7 +36,7 @@ public class InteractShop implements Listener {
private ShopChest plugin; private ShopChest plugin;
private Permission perm = ShopChest.perm; private Permission perm = ShopChest.perm;
private Economy econ = ShopChest.econ; private Economy econ = ShopChest.econ;
private SQLite sqlite = ShopChest.sqlite; private Database database = ShopChest.database;
public InteractShop(ShopChest plugin) { public InteractShop(ShopChest plugin) {
this.plugin = plugin; this.plugin = plugin;
@ -233,7 +233,7 @@ public class InteractShop implements Listener {
shop.createHologram(); shop.createHologram();
shop.createItem(); shop.createItem();
sqlite.addShop(shop); database.addShop(shop);
ShopUtils.addShop(shop); ShopUtils.addShop(shop);
executor.sendMessage(Config.shop_created()); executor.sendMessage(Config.shop_created());
@ -248,7 +248,7 @@ public class InteractShop implements Listener {
ShopUtils.removeShop(shop); ShopUtils.removeShop(shop);
sqlite.removeShop(shop); database.removeShop(shop);
shop.removeHologram(); shop.removeHologram();

View File

@ -77,11 +77,11 @@ public class ProtectChest implements Listener {
if (b.getLocation().equals(r.getLocation())) { if (b.getLocation().equals(r.getLocation())) {
shop = ShopUtils.getShop(l.getLocation()); shop = ShopUtils.getShop(l.getLocation());
ShopUtils.removeShop(shop); ShopUtils.removeShop(shop);
ShopChest.sqlite.removeShop(shop); ShopChest.database.removeShop(shop);
} else if (b.getLocation().equals(l.getLocation())) { } else if (b.getLocation().equals(l.getLocation())) {
shop = ShopUtils.getShop(r.getLocation()); shop = ShopUtils.getShop(r.getLocation());
ShopUtils.removeShop(shop); ShopUtils.removeShop(shop);
ShopChest.sqlite.removeShop(shop); ShopChest.database.removeShop(shop);
} else { } else {
return; return;
} }
@ -92,7 +92,7 @@ public class ProtectChest implements Listener {
newShop.createHologram(); newShop.createHologram();
newShop.createItem(); newShop.createItem();
ShopUtils.addShop(newShop); ShopUtils.addShop(newShop);
ShopChest.sqlite.addShop(newShop); ShopChest.database.addShop(newShop);
} }

View File

@ -5,6 +5,7 @@ import de.epiceric.shopchest.config.Config;
import de.epiceric.shopchest.interfaces.Hologram; import de.epiceric.shopchest.interfaces.Hologram;
import de.epiceric.shopchest.interfaces.Utils; import de.epiceric.shopchest.interfaces.Utils;
import de.epiceric.shopchest.shop.Shop; import de.epiceric.shopchest.shop.Shop;
import de.epiceric.shopchest.sql.Database;
import de.epiceric.shopchest.utils.ShopUtils; import de.epiceric.shopchest.utils.ShopUtils;
import net.minecraft.server.v1_8_R1.EntityArmorStand; import net.minecraft.server.v1_8_R1.EntityArmorStand;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -36,10 +37,10 @@ public class Utils_1_8_R1 extends Utils {
int count = 0; int count = 0;
for (int id = 1; id < ShopChest.sqlite.getHighestID() + 1; id++) { for (int id = 1; id < ShopChest.database.getHighestID() + 1; id++) {
try { try {
Shop shop = ShopChest.sqlite.getShop(id); Shop shop = (Shop) ShopChest.database.get(id, Database.ShopInfo.SHOP);
shop.createHologram(); shop.createHologram();
shop.createItem(); shop.createItem();
ShopUtils.addShop(shop); ShopUtils.addShop(shop);

View File

@ -5,6 +5,7 @@ import de.epiceric.shopchest.config.Config;
import de.epiceric.shopchest.interfaces.Hologram; import de.epiceric.shopchest.interfaces.Hologram;
import de.epiceric.shopchest.interfaces.Utils; import de.epiceric.shopchest.interfaces.Utils;
import de.epiceric.shopchest.shop.Shop; import de.epiceric.shopchest.shop.Shop;
import de.epiceric.shopchest.sql.Database;
import de.epiceric.shopchest.utils.ShopUtils; import de.epiceric.shopchest.utils.ShopUtils;
import net.minecraft.server.v1_8_R2.EntityArmorStand; import net.minecraft.server.v1_8_R2.EntityArmorStand;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -36,10 +37,10 @@ public class Utils_1_8_R2 extends Utils {
int count = 0; int count = 0;
for (int id = 1; id < ShopChest.sqlite.getHighestID() + 1; id++) { for (int id = 1; id < ShopChest.database.getHighestID() + 1; id++) {
try { try {
Shop shop = ShopChest.sqlite.getShop(id); Shop shop = (Shop) ShopChest.database.get(id, Database.ShopInfo.SHOP);
shop.createHologram(); shop.createHologram();
shop.createItem(); shop.createItem();
ShopUtils.addShop(shop); ShopUtils.addShop(shop);

View File

@ -5,6 +5,7 @@ import de.epiceric.shopchest.config.Config;
import de.epiceric.shopchest.interfaces.Hologram; import de.epiceric.shopchest.interfaces.Hologram;
import de.epiceric.shopchest.interfaces.Utils; import de.epiceric.shopchest.interfaces.Utils;
import de.epiceric.shopchest.shop.Shop; import de.epiceric.shopchest.shop.Shop;
import de.epiceric.shopchest.sql.Database;
import de.epiceric.shopchest.utils.ShopUtils; import de.epiceric.shopchest.utils.ShopUtils;
import net.minecraft.server.v1_8_R3.EntityArmorStand; import net.minecraft.server.v1_8_R3.EntityArmorStand;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -36,10 +37,10 @@ public class Utils_1_8_R3 extends Utils {
int count = 0; int count = 0;
for (int id = 1; id < ShopChest.sqlite.getHighestID() + 1; id++) { for (int id = 1; id < ShopChest.database.getHighestID() + 1; id++) {
try { try {
Shop shop = ShopChest.sqlite.getShop(id); Shop shop = (Shop) ShopChest.database.get(id, Database.ShopInfo.SHOP);
shop.createHologram(); shop.createHologram();
shop.createItem(); shop.createItem();
ShopUtils.addShop(shop); ShopUtils.addShop(shop);

View File

@ -5,6 +5,7 @@ import de.epiceric.shopchest.config.Config;
import de.epiceric.shopchest.interfaces.Hologram; import de.epiceric.shopchest.interfaces.Hologram;
import de.epiceric.shopchest.interfaces.Utils; import de.epiceric.shopchest.interfaces.Utils;
import de.epiceric.shopchest.shop.Shop; import de.epiceric.shopchest.shop.Shop;
import de.epiceric.shopchest.sql.Database;
import de.epiceric.shopchest.utils.ShopUtils; import de.epiceric.shopchest.utils.ShopUtils;
import net.minecraft.server.v1_9_R1.EntityArmorStand; import net.minecraft.server.v1_9_R1.EntityArmorStand;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -36,10 +37,10 @@ public class Utils_1_9_R1 extends Utils {
int count = 0; int count = 0;
for (int id = 1; id < ShopChest.sqlite.getHighestID() + 1; id++) { for (int id = 1; id < ShopChest.database.getHighestID() + 1; id++) {
try { try {
Shop shop = ShopChest.sqlite.getShop(id); Shop shop = (Shop) ShopChest.database.get(id, Database.ShopInfo.SHOP);
shop.createHologram(); shop.createHologram();
shop.createItem(); shop.createItem();
ShopUtils.addShop(shop); ShopUtils.addShop(shop);

View File

@ -5,6 +5,7 @@ import de.epiceric.shopchest.config.Config;
import de.epiceric.shopchest.interfaces.Hologram; import de.epiceric.shopchest.interfaces.Hologram;
import de.epiceric.shopchest.interfaces.Utils; import de.epiceric.shopchest.interfaces.Utils;
import de.epiceric.shopchest.shop.Shop; import de.epiceric.shopchest.shop.Shop;
import de.epiceric.shopchest.sql.Database;
import de.epiceric.shopchest.utils.ShopUtils; import de.epiceric.shopchest.utils.ShopUtils;
import net.minecraft.server.v1_9_R2.EntityArmorStand; import net.minecraft.server.v1_9_R2.EntityArmorStand;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -36,10 +37,10 @@ public class Utils_1_9_R2 extends Utils {
int count = 0; int count = 0;
for (int id = 1; id < ShopChest.sqlite.getHighestID() + 1; id++) { for (int id = 1; id < ShopChest.database.getHighestID() + 1; id++) {
try { try {
Shop shop = ShopChest.sqlite.getShop(id); Shop shop = (Shop) ShopChest.database.get(id, Database.ShopInfo.SHOP);
shop.createHologram(); shop.createHologram();
shop.createItem(); shop.createItem();
ShopUtils.addShop(shop); ShopUtils.addShop(shop);

View File

@ -34,6 +34,7 @@ public class Shop {
private double buyPrice; private double buyPrice;
private double sellPrice; private double sellPrice;
private ShopType shopType; private ShopType shopType;
public Shop(ShopChest plugin, OfflinePlayer vendor, ItemStack product, Location location, double buyPrice, double sellPrice, ShopType shopType) { public Shop(ShopChest plugin, OfflinePlayer vendor, ItemStack product, Location location, double buyPrice, double sellPrice, ShopType shopType) {
this.plugin = plugin; this.plugin = plugin;
this.vendor = vendor; this.vendor = vendor;

View File

@ -11,13 +11,52 @@ import org.bukkit.OfflinePlayer;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.io.File;
import java.io.IOException;
import java.sql.*; import java.sql.*;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Level;
public abstract class Database { public class Database {
public String SQLiteCreateTokensTable = "CREATE TABLE IF NOT EXISTS shop_list (" + private ShopChest plugin;
private Connection connection;
public Database(ShopChest instance) {
plugin = instance;
initialize();
}
private Connection getSQLConnection() {
File dbFile = new File(plugin.getDataFolder(), "shops.db");
if (!dbFile.exists()) {
try {
dbFile.createNewFile();
} catch (IOException ex) {
ex.printStackTrace();
}
}
try {
if (connection != null && !connection.isClosed()) {
return connection;
}
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:" + dbFile);
return connection;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
private void initialize() {
connection = getSQLConnection();
try {
String queryCreateTable = "CREATE TABLE IF NOT EXISTS shop_list (" +
"`id` int(11) NOT NULL," + "`id` int(11) NOT NULL," +
"`vendor` varchar(32) NOT NULL," + "`vendor` varchar(32) NOT NULL," +
"`product` varchar(32) NOT NULL," + "`product` varchar(32) NOT NULL," +
@ -30,41 +69,23 @@ public abstract class Database {
"`shoptype` varchar(32) NOT NULL," + "`shoptype` varchar(32) NOT NULL," +
"PRIMARY KEY (`id`)" + "PRIMARY KEY (`id`)" +
");"; ");";
// The name of the table we created back in SQLite class.
public String table = "shop_list";
public String world = "";
public String vendor = "";
public ItemStack product = null;
public Location location = null;
public double buyPrice = 0;
public double sellPrice = 0;
public ShopType shopType = ShopType.NORMAL;
ShopChest plugin;
Connection connection;
public Database(ShopChest instance) { Statement s = connection.createStatement();
plugin = instance; s.executeUpdate(queryCreateTable);
} s.close();
public abstract Connection getSQLConnection(); PreparedStatement ps = connection.prepareStatement("SELECT * FROM shop_list WHERE id = ?");
public abstract void load();
public void initialize() {
connection = getSQLConnection();
try {
PreparedStatement ps = connection.prepareStatement("SELECT * FROM " + table + " WHERE id = ?");
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery();
close(ps, rs); close(ps, rs);
} catch (SQLException ex) { } catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, "Unable to retreive connection", ex); ex.printStackTrace();
} }
} }
public int getNextFreeID() { public int getNextFreeID() {
for (int i = 1; i < getHighestID() + 1; i++) { for (int i = 1; i < getHighestID() + 1; i++) {
if (getProduct(i) == null) { if (get(i, ShopInfo.X) == null) {
return i; return i;
} else { } else {
if (i == getHighestID()) { if (i == getHighestID()) {
@ -77,383 +98,142 @@ public abstract class Database {
} }
public int getHighestID() { public int getHighestID() {
Connection conn = null;
PreparedStatement ps = null; PreparedStatement ps = null;
ResultSet rs = null; ResultSet rs = null;
int highestID = 1; int highestID = 1;
try {
conn = getSQLConnection();
ps = conn.prepareStatement("SELECT * FROM " + table + ";");
try {
ps = connection.prepareStatement("SELECT * FROM shop_list;");
rs = ps.executeQuery(); rs = ps.executeQuery();
while (rs.next()) { while (rs.next()) {
if (rs.getInt("id") > highestID) { if (rs.getInt("id") > highestID) {
highestID = rs.getInt("id"); highestID = rs.getInt("id");
} }
} }
return highestID; return highestID;
} catch (SQLException ex) { } catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); ex.printStackTrace();
} finally { } finally {
try { close(ps, rs);
if (ps != null)
ps.close();
if (conn != null)
conn.close();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
}
} }
return 0; return 0;
} }
public int getShopID(Shop shop) { public int getShopID(Shop shop) {
for (int i = 1; i < getHighestID() + 1; i++) { for (int i = 1; i < getHighestID() + 1; i++) {
try { try {
Shop s = getShop(i); Shop s = (Shop) get(i, null);
if (s.getLocation().equals(shop.getLocation())) { if (s.getLocation().equals(shop.getLocation())) {
return i; return i;
} }
} catch (NullPointerException ex) { } catch (NullPointerException ex) { /* Empty catch block... */ }
continue;
}
} }
return 0; return 0;
} }
public void removeShop(Shop shop) { public void removeShop(Shop shop) {
int id = getShopID(shop); int id = getShopID(shop);
if (id == 0) return; if (id == 0) return;
if (shop.hasItem()) shop.getItem().remove(); if (shop.hasItem()) shop.getItem().remove();
Connection conn = null;
PreparedStatement ps = null; PreparedStatement ps = null;
try { try {
conn = getSQLConnection(); ps = connection.prepareStatement("DELETE FROM shop_list WHERE id = " + id + ";");
ps = conn.prepareStatement("DELETE FROM " + table + " where id = " + id + ";");
ps.executeUpdate(); ps.executeUpdate();
} catch (SQLException ex) { } catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); ex.printStackTrace();
} finally { } finally {
try { close(ps, null);
if (ps != null)
ps.close();
if (conn != null)
conn.close();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
}
} }
} }
public void removeShop(int id) { public Object get(int id, ShopInfo shopInfo) {
if (id == 0) return;
removeShop(getShop(id));
}
private World getWorld(int id) {
Connection conn = null;
PreparedStatement ps = null; PreparedStatement ps = null;
ResultSet rs = null; ResultSet rs = null;
try {
conn = getSQLConnection();
ps = conn.prepareStatement("SELECT * FROM " + table + " WHERE id = " + id + ";");
try {
ps = connection.prepareStatement("SELECT * FROM shop_list WHERE id = " + id + ";");
rs = ps.executeQuery(); rs = ps.executeQuery();
while (rs.next()) { while (rs.next()) {
if (rs.getInt("id") == id) { if (rs.getInt("id") == id) {
return Bukkit.getWorld(rs.getString("world"));
}
}
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
} finally {
try {
if (ps != null)
ps.close();
if (conn != null)
conn.close();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
}
}
return null;
}
public OfflinePlayer getVendor(int id) { switch (shopInfo) {
Connection conn = null; case SHOP:
PreparedStatement ps = null; Shop shop = ShopUtils.getShop((Location) get(id, ShopInfo.LOCATION));
ResultSet rs = null; if (shop != null)
try { return shop;
conn = getSQLConnection(); else {
ps = conn.prepareStatement("SELECT * FROM " + table + " WHERE id = " + id + ";"); return new Shop(plugin,
(OfflinePlayer) get(id, ShopInfo.VENDOR),
rs = ps.executeQuery(); (ItemStack) get(id, ShopInfo.PRODUCT),
while (rs.next()) { (Location) get(id, ShopInfo.LOCATION),
if (rs.getInt("id") == id) { (double) get(id, ShopInfo.BUYPRICE),
(double) get(id, ShopInfo.SELLPRICE),
(ShopType) get(id, ShopInfo.SHOPTYPE));
}
case VENDOR:
return Bukkit.getOfflinePlayer(UUID.fromString(rs.getString("vendor"))); return Bukkit.getOfflinePlayer(UUID.fromString(rs.getString("vendor")));
} case PRODUCT:
}
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
} finally {
try {
if (ps != null)
ps.close();
if (conn != null)
conn.close();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
}
}
return null;
}
public ItemStack getProduct(int id) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = getSQLConnection();
ps = conn.prepareStatement("SELECT * FROM " + table + " WHERE id = " + id + ";");
rs = ps.executeQuery();
while (rs.next()) {
if (rs.getInt("id") == id) {
return Utils.decode(rs.getString("product")); return Utils.decode(rs.getString("product"));
} case WORLD:
} return Bukkit.getWorld(rs.getString("world"));
} catch (SQLException ex) { case X:
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
} finally {
try {
if (ps != null)
ps.close();
if (conn != null)
conn.close();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
}
}
return null;
}
private int getX(int id) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = getSQLConnection();
ps = conn.prepareStatement("SELECT * FROM " + table + " WHERE id = " + id + ";");
rs = ps.executeQuery();
while (rs.next()) {
if (rs.getInt("id") == id) {
return rs.getInt("x"); return rs.getInt("x");
} case Y:
}
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
} finally {
try {
if (ps != null)
ps.close();
if (conn != null)
conn.close();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
}
}
return 0;
}
private int getY(int id) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = getSQLConnection();
ps = conn.prepareStatement("SELECT * FROM " + table + " WHERE id = " + id + ";");
rs = ps.executeQuery();
while (rs.next()) {
if (rs.getInt("id") == id) {
return rs.getInt("y"); return rs.getInt("y");
} case Z:
}
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
} finally {
try {
if (ps != null)
ps.close();
if (conn != null)
conn.close();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
}
}
return 0;
}
private int getZ(int id) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = getSQLConnection();
ps = conn.prepareStatement("SELECT * FROM " + table + " WHERE id = " + id + ";");
rs = ps.executeQuery();
while (rs.next()) {
if (rs.getInt("id") == id) {
return rs.getInt("z"); return rs.getInt("z");
} case LOCATION:
} return new Location((World) get(id, ShopInfo.WORLD), (int) get(id, ShopInfo.X), (int) get(id, ShopInfo.Y), (int) get(id, ShopInfo.Z));
} catch (SQLException ex) { case BUYPRICE:
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
} finally {
try {
if (ps != null)
ps.close();
if (conn != null)
conn.close();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
}
}
return 0;
}
public Location getLocation(int id) {
return new Location(getWorld(id), getX(id), getY(id), getZ(id));
}
public double getBuyPrice(int id) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = getSQLConnection();
ps = conn.prepareStatement("SELECT * FROM " + table + " WHERE id = " + id + ";");
rs = ps.executeQuery();
while (rs.next()) {
if (rs.getInt("id") == id) {
return rs.getDouble("buyprice"); return rs.getDouble("buyprice");
} case SELLPRICE:
}
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
} finally {
try {
if (ps != null)
ps.close();
if (conn != null)
conn.close();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
}
}
return 0;
}
public double getSellPrice(int id) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = getSQLConnection();
ps = conn.prepareStatement("SELECT * FROM " + table + " WHERE id = " + id + ";");
rs = ps.executeQuery();
while (rs.next()) {
if (rs.getInt("id") == id) {
return rs.getDouble("sellprice"); return rs.getDouble("sellprice");
} case SHOPTYPE:
}
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
} finally {
try {
if (ps != null)
ps.close();
if (conn != null)
conn.close();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
}
}
return 0;
}
public ShopType getShopType(int id) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = getSQLConnection();
ps = conn.prepareStatement("SELECT * FROM " + table + " WHERE id = " + id + ";");
rs = ps.executeQuery();
while (rs.next()) {
if (rs.getInt("id") == id) {
String shoptype = rs.getString("shoptype"); String shoptype = rs.getString("shoptype");
if (shoptype.equals("INFINITE")) { if (shoptype.equals("INFINITE")) {
Shop shop = new Shop(plugin, getVendor(id), getProduct(id), getLocation(id), getBuyPrice(id), getSellPrice(id), ShopType.ADMIN);
setShop(id, shop); Shop newShop = new Shop(plugin,
(OfflinePlayer) get(id, ShopInfo.VENDOR),
(ItemStack) get(id, ShopInfo.PRODUCT),
(Location) get(id, ShopInfo.LOCATION),
(double) get(id, ShopInfo.BUYPRICE),
(double) get(id, ShopInfo.SELLPRICE),
ShopType.ADMIN);
setShop(id, newShop);
return ShopType.ADMIN; return ShopType.ADMIN;
} }
return ShopType.valueOf(shoptype); return ShopType.valueOf(shoptype);
} }
} }
}
} catch (SQLException ex) { } catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); ex.printStackTrace();
} finally { } finally {
try { close(ps, rs);
if (ps != null)
ps.close();
if (conn != null)
conn.close();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
}
} }
return ShopType.NORMAL; return null;
} }
public Shop getShop(int id) {
OfflinePlayer vendor = getVendor(id);
Location location = getLocation(id);
ItemStack product = getProduct(id);
double buyPrice = getBuyPrice(id);
double sellPrice = getSellPrice(id);
ShopType shopType = getShopType(id);
if (ShopUtils.isShop(location)) return ShopUtils.getShop(location);
else return new Shop(plugin, vendor, product, location, buyPrice, sellPrice, shopType);
}
public void setShop(int id, Shop shop) { public void setShop(int id, Shop shop) {
Connection conn = null;
PreparedStatement ps = null; PreparedStatement ps = null;
try { try {
conn = getSQLConnection(); ps = connection.prepareStatement("REPLACE INTO shop_list (id,vendor,product,world,x,y,z,buyprice,sellprice,shoptype) VALUES(?,?,?,?,?,?,?,?,?,?)");
ps = conn.prepareStatement("REPLACE INTO " + table + " (id,vendor,product,world,x,y,z,buyprice,sellprice,shoptype) VALUES(?,?,?,?,?,?,?,?,?,?)");
ps.setInt(1, id); ps.setInt(1, id);
ps.setString(2, shop.getVendor().getUniqueId().toString()); ps.setString(2, shop.getVendor().getUniqueId().toString());
@ -467,36 +247,40 @@ public abstract class Database {
ps.setString(10, shop.getShopType().toString()); ps.setString(10, shop.getShopType().toString());
ps.executeUpdate(); ps.executeUpdate();
return;
} catch (SQLException ex) { } catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); ex.printStackTrace();
} finally { } finally {
try { close(ps, null);
if (ps != null)
ps.close();
if (conn != null)
conn.close();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
} }
} }
return;
}
public void addShop(Shop shop) { public void addShop(Shop shop) {
int id = getNextFreeID(); int id = getNextFreeID();
setShop(id, shop); setShop(id, shop);
} }
private void close(PreparedStatement ps, ResultSet rs) {
public void close(PreparedStatement ps, ResultSet rs) {
try { try {
if (ps != null) if (ps != null)
ps.close(); ps.close();
if (rs != null) if (rs != null)
rs.close(); rs.close();
} catch (SQLException ex) { } catch (SQLException ex) {
Error.close(plugin, ex); ex.printStackTrace();
} }
} }
public enum ShopInfo {
SHOP,
VENDOR,
PRODUCT,
WORLD,
X,
Y,
Z,
LOCATION,
BUYPRICE,
SELLPRICE,
SHOPTYPE;
}
} }

View File

@ -1,58 +0,0 @@
package de.epiceric.shopchest.sql;
import de.epiceric.shopchest.ShopChest;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
public class SQLite extends Database {
String dbname;
public SQLite(ShopChest instance) {
super(instance);
dbname = "shops";
}
// SQL creation stuff, You can leave the below stuff untouched.
public Connection getSQLConnection() {
File dataFolder = new File(plugin.getDataFolder(), dbname + ".db");
if (!dataFolder.exists()) {
try {
dataFolder.createNewFile();
} catch (IOException e) {
plugin.getLogger().log(Level.SEVERE, "File write error: " + dbname + ".db");
}
}
try {
if (connection != null && !connection.isClosed()) {
return connection;
}
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:" + dataFolder);
return connection;
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, "SQLite exception on initialize", ex);
} catch (ClassNotFoundException ex) {
plugin.getLogger().log(Level.SEVERE, "You need the SQLite JBDC library. Google it. Put it in /lib folder.");
}
return null;
}
public void load() {
connection = getSQLConnection();
try {
Statement s = connection.createStatement();
s.executeUpdate(SQLiteCreateTokensTable);
s.close();
} catch (SQLException e) {
e.printStackTrace();
}
initialize();
}
}

View File

@ -14,9 +14,11 @@ public class ClickType {
private double buyPrice; private double buyPrice;
private double sellPrice; private double sellPrice;
private ShopType shopType; private ShopType shopType;
public ClickType(EnumClickType enumClickType) { public ClickType(EnumClickType enumClickType) {
this.enumClickType = enumClickType; this.enumClickType = enumClickType;
} }
public ClickType(EnumClickType enumClickType, ItemStack product, double buyPrice, double sellPrice, ShopType shopType) { public ClickType(EnumClickType enumClickType, ItemStack product, double buyPrice, double sellPrice, ShopType shopType) {
this.enumClickType = enumClickType; this.enumClickType = enumClickType;
this.product = product; this.product = product;

View File

@ -11,6 +11,7 @@ public class UpdateChecker {
private String url; private String url;
private String version; private String version;
private String link; private String link;
public UpdateChecker(ShopChest plugin, String url) { public UpdateChecker(ShopChest plugin, String url) {
this.plugin = plugin; this.plugin = plugin;
this.url = url; this.url = url;