1
0
mirror of https://e.coding.net/circlecloud/QuickShop.git synced 2024-11-22 01:58:54 +00:00

fix: 修复部分包未导入

This commit is contained in:
502647092 2018-02-01 14:26:48 +08:00
parent de3fa516e1
commit a11686a839
3 changed files with 126 additions and 92 deletions

View File

@ -4,15 +4,19 @@ import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
public class MySQLCore implements DatabaseCore { public class MySQLCore implements DatabaseCore {
private String url; private String url;
/** The connection properties... user, pass, autoReconnect.. */ /**
* The connection properties... user, pass, autoReconnect..
*/
private Properties info; private Properties info;
private static final int MAX_CONNECTIONS = 8; private static final int MAX_CONNECTIONS = 8;
private static final List<Connection> pool = Collections.synchronizedList(new ArrayList<Connection>()); private static final List<Connection> POOL = Collections.synchronizedList(new ArrayList<Connection>());
public MySQLCore(String host, String user, String pass, String database, String port) { public MySQLCore(String host, String user, String pass, String database, String port) {
info = new Properties(); info = new Properties();
@ -22,18 +26,18 @@ public class MySQLCore implements DatabaseCore {
info.put("useUnicode", "true"); info.put("useUnicode", "true");
info.put("characterEncoding", "utf8"); info.put("characterEncoding", "utf8");
this.url = "jdbc:mysql://" + host + ":" + port + "/" + database; this.url = "jdbc:mysql://" + host + ":" + port + "/" + database;
for (int i = 0; i < MAX_CONNECTIONS; i++) for (int i = 0; i < MAX_CONNECTIONS; i++) {POOL.add(null);}
pool.add(null);
} }
/** /**
* Gets the database connection for executing queries on. * Gets the database connection for executing queries on.
* *
* @return The database connection * @return The database connection
*/ */
@Override
public Connection getConnection() { public Connection getConnection() {
for (int i = 0; i < MAX_CONNECTIONS; i++) { for (int i = 0; i < MAX_CONNECTIONS; i++) {
Connection connection = pool.get(i); Connection connection = POOL.get(i);
try { try {
// If we have a current connection, fetch it // If we have a current connection, fetch it
if (connection != null && !connection.isClosed()) { if (connection != null && !connection.isClosed()) {
@ -43,7 +47,7 @@ public class MySQLCore implements DatabaseCore {
// Else, it is invalid, so we return another connection. // Else, it is invalid, so we return another connection.
} }
connection = DriverManager.getConnection(this.url, info); connection = DriverManager.getConnection(this.url, info);
pool.set(i, connection); POOL.set(i, connection);
return connection; return connection;
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -1,5 +1,6 @@
package org.maxgamer.QuickShop.Listeners; package org.maxgamer.QuickShop.Listeners;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -7,12 +8,10 @@ import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.maxgamer.QuickShop.QuickShop; import org.maxgamer.QuickShop.QuickShop;
/** /**
*
* @author Netherfoam * @author Netherfoam
*
*/ */
public class ChatListener implements Listener { public class ChatListener implements Listener {
QuickShop plugin; private QuickShop plugin;
public ChatListener(final QuickShop plugin) { public ChatListener(final QuickShop plugin) {
this.plugin = plugin; this.plugin = plugin;
@ -23,12 +22,7 @@ public class ChatListener implements Listener {
if (!plugin.getShopManager().getActions().containsKey(e.getPlayer().getName())) { if (!plugin.getShopManager().getActions().containsKey(e.getPlayer().getName())) {
return; return;
} }
e.setCancelled(true); e.setCancelled(true);
Bukkit.getScheduler().runTask(plugin, new Runnable() { Bukkit.getScheduler().runTask(plugin, () -> plugin.getShopManager().handleChat(e.getPlayer(), e.getMessage()));
@Override
public void run() {
plugin.getShopManager().handleChat(e.getPlayer(), e.getMessage());
}
});
} }
} }

View File

@ -1,6 +1,18 @@
package org.maxgamer.QuickShop.Shop; package org.maxgamer.QuickShop.Shop;
import org.bukkit.*; import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState; import org.bukkit.block.BlockState;
@ -14,12 +26,6 @@ import org.maxgamer.QuickShop.QuickShop;
import org.maxgamer.QuickShop.Util.MsgUtil; import org.maxgamer.QuickShop.Util.MsgUtil;
import org.maxgamer.QuickShop.Util.Util; import org.maxgamer.QuickShop.Util.Util;
import java.util.HashMap;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
public class ShopManager { public class ShopManager {
private final Map<String, Info> actions = new ConcurrentHashMap<String, Info>(); private final Map<String, Info> actions = new ConcurrentHashMap<String, Info>();
@ -35,9 +41,9 @@ public class ShopManager {
* shop. * shop.
* *
* @param p * @param p
* The player to check * The player to check
* @param b * @param b
* The block to check * The block to check
* @return True if they're allowed to place a shop there. * @return True if they're allowed to place a shop there.
*/ */
public boolean canBuildShop(final Player p, final Block b, final BlockFace bf) { public boolean canBuildShop(final Player p, final Block b, final BlockFace bf) {
@ -61,7 +67,11 @@ public class ShopManager {
} }
/* 修复其他插件调用产生的报错... */ /* 修复其他插件调用产生的报错... */
try { try {
final PlayerInteractEvent pie = new PlayerInteractEvent(p, Action.RIGHT_CLICK_BLOCK, new ItemStack(Material.AIR), b, bf); // PIE = PlayerInteractEvent - What else? final PlayerInteractEvent pie = new PlayerInteractEvent(p,
Action.RIGHT_CLICK_BLOCK,
new ItemStack(Material.AIR),
b,
bf); // PIE = PlayerInteractEvent - What else?
Bukkit.getPluginManager().callEvent(pie); Bukkit.getPluginManager().callEvent(pie);
pie.getPlayer().closeInventory(); // If the player has chat open, this will close their chat. pie.getPlayer().closeInventory(); // If the player has chat open, this will close their chat.
if (pie.isCancelled()) { return false; } if (pie.isCancelled()) { return false; }
@ -95,40 +105,40 @@ public class ShopManager {
} }
public void createShop(final Shop shop) { public void createShop(final Shop shop) {
final Location loc = shop.getLocation(); final Location loc = shop.getLocation();
final ItemStack item = shop.getItem(); final ItemStack item = shop.getItem();
final String serializeItem = Util.serialize(item); final String serializeItem = Util.serialize(item);
final String worldName = loc.getWorld().getName(); final String worldName = loc.getWorld().getName();
final int x = loc.getBlockX(); final int x = loc.getBlockX();
final int y = loc.getBlockY(); final int y = loc.getBlockY();
final int z = loc.getBlockZ(); final int z = loc.getBlockZ();
// Async database execute // Async database execute
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() { plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
// Write it to the database // Write it to the database
final String q = "INSERT INTO shops (owner, price, itemConfig, x, y, z, world, unlimited, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"; final String q = "INSERT INTO shops (owner, price, itemConfig, x, y, z, world, unlimited, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
plugin.getDB().execute(q, plugin.getDB().execute(q,
shop.getOwner(), shop.getOwner(),
shop.getPrice(), shop.getPrice(),
serializeItem, serializeItem,
x, x,
y, y,
z, z,
worldName, worldName,
(shop.isUnlimited() ? 1 : 0), (shop.isUnlimited() ? 1 : 0),
shop.getShopType().toID()); shop.getShopType().toID());
} catch (final Exception e) { } catch (final Exception e) {
plugin.getLogger().warning("无法保存商店到数据库! 下次重启商店将会消失!"); plugin.getLogger().warning("无法保存商店到数据库! 下次重启商店将会消失!");
plugin.getLogger().warning("错误信息: " + e.getMessage()); plugin.getLogger().warning("错误信息: " + e.getMessage());
e.printStackTrace(); e.printStackTrace();
} }
} }
}); });
// Add it to the world // Add it to the world
addShop(worldName, shop); addShop(worldName, shop);
} }
public String format(final double d) { public String format(final double d) {
return plugin.getEcon().format(d); return plugin.getEcon().format(d);
@ -136,7 +146,7 @@ public class ShopManager {
/** /**
* @return Returns the HashMap<Player name, shopInfo>. Info contains what * @return Returns the HashMap<Player name, shopInfo>. Info contains what
* their last question etc was. * their last question etc was.
*/ */
public Map<String, Info> getActions() { public Map<String, Info> getActions() {
return this.actions; return this.actions;
@ -150,7 +160,7 @@ public class ShopManager {
* Gets a shop in a specific location * Gets a shop in a specific location
* *
* @param loc * @param loc
* The location to get the shop from * The location to get the shop from
* @return The shop at that location * @return The shop at that location
*/ */
public Shop getShop(final Location loc) { public Shop getShop(final Location loc) {
@ -184,8 +194,8 @@ public class ShopManager {
* Returns a hashmap of Shops * Returns a hashmap of Shops
* *
* @param c * @param c
* The chunk to search. Referencing doesn't matter, only * The chunk to search. Referencing doesn't matter, only
* coordinates and world are used. * coordinates and world are used.
* @return * @return
*/ */
public HashMap<Location, Shop> getShops(final Chunk c) { public HashMap<Location, Shop> getShops(final Chunk c) {
@ -200,8 +210,8 @@ public class ShopManager {
* Returns a hashmap of Chunk -> Shop * Returns a hashmap of Chunk -> Shop
* *
* @param world * @param world
* The name of the world (case sensitive) to get the list of * The name of the world (case sensitive) to get the list of
* shops from * shops from
* @return a hashmap of Chunk -> Shop * @return a hashmap of Chunk -> Shop
*/ */
public HashMap<ShopChunk, HashMap<Location, Shop>> getShops(final String world) { public HashMap<ShopChunk, HashMap<Location, Shop>> getShops(final String world) {
@ -220,7 +230,8 @@ public class ShopManager {
final Map<String, Info> actions = getActions(); final Map<String, Info> actions = getActions();
// They wanted to do something. // They wanted to do something.
final Info info = actions.remove(p.getName()); final Info info = actions.remove(p.getName());
if (info == null) { return; // multithreaded means this can happen if (info == null) {
return; // multithreaded means this can happen
} }
/* Creation handling */ /* Creation handling */
if (info.getAction() == ShopAction.CREATE) { if (info.getAction() == ShopAction.CREATE) {
@ -258,9 +269,9 @@ public class ShopManager {
* from the database. Do not use this method to create a shop. * from the database. Do not use this method to create a shop.
* *
* @param world * @param world
* The world the shop is in * The world the shop is in
* @param shop * @param shop
* The shop to load * The shop to load
*/ */
public void loadShop(final String world, final Shop shop) { public void loadShop(final String world, final Shop shop) {
this.addShop(world, shop); this.addShop(world, shop);
@ -271,7 +282,7 @@ public class ShopManager {
* REQUIRES * the world to be loaded * REQUIRES * the world to be loaded
* *
* @param shop * @param shop
* The shop to remove * The shop to remove
*/ */
public void removeShop(final Shop shop) { public void removeShop(final Shop shop) {
final Location loc = shop.getLocation(); final Location loc = shop.getLocation();
@ -289,9 +300,9 @@ public class ShopManager {
* loaded * loaded
* *
* @param world * @param world
* The name of the world * The name of the world
* @param shop * @param shop
* The shop to add * The shop to add
*/ */
private void addShop(final String world, final Shop shop) { private void addShop(final String world, final Shop shop) {
HashMap<ShopChunk, HashMap<Location, Shop>> inWorld = this.getShops().get(world); HashMap<ShopChunk, HashMap<Location, Shop>> inWorld = this.getShops().get(world);
@ -342,17 +353,21 @@ public class ShopManager {
// Tries to check their balance nicely to see if // Tries to check their balance nicely to see if
// they can afford it. // they can afford it.
if (plugin.getEcon().getBalance(shop.getOwner()) < amount * shop.getPrice()) { if (plugin.getEcon().getBalance(shop.getOwner()) < amount * shop.getPrice()) {
p.sendMessage(MsgUtil.p("the-owner-cant-afford-to-buy-from-you", format(amount * shop.getPrice()), format(plugin.getEcon().getBalance(shop.getOwner())))); p.sendMessage(MsgUtil.p("the-owner-cant-afford-to-buy-from-you",
format(amount * shop.getPrice()),
format(plugin.getEcon().getBalance(shop.getOwner()))));
return; return;
} }
final ShopPurchaseEvent e = new ShopPurchaseEvent(shop, p, amount); final ShopPurchaseEvent e = new ShopPurchaseEvent(shop, p, amount);
Bukkit.getPluginManager().callEvent(e); Bukkit.getPluginManager().callEvent(e);
if (e.isCancelled()) { if (e.isCancelled()) {
return; // Cancelled return; // Cancelled
} }
// Check for plugins faking econ.has(amount) // Check for plugins faking econ.has(amount)
if (!plugin.getEcon().withdraw(shop.getOwner(), total)) { if (!plugin.getEcon().withdraw(shop.getOwner(), total)) {
p.sendMessage(MsgUtil.p("the-owner-cant-afford-to-buy-from-you", format(amount * shop.getPrice()), format(plugin.getEcon().getBalance(shop.getOwner())))); p.sendMessage(MsgUtil.p("the-owner-cant-afford-to-buy-from-you",
format(amount * shop.getPrice()),
format(plugin.getEcon().getBalance(shop.getOwner()))));
return; return;
} }
if (tax != 0) { if (tax != 0) {
@ -364,7 +379,10 @@ public class ShopManager {
// Notify the owner of the purchase. // Notify the owner of the purchase.
String msg = MsgUtil.p("player-sold-to-your-store", p.getName(), "" + amount, shop.getDataName()); String msg = MsgUtil.p("player-sold-to-your-store", p.getName(), "" + amount, shop.getDataName());
if (space == amount) { if (space == amount) {
msg += "\n" + MsgUtil.p("shop-out-of-space", "" + shop.getLocation().getBlockX(), "" + shop.getLocation().getBlockY(), "" + shop.getLocation().getBlockZ()); msg += "\n" + MsgUtil.p("shop-out-of-space",
"" + shop.getLocation().getBlockX(),
"" + shop.getLocation().getBlockY(),
"" + shop.getLocation().getBlockZ());
} }
MsgUtil.send(shop.getOwner(), msg); MsgUtil.send(shop.getOwner(), msg);
} }
@ -426,7 +444,13 @@ public class ShopManager {
createShop(shop); createShop(shop);
p.sendMessage(MsgUtil.p("success-created-shop")); p.sendMessage(MsgUtil.p("success-created-shop"));
final Location loc = shop.getLocation(); final Location loc = shop.getLocation();
plugin.log(String.format("玩家: %s 创建了一个 %s 商店 在 (%s - %s, %s, %s)", p.getName(), shop.getDataName(), loc.getWorld().getName(), loc.getX(), loc.getY(), loc.getZ())); plugin.log(String.format("玩家: %s 创建了一个 %s 商店 在 (%s - %s, %s, %s)",
p.getName(),
shop.getDataName(),
loc.getWorld().getName(),
loc.getX(),
loc.getY(),
loc.getZ()));
if (!plugin.getConfig().getBoolean("shop.lock")) { if (!plugin.getConfig().getBoolean("shop.lock")) {
// Warn them if they haven't been warned since // Warn them if they haven't been warned since
// reboot // reboot
@ -462,8 +486,7 @@ public class ShopManager {
} }
} }
} }
/* They didn't enter a number. */ /* They didn't enter a number. */ catch (final NumberFormatException ex) {
catch (final NumberFormatException ex) {
p.sendMessage(MsgUtil.p("shop-creation-cancelled")); p.sendMessage(MsgUtil.p("shop-creation-cancelled"));
} }
} }
@ -485,21 +508,25 @@ public class ShopManager {
// Check their balance. Works with *most* economy // Check their balance. Works with *most* economy
// plugins* // plugins*
if (plugin.getEcon().getBalance(p.getName()) < amount * shop.getPrice()) { if (plugin.getEcon().getBalance(p.getName()) < amount * shop.getPrice()) {
p.sendMessage(MsgUtil.p("you-cant-afford-to-buy", format(amount * shop.getPrice()), format(plugin.getEcon().getBalance(p.getName())))); p.sendMessage(MsgUtil.p("you-cant-afford-to-buy",
format(amount * shop.getPrice()),
format(plugin.getEcon().getBalance(p.getName()))));
return; return;
} }
final ShopPurchaseEvent e = new ShopPurchaseEvent(shop, p, amount); final ShopPurchaseEvent e = new ShopPurchaseEvent(shop, p, amount);
Bukkit.getPluginManager().callEvent(e); Bukkit.getPluginManager().callEvent(e);
if (e.isCancelled()) { if (e.isCancelled()) {
return; // Cancelled return; // Cancelled
} }
// Don't tax them if they're purchasing from // Don't tax them if they're purchasing from
// themselves. // themselves.
// Do charge an amount of tax though. // Do charge an amount of tax though.
final double tax = plugin.getConfigManager().getTax(); final double tax = plugin.getConfigManager().getTax();
final double total = amount * shop.getPrice(); final double total = amount * shop.getPrice();
if (!plugin.getEcon().withdraw(p.getName(), total)) { if (!plugin.getEcon().withdraw(p.getName(), total)) {
p.sendMessage(MsgUtil.p("you-cant-afford-to-buy", format(amount * shop.getPrice()), format(plugin.getEcon().getBalance(p.getName())))); p.sendMessage(MsgUtil.p("you-cant-afford-to-buy",
format(amount * shop.getPrice()),
format(plugin.getEcon().getBalance(p.getName()))));
return; return;
} }
if (!shop.isUnlimited() || plugin.getConfig().getBoolean("shop.pay-unlimited-shop-owners")) { if (!shop.isUnlimited() || plugin.getConfig().getBoolean("shop.pay-unlimited-shop-owners")) {
@ -512,13 +539,21 @@ public class ShopManager {
if (plugin.getConfigManager().isShowTax()) { if (plugin.getConfigManager().isShowTax()) {
String msg = MsgUtil.p("player-bought-from-your-store-tax", p.getName(), "" + amount, shop.getDataName(), Util.format((tax * total))); String msg = MsgUtil.p("player-bought-from-your-store-tax", p.getName(), "" + amount, shop.getDataName(), Util.format((tax * total)));
if (stock == amount) { if (stock == amount) {
msg += "\n" + MsgUtil.p("shop-out-of-stock", "" + shop.getLocation().getBlockX(), "" + shop.getLocation().getBlockY(), "" + shop.getLocation().getBlockZ(), shop.getDataName()); msg += "\n" + MsgUtil.p("shop-out-of-stock",
"" + shop.getLocation().getBlockX(),
"" + shop.getLocation().getBlockY(),
"" + shop.getLocation().getBlockZ(),
shop.getDataName());
} }
MsgUtil.send(shop.getOwner(), msg); MsgUtil.send(shop.getOwner(), msg);
} else { } else {
String msg = MsgUtil.p("player-bought-from-your-store", p.getName(), "" + amount, shop.getDataName()); String msg = MsgUtil.p("player-bought-from-your-store", p.getName(), "" + amount, shop.getDataName());
if (stock == amount) { if (stock == amount) {
msg += "\n" + MsgUtil.p("shop-out-of-stock", "" + shop.getLocation().getBlockX(), "" + shop.getLocation().getBlockY(), "" + shop.getLocation().getBlockZ(), shop.getDataName()); msg += "\n" + MsgUtil.p("shop-out-of-stock",
"" + shop.getLocation().getBlockX(),
"" + shop.getLocation().getBlockY(),
"" + shop.getLocation().getBlockZ(),
shop.getDataName());
} }
MsgUtil.send(shop.getOwner(), msg); MsgUtil.send(shop.getOwner(), msg);
} }
@ -582,7 +617,8 @@ public class ShopManager {
} }
shops = chunks.next().values().iterator(); shops = chunks.next().values().iterator();
} }
if (!shops.hasNext()) { return this.next(); // Skip to the next one (Empty iterator?) if (!shops.hasNext()) {
return this.next(); // Skip to the next one (Empty iterator?)
} }
current = shops.next(); current = shops.next();
return current; return current;