mirror of
https://github.com/amalthea-mc/ShopChest.git
synced 2024-11-26 04:12:22 +00:00
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:
parent
e522746eeb
commit
05cd2eeb4e
@ -336,7 +336,7 @@ public class Commands extends BukkitCommand {
|
|||||||
itemStack.setItemMeta(p.getItemInHand().getItemMeta());
|
itemStack.setItemMeta(p.getItemInHand().getItemMeta());
|
||||||
|
|
||||||
if (Enchantment.DURABILITY.canEnchantItem(itemStack)) {
|
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));
|
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CANNOT_SELL_BROKEN_ITEM));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -40,11 +40,6 @@ public class Config {
|
|||||||
/** The database type used for ShopChest. **/
|
/** The database type used for ShopChest. **/
|
||||||
public Database.DatabaseType database_type;
|
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>
|
* <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.
|
* 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 **/
|
/** Whether the shop items should be shown **/
|
||||||
public boolean show_shop_items;
|
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>
|
* <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)
|
* (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_username = plugin.getConfig().getString("database.mysql.username");
|
||||||
database_mysql_password = plugin.getConfig().getString("database.mysql.password");
|
database_mysql_password = plugin.getConfig().getString("database.mysql.password");
|
||||||
database_type = Database.DatabaseType.valueOf(plugin.getConfig().getString("database.type"));
|
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);
|
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_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);
|
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");
|
blacklist = (plugin.getConfig().getStringList("blacklist") == null) ? new ArrayList<String>() : plugin.getConfig().getStringList("blacklist");
|
||||||
|
@ -245,7 +245,7 @@ public class ShopInteractListener implements Listener {
|
|||||||
* @param shopType Type of the shop
|
* @param shopType Type of the shop
|
||||||
*/
|
*/
|
||||||
private void create(Player executor, Location location, ItemStack product, double buyPrice, double sellPrice, ShopType shopType) {
|
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) {
|
if (id == 0) {
|
||||||
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.ERROR_OCCURRED, new LocalizedMessage.ReplacedRegex(Regex.ERROR, "Could not connect to database")));
|
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.ERROR_OCCURRED, new LocalizedMessage.ReplacedRegex(Regex.ERROR, "Could not connect to database")));
|
||||||
|
@ -19,12 +19,8 @@ public abstract class Database {
|
|||||||
public ShopChest plugin;
|
public ShopChest plugin;
|
||||||
public Connection connection;
|
public Connection connection;
|
||||||
|
|
||||||
private int attempts;
|
|
||||||
|
|
||||||
public Database(ShopChest plugin) {
|
public Database(ShopChest plugin) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
this.attempts = plugin.getShopChestConfig().database_reconnect_attempts;
|
|
||||||
|
|
||||||
initialize();
|
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)
|
* @return Lowest possible ID which is not used (> 0)
|
||||||
*/
|
*/
|
||||||
public int getNextFreeID(int reconnectAttempts) {
|
public int getNextFreeID() {
|
||||||
if (!isConnected()) {
|
for (int i = 1; i < getHighestID() + 1; i++) {
|
||||||
if (reconnectAttempts > 0) {
|
if (get(i, ShopInfo.X) == null) {
|
||||||
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) {
|
|
||||||
return i;
|
return i;
|
||||||
} else {
|
} else {
|
||||||
if (i == getHighestID(attempts)) {
|
if (i == getHighestID()) {
|
||||||
return i + 1;
|
return i + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -97,15 +84,7 @@ public abstract class Database {
|
|||||||
/**
|
/**
|
||||||
* @return Highest ID which is used
|
* @return Highest ID which is used
|
||||||
*/
|
*/
|
||||||
public int getHighestID(int reconnectAttempts) {
|
public int getHighestID() {
|
||||||
if (!isConnected()) {
|
|
||||||
if (reconnectAttempts > 0) {
|
|
||||||
connection = getConnection();
|
|
||||||
plugin.getLogger().info("Reconnecting to database (" + reconnectAttempts + ") ...");
|
|
||||||
return getHighestID(reconnectAttempts - 1);
|
|
||||||
} else return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
|
|
||||||
@ -137,16 +116,7 @@ public abstract class Database {
|
|||||||
*
|
*
|
||||||
* @param shop Shop to remove
|
* @param shop Shop to remove
|
||||||
*/
|
*/
|
||||||
public void removeShop(Shop shop, int reconnectAttempts) {
|
public void removeShop(Shop shop) {
|
||||||
if (!isConnected()) {
|
|
||||||
if (reconnectAttempts > 0) {
|
|
||||||
connection = getConnection();
|
|
||||||
plugin.getLogger().info("Reconnecting to database (" + reconnectAttempts + ") ...");
|
|
||||||
removeShop(shop, reconnectAttempts - 1);
|
|
||||||
return;
|
|
||||||
} else return;
|
|
||||||
}
|
|
||||||
|
|
||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -165,15 +135,7 @@ public abstract class Database {
|
|||||||
* @param shopInfo What to get
|
* @param shopInfo What to get
|
||||||
* @return Value you wanted to get. This needs to be casted to the right type!
|
* @return Value you wanted to get. This needs to be casted to the right type!
|
||||||
*/
|
*/
|
||||||
public Object get(int id, ShopInfo shopInfo, int reconnectAttempts) {
|
public Object get(int id, ShopInfo shopInfo) {
|
||||||
if (!isConnected()) {
|
|
||||||
if (reconnectAttempts > 0) {
|
|
||||||
connection = getConnection();
|
|
||||||
plugin.getLogger().info("Reconnecting to database (" + reconnectAttempts + ") ...");
|
|
||||||
return get(id, shopInfo, reconnectAttempts - 1);
|
|
||||||
} else return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
|
|
||||||
@ -186,17 +148,17 @@ public abstract class Database {
|
|||||||
|
|
||||||
switch (shopInfo) {
|
switch (shopInfo) {
|
||||||
case SHOP:
|
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)
|
if (shop != null)
|
||||||
return shop;
|
return shop;
|
||||||
else {
|
else {
|
||||||
return new Shop(id, plugin,
|
return new Shop(id, plugin,
|
||||||
(OfflinePlayer) get(id, ShopInfo.VENDOR, attempts),
|
(OfflinePlayer) get(id, ShopInfo.VENDOR),
|
||||||
(ItemStack) get(id, ShopInfo.PRODUCT, attempts),
|
(ItemStack) get(id, ShopInfo.PRODUCT),
|
||||||
(Location) get(id, ShopInfo.LOCATION, attempts),
|
(Location) get(id, ShopInfo.LOCATION),
|
||||||
(double) get(id, ShopInfo.BUYPRICE, attempts),
|
(double) get(id, ShopInfo.BUYPRICE),
|
||||||
(double) get(id, ShopInfo.SELLPRICE, attempts),
|
(double) get(id, ShopInfo.SELLPRICE),
|
||||||
(ShopType) get(id, ShopInfo.SHOPTYPE, attempts));
|
(ShopType) get(id, ShopInfo.SHOPTYPE));
|
||||||
}
|
}
|
||||||
case VENDOR:
|
case VENDOR:
|
||||||
return Bukkit.getOfflinePlayer(UUID.fromString(rs.getString("vendor")));
|
return Bukkit.getOfflinePlayer(UUID.fromString(rs.getString("vendor")));
|
||||||
@ -211,7 +173,7 @@ public abstract class Database {
|
|||||||
case Z:
|
case Z:
|
||||||
return rs.getInt("z");
|
return rs.getInt("z");
|
||||||
case LOCATION:
|
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:
|
case BUYPRICE:
|
||||||
return rs.getDouble("buyprice");
|
return rs.getDouble("buyprice");
|
||||||
case SELLPRICE:
|
case SELLPRICE:
|
||||||
@ -222,14 +184,14 @@ public abstract class Database {
|
|||||||
if (shoptype.equals("INFINITE")) {
|
if (shoptype.equals("INFINITE")) {
|
||||||
|
|
||||||
Shop newShop = new Shop(id, plugin,
|
Shop newShop = new Shop(id, plugin,
|
||||||
(OfflinePlayer) get(id, ShopInfo.VENDOR, attempts),
|
(OfflinePlayer) get(id, ShopInfo.VENDOR),
|
||||||
(ItemStack) get(id, ShopInfo.PRODUCT, attempts),
|
(ItemStack) get(id, ShopInfo.PRODUCT),
|
||||||
(Location) get(id, ShopInfo.LOCATION, attempts),
|
(Location) get(id, ShopInfo.LOCATION),
|
||||||
(double) get(id, ShopInfo.BUYPRICE, attempts),
|
(double) get(id, ShopInfo.BUYPRICE),
|
||||||
(double) get(id, ShopInfo.SELLPRICE, attempts),
|
(double) get(id, ShopInfo.SELLPRICE),
|
||||||
ShopType.ADMIN);
|
ShopType.ADMIN);
|
||||||
|
|
||||||
addShop(newShop, attempts);
|
addShop(newShop);
|
||||||
|
|
||||||
return ShopType.ADMIN;
|
return ShopType.ADMIN;
|
||||||
}
|
}
|
||||||
@ -251,16 +213,7 @@ public abstract class Database {
|
|||||||
* Adds a shop to the database
|
* Adds a shop to the database
|
||||||
* @param shop Shop to add
|
* @param shop Shop to add
|
||||||
*/
|
*/
|
||||||
public void addShop(Shop shop, int reconnectAttempts) {
|
public void addShop(Shop shop) {
|
||||||
if (!isConnected()) {
|
|
||||||
if (reconnectAttempts > 0) {
|
|
||||||
connection = getConnection();
|
|
||||||
plugin.getLogger().info("Reconnecting to database (" + reconnectAttempts + ") ...");
|
|
||||||
addShop(shop, reconnectAttempts - 1);
|
|
||||||
return;
|
|
||||||
} else return;
|
|
||||||
}
|
|
||||||
|
|
||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -20,7 +20,7 @@ public class MySQL extends Database {
|
|||||||
|
|
||||||
Class.forName("com.mysql.jdbc.Driver");
|
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 + "\"");
|
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);
|
connection = DriverManager.getConnection(connectUrl, plugin.getShopChestConfig().database_mysql_username, plugin.getShopChestConfig().database_mysql_password);
|
||||||
|
@ -82,7 +82,7 @@ public class ShopUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (addToDatabase)
|
if (addToDatabase)
|
||||||
plugin.getShopDatabase().addShop(shop, plugin.getShopChestConfig().database_reconnect_attempts);
|
plugin.getShopDatabase().addShop(shop);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,7 +109,7 @@ public class ShopUtils {
|
|||||||
shop.removeHologram();
|
shop.removeHologram();
|
||||||
|
|
||||||
if (removeFromDatabase)
|
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;
|
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 {
|
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);
|
addShop(shop, false);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -14,6 +14,9 @@ language-file: "en_US"
|
|||||||
# Set whether the floating shop items on top of the chest should be shown
|
# Set whether the floating shop items on top of the chest should be shown
|
||||||
show-shop-items: true
|
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
|
# 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
|
# 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
|
# 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!
|
# Either use 'SQLite' or 'MySQL'. Otherwise you will break the plugin!
|
||||||
type: "SQLite"
|
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...
|
# If the specified type is 'MySQL', here you configure the...
|
||||||
# (You can leave this empty if you're using SQLite)
|
# (You can leave this empty if you're using SQLite)
|
||||||
mysql:
|
mysql:
|
||||||
|
Loading…
Reference in New Issue
Block a user