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.
This commit is contained in:
Eric 2016-08-02 13:23:47 +02:00
parent e522746eeb
commit 05cd2eeb4e
7 changed files with 36 additions and 86 deletions

View File

@ -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;
}

View File

@ -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;
/**
* <p>The minimum prices for certain items</p>
* This returns a key set, which contains e.g "STONE", "STONE:1", of the <i>minimum-prices</i> 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;
/**
* <p>Whether shops should automatically be removed from the database if an error occurred while loading</p>
* (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<String>() : 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<String>() : plugin.getConfig().getConfigurationSection("shop-limits.group").getKeys(true);
shopLimits_player = (plugin.getConfig().getConfigurationSection("shop-limits.player") == null) ? new HashSet<String>() : plugin.getConfig().getConfigurationSection("shop-limits.player").getKeys(true);
blacklist = (plugin.getConfig().getStringList("blacklist") == null) ? new ArrayList<String>() : plugin.getConfig().getStringList("blacklist");

View File

@ -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")));

View File

@ -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 {

View File

@ -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);

View File

@ -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;

View File

@ -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: