From 05cd2eeb4e7e68d022926d874b1a1761355c75c4 Mon Sep 17 00:00:00 2001 From: Eric Date: Tue, 2 Aug 2016 13:23:47 +0200 Subject: [PATCH] Players can now sell broken items if configured To automatically reconnect to the MySQL server, ShopChest is now using a much simpler way: Just added the attribute "autoReconnect=true" to the end of the link, instead of using a configurable value of reconnect attempts. --- .../java/de/epiceric/shopchest/Commands.java | 2 +- .../de/epiceric/shopchest/config/Config.java | 10 +- .../listeners/ShopInteractListener.java | 2 +- .../de/epiceric/shopchest/sql/Database.java | 91 +++++-------------- .../java/de/epiceric/shopchest/sql/MySQL.java | 2 +- .../epiceric/shopchest/utils/ShopUtils.java | 8 +- ShopChest/src/main/resources/config.yml | 7 +- 7 files changed, 36 insertions(+), 86 deletions(-) diff --git a/ShopChest/src/main/java/de/epiceric/shopchest/Commands.java b/ShopChest/src/main/java/de/epiceric/shopchest/Commands.java index eeb432f..e656c7b 100644 --- a/ShopChest/src/main/java/de/epiceric/shopchest/Commands.java +++ b/ShopChest/src/main/java/de/epiceric/shopchest/Commands.java @@ -336,7 +336,7 @@ public class Commands extends BukkitCommand { itemStack.setItemMeta(p.getItemInHand().getItemMeta()); if (Enchantment.DURABILITY.canEnchantItem(itemStack)) { - if (itemStack.getDurability() > 0) { + if (itemStack.getDurability() > 0 && !plugin.getShopChestConfig().allow_broken_items) { p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CANNOT_SELL_BROKEN_ITEM)); return; } diff --git a/ShopChest/src/main/java/de/epiceric/shopchest/config/Config.java b/ShopChest/src/main/java/de/epiceric/shopchest/config/Config.java index 42b813b..10a6614 100644 --- a/ShopChest/src/main/java/de/epiceric/shopchest/config/Config.java +++ b/ShopChest/src/main/java/de/epiceric/shopchest/config/Config.java @@ -40,11 +40,6 @@ public class Config { /** The database type used for ShopChest. **/ public Database.DatabaseType database_type; - /** - * The amount of attempts, ShopChest tries to reconnect to the database, when the connection is lost, until giving up - **/ - public int database_reconnect_attempts; - /** *

The minimum prices for certain items

* This returns a key set, which contains e.g "STONE", "STONE:1", of the minimum-prices section in ShopChest's config. @@ -87,6 +82,9 @@ public class Config { /** Whether the shop items should be shown **/ public boolean show_shop_items; + /** Whether players are allowed to sell/buy broken items **/ + public boolean allow_broken_items; + /** *

Whether shops should automatically be removed from the database if an error occurred while loading

* (e.g. when no chest is found at a shop's location) @@ -260,8 +258,8 @@ public class Config { database_mysql_username = plugin.getConfig().getString("database.mysql.username"); database_mysql_password = plugin.getConfig().getString("database.mysql.password"); database_type = Database.DatabaseType.valueOf(plugin.getConfig().getString("database.type")); - database_reconnect_attempts = plugin.getConfig().getInt("database.reconnect-attempts"); minimum_prices = (plugin.getConfig().getConfigurationSection("minimum-prices") == null) ? new HashSet() : plugin.getConfig().getConfigurationSection("minimum-prices").getKeys(true); + allow_broken_items = (plugin.getConfig().getBoolean("allow-broken-items")); shopLimits_group = (plugin.getConfig().getConfigurationSection("shop-limits.group") == null) ? new HashSet() : plugin.getConfig().getConfigurationSection("shop-limits.group").getKeys(true); shopLimits_player = (plugin.getConfig().getConfigurationSection("shop-limits.player") == null) ? new HashSet() : plugin.getConfig().getConfigurationSection("shop-limits.player").getKeys(true); blacklist = (plugin.getConfig().getStringList("blacklist") == null) ? new ArrayList() : plugin.getConfig().getStringList("blacklist"); diff --git a/ShopChest/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java b/ShopChest/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java index 73d6cab..30c5ea9 100644 --- a/ShopChest/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java +++ b/ShopChest/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java @@ -245,7 +245,7 @@ public class ShopInteractListener implements Listener { * @param shopType Type of the shop */ private void create(Player executor, Location location, ItemStack product, double buyPrice, double sellPrice, ShopType shopType) { - int id = database.getNextFreeID(plugin.getShopChestConfig().database_reconnect_attempts); + int id = database.getNextFreeID(); if (id == 0) { executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.ERROR_OCCURRED, new LocalizedMessage.ReplacedRegex(Regex.ERROR, "Could not connect to database"))); diff --git a/ShopChest/src/main/java/de/epiceric/shopchest/sql/Database.java b/ShopChest/src/main/java/de/epiceric/shopchest/sql/Database.java index e969d3a..adbcb12 100644 --- a/ShopChest/src/main/java/de/epiceric/shopchest/sql/Database.java +++ b/ShopChest/src/main/java/de/epiceric/shopchest/sql/Database.java @@ -19,12 +19,8 @@ public abstract class Database { public ShopChest plugin; public Connection connection; - private int attempts; - public Database(ShopChest plugin) { this.plugin = plugin; - this.attempts = plugin.getShopChestConfig().database_reconnect_attempts; - initialize(); } @@ -69,23 +65,14 @@ public abstract class Database { } /** - * @param reconnectAttempts Attempts to reconnect to the database if not connected * @return Lowest possible ID which is not used (> 0) */ - public int getNextFreeID(int reconnectAttempts) { - if (!isConnected()) { - if (reconnectAttempts > 0) { - connection = getConnection(); - plugin.getLogger().info("Reconnecting to database (" + reconnectAttempts + ") ..."); - return getNextFreeID(reconnectAttempts - 1); - } else return 0; - } - - for (int i = 1; i < getHighestID(attempts) + 1; i++) { - if (get(i, ShopInfo.X, attempts) == null) { + public int getNextFreeID() { + for (int i = 1; i < getHighestID() + 1; i++) { + if (get(i, ShopInfo.X) == null) { return i; } else { - if (i == getHighestID(attempts)) { + if (i == getHighestID()) { return i + 1; } } @@ -97,15 +84,7 @@ public abstract class Database { /** * @return Highest ID which is used */ - public int getHighestID(int reconnectAttempts) { - if (!isConnected()) { - if (reconnectAttempts > 0) { - connection = getConnection(); - plugin.getLogger().info("Reconnecting to database (" + reconnectAttempts + ") ..."); - return getHighestID(reconnectAttempts - 1); - } else return 0; - } - + public int getHighestID() { PreparedStatement ps = null; ResultSet rs = null; @@ -137,16 +116,7 @@ public abstract class Database { * * @param shop Shop to remove */ - public void removeShop(Shop shop, int reconnectAttempts) { - if (!isConnected()) { - if (reconnectAttempts > 0) { - connection = getConnection(); - plugin.getLogger().info("Reconnecting to database (" + reconnectAttempts + ") ..."); - removeShop(shop, reconnectAttempts - 1); - return; - } else return; - } - + public void removeShop(Shop shop) { PreparedStatement ps = null; try { @@ -165,15 +135,7 @@ public abstract class Database { * @param shopInfo What to get * @return Value you wanted to get. This needs to be casted to the right type! */ - public Object get(int id, ShopInfo shopInfo, int reconnectAttempts) { - if (!isConnected()) { - if (reconnectAttempts > 0) { - connection = getConnection(); - plugin.getLogger().info("Reconnecting to database (" + reconnectAttempts + ") ..."); - return get(id, shopInfo, reconnectAttempts - 1); - } else return null; - } - + public Object get(int id, ShopInfo shopInfo) { PreparedStatement ps = null; ResultSet rs = null; @@ -186,17 +148,17 @@ public abstract class Database { switch (shopInfo) { case SHOP: - Shop shop = plugin.getShopUtils().getShop((Location) get(id, ShopInfo.LOCATION, attempts)); + Shop shop = plugin.getShopUtils().getShop((Location) get(id, ShopInfo.LOCATION)); if (shop != null) return shop; else { return new Shop(id, plugin, - (OfflinePlayer) get(id, ShopInfo.VENDOR, attempts), - (ItemStack) get(id, ShopInfo.PRODUCT, attempts), - (Location) get(id, ShopInfo.LOCATION, attempts), - (double) get(id, ShopInfo.BUYPRICE, attempts), - (double) get(id, ShopInfo.SELLPRICE, attempts), - (ShopType) get(id, ShopInfo.SHOPTYPE, attempts)); + (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"))); @@ -211,7 +173,7 @@ public abstract class Database { case Z: return rs.getInt("z"); case LOCATION: - return new Location((World) get(id, ShopInfo.WORLD, attempts), (int) get(id, ShopInfo.X, attempts), (int) get(id, ShopInfo.Y, attempts), (int) get(id, ShopInfo.Z, attempts)); + 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: @@ -222,14 +184,14 @@ public abstract class Database { if (shoptype.equals("INFINITE")) { Shop newShop = new Shop(id, plugin, - (OfflinePlayer) get(id, ShopInfo.VENDOR, attempts), - (ItemStack) get(id, ShopInfo.PRODUCT, attempts), - (Location) get(id, ShopInfo.LOCATION, attempts), - (double) get(id, ShopInfo.BUYPRICE, attempts), - (double) get(id, ShopInfo.SELLPRICE, attempts), + (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, attempts); + addShop(newShop); return ShopType.ADMIN; } @@ -251,16 +213,7 @@ public abstract class Database { * Adds a shop to the database * @param shop Shop to add */ - public void addShop(Shop shop, int reconnectAttempts) { - if (!isConnected()) { - if (reconnectAttempts > 0) { - connection = getConnection(); - plugin.getLogger().info("Reconnecting to database (" + reconnectAttempts + ") ..."); - addShop(shop, reconnectAttempts - 1); - return; - } else return; - } - + public void addShop(Shop shop) { PreparedStatement ps = null; try { diff --git a/ShopChest/src/main/java/de/epiceric/shopchest/sql/MySQL.java b/ShopChest/src/main/java/de/epiceric/shopchest/sql/MySQL.java index ab3b992..d5fe3c0 100644 --- a/ShopChest/src/main/java/de/epiceric/shopchest/sql/MySQL.java +++ b/ShopChest/src/main/java/de/epiceric/shopchest/sql/MySQL.java @@ -20,7 +20,7 @@ public class MySQL extends Database { Class.forName("com.mysql.jdbc.Driver"); - String connectUrl = "jdbc:mysql://" + plugin.getShopChestConfig().database_mysql_host + ":" + plugin.getShopChestConfig().database_mysql_port + "/" + plugin.getShopChestConfig().database_mysql_database; + String connectUrl = "jdbc:mysql://" + plugin.getShopChestConfig().database_mysql_host + ":" + plugin.getShopChestConfig().database_mysql_port + "/" + plugin.getShopChestConfig().database_mysql_database + "?autoReconnect=true"; plugin.getLogger().info("Connecting to MySQL Server \"" + connectUrl + "\" as user \"" + plugin.getShopChestConfig().database_mysql_username + "\""); connection = DriverManager.getConnection(connectUrl, plugin.getShopChestConfig().database_mysql_username, plugin.getShopChestConfig().database_mysql_password); diff --git a/ShopChest/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java b/ShopChest/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java index 2190d16..36471fc 100644 --- a/ShopChest/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java +++ b/ShopChest/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java @@ -82,7 +82,7 @@ public class ShopUtils { } if (addToDatabase) - plugin.getShopDatabase().addShop(shop, plugin.getShopChestConfig().database_reconnect_attempts); + plugin.getShopDatabase().addShop(shop); } @@ -109,7 +109,7 @@ public class ShopUtils { shop.removeHologram(); if (removeFromDatabase) - plugin.getShopDatabase().removeShop(shop, plugin.getShopChestConfig().database_reconnect_attempts); + plugin.getShopDatabase().removeShop(shop); } /** @@ -218,10 +218,10 @@ public class ShopUtils { } int count = 0; - for (int id = 1; id < plugin.getShopDatabase().getHighestID(plugin.getShopChestConfig().database_reconnect_attempts) + 1; id++) { + for (int id = 1; id < plugin.getShopDatabase().getHighestID() + 1; id++) { try { - Shop shop = (Shop) plugin.getShopDatabase().get(id, Database.ShopInfo.SHOP, plugin.getShopChestConfig().database_reconnect_attempts); + Shop shop = (Shop) plugin.getShopDatabase().get(id, Database.ShopInfo.SHOP); addShop(shop, false); } catch (Exception e) { continue; diff --git a/ShopChest/src/main/resources/config.yml b/ShopChest/src/main/resources/config.yml index b9edac4..ff9d2da 100644 --- a/ShopChest/src/main/resources/config.yml +++ b/ShopChest/src/main/resources/config.yml @@ -14,6 +14,9 @@ language-file: "en_US" # Set whether the floating shop items on top of the chest should be shown show-shop-items: true +# Set whether players should be allowed to sell/buy broken items +allow-broken-items: false + # Set whether shops should automatically be removed from the database if an error occurred while loading # e.g. when no chest is found at a shop's location # This might be useful if you're removing shop chests with WorldEdit, resetting plots, or similar @@ -69,10 +72,6 @@ database: # Either use 'SQLite' or 'MySQL'. Otherwise you will break the plugin! type: "SQLite" - # Set the amount of attempts, ShopChest tries to reconnect to the database, - # when the connection is lost, until giving up. - reconnect-attemps: 5 - # If the specified type is 'MySQL', here you configure the... # (You can leave this empty if you're using SQLite) mysql: