From ecf48629e5d20867bd928d69c5b25283245659c2 Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 13 Apr 2016 17:30:34 +0200 Subject: [PATCH] Added Admin Shops Admin Shops are basically the same as infinite shops, but the vendor won't get money and won't get a message if someone bought from or sold to it. As I rewrote the buy and sell methods, I also fixed a bug that you had to pay even though you didn't have enough space in your inventory and that the vendor had to pay even though his shop's inventory was full --- config.yml | 15 +- src/de/epiceric/shopchest/Commands.java | 19 ++- src/de/epiceric/shopchest/ShopChest.java | 21 ++- src/de/epiceric/shopchest/config/Config.java | 11 +- .../shopchest/event/InteractShop.java | 136 ++++++++++++------ .../shopchest/event/ProtectChest.java | 2 +- src/de/epiceric/shopchest/shop/Shop.java | 16 ++- src/de/epiceric/shopchest/sql/Database.java | 77 ++++++++-- src/de/epiceric/shopchest/sql/SQLite.java | 15 -- .../epiceric/shopchest/utils/ClickType.java | 12 +- 10 files changed, 232 insertions(+), 92 deletions(-) diff --git a/config.yml b/config.yml index f3e4076..509c52d 100644 --- a/config.yml +++ b/config.yml @@ -135,8 +135,11 @@ messages: # ... when the shop is infinite. is-infinite: "&6Type: Infinite" - # ... when the shop is not infinite. - is-not-infinite: "&6Type: Normal" + # ... when the shop is normal. + is-normal: "&6Type: Normal" + + # ... when the shop is an admin shop. + is-admin: "&6Type: Admin" # Set the message when the clicked block is not a chest. block-no-chest: "&cBlock is not a chest" @@ -148,9 +151,17 @@ messages: # Usable regex: %AMOUNT%, %ITEMNAME%, %BUY-PRICE%, %CURRENCY-SYMBOL%, %VENDOR% buy-success: "&aYou bought &6%AMOUNT% x %ITEMNAME%&a for &6%BUY-PRICE%%CURRENCY-SYMBOL%&a from &6%VENDOR%&a." + # Set the message when the player successfully bought something from an admin shop. + # Usable regex: %AMOUNT%, %ITEMNAME%, %BUY-PRICE%, %CURRENCY-SYMBOL% + buy-success-admin: "&aYou bought &6%AMOUNT% x %ITEMNAME%&a for &6%BUY-PRICE%%CURRENCY-SYMBOL%&a." + # Set the message when the player successfully sold something. # Usable regex: %AMOUNT%, %ITEMNAME%, %SELL-PRICE%, %CURRENCY-SYMBOL%, %VENDOR% sell-success: "&aYou sold &6%AMOUNT% x %ITEMNAME%&a for &6%SELL-PRICE%%CURRENCY-SYMBOL%&a to &6%VENDOR%&a." + + # Set the message when the player successfully sold something to an admin shop. + # Usable regex: %AMOUNT%, %ITEMNAME%, %SELL-PRICE%, %CURRENCY-SYMBOL% + sell-success-admin: "&aYou sold &6%AMOUNT% x %ITEMNAME%&a for &6%SELL-PRICE%%CURRENCY-SYMBOL%&a." # Set the message when a player bought something from the players' shop. # Usable regex: %AMOUNT%, %ITEMNAME%, %BUY-PRICE%, %CURRENCY-SYMBOL%, %PLAYER% diff --git a/src/de/epiceric/shopchest/Commands.java b/src/de/epiceric/shopchest/Commands.java index 379af76..3f1310c 100644 --- a/src/de/epiceric/shopchest/Commands.java +++ b/src/de/epiceric/shopchest/Commands.java @@ -16,6 +16,7 @@ import de.epiceric.shopchest.config.Config; import de.epiceric.shopchest.interfaces.JsonBuilder; import de.epiceric.shopchest.interfaces.Utils; import de.epiceric.shopchest.interfaces.jsonbuilder.*; +import de.epiceric.shopchest.shop.Shop.ShopType; import de.epiceric.shopchest.utils.ClickType; import de.epiceric.shopchest.utils.ClickType.EnumClickType; import de.epiceric.shopchest.utils.ShopUtils; @@ -61,7 +62,7 @@ public class Commands extends BukkitCommand { if (args.length == 4) { - create(args, false, p); + create(args, ShopType.NORMAL, p); return true; @@ -71,7 +72,7 @@ public class Commands extends BukkitCommand { if (perm.has(p, "shopchest.create.infinite")) { - create(args, true, p); + create(args, ShopType.INFINITE, p); return true; } else { @@ -83,9 +84,15 @@ public class Commands extends BukkitCommand { } else if (args[4].equalsIgnoreCase("normal")){ - create(args, false, p); + create(args, ShopType.NORMAL, p); return true; + } else if (args[4].equalsIgnoreCase("admin")) { + + create(args, ShopType.ADMIN, p); + return true; + + } else { sendBasicHelpMessage(p); @@ -200,7 +207,7 @@ public class Commands extends BukkitCommand { } - private void create(String[] args, boolean infinite, Player p) { + private void create(String[] args, ShopType shopType, Player p) { int amount; double buyPrice, sellPrice; @@ -298,7 +305,7 @@ public class Commands extends BukkitCommand { } } - ClickType.addPlayerClickType(p, new ClickType(EnumClickType.CREATE, itemStack, buyPrice, sellPrice, infinite)); + ClickType.addPlayerClickType(p, new ClickType(EnumClickType.CREATE, itemStack, buyPrice, sellPrice, shopType)); p.sendMessage(Config.click_chest_to_create()); } @@ -315,7 +322,7 @@ public class Commands extends BukkitCommand { private void sendBasicHelpMessage(Player player) { - player.sendMessage(ChatColor.GREEN + "/" + Config.main_command_name() + " create [infinite|normal]- " + Config.cmdDesc_create()); + player.sendMessage(ChatColor.GREEN + "/" + Config.main_command_name() + " create [infinite|normal|admin] - " + Config.cmdDesc_create()); player.sendMessage(ChatColor.GREEN + "/" + Config.main_command_name() + " remove - " + Config.cmdDesc_remove()); player.sendMessage(ChatColor.GREEN + "/" + Config.main_command_name() + " info - " + Config.cmdDesc_info()); player.sendMessage(ChatColor.GREEN + "/" + Config.main_command_name() + " reload - " + Config.cmdDesc_reload()); diff --git a/src/de/epiceric/shopchest/ShopChest.java b/src/de/epiceric/shopchest/ShopChest.java index cfae811..37eac13 100644 --- a/src/de/epiceric/shopchest/ShopChest.java +++ b/src/de/epiceric/shopchest/ShopChest.java @@ -29,6 +29,7 @@ import de.epiceric.shopchest.event.UpdateHolograms; import de.epiceric.shopchest.interfaces.Utils; 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.interfaces.JsonBuilder; import de.epiceric.shopchest.interfaces.jsonbuilder.*; @@ -110,7 +111,7 @@ public class ShopChest extends JavaPlugin{ int value = 0; for (Shop shop : ShopUtils.getShops()) { - if (shop.isInfinite()) value++; + if (shop.getShopType() == ShopType.INFINITE) value++; } return value; @@ -124,7 +125,7 @@ public class ShopChest extends JavaPlugin{ int value = 0; for (Shop shop : ShopUtils.getShops()) { - if (!shop.isInfinite()) value++; + if (shop.getShopType() == ShopType.NORMAL) value++; } return value; @@ -132,6 +133,22 @@ public class ShopChest extends JavaPlugin{ }); + shopType.addPlotter(new Plotter("Admin") { + + @Override + public int getValue() { + int value = 0; + + for (Shop shop : ShopUtils.getShops()) { + if (shop.getShopType() == ShopType.ADMIN) value++; + } + + return value; + + } + + }); + metrics.start(); } catch (IOException e) { logger.severe("Could not submit stats."); diff --git a/src/de/epiceric/shopchest/config/Config.java b/src/de/epiceric/shopchest/config/Config.java index 2b40253..d614823 100644 --- a/src/de/epiceric/shopchest/config/Config.java +++ b/src/de/epiceric/shopchest/config/Config.java @@ -49,7 +49,8 @@ public class Config { public static String cmdDesc_update() { return plugin.getConfig().getString("messages.command-description.update").replaceAll("(&([a-f0-9k-or]))", "\u00A7$2");} public static String cmdDesc_limits() { return plugin.getConfig().getString("messages.command-description.limits").replaceAll("(&([a-f0-9k-or]))", "\u00A7$2");} public static String shopInfo_isInfinite() { return plugin.getConfig().getString("messages.shop-info.is-infinite").replaceAll("(&([a-f0-9k-or]))", "\u00A7$2");}; - public static String shopInfo_isNormal() { return plugin.getConfig().getString("messages.shop-info.is-not-infinite").replaceAll("(&([a-f0-9k-or]))", "\u00A7$2");}; + public static String shopInfo_isNormal() { return plugin.getConfig().getString("messages.shop-info.is-normal").replaceAll("(&([a-f0-9k-or]))", "\u00A7$2");}; + public static String shopInfo_isAdmin() { return plugin.getConfig().getString("messages.shop-info.is-admin").replaceAll("(&([a-f0-9k-or]))", "\u00A7$2");}; public static String noPermission_create() { return plugin.getConfig().getString("messages.no-permission.create").replaceAll("(&([a-f0-9k-or]))", "\u00A7$2");} public static String noPermission_createInfinite() { return plugin.getConfig().getString("messages.no-permission.create-infinite").replaceAll("(&([a-f0-9k-or]))", "\u00A7$2");} public static String noPermission_openOthers() { return plugin.getConfig().getString("messages.no-permission.open-others").replaceAll("(&([a-f0-9k-or]))", "\u00A7$2");} @@ -142,10 +143,18 @@ public class Config { return plugin.getConfig().getString("messages.buy-success").replace(Regex.currencySymbol, currency_symbol()).replace(Regex.amount, String.valueOf(amount)).replace(Regex.itemName, itemName).replace(Regex.buyPrice, String.valueOf(buyPrice)).replace(Regex.vendor, vendor).replaceAll("(&([a-f0-9k-or]))", "\u00A7$2"); } + public static String buy_success_admin(int amount, String itemName, double buyPrice) { + return plugin.getConfig().getString("messages.buy-success-admin").replace(Regex.currencySymbol, currency_symbol()).replace(Regex.amount, String.valueOf(amount)).replace(Regex.itemName, itemName).replace(Regex.buyPrice, String.valueOf(buyPrice)).replaceAll("(&([a-f0-9k-or]))", "\u00A7$2"); + } + public static String sell_success(int amount, String itemName, double sellPrice, String vendor) { return plugin.getConfig().getString("messages.sell-success").replace(Regex.currencySymbol, currency_symbol()).replace(Regex.amount, String.valueOf(amount)).replace(Regex.itemName, itemName).replace(Regex.sellPrice, String.valueOf(sellPrice)).replace(Regex.vendor, vendor).replaceAll("(&([a-f0-9k-or]))", "\u00A7$2"); } + public static String sell_success_admin(int amount, String itemName, double sellPrice) { + return plugin.getConfig().getString("messages.sell-success-admin").replace(Regex.currencySymbol, currency_symbol()).replace(Regex.amount, String.valueOf(amount)).replace(Regex.itemName, itemName).replace(Regex.sellPrice, String.valueOf(sellPrice)).replaceAll("(&([a-f0-9k-or]))", "\u00A7$2"); + } + public static String someone_bought(int amount, String itemName, double buyPrice, String player) { return plugin.getConfig().getString("messages.someone-bought").replace(Regex.currencySymbol, currency_symbol()).replace(Regex.amount, String.valueOf(amount)).replace(Regex.itemName, itemName).replace(Regex.buyPrice, String.valueOf(buyPrice)).replace(Regex.player, player).replaceAll("(&([a-f0-9k-or]))", "\u00A7$2"); } diff --git a/src/de/epiceric/shopchest/event/InteractShop.java b/src/de/epiceric/shopchest/event/InteractShop.java index 2bf5b58..ccf0f11 100644 --- a/src/de/epiceric/shopchest/event/InteractShop.java +++ b/src/de/epiceric/shopchest/event/InteractShop.java @@ -25,6 +25,7 @@ import com.griefcraft.model.Protection; import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.config.Config; import de.epiceric.shopchest.shop.Shop; +import de.epiceric.shopchest.shop.Shop.ShopType; import de.epiceric.shopchest.sql.SQLite; import de.epiceric.shopchest.utils.ClickType; import de.epiceric.shopchest.utils.EnchantmentNames; @@ -92,9 +93,9 @@ public class InteractShop implements Listener{ ItemStack product = clickType.getProduct(); double buyPrice = clickType.getBuyPrice(); double sellPrice = clickType.getSellPrice(); - boolean infinite = clickType.isInfinite(); + ShopType shopType = clickType.getShopType(); - create(p, b.getLocation(), product, buyPrice, sellPrice, infinite); + create(p, b.getLocation(), product, buyPrice, sellPrice, shopType); } else { p.sendMessage(Config.chest_already_shop()); } @@ -165,7 +166,7 @@ public class InteractShop implements Listener{ e.setCancelled(true); if (perm.has(p, "shopchest.buy")) { - if (shop.isInfinite()) { + if (shop.getShopType() == ShopType.INFINITE || shop.getShopType() == ShopType.ADMIN) { buy(p, shop); } else { Chest c = (Chest) b.getState(); @@ -224,9 +225,9 @@ public class InteractShop implements Listener{ } - private void create(Player executor, Location location, ItemStack product, double buyPrice, double sellPrice, boolean infinite) { + private void create(Player executor, Location location, ItemStack product, double buyPrice, double sellPrice, ShopType shopType) { - Shop shop = new Shop(plugin, executor, product, location, buyPrice, sellPrice, infinite); + Shop shop = new Shop(plugin, executor, product, location, buyPrice, sellPrice, shopType); shop.createHologram(); shop.createItem(); @@ -263,9 +264,13 @@ public class InteractShop implements Listener{ String product = Config.shopInfo_product(shop.getProduct().getAmount(), ItemNames.lookup(shop.getProduct())); String enchantmentString = ""; String price = Config.shopInfo_price(shop.getBuyPrice(), shop.getSellPrice()); - String infinite = (shop.isInfinite() ? Config.shopInfo_isInfinite() : Config.shopInfo_isNormal()); + String shopType; String stock = Config.shopInfo_stock(amount); + if (shop.getShopType() == ShopType.NORMAL) shopType = Config.shopInfo_isNormal(); + else if (shop.getShopType() == ShopType.INFINITE) shopType = Config.shopInfo_isInfinite(); + else shopType = Config.shopInfo_isAdmin(); + Map enchantmentMap; if (shop.getProduct().getItemMeta() instanceof EnchantmentStorageMeta) { @@ -295,7 +300,7 @@ public class InteractShop implements Listener{ executor.sendMessage(stock); if (enchantmentString.length() > 0) executor.sendMessage(Config.shopInfo_enchantment(enchantmentString)); executor.sendMessage(price); - executor.sendMessage(infinite); + executor.sendMessage(shopType); executor.sendMessage(" "); @@ -333,22 +338,24 @@ public class InteractShop implements Listener{ for (int value : slotFree.values()) { freeAmount += value; } - - EconomyResponse r = econ.withdrawPlayer(executor, shop.getBuyPrice()); - EconomyResponse r2 = econ.depositPlayer(shop.getVendor(), shop.getBuyPrice()); - - if (r.transactionSuccess()) { - if (r2.transactionSuccess()) { - if (freeAmount >= leftAmount) { - for (int slot : slotFree.keySet()) { - if (leftAmount >= 0) { - int amountInSlot = -(slotFree.get(slot) - product.getMaxStackSize()); - if (amountInSlot == -0) amountInSlot = 0; - for (int i = amountInSlot; i < product.getMaxStackSize() + 1; i++) { + + if (freeAmount >= leftAmount) { + + EconomyResponse r = econ.withdrawPlayer(executor, shop.getBuyPrice()); + EconomyResponse r2 = null; + if (shop.getShopType() != ShopType.ADMIN) r2 = econ.depositPlayer(shop.getVendor(), shop.getBuyPrice()); + + if (r.transactionSuccess()) { + if (r2 != null) { + if (r2.transactionSuccess()) { + for (int slot : slotFree.keySet()) { + int amountInSlot = product.getMaxStackSize() - slotFree.get(slot); + + for (int i = amountInSlot; i < product.getMaxStackSize(); i++) { if (leftAmount > 0) { ItemStack boughtProduct = new ItemStack(product.clone().getType(), 1, product.clone().getDurability()); boughtProduct.setItemMeta(product.clone().getItemMeta()); - if (!shop.isInfinite()) c.getInventory().removeItem(boughtProduct); + if (shop.getShopType() == ShopType.NORMAL) c.getInventory().removeItem(boughtProduct); inventory.addItem(boughtProduct); executor.updateInventory(); leftAmount--; @@ -357,19 +364,35 @@ public class InteractShop implements Listener{ if (shop.getVendor().isOnline()) shop.getVendor().getPlayer().sendMessage(Config.someone_bought(product.getAmount(), ItemNames.lookup(product), shop.getBuyPrice(), executor.getName())); return; } - } + } } - } + } else { + executor.sendMessage(Config.error_occurred(r2.errorMessage)); + } } else { - executor.sendMessage(Config.not_enough_inventory_space()); + for (int slot : slotFree.keySet()) { + int amountInSlot = product.getMaxStackSize() - slotFree.get(slot); + + for (int i = amountInSlot; i < product.getMaxStackSize(); i++) { + if (leftAmount > 0) { + ItemStack boughtProduct = new ItemStack(product.clone().getType(), 1, product.clone().getDurability()); + boughtProduct.setItemMeta(product.clone().getItemMeta()); + inventory.addItem(boughtProduct); + executor.updateInventory(); + leftAmount--; + } else if (leftAmount == 0) { + executor.sendMessage(Config.buy_success_admin(product.getAmount(), ItemNames.lookup(product), shop.getBuyPrice())); + return; + } + } + } } } else { - executor.sendMessage(Config.error_occurred(r2.errorMessage)); - } + executor.sendMessage(Config.error_occurred(r.errorMessage)); + } } else { - executor.sendMessage(Config.error_occurred(r.errorMessage)); - } - + executor.sendMessage(Config.not_enough_inventory_space()); + } } else { executor.sendMessage(Config.not_enough_money()); } @@ -409,22 +432,24 @@ public class InteractShop implements Listener{ freeAmount += value; } - EconomyResponse r = econ.withdrawPlayer(shop.getVendor(), shop.getSellPrice()); - EconomyResponse r2 = econ.depositPlayer(executor, shop.getSellPrice()); - - if (r.transactionSuccess()) { - if (r2.transactionSuccess()) { - if (freeAmount >= leftAmount) { - for (int slot : slotFree.keySet()) { - if (leftAmount >= 0) { - int amountInSlot = -(slotFree.get(slot) - product.getMaxStackSize()); - if (amountInSlot == -0) amountInSlot = 0; - for (int i = amountInSlot; i < product.getMaxStackSize() + 1; i++) { + if (freeAmount >= leftAmount) { + + EconomyResponse r = econ.depositPlayer(executor, shop.getSellPrice()); + EconomyResponse r2 = null; + if (shop.getShopType() != ShopType.ADMIN) r2 = econ.withdrawPlayer(shop.getVendor(), shop.getSellPrice()); + + if (r.transactionSuccess()) { + if (r2 != null) { + if (r2.transactionSuccess()) { + for (int slot : slotFree.keySet()) { + int amountInSlot = product.getMaxStackSize() - slotFree.get(slot); + + for (int i = amountInSlot; i < product.getMaxStackSize(); i++) { if (leftAmount > 0) { - ItemStack boughtProduct = new ItemStack(product.clone().getType(), 1, product.clone().getDurability()); - boughtProduct.setItemMeta(product.clone().getItemMeta()); - if (!shop.isInfinite()) inventory.addItem(boughtProduct); - executor.getInventory().removeItem(boughtProduct); + ItemStack soldProduct = new ItemStack(product.clone().getType(), 1, product.clone().getDurability()); + soldProduct.setItemMeta(product.clone().getItemMeta()); + if (shop.getShopType() == ShopType.NORMAL) inventory.addItem(soldProduct); + executor.getInventory().removeItem(soldProduct); executor.updateInventory(); leftAmount--; } else if (leftAmount == 0) { @@ -432,17 +457,34 @@ public class InteractShop implements Listener{ if (shop.getVendor().isOnline()) shop.getVendor().getPlayer().sendMessage(Config.someone_sold(product.getAmount(), ItemNames.lookup(product), shop.getBuyPrice(), executor.getName())); return; } - } + } } + } else { + executor.sendMessage(Config.error_occurred(r2.errorMessage)); } } else { - executor.sendMessage(Config.chest_not_enough_inventory_space()); + for (int slot : slotFree.keySet()) { + int amountInSlot = product.getMaxStackSize() - slotFree.get(slot); + + for (int i = amountInSlot; i < product.getMaxStackSize(); i++) { + if (leftAmount > 0) { + ItemStack soldProduct = new ItemStack(product.clone().getType(), 1, product.clone().getDurability()); + soldProduct.setItemMeta(product.clone().getItemMeta()); + executor.getInventory().removeItem(soldProduct); + executor.updateInventory(); + leftAmount--; + } else if (leftAmount == 0) { + executor.sendMessage(Config.sell_success_admin(product.getAmount(), ItemNames.lookup(product), shop.getSellPrice())); + return; + } + } + } } } else { - executor.sendMessage(Config.error_occurred(r2.errorMessage)); + executor.sendMessage(Config.error_occurred(r.errorMessage)); } } else { - executor.sendMessage(Config.error_occurred(r.errorMessage)); + executor.sendMessage(Config.chest_not_enough_inventory_space()); } } else { executor.sendMessage(Config.vendor_not_enough_money()); diff --git a/src/de/epiceric/shopchest/event/ProtectChest.java b/src/de/epiceric/shopchest/event/ProtectChest.java index db54370..4682f67 100644 --- a/src/de/epiceric/shopchest/event/ProtectChest.java +++ b/src/de/epiceric/shopchest/event/ProtectChest.java @@ -88,7 +88,7 @@ public class ProtectChest implements Listener { if (shop.hasItem()) shop.getItem().remove(); - Shop newShop = new Shop(ShopChest.getInstance(), shop.getVendor(), shop.getProduct(), shop.getLocation(), shop.getBuyPrice(), shop.getSellPrice(), shop.isInfinite()); + Shop newShop = new Shop(ShopChest.getInstance(), shop.getVendor(), shop.getProduct(), shop.getLocation(), shop.getBuyPrice(), shop.getSellPrice(), shop.getShopType()); newShop.createHologram(); newShop.createItem(); ShopUtils.addShop(newShop); diff --git a/src/de/epiceric/shopchest/shop/Shop.java b/src/de/epiceric/shopchest/shop/Shop.java index 55d7807..91d19b0 100644 --- a/src/de/epiceric/shopchest/shop/Shop.java +++ b/src/de/epiceric/shopchest/shop/Shop.java @@ -26,6 +26,12 @@ import de.epiceric.shopchest.utils.ItemNames; public class Shop { + public enum ShopType { + NORMAL, + INFINITE, + ADMIN; + } + private ShopChest plugin; private OfflinePlayer vendor; private ItemStack product; @@ -34,16 +40,16 @@ public class Shop { private Item item; private double buyPrice; private double sellPrice; - private boolean infinite; + private ShopType shopType; - public Shop(ShopChest plugin, OfflinePlayer vendor, ItemStack product, Location location, double buyPrice, double sellPrice, boolean infinite) { + public Shop(ShopChest plugin, OfflinePlayer vendor, ItemStack product, Location location, double buyPrice, double sellPrice, ShopType shopType) { this.plugin = plugin; this.vendor = vendor; this.product = product; this.location = location; this.buyPrice = buyPrice; this.sellPrice = sellPrice; - this.infinite = infinite; + this.shopType = shopType; } public void removeHologram() { @@ -175,8 +181,8 @@ public class Shop { return sellPrice; } - public boolean isInfinite() { - return infinite; + public ShopType getShopType() { + return shopType; } public Hologram getHologram() { diff --git a/src/de/epiceric/shopchest/sql/Database.java b/src/de/epiceric/shopchest/sql/Database.java index d90dded..dff3b25 100644 --- a/src/de/epiceric/shopchest/sql/Database.java +++ b/src/de/epiceric/shopchest/sql/Database.java @@ -4,6 +4,7 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Statement; import java.util.UUID; import java.util.logging.Level; @@ -16,9 +17,25 @@ import org.bukkit.inventory.ItemStack; import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.interfaces.Utils; import de.epiceric.shopchest.shop.Shop; +import de.epiceric.shopchest.shop.Shop.ShopType; import de.epiceric.shopchest.utils.ShopUtils; public abstract 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`)" + + ");"; + ShopChest plugin; Connection connection; // The name of the table we created back in SQLite class. @@ -30,7 +47,7 @@ public abstract class Database { public Location location = null; public double buyPrice = 0; public double sellPrice = 0; - public boolean infinite = false; + public ShopType shopType = ShopType.NORMAL; public Database(ShopChest instance){ plugin = instance; @@ -52,6 +69,38 @@ public abstract class Database { } } + public void renameColumnInfiniteToShopType() { + Connection conn = null; + Statement s = null; + try { + conn = getSQLConnection(); + s = conn.createStatement(); + s.execute("ALTER TABLE " + table + " RENAME TO " + table + "_old"); + s.close(); + s.execute(SQLiteCreateTokensTable); + s.close(); + s.execute("INSERT INTO " + table + "(id, vendor, product, world, x, y, z, buyprice, sellprice, shoptype) SELECT id, vendor, product, world, x, y, z, buyprice, sellprice, infinite FROM " + table + "_old"); + s.close(); + conn.close(); + + conn = getSQLConnection(); + s = conn.createStatement(); + s.execute("DROP TABLE " + table + "_old"); + s.close(); + } catch (SQLException ex) { + plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); + } finally { + try { + if (s != null) + s.close(); + if (conn != null) + conn.close(); + } catch (SQLException ex) { + plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); + } + } + } + public int getNextFreeID() { for (int i = 1; i < getHighestID() + 1; i++) { if (getProduct(i) == null) { @@ -389,7 +438,7 @@ public abstract class Database { return 0; } - public boolean isInfinite(int id) { + public ShopType getShopType(int id) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; @@ -400,10 +449,22 @@ public abstract class Database { rs = ps.executeQuery(); while(rs.next()){ if(rs.getInt("id") == id){ - return rs.getBoolean("infinite"); + if (rs.getString("shoptype").equals("0") || rs.getString("shoptype").equals("1")) { + ps = conn.prepareStatement("UPDATE " + table + " SET shoptype = REPLACE(shoptype, '0', 'NORMAL')"); + ps.executeUpdate(); + ps.close(); + ps = conn.prepareStatement("UPDATE " + table + " SET shoptype = REPLACE(shoptype, '1', 'INFINITE')"); + ps.executeUpdate(); + return getShopType(id); + } + return ShopType.valueOf(rs.getString("shoptype")); } } } catch (SQLException ex) { + if (ex.getMessage().equals("no such column: 'shoptype'")){ + renameColumnInfiniteToShopType(); + return getShopType(id); + } plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); } finally { try { @@ -416,7 +477,7 @@ public abstract class Database { } } - return false; + return ShopType.NORMAL; } public Shop getShop(int id) { @@ -425,10 +486,10 @@ public abstract class Database { ItemStack product = getProduct(id); double buyPrice = getBuyPrice(id); double sellPrice = getSellPrice(id); - boolean infinite = isInfinite(id); + ShopType shopType = getShopType(id); if (ShopUtils.isShop(location)) return ShopUtils.getShop(location); - else return new Shop(plugin, vendor, product, location, buyPrice, sellPrice, infinite); + else return new Shop(plugin, vendor, product, location, buyPrice, sellPrice, shopType); } @@ -437,7 +498,7 @@ public abstract class Database { PreparedStatement ps = null; try { conn = getSQLConnection(); - ps = conn.prepareStatement("REPLACE INTO " + table + " (id,vendor,product,world,x,y,z,buyprice,sellprice,infinite) VALUES(?,?,?,?,?,?,?,?,?,?)"); + ps = conn.prepareStatement("REPLACE INTO " + table + " (id,vendor,product,world,x,y,z,buyprice,sellprice,shoptype) VALUES(?,?,?,?,?,?,?,?,?,?)"); ps.setInt(1, id); ps.setString(2, shop.getVendor().getUniqueId().toString()); @@ -448,7 +509,7 @@ public abstract class Database { ps.setInt(7, shop.getLocation().getBlockZ()); ps.setDouble(8, shop.getBuyPrice()); ps.setDouble(9, shop.getSellPrice()); - ps.setBoolean(10, shop.isInfinite()); + ps.setString(10, shop.getShopType().toString()); ps.executeUpdate(); return; diff --git a/src/de/epiceric/shopchest/sql/SQLite.java b/src/de/epiceric/shopchest/sql/SQLite.java index 75389a5..3b8ecd5 100644 --- a/src/de/epiceric/shopchest/sql/SQLite.java +++ b/src/de/epiceric/shopchest/sql/SQLite.java @@ -19,21 +19,6 @@ public class SQLite extends Database { dbname = "shops"; } - 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," + - "`infinite` boolean NOT NULL," + - "PRIMARY KEY (`id`)" + - ");"; - - // SQL creation stuff, You can leave the below stuff untouched. public Connection getSQLConnection() { File dataFolder = new File(plugin.getDataFolder(), dbname+".db"); diff --git a/src/de/epiceric/shopchest/utils/ClickType.java b/src/de/epiceric/shopchest/utils/ClickType.java index 066b32d..44be9cc 100644 --- a/src/de/epiceric/shopchest/utils/ClickType.java +++ b/src/de/epiceric/shopchest/utils/ClickType.java @@ -5,6 +5,8 @@ import java.util.HashMap; import org.bukkit.OfflinePlayer; import org.bukkit.inventory.ItemStack; +import de.epiceric.shopchest.shop.Shop.ShopType; + public class ClickType { private static HashMap playerClickType = new HashMap<>(); @@ -32,18 +34,18 @@ public class ClickType { private ItemStack product; private double buyPrice; private double sellPrice; - private boolean infinite; + private ShopType shopType; public ClickType(EnumClickType enumClickType) { this.enumClickType = enumClickType; } - public ClickType(EnumClickType enumClickType, ItemStack product, double buyPrice, double sellPrice, boolean infinite) { + public ClickType(EnumClickType enumClickType, ItemStack product, double buyPrice, double sellPrice, ShopType shopType) { this.enumClickType = enumClickType; this.product = product; this.sellPrice = sellPrice; this.buyPrice = buyPrice; - this.infinite = infinite; + this.shopType = shopType; } public EnumClickType getClickType() { @@ -62,8 +64,8 @@ public class ClickType { return sellPrice; } - public boolean isInfinite() { - return infinite; + public ShopType getShopType() { + return shopType; } }