mirror of
https://github.com/amalthea-mc/ShopChest.git
synced 2024-12-23 01:21:47 +00:00
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:
parent
e00f30bf0b
commit
654fa260f4
@ -10,7 +10,7 @@ import de.epiceric.shopchest.interfaces.jsonbuilder.*;
|
||||
import de.epiceric.shopchest.interfaces.utils.*;
|
||||
import de.epiceric.shopchest.shop.Shop;
|
||||
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.Graph;
|
||||
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.java.JavaPlugin;
|
||||
|
||||
import java.io.*;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@ -36,7 +36,7 @@ public class ShopChest extends JavaPlugin {
|
||||
public static Permission perm = null;
|
||||
public static LWC lwc = null;
|
||||
public static boolean lockette = false;
|
||||
public static SQLite sqlite;
|
||||
public static Database database;
|
||||
public static boolean isUpdateNeeded = false;
|
||||
public static String latestVersion = "";
|
||||
public static String downloadLink = "";
|
||||
@ -121,8 +121,7 @@ public class ShopChest extends JavaPlugin {
|
||||
reloadConfig();
|
||||
saveDefaultConfig();
|
||||
|
||||
sqlite = new SQLite(this);
|
||||
sqlite.load();
|
||||
database = new Database(this);
|
||||
|
||||
switch (Utils.getVersion(getServer())) {
|
||||
|
||||
@ -257,10 +256,10 @@ public class ShopChest extends JavaPlugin {
|
||||
private void initializeShops() {
|
||||
int count = 0;
|
||||
|
||||
for (int id = 1; id < sqlite.getHighestID() + 1; id++) {
|
||||
for (int id = 1; id < database.getHighestID() + 1; id++) {
|
||||
|
||||
try {
|
||||
Shop shop = sqlite.getShop(id);
|
||||
Shop shop = (Shop) database.get(id, Database.ShopInfo.SHOP);
|
||||
shop.createHologram();
|
||||
shop.createItem();
|
||||
ShopUtils.addShop(shop);
|
||||
|
@ -6,7 +6,7 @@ import de.epiceric.shopchest.config.Config;
|
||||
import de.epiceric.shopchest.interfaces.Utils;
|
||||
import de.epiceric.shopchest.shop.Shop;
|
||||
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 net.milkbowl.vault.economy.Economy;
|
||||
import net.milkbowl.vault.economy.EconomyResponse;
|
||||
@ -36,7 +36,7 @@ public class InteractShop implements Listener {
|
||||
private ShopChest plugin;
|
||||
private Permission perm = ShopChest.perm;
|
||||
private Economy econ = ShopChest.econ;
|
||||
private SQLite sqlite = ShopChest.sqlite;
|
||||
private Database database = ShopChest.database;
|
||||
|
||||
public InteractShop(ShopChest plugin) {
|
||||
this.plugin = plugin;
|
||||
@ -190,7 +190,7 @@ public class InteractShop implements Listener {
|
||||
|
||||
if (shop.getSellPrice() > 0) {
|
||||
if (perm.has(p, "shopchest.sell")) {
|
||||
if (shop.getShopType() == ShopType.ADMIN) {
|
||||
if (shop.getShopType() == ShopType.ADMIN) {
|
||||
if (Utils.getAmount(p.getInventory(), shop.getProduct().getType(), shop.getProduct().getDurability(), shop.getProduct().getItemMeta()) >= shop.getProduct().getAmount()) {
|
||||
sell(p, shop);
|
||||
} else {
|
||||
@ -233,7 +233,7 @@ public class InteractShop implements Listener {
|
||||
shop.createHologram();
|
||||
shop.createItem();
|
||||
|
||||
sqlite.addShop(shop);
|
||||
database.addShop(shop);
|
||||
|
||||
ShopUtils.addShop(shop);
|
||||
executor.sendMessage(Config.shop_created());
|
||||
@ -248,7 +248,7 @@ public class InteractShop implements Listener {
|
||||
|
||||
ShopUtils.removeShop(shop);
|
||||
|
||||
sqlite.removeShop(shop);
|
||||
database.removeShop(shop);
|
||||
|
||||
shop.removeHologram();
|
||||
|
||||
|
@ -77,11 +77,11 @@ public class ProtectChest implements Listener {
|
||||
if (b.getLocation().equals(r.getLocation())) {
|
||||
shop = ShopUtils.getShop(l.getLocation());
|
||||
ShopUtils.removeShop(shop);
|
||||
ShopChest.sqlite.removeShop(shop);
|
||||
ShopChest.database.removeShop(shop);
|
||||
} else if (b.getLocation().equals(l.getLocation())) {
|
||||
shop = ShopUtils.getShop(r.getLocation());
|
||||
ShopUtils.removeShop(shop);
|
||||
ShopChest.sqlite.removeShop(shop);
|
||||
ShopChest.database.removeShop(shop);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
@ -92,7 +92,7 @@ public class ProtectChest implements Listener {
|
||||
newShop.createHologram();
|
||||
newShop.createItem();
|
||||
ShopUtils.addShop(newShop);
|
||||
ShopChest.sqlite.addShop(newShop);
|
||||
ShopChest.database.addShop(newShop);
|
||||
|
||||
}
|
||||
|
||||
|
@ -5,6 +5,7 @@ import de.epiceric.shopchest.config.Config;
|
||||
import de.epiceric.shopchest.interfaces.Hologram;
|
||||
import de.epiceric.shopchest.interfaces.Utils;
|
||||
import de.epiceric.shopchest.shop.Shop;
|
||||
import de.epiceric.shopchest.sql.Database;
|
||||
import de.epiceric.shopchest.utils.ShopUtils;
|
||||
import net.minecraft.server.v1_8_R1.EntityArmorStand;
|
||||
import org.bukkit.Bukkit;
|
||||
@ -36,10 +37,10 @@ public class Utils_1_8_R1 extends Utils {
|
||||
|
||||
int count = 0;
|
||||
|
||||
for (int id = 1; id < ShopChest.sqlite.getHighestID() + 1; id++) {
|
||||
for (int id = 1; id < ShopChest.database.getHighestID() + 1; id++) {
|
||||
|
||||
try {
|
||||
Shop shop = ShopChest.sqlite.getShop(id);
|
||||
Shop shop = (Shop) ShopChest.database.get(id, Database.ShopInfo.SHOP);
|
||||
shop.createHologram();
|
||||
shop.createItem();
|
||||
ShopUtils.addShop(shop);
|
||||
|
@ -5,6 +5,7 @@ import de.epiceric.shopchest.config.Config;
|
||||
import de.epiceric.shopchest.interfaces.Hologram;
|
||||
import de.epiceric.shopchest.interfaces.Utils;
|
||||
import de.epiceric.shopchest.shop.Shop;
|
||||
import de.epiceric.shopchest.sql.Database;
|
||||
import de.epiceric.shopchest.utils.ShopUtils;
|
||||
import net.minecraft.server.v1_8_R2.EntityArmorStand;
|
||||
import org.bukkit.Bukkit;
|
||||
@ -36,10 +37,10 @@ public class Utils_1_8_R2 extends Utils {
|
||||
|
||||
int count = 0;
|
||||
|
||||
for (int id = 1; id < ShopChest.sqlite.getHighestID() + 1; id++) {
|
||||
for (int id = 1; id < ShopChest.database.getHighestID() + 1; id++) {
|
||||
|
||||
try {
|
||||
Shop shop = ShopChest.sqlite.getShop(id);
|
||||
Shop shop = (Shop) ShopChest.database.get(id, Database.ShopInfo.SHOP);
|
||||
shop.createHologram();
|
||||
shop.createItem();
|
||||
ShopUtils.addShop(shop);
|
||||
|
@ -5,6 +5,7 @@ import de.epiceric.shopchest.config.Config;
|
||||
import de.epiceric.shopchest.interfaces.Hologram;
|
||||
import de.epiceric.shopchest.interfaces.Utils;
|
||||
import de.epiceric.shopchest.shop.Shop;
|
||||
import de.epiceric.shopchest.sql.Database;
|
||||
import de.epiceric.shopchest.utils.ShopUtils;
|
||||
import net.minecraft.server.v1_8_R3.EntityArmorStand;
|
||||
import org.bukkit.Bukkit;
|
||||
@ -36,10 +37,10 @@ public class Utils_1_8_R3 extends Utils {
|
||||
|
||||
int count = 0;
|
||||
|
||||
for (int id = 1; id < ShopChest.sqlite.getHighestID() + 1; id++) {
|
||||
for (int id = 1; id < ShopChest.database.getHighestID() + 1; id++) {
|
||||
|
||||
try {
|
||||
Shop shop = ShopChest.sqlite.getShop(id);
|
||||
Shop shop = (Shop) ShopChest.database.get(id, Database.ShopInfo.SHOP);
|
||||
shop.createHologram();
|
||||
shop.createItem();
|
||||
ShopUtils.addShop(shop);
|
||||
|
@ -5,6 +5,7 @@ import de.epiceric.shopchest.config.Config;
|
||||
import de.epiceric.shopchest.interfaces.Hologram;
|
||||
import de.epiceric.shopchest.interfaces.Utils;
|
||||
import de.epiceric.shopchest.shop.Shop;
|
||||
import de.epiceric.shopchest.sql.Database;
|
||||
import de.epiceric.shopchest.utils.ShopUtils;
|
||||
import net.minecraft.server.v1_9_R1.EntityArmorStand;
|
||||
import org.bukkit.Bukkit;
|
||||
@ -36,10 +37,10 @@ public class Utils_1_9_R1 extends Utils {
|
||||
|
||||
int count = 0;
|
||||
|
||||
for (int id = 1; id < ShopChest.sqlite.getHighestID() + 1; id++) {
|
||||
for (int id = 1; id < ShopChest.database.getHighestID() + 1; id++) {
|
||||
|
||||
try {
|
||||
Shop shop = ShopChest.sqlite.getShop(id);
|
||||
Shop shop = (Shop) ShopChest.database.get(id, Database.ShopInfo.SHOP);
|
||||
shop.createHologram();
|
||||
shop.createItem();
|
||||
ShopUtils.addShop(shop);
|
||||
|
@ -5,6 +5,7 @@ import de.epiceric.shopchest.config.Config;
|
||||
import de.epiceric.shopchest.interfaces.Hologram;
|
||||
import de.epiceric.shopchest.interfaces.Utils;
|
||||
import de.epiceric.shopchest.shop.Shop;
|
||||
import de.epiceric.shopchest.sql.Database;
|
||||
import de.epiceric.shopchest.utils.ShopUtils;
|
||||
import net.minecraft.server.v1_9_R2.EntityArmorStand;
|
||||
import org.bukkit.Bukkit;
|
||||
@ -36,10 +37,10 @@ public class Utils_1_9_R2 extends Utils {
|
||||
|
||||
int count = 0;
|
||||
|
||||
for (int id = 1; id < ShopChest.sqlite.getHighestID() + 1; id++) {
|
||||
for (int id = 1; id < ShopChest.database.getHighestID() + 1; id++) {
|
||||
|
||||
try {
|
||||
Shop shop = ShopChest.sqlite.getShop(id);
|
||||
Shop shop = (Shop) ShopChest.database.get(id, Database.ShopInfo.SHOP);
|
||||
shop.createHologram();
|
||||
shop.createItem();
|
||||
ShopUtils.addShop(shop);
|
||||
|
@ -34,6 +34,7 @@ public class Shop {
|
||||
private double buyPrice;
|
||||
private double sellPrice;
|
||||
private ShopType shopType;
|
||||
|
||||
public Shop(ShopChest plugin, OfflinePlayer vendor, ItemStack product, Location location, double buyPrice, double sellPrice, ShopType shopType) {
|
||||
this.plugin = plugin;
|
||||
this.vendor = vendor;
|
||||
|
@ -11,60 +11,81 @@ import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.sql.*;
|
||||
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 (" +
|
||||
"`id` int(11) NOT NULL," +
|
||||
"`vendor` varchar(32) NOT NULL," +
|
||||
"`product` varchar(32) NOT NULL," +
|
||||
"`world` varchar(32) NOT NULL," +
|
||||
"`x` int(11) NOT NULL," +
|
||||
"`y` int(11) NOT NULL," +
|
||||
"`z` int(11) NOT NULL," +
|
||||
"`buyprice` float(32) NOT NULL," +
|
||||
"`sellprice` float(32) NOT NULL," +
|
||||
"`shoptype` varchar(32) NOT NULL," +
|
||||
"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;
|
||||
private ShopChest plugin;
|
||||
private Connection connection;
|
||||
|
||||
public Database(ShopChest instance) {
|
||||
plugin = instance;
|
||||
initialize();
|
||||
}
|
||||
|
||||
public abstract Connection getSQLConnection();
|
||||
private Connection getSQLConnection() {
|
||||
File dbFile = new File(plugin.getDataFolder(), "shops.db");
|
||||
|
||||
public abstract void load();
|
||||
if (!dbFile.exists()) {
|
||||
try {
|
||||
dbFile.createNewFile();
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void initialize() {
|
||||
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 {
|
||||
PreparedStatement ps = connection.prepareStatement("SELECT * FROM " + table + " WHERE id = ?");
|
||||
String queryCreateTable = "CREATE TABLE IF NOT EXISTS shop_list (" +
|
||||
"`id` int(11) NOT NULL," +
|
||||
"`vendor` varchar(32) NOT NULL," +
|
||||
"`product` varchar(32) NOT NULL," +
|
||||
"`world` varchar(32) NOT NULL," +
|
||||
"`x` int(11) NOT NULL," +
|
||||
"`y` int(11) NOT NULL," +
|
||||
"`z` int(11) NOT NULL," +
|
||||
"`buyprice` float(32) NOT NULL," +
|
||||
"`sellprice` float(32) NOT NULL," +
|
||||
"`shoptype` varchar(32) NOT NULL," +
|
||||
"PRIMARY KEY (`id`)" +
|
||||
");";
|
||||
|
||||
Statement s = connection.createStatement();
|
||||
s.executeUpdate(queryCreateTable);
|
||||
s.close();
|
||||
|
||||
PreparedStatement ps = connection.prepareStatement("SELECT * FROM shop_list WHERE id = ?");
|
||||
ResultSet rs = ps.executeQuery();
|
||||
close(ps, rs);
|
||||
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, "Unable to retreive connection", ex);
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public int getNextFreeID() {
|
||||
for (int i = 1; i < getHighestID() + 1; i++) {
|
||||
if (getProduct(i) == null) {
|
||||
if (get(i, ShopInfo.X) == null) {
|
||||
return i;
|
||||
} else {
|
||||
if (i == getHighestID()) {
|
||||
@ -77,383 +98,142 @@ public abstract class Database {
|
||||
}
|
||||
|
||||
public int getHighestID() {
|
||||
Connection conn = null;
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
|
||||
int highestID = 1;
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
ps = conn.prepareStatement("SELECT * FROM " + table + ";");
|
||||
|
||||
try {
|
||||
ps = connection.prepareStatement("SELECT * FROM shop_list;");
|
||||
rs = ps.executeQuery();
|
||||
|
||||
while (rs.next()) {
|
||||
if (rs.getInt("id") > highestID) {
|
||||
highestID = rs.getInt("id");
|
||||
}
|
||||
}
|
||||
|
||||
return highestID;
|
||||
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (ps != null)
|
||||
ps.close();
|
||||
if (conn != null)
|
||||
conn.close();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
|
||||
}
|
||||
close(ps, rs);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getShopID(Shop shop) {
|
||||
|
||||
|
||||
for (int i = 1; i < getHighestID() + 1; i++) {
|
||||
|
||||
try {
|
||||
Shop s = getShop(i);
|
||||
Shop s = (Shop) get(i, null);
|
||||
if (s.getLocation().equals(shop.getLocation())) {
|
||||
return i;
|
||||
}
|
||||
} catch (NullPointerException ex) {
|
||||
continue;
|
||||
}
|
||||
|
||||
} catch (NullPointerException ex) { /* Empty catch block... */ }
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void removeShop(Shop shop) {
|
||||
|
||||
int id = getShopID(shop);
|
||||
if (id == 0) return;
|
||||
|
||||
if (shop.hasItem()) shop.getItem().remove();
|
||||
|
||||
Connection conn = null;
|
||||
PreparedStatement ps = null;
|
||||
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
ps = conn.prepareStatement("DELETE FROM " + table + " where id = " + id + ";");
|
||||
ps = connection.prepareStatement("DELETE FROM shop_list WHERE id = " + id + ";");
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (ps != null)
|
||||
ps.close();
|
||||
if (conn != null)
|
||||
conn.close();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
|
||||
}
|
||||
close(ps, null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void removeShop(int id) {
|
||||
|
||||
if (id == 0) return;
|
||||
removeShop(getShop(id));
|
||||
|
||||
}
|
||||
|
||||
private World getWorld(int id) {
|
||||
Connection conn = null;
|
||||
public Object get(int id, ShopInfo shopInfo) {
|
||||
PreparedStatement ps = 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();
|
||||
|
||||
while (rs.next()) {
|
||||
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) {
|
||||
Connection conn = null;
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
ps = conn.prepareStatement("SELECT * FROM " + table + " WHERE id = " + id + ";");
|
||||
switch (shopInfo) {
|
||||
case SHOP:
|
||||
Shop shop = ShopUtils.getShop((Location) get(id, ShopInfo.LOCATION));
|
||||
if (shop != null)
|
||||
return shop;
|
||||
else {
|
||||
return 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) get(id, ShopInfo.SHOPTYPE));
|
||||
}
|
||||
case VENDOR:
|
||||
return Bukkit.getOfflinePlayer(UUID.fromString(rs.getString("vendor")));
|
||||
case PRODUCT:
|
||||
return Utils.decode(rs.getString("product"));
|
||||
case WORLD:
|
||||
return Bukkit.getWorld(rs.getString("world"));
|
||||
case X:
|
||||
return rs.getInt("x");
|
||||
case Y:
|
||||
return rs.getInt("y");
|
||||
case 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));
|
||||
case BUYPRICE:
|
||||
return rs.getDouble("buyprice");
|
||||
case SELLPRICE:
|
||||
return rs.getDouble("sellprice");
|
||||
case SHOPTYPE:
|
||||
String shoptype = rs.getString("shoptype");
|
||||
|
||||
rs = ps.executeQuery();
|
||||
while (rs.next()) {
|
||||
if (rs.getInt("id") == id) {
|
||||
return Bukkit.getOfflinePlayer(UUID.fromString(rs.getString("vendor")));
|
||||
}
|
||||
}
|
||||
} 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;
|
||||
}
|
||||
if (shoptype.equals("INFINITE")) {
|
||||
|
||||
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 + ";");
|
||||
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);
|
||||
|
||||
rs = ps.executeQuery();
|
||||
while (rs.next()) {
|
||||
if (rs.getInt("id") == id) {
|
||||
return Utils.decode(rs.getString("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;
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
} 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");
|
||||
}
|
||||
}
|
||||
} 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");
|
||||
}
|
||||
}
|
||||
} 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 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");
|
||||
}
|
||||
}
|
||||
} 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");
|
||||
}
|
||||
}
|
||||
} 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");
|
||||
if (shoptype.equals("INFINITE")) {
|
||||
Shop shop = new Shop(plugin, getVendor(id), getProduct(id), getLocation(id), getBuyPrice(id), getSellPrice(id), ShopType.ADMIN);
|
||||
setShop(id, shop);
|
||||
return ShopType.ADMIN;
|
||||
setShop(id, newShop);
|
||||
return ShopType.ADMIN;
|
||||
}
|
||||
return ShopType.valueOf(shoptype);
|
||||
}
|
||||
return ShopType.valueOf(shoptype);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (ps != null)
|
||||
ps.close();
|
||||
if (conn != null)
|
||||
conn.close();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
|
||||
}
|
||||
close(ps, rs);
|
||||
}
|
||||
|
||||
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) {
|
||||
Connection conn = null;
|
||||
PreparedStatement ps = null;
|
||||
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
ps = conn.prepareStatement("REPLACE INTO " + table + " (id,vendor,product,world,x,y,z,buyprice,sellprice,shoptype) VALUES(?,?,?,?,?,?,?,?,?,?)");
|
||||
ps = connection.prepareStatement("REPLACE INTO shop_list (id,vendor,product,world,x,y,z,buyprice,sellprice,shoptype) VALUES(?,?,?,?,?,?,?,?,?,?)");
|
||||
|
||||
ps.setInt(1, id);
|
||||
ps.setString(2, shop.getVendor().getUniqueId().toString());
|
||||
@ -467,20 +247,11 @@ public abstract class Database {
|
||||
ps.setString(10, shop.getShopType().toString());
|
||||
|
||||
ps.executeUpdate();
|
||||
return;
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (ps != null)
|
||||
ps.close();
|
||||
if (conn != null)
|
||||
conn.close();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
|
||||
}
|
||||
close(ps, null);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
public void addShop(Shop shop) {
|
||||
@ -488,15 +259,28 @@ public abstract class Database {
|
||||
setShop(id, shop);
|
||||
}
|
||||
|
||||
|
||||
public void close(PreparedStatement ps, ResultSet rs) {
|
||||
private void close(PreparedStatement ps, ResultSet rs) {
|
||||
try {
|
||||
if (ps != null)
|
||||
ps.close();
|
||||
if (rs != null)
|
||||
rs.close();
|
||||
} catch (SQLException ex) {
|
||||
Error.close(plugin, ex);
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public enum ShopInfo {
|
||||
SHOP,
|
||||
VENDOR,
|
||||
PRODUCT,
|
||||
WORLD,
|
||||
X,
|
||||
Y,
|
||||
Z,
|
||||
LOCATION,
|
||||
BUYPRICE,
|
||||
SELLPRICE,
|
||||
SHOPTYPE;
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -14,9 +14,11 @@ public class ClickType {
|
||||
private double buyPrice;
|
||||
private double sellPrice;
|
||||
private ShopType shopType;
|
||||
|
||||
public ClickType(EnumClickType enumClickType) {
|
||||
this.enumClickType = enumClickType;
|
||||
}
|
||||
|
||||
public ClickType(EnumClickType enumClickType, ItemStack product, double buyPrice, double sellPrice, ShopType shopType) {
|
||||
this.enumClickType = enumClickType;
|
||||
this.product = product;
|
||||
|
@ -11,6 +11,7 @@ public class UpdateChecker {
|
||||
private String url;
|
||||
private String version;
|
||||
private String link;
|
||||
|
||||
public UpdateChecker(ShopChest plugin, String url) {
|
||||
this.plugin = plugin;
|
||||
this.url = url;
|
||||
|
Loading…
Reference in New Issue
Block a user