Change get(...) to getShop(...) in database

This commit is contained in:
Eric 2016-11-26 17:07:21 +01:00
parent 4f894ddac7
commit 73f5d0f79e
2 changed files with 50 additions and 88 deletions

View File

@ -108,7 +108,7 @@ public abstract class Database {
public int getNextFreeID() { public int getNextFreeID() {
int highestId = getHighestID(); int highestId = getHighestID();
for (int i = 1; i <= highestId + 1; i++) { for (int i = 1; i <= highestId + 1; i++) {
if (get(i, ShopInfo.X) == null) { if (!isShop(i)) {
plugin.debug("Next free id: " + i); plugin.debug("Next free id: " + i);
return i; return i;
} }
@ -173,10 +173,9 @@ public abstract class Database {
/** /**
* @param id ID of the shop * @param id ID of the shop
* @param shopInfo What to get * @return Whether a shop with the given ID exists
* @return Value you wanted to get. This needs to be casted to the right type!
*/ */
public Object get(int id, ShopInfo shopInfo) { public boolean isShop(int id) {
PreparedStatement ps = null; PreparedStatement ps = null;
ResultSet rs = null; ResultSet rs = null;
@ -186,88 +185,65 @@ public abstract class Database {
while (rs.next()) { while (rs.next()) {
if (rs.getInt("id") == id) { if (rs.getInt("id") == id) {
return true;
}
}
} catch (SQLException ex) {
plugin.getLogger().severe("Failed to access database");
plugin.debug("Failed to check if shop with ID exists (#" + id + ")");
plugin.debug(ex);
} finally {
close(ps, rs);
}
switch (shopInfo) { return false;
case SHOP: }
/**
* @param id ID of the shop
* @return Shop with the given ID
*/
public Shop getShop(int id) {
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = connection.prepareStatement("SELECT * FROM shop_list WHERE id = " + id + ";");
rs = ps.executeQuery();
while (rs.next()) {
if (rs.getInt("id") == id) {
plugin.debug("Getting Shop... (#" + id + ")"); plugin.debug("Getting Shop... (#" + id + ")");
Shop shop = plugin.getShopUtils().getShop((Location) get(id, ShopInfo.LOCATION)); World world = Bukkit.getWorld(rs.getString("world"));
int x = rs.getInt("x");
int y = rs.getInt("y");
int z = rs.getInt("z");
Location location = new Location(world, x, y, z);
Shop shop = plugin.getShopUtils().getShop(location);
if (shop != null) { if (shop != null) {
plugin.debug("Shop already exists, returning existing one (#" + id + ")."); plugin.debug("Shop already exists, returning existing one (#" + id + ").");
return shop; return shop;
} else { } else {
plugin.debug("Creating new shop... (#" + id + ")"); plugin.debug("Creating new shop... (#" + id + ")");
return new Shop(id, 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:
String vendor = rs.getString("vendor");
plugin.debug("Getting vendor: " + vendor + " (#" + id + ")");
return Bukkit.getOfflinePlayer(UUID.fromString(vendor));
case PRODUCT:
String product = rs.getString("product");
plugin.debug("Getting product: " + product + " (#" + id + ")");
return Utils.decode(product);
case WORLD:
String world = rs.getString("world");
plugin.debug("Getting world: " + world + " (#" + id + ")");
return Bukkit.getWorld(world);
case X:
int x = rs.getInt("x");
plugin.debug("Getting x: " + x + " (#" + id + ")");
return x;
case Y:
int y = rs.getInt("y");
plugin.debug("Getting y: " + y + " (#" + id + ")");
return y;
case Z:
int z = rs.getInt("z");
plugin.debug("Getting z: " + z + " (#" + id + ")");
return z;
case LOCATION:
plugin.debug("Getting location... (#" + id + ")");
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:
double buyprice = rs.getDouble("buyprice");
plugin.debug("Getting buyprice: " + buyprice + " (#" + id + ")");
return buyprice;
case SELLPRICE:
double sellprice = rs.getDouble("sellprice");
plugin.debug("Getting sellprice: " + sellprice + " (#" + id + ")");
return sellprice;
case SHOPTYPE:
String shoptype = rs.getString("shoptype");
plugin.debug("Getting shoptype: " + shoptype + " (#" + id + ")");
if (shoptype.equals("INFINITE")) { OfflinePlayer vendor = Bukkit.getOfflinePlayer(UUID.fromString(rs.getString("vendor")));
ItemStack product = Utils.decode(rs.getString("product"));
double buyPrice = rs.getDouble("buyprice");
double sellPrice = rs.getDouble("sellprice");
ShopType shopType = ShopType.valueOf(rs.getString("shoptype"));
Shop newShop = new Shop(id, plugin, return new Shop(id, plugin, vendor, product, location, buyPrice, sellPrice, shopType);
(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);
addShop(newShop);
return ShopType.ADMIN;
}
return ShopType.valueOf(shoptype);
} }
} }
} }
plugin.debug("Shop with ID not found, returning null. (#" + id + ")"); plugin.debug("Shop with ID not found, returning null. (#" + id + ")");
} catch (SQLException ex) { } catch (SQLException ex) {
plugin.getLogger().severe("Failed to access database"); plugin.getLogger().severe("Failed to access database");
plugin.debug("Failed to get shop info " + shopInfo.toString() + "(#" + id + ")"); plugin.debug("Failed to get shop (#" + id + ")");
plugin.debug(ex); plugin.debug(ex);
} finally { } finally {
close(ps, rs); close(ps, rs);
@ -402,20 +378,6 @@ public abstract class Database {
} }
} }
public enum ShopInfo {
SHOP,
VENDOR,
PRODUCT,
WORLD,
X,
Y,
Z,
LOCATION,
BUYPRICE,
SELLPRICE,
SHOPTYPE
}
public enum DatabaseType { public enum DatabaseType {
SQLite, SQLite,
MySQL MySQL

View File

@ -193,7 +193,7 @@ public class ShopUtils {
try { try {
plugin.debug("Trying to add shop. (#" + id + ")"); plugin.debug("Trying to add shop. (#" + id + ")");
Shop shop = (Shop) plugin.getShopDatabase().get(id, Database.ShopInfo.SHOP); Shop shop = plugin.getShopDatabase().getShop(id);
addShop(shop, false); addShop(shop, false);
} catch (Exception e) { } catch (Exception e) {
plugin.debug("Error while adding shop (#" + id + "):"); plugin.debug("Error while adding shop (#" + id + "):");