From 62bfb22becc402a19b8053cbf167de4195273c7b Mon Sep 17 00:00:00 2001 From: 502647092 Date: Sat, 12 Dec 2015 11:17:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BB=A3=E7=A0=81=20?= =?UTF-8?q?=E6=B8=85=E7=90=86=E6=97=A0=E6=95=88=E7=9A=84return...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- .../QuickShop/Command/CommandEmpty.java | 3 +- .../QuickShop/Database/SQLiteCore.java | 112 +++++++-------- .../QuickShop/Listeners/BlockListener.java | 3 +- .../QuickShop/Listeners/LockListener.java | 6 +- .../org/maxgamer/QuickShop/QuickShop.java | 5 +- .../QuickShop/Shop/ContainerShop.java | 8 +- .../maxgamer/QuickShop/Shop/ShopChunk.java | 37 +++-- .../maxgamer/QuickShop/Shop/ShopManager.java | 128 +++++++++--------- .../org/maxgamer/QuickShop/Util/Util.java | 8 +- 9 files changed, 149 insertions(+), 161 deletions(-) diff --git a/src/main/java/org/maxgamer/QuickShop/Command/CommandEmpty.java b/src/main/java/org/maxgamer/QuickShop/Command/CommandEmpty.java index ce8a334..ad5c1ec 100644 --- a/src/main/java/org/maxgamer/QuickShop/Command/CommandEmpty.java +++ b/src/main/java/org/maxgamer/QuickShop/Command/CommandEmpty.java @@ -34,11 +34,10 @@ public class CommandEmpty extends BaseCommand { final ContainerShop cs = (ContainerShop) shop; cs.getInventory().clear(); sender.sendMessage(MsgUtil.p("empty-success")); - return; } else { sender.sendMessage(MsgUtil.p("not-looking-at-shop")); - return; } + return; } } sender.sendMessage(MsgUtil.p("not-looking-at-shop")); diff --git a/src/main/java/org/maxgamer/QuickShop/Database/SQLiteCore.java b/src/main/java/org/maxgamer/QuickShop/Database/SQLiteCore.java index 2038f8d..6310be3 100644 --- a/src/main/java/org/maxgamer/QuickShop/Database/SQLiteCore.java +++ b/src/main/java/org/maxgamer/QuickShop/Database/SQLiteCore.java @@ -10,64 +10,17 @@ import java.util.LinkedList; public class SQLiteCore implements DatabaseCore { private Connection connection; - private File dbFile; + private final File dbFile; private volatile Thread watcher; private volatile LinkedList queue = new LinkedList(); - public SQLiteCore(File dbFile) { + public SQLiteCore(final File dbFile) { this.dbFile = dbFile; } - /** - * Gets the database connection for executing queries on. - * - * @return The database connection - */ - public Connection getConnection() { - try { - // If we have a current connection, fetch it - if (this.connection != null && !this.connection.isClosed()) { - return this.connection; - } - } catch (SQLException e) { - e.printStackTrace(); - } - if (this.dbFile.exists()) { - // So we need a new connection - try { - Class.forName("org.sqlite.JDBC"); - this.connection = DriverManager.getConnection("jdbc:sqlite:" + this.dbFile); - return this.connection; - } catch (ClassNotFoundException e) { - e.printStackTrace(); - return null; - } catch (SQLException e) { - e.printStackTrace(); - return null; - } - } else { - // So we need a new file too. - try { - // Create the file - this.dbFile.createNewFile(); - // Now we won't need a new file, just a connection. - // This will return that new connection. - return this.getConnection(); - } catch (IOException e) { - e.printStackTrace(); - return null; - } - } - } - @Override - public void queue(BufferStatement bs) { - synchronized (queue) { - queue.add(bs); - } - if (watcher == null || !watcher.isAlive()) { - startWatcher(); - } + public void close() { + flush(); } @Override @@ -79,19 +32,66 @@ public class SQLiteCore implements DatabaseCore { } synchronized (dbFile) { try { - PreparedStatement ps = bs.prepareStatement(getConnection()); + final PreparedStatement ps = bs.prepareStatement(getConnection()); ps.execute(); ps.close(); - } catch (SQLException e) { + } catch (final SQLException e) { e.printStackTrace(); } } } } + /** + * Gets the database connection for executing queries on. + * + * @return The database connection + */ @Override - public void close() { - flush(); + public Connection getConnection() { + try { + // If we have a current connection, fetch it + if (this.connection != null && !this.connection.isClosed()) { + return this.connection; + } + } catch (final SQLException e) { + e.printStackTrace(); + } + if (this.dbFile.exists()) { + // So we need a new connection + try { + Class.forName("org.sqlite.JDBC"); + this.connection = DriverManager.getConnection("jdbc:sqlite:" + this.dbFile); + return this.connection; + } catch (final ClassNotFoundException e) { + e.printStackTrace(); + return null; + } catch (final SQLException e) { + e.printStackTrace(); + return null; + } + } + // So we need a new file too. + try { + // Create the file + this.dbFile.createNewFile(); + // Now we won't need a new file, just a connection. + // This will return that new connection. + return this.getConnection(); + } catch (final IOException e) { + e.printStackTrace(); + return null; + } + } + + @Override + public void queue(final BufferStatement bs) { + synchronized (queue) { + queue.add(bs); + } + if (watcher == null || !watcher.isAlive()) { + startWatcher(); + } } private void startWatcher() { @@ -100,7 +100,7 @@ public class SQLiteCore implements DatabaseCore { public void run() { try { Thread.sleep(30000); - } catch (InterruptedException e) { + } catch (final InterruptedException e) { } flush(); } diff --git a/src/main/java/org/maxgamer/QuickShop/Listeners/BlockListener.java b/src/main/java/org/maxgamer/QuickShop/Listeners/BlockListener.java index 83f6e94..7d19182 100644 --- a/src/main/java/org/maxgamer/QuickShop/Listeners/BlockListener.java +++ b/src/main/java/org/maxgamer/QuickShop/Listeners/BlockListener.java @@ -61,9 +61,8 @@ public class BlockListener implements Listener { final Shop shop = getShopNextTo(b.getLocation()); if (shop == null) { return; - } else { - e.setCancelled(true); } + e.setCancelled(true); } } diff --git a/src/main/java/org/maxgamer/QuickShop/Listeners/LockListener.java b/src/main/java/org/maxgamer/QuickShop/Listeners/LockListener.java index a1d9b31..0b9acb3 100644 --- a/src/main/java/org/maxgamer/QuickShop/Listeners/LockListener.java +++ b/src/main/java/org/maxgamer/QuickShop/Listeners/LockListener.java @@ -50,9 +50,8 @@ public class LockListener implements Listener { final Shop shop = plugin.getShopManager().getShop(b.getLocation()); if (shop == null) { return; - } else { - e.setCancelled(true); } + e.setCancelled(true); } } @@ -131,9 +130,8 @@ public class LockListener implements Listener { c = Util.getSecondHalf(c); if (c == null) { return; // You didn't place a hopper on a shop. Meh. - } else { - shop = plugin.getShopManager().getShop(c.getLocation()); } + shop = plugin.getShopManager().getShop(c.getLocation()); if (shop == null) { return; } diff --git a/src/main/java/org/maxgamer/QuickShop/QuickShop.java b/src/main/java/org/maxgamer/QuickShop/QuickShop.java index bcb225a..722b03d 100644 --- a/src/main/java/org/maxgamer/QuickShop/QuickShop.java +++ b/src/main/java/org/maxgamer/QuickShop/QuickShop.java @@ -144,10 +144,9 @@ public class QuickShop extends JavaPlugin { getLogger().warning("卸载插件!!!"); this.getPluginLoader().disablePlugin(this); return false; - } else { - this.economy = new Economy(core); - return true; } + this.economy = new Economy(core); + return true; } /** diff --git a/src/main/java/org/maxgamer/QuickShop/Shop/ContainerShop.java b/src/main/java/org/maxgamer/QuickShop/Shop/ContainerShop.java index 73148ce..d981adf 100644 --- a/src/main/java/org/maxgamer/QuickShop/Shop/ContainerShop.java +++ b/src/main/java/org/maxgamer/QuickShop/Shop/ContainerShop.java @@ -420,13 +420,11 @@ public class ContainerShop implements Shop { // They're both buying or both selling => Not a double shop, // just two shops. return false; - } else { - // One is buying, one is selling. - return true; } - } else { - return false; + // One is buying, one is selling. + return true; } + return false; } @Override diff --git a/src/main/java/org/maxgamer/QuickShop/Shop/ShopChunk.java b/src/main/java/org/maxgamer/QuickShop/Shop/ShopChunk.java index 851cd9d..c413928 100644 --- a/src/main/java/org/maxgamer/QuickShop/Shop/ShopChunk.java +++ b/src/main/java/org/maxgamer/QuickShop/Shop/ShopChunk.java @@ -1,20 +1,33 @@ package org.maxgamer.QuickShop.Shop; public class ShopChunk { - private String world; - private int x; - private int z; + private final String world; + private final int x; + private final int z; private int hash = 0; - public ShopChunk(String world, int x, int z) { + public ShopChunk(final String world, final int x, final int z) { this.world = world; this.x = x; this.z = z; - this.hash = this.x * this.z; // We don't need to use the world's hash, + this.hash = this.x * this.z; // We don't need to use the world's hash, // as these are seperated by world in // memory } + @Override + public boolean equals(final Object obj) { + if (obj.getClass() != this.getClass()) { + return false; + } + final ShopChunk shopChunk = (ShopChunk) obj; + return (this.getWorld().equals(shopChunk.getWorld()) && this.getX() == shopChunk.getX() && this.getZ() == shopChunk.getZ()); + } + + public String getWorld() { + return this.world; + } + public int getX() { return this.x; } @@ -23,20 +36,6 @@ public class ShopChunk { return this.z; } - public String getWorld() { - return this.world; - } - - @Override - public boolean equals(Object obj) { - if (obj.getClass() != this.getClass()) { - return false; - } else { - ShopChunk shopChunk = (ShopChunk) obj; - return (this.getWorld().equals(shopChunk.getWorld()) && this.getX() == shopChunk.getX() && this.getZ() == shopChunk.getZ()); - } - } - @Override public int hashCode() { return hash; diff --git a/src/main/java/org/maxgamer/QuickShop/Shop/ShopManager.java b/src/main/java/org/maxgamer/QuickShop/Shop/ShopManager.java index 3a62e9a..9259093 100644 --- a/src/main/java/org/maxgamer/QuickShop/Shop/ShopManager.java +++ b/src/main/java/org/maxgamer/QuickShop/Shop/ShopManager.java @@ -25,9 +25,71 @@ import org.maxgamer.QuickShop.Util.MsgUtil; import org.maxgamer.QuickShop.Util.Util; public class ShopManager { - private final HashMap actions = new HashMap(); + public class ShopIterator implements Iterator { + private Iterator> chunks; + private Shop current; + private Iterator shops; + private final Iterator>> worlds; + public ShopIterator() { + worlds = getShops().values().iterator(); + } + + /** + * Returns true if there is still more shops to iterate over. + */ + @Override + public boolean hasNext() { + if (shops == null || !shops.hasNext()) { + if (chunks == null || !chunks.hasNext()) { + if (!worlds.hasNext()) { + return false; + } + chunks = worlds.next().values().iterator(); + return hasNext(); + } + shops = chunks.next().values().iterator(); + return hasNext(); + } + return true; + } + + /** + * Fetches the next shop. Throws NoSuchElementException if there are no + * more shops. + */ + @Override + public Shop next() { + if (shops == null || !shops.hasNext()) { + if (chunks == null || !chunks.hasNext()) { + if (!worlds.hasNext()) { + throw new NoSuchElementException("No more shops to iterate over!"); + } + chunks = worlds.next().values().iterator(); + } + shops = chunks.next().values().iterator(); + } + if (!shops.hasNext()) { + return this.next(); // Skip to the next one (Empty iterator?) + } + current = shops.next(); + return current; + } + + /** + * Removes the current shop. This method will delete the shop from + * memory and the database. + */ + @Override + public void remove() { + current.delete(false); + shops.remove(); + } + } + + private final HashMap actions = new HashMap(); private final QuickShop plugin; + private final HashMap>> shops = new HashMap>>(); public ShopManager(final QuickShop plugin) { @@ -552,68 +614,4 @@ public class ShopManager { // Put the shop in its location in the chunk list. inChunk.put(shop.getLocation(), shop); } - - public class ShopIterator implements Iterator { - private Iterator> chunks; - private Shop current; - private Iterator shops; - private final Iterator>> worlds; - - public ShopIterator() { - worlds = getShops().values().iterator(); - } - - /** - * Returns true if there is still more shops to iterate over. - */ - @Override - public boolean hasNext() { - if (shops == null || !shops.hasNext()) { - if (chunks == null || !chunks.hasNext()) { - if (!worlds.hasNext()) { - return false; - } else { - chunks = worlds.next().values().iterator(); - return hasNext(); - } - } else { - shops = chunks.next().values().iterator(); - return hasNext(); - } - } - return true; - } - - /** - * Fetches the next shop. Throws NoSuchElementException if there are no - * more shops. - */ - @Override - public Shop next() { - if (shops == null || !shops.hasNext()) { - if (chunks == null || !chunks.hasNext()) { - if (!worlds.hasNext()) { - throw new NoSuchElementException("No more shops to iterate over!"); - } - chunks = worlds.next().values().iterator(); - } - shops = chunks.next().values().iterator(); - } - if (!shops.hasNext()) { - return this.next(); // Skip to the next one (Empty iterator?) - } - current = shops.next(); - return current; - } - - /** - * Removes the current shop. This method will delete the shop from - * memory and the database. - */ - @Override - public void remove() { - current.delete(false); - shops.remove(); - } - } } \ No newline at end of file diff --git a/src/main/java/org/maxgamer/QuickShop/Util/Util.java b/src/main/java/org/maxgamer/QuickShop/Util/Util.java index f044f7f..027e90a 100644 --- a/src/main/java/org/maxgamer/QuickShop/Util/Util.java +++ b/src/main/java/org/maxgamer/QuickShop/Util/Util.java @@ -117,9 +117,8 @@ public class Util { public static String firstUppercase(final String string) { if (string.length() > 1) { return Character.toUpperCase(string.charAt(0)) + string.substring(1).toLowerCase(); - } else { - return string.toUpperCase(); } + return string.toUpperCase(); } /** @@ -413,10 +412,9 @@ public class Util { if (loc.getWorld().isChunkLoaded(x, z)) { // System.out.println("Chunk is loaded " + x + ", " + z); return true; - } else { - // System.out.println("Chunk is NOT loaded " + x + ", " + z); - return false; } + // System.out.println("Chunk is NOT loaded " + x + ", " + z); + return false; } /**