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

整理代码 清理无效的return...

Signed-off-by: 502647092 <jtb1@163.com>
This commit is contained in:
502647092 2015-12-12 11:17:35 +08:00
parent fba0fbab53
commit 62bfb22bec
9 changed files with 149 additions and 161 deletions

View File

@ -34,11 +34,10 @@ public class CommandEmpty extends BaseCommand {
final ContainerShop cs = (ContainerShop) shop; final ContainerShop cs = (ContainerShop) shop;
cs.getInventory().clear(); cs.getInventory().clear();
sender.sendMessage(MsgUtil.p("empty-success")); sender.sendMessage(MsgUtil.p("empty-success"));
return;
} else { } else {
sender.sendMessage(MsgUtil.p("not-looking-at-shop")); sender.sendMessage(MsgUtil.p("not-looking-at-shop"));
return;
} }
return;
} }
} }
sender.sendMessage(MsgUtil.p("not-looking-at-shop")); sender.sendMessage(MsgUtil.p("not-looking-at-shop"));

View File

@ -10,64 +10,17 @@ import java.util.LinkedList;
public class SQLiteCore implements DatabaseCore { public class SQLiteCore implements DatabaseCore {
private Connection connection; private Connection connection;
private File dbFile; private final File dbFile;
private volatile Thread watcher; private volatile Thread watcher;
private volatile LinkedList<BufferStatement> queue = new LinkedList<BufferStatement>(); private volatile LinkedList<BufferStatement> queue = new LinkedList<BufferStatement>();
public SQLiteCore(File dbFile) { public SQLiteCore(final File dbFile) {
this.dbFile = 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 @Override
public void queue(BufferStatement bs) { public void close() {
synchronized (queue) { flush();
queue.add(bs);
}
if (watcher == null || !watcher.isAlive()) {
startWatcher();
}
} }
@Override @Override
@ -79,19 +32,66 @@ public class SQLiteCore implements DatabaseCore {
} }
synchronized (dbFile) { synchronized (dbFile) {
try { try {
PreparedStatement ps = bs.prepareStatement(getConnection()); final PreparedStatement ps = bs.prepareStatement(getConnection());
ps.execute(); ps.execute();
ps.close(); ps.close();
} catch (SQLException e) { } catch (final SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
} }
/**
* Gets the database connection for executing queries on.
*
* @return The database connection
*/
@Override @Override
public void close() { public Connection getConnection() {
flush(); 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() { private void startWatcher() {
@ -100,7 +100,7 @@ public class SQLiteCore implements DatabaseCore {
public void run() { public void run() {
try { try {
Thread.sleep(30000); Thread.sleep(30000);
} catch (InterruptedException e) { } catch (final InterruptedException e) {
} }
flush(); flush();
} }

View File

@ -61,9 +61,8 @@ public class BlockListener implements Listener {
final Shop shop = getShopNextTo(b.getLocation()); final Shop shop = getShopNextTo(b.getLocation());
if (shop == null) { if (shop == null) {
return; return;
} else {
e.setCancelled(true);
} }
e.setCancelled(true);
} }
} }

View File

@ -50,9 +50,8 @@ public class LockListener implements Listener {
final Shop shop = plugin.getShopManager().getShop(b.getLocation()); final Shop shop = plugin.getShopManager().getShop(b.getLocation());
if (shop == null) { if (shop == null) {
return; return;
} else {
e.setCancelled(true);
} }
e.setCancelled(true);
} }
} }
@ -131,9 +130,8 @@ public class LockListener implements Listener {
c = Util.getSecondHalf(c); c = Util.getSecondHalf(c);
if (c == null) { if (c == null) {
return; // You didn't place a hopper on a shop. Meh. 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) { if (shop == null) {
return; return;
} }

View File

@ -144,11 +144,10 @@ public class QuickShop extends JavaPlugin {
getLogger().warning("卸载插件!!!"); getLogger().warning("卸载插件!!!");
this.getPluginLoader().disablePlugin(this); this.getPluginLoader().disablePlugin(this);
return false; return false;
} else { }
this.economy = new Economy(core); this.economy = new Economy(core);
return true; return true;
} }
}
/** /**
* Logs the given string to qs.log, if QuickShop is configured to do so. * Logs the given string to qs.log, if QuickShop is configured to do so.

View File

@ -420,14 +420,12 @@ public class ContainerShop implements Shop {
// They're both buying or both selling => Not a double shop, // They're both buying or both selling => Not a double shop,
// just two shops. // just two shops.
return false; return false;
} else { }
// One is buying, one is selling. // One is buying, one is selling.
return true; return true;
} }
} else {
return false; return false;
} }
}
@Override @Override
public boolean isSelling() { public boolean isSelling() {

View File

@ -1,12 +1,12 @@
package org.maxgamer.QuickShop.Shop; package org.maxgamer.QuickShop.Shop;
public class ShopChunk { public class ShopChunk {
private String world; private final String world;
private int x; private final int x;
private int z; private final int z;
private int hash = 0; 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.world = world;
this.x = x; this.x = x;
this.z = z; this.z = z;
@ -15,6 +15,19 @@ public class ShopChunk {
// memory // 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() { public int getX() {
return this.x; return this.x;
} }
@ -23,20 +36,6 @@ public class ShopChunk {
return this.z; 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 @Override
public int hashCode() { public int hashCode() {
return hash; return hash;

View File

@ -25,9 +25,71 @@ import org.maxgamer.QuickShop.Util.MsgUtil;
import org.maxgamer.QuickShop.Util.Util; import org.maxgamer.QuickShop.Util.Util;
public class ShopManager { public class ShopManager {
private final HashMap<String, Info> actions = new HashMap<String, Info>(); public class ShopIterator implements Iterator<Shop> {
private Iterator<HashMap<Location, Shop>> chunks;
private Shop current;
private Iterator<Shop> shops;
private final Iterator<HashMap<ShopChunk, HashMap<Location, Shop>>> 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<String, Info> actions = new HashMap<String, Info>();
private final QuickShop plugin; private final QuickShop plugin;
private final HashMap<String, HashMap<ShopChunk, HashMap<Location, Shop>>> shops = new HashMap<String, HashMap<ShopChunk, HashMap<Location, Shop>>>(); private final HashMap<String, HashMap<ShopChunk, HashMap<Location, Shop>>> shops = new HashMap<String, HashMap<ShopChunk, HashMap<Location, Shop>>>();
public ShopManager(final QuickShop plugin) { public ShopManager(final QuickShop plugin) {
@ -552,68 +614,4 @@ public class ShopManager {
// Put the shop in its location in the chunk list. // Put the shop in its location in the chunk list.
inChunk.put(shop.getLocation(), shop); inChunk.put(shop.getLocation(), shop);
} }
public class ShopIterator implements Iterator<Shop> {
private Iterator<HashMap<Location, Shop>> chunks;
private Shop current;
private Iterator<Shop> shops;
private final Iterator<HashMap<ShopChunk, HashMap<Location, Shop>>> 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();
}
}
} }

View File

@ -117,9 +117,8 @@ public class Util {
public static String firstUppercase(final String string) { public static String firstUppercase(final String string) {
if (string.length() > 1) { if (string.length() > 1) {
return Character.toUpperCase(string.charAt(0)) + string.substring(1).toLowerCase(); return Character.toUpperCase(string.charAt(0)) + string.substring(1).toLowerCase();
} else {
return string.toUpperCase();
} }
return string.toUpperCase();
} }
/** /**
@ -413,11 +412,10 @@ public class Util {
if (loc.getWorld().isChunkLoaded(x, z)) { if (loc.getWorld().isChunkLoaded(x, z)) {
// System.out.println("Chunk is loaded " + x + ", " + z); // System.out.println("Chunk is loaded " + x + ", " + z);
return true; return true;
} else { }
// System.out.println("Chunk is NOT loaded " + x + ", " + z); // System.out.println("Chunk is NOT loaded " + x + ", " + z);
return false; return false;
} }
}
/** /**
* @param mat * @param mat