Added type parameter to callback

This commit is contained in:
Eric 2017-07-30 13:07:07 +02:00
parent a4517b3657
commit 49d90da57a
8 changed files with 88 additions and 88 deletions

View File

@ -9,18 +9,30 @@ import de.epiceric.shopchest.config.HologramFormat;
import de.epiceric.shopchest.config.Placeholder;
import de.epiceric.shopchest.event.ShopInitializedEvent;
import de.epiceric.shopchest.external.PlotSquaredShopFlag;
import de.epiceric.shopchest.external.WorldGuardShopFlag;
import de.epiceric.shopchest.language.LanguageUtils;
import de.epiceric.shopchest.language.LocalizedMessage;
import de.epiceric.shopchest.listeners.*;
import de.epiceric.shopchest.listeners.AreaShopListener;
import de.epiceric.shopchest.listeners.BlockExplodeListener;
import de.epiceric.shopchest.listeners.ChestProtectListener;
import de.epiceric.shopchest.listeners.NotifyPlayerOnJoinListener;
import de.epiceric.shopchest.listeners.ShopInteractListener;
import de.epiceric.shopchest.listeners.ShopItemListener;
import de.epiceric.shopchest.listeners.ShopUpdateListener;
import de.epiceric.shopchest.listeners.WorldGuardListener;
import de.epiceric.shopchest.nms.JsonBuilder;
import de.epiceric.shopchest.shop.Shop;
import de.epiceric.shopchest.shop.Shop.ShopType;
import de.epiceric.shopchest.sql.Database;
import de.epiceric.shopchest.sql.MySQL;
import de.epiceric.shopchest.sql.SQLite;
import de.epiceric.shopchest.utils.*;
import de.epiceric.shopchest.utils.Callback;
import de.epiceric.shopchest.utils.Permissions;
import de.epiceric.shopchest.utils.ShopUpdater;
import de.epiceric.shopchest.utils.ShopUtils;
import de.epiceric.shopchest.utils.UpdateChecker;
import de.epiceric.shopchest.utils.UpdateChecker.UpdateCheckerResult;
import de.epiceric.shopchest.external.WorldGuardShopFlag;
import de.epiceric.shopchest.utils.Utils;
import fr.xephi.authme.AuthMe;
import me.ryanhamshire.GriefPrevention.GriefPrevention;
import me.wiefferink.areashop.AreaShop;
@ -36,7 +48,10 @@ import org.bukkit.scheduler.BukkitRunnable;
import pl.islandworld.IslandWorld;
import us.talabrek.ultimateskyblock.api.uSkyBlockAPI;
import java.io.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
@ -375,15 +390,12 @@ public class ShopChest extends JavaPlugin {
*/
private void initializeShops() {
debug("Initializing Shops...");
shopUtils.reloadShops(false, true, new Callback(this) {
shopUtils.reloadShops(false, true, new Callback<Integer>(this) {
@Override
public void onResult(Object result) {
if (result instanceof Integer) {
int count = (int) result;
Bukkit.getServer().getPluginManager().callEvent(new ShopInitializedEvent(count));
getLogger().info("Initialized " + count + " Shops");
debug("Initialized " + count + " Shops");
}
public void onResult(Integer result) {
Bukkit.getServer().getPluginManager().callEvent(new ShopInitializedEvent(result));
getLogger().info("Initialized " + result + " Shops");
debug("Initialized " + result + " Shops");
}
});
}

View File

@ -186,14 +186,12 @@ class ShopCommandExecutor implements CommandExecutor {
return;
}
shopUtils.reloadShops(true, true, new Callback(plugin) {
shopUtils.reloadShops(true, true, new Callback<Integer>(plugin) {
@Override
public void onResult(Object result) {
if (result instanceof Integer) {
int count = (int) result;
sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.RELOADED_SHOPS, new LocalizedMessage.ReplacedPlaceholder(Placeholder.AMOUNT, String.valueOf(count))));
plugin.debug(sender.getName() + " has reloaded " + count + " shops");
}
public void onResult(Integer result) {
sender.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.RELOADED_SHOPS,
new LocalizedMessage.ReplacedPlaceholder(Placeholder.AMOUNT, String.valueOf(result))));
plugin.debug(sender.getName() + " has reloaded " + result + " shops");
}
});
}

View File

@ -68,9 +68,9 @@ public class ChestProtectListener implements Listener {
Location loc = (b.getLocation().equals(l.getLocation()) ? r.getLocation() : l.getLocation());
final Shop newShop = new Shop(shop.getID(), plugin, shop.getVendor(), shop.getProduct(), loc, shop.getBuyPrice(), shop.getSellPrice(), shop.getShopType());
shopUtils.removeShop(shop, true, new Callback(plugin) {
shopUtils.removeShop(shop, true, new Callback<Void>(plugin) {
@Override
public void onResult(Object result) {
public void onResult(Void result) {
newShop.create(true);
shopUtils.addShop(newShop, true);
}
@ -228,9 +228,9 @@ public class ChestProtectListener implements Listener {
if (b.getRelative(BlockFace.UP).getType() == Material.AIR) {
final Shop newShop = new Shop(shop.getID(), plugin, shop.getVendor(), shop.getProduct(), shop.getLocation(), shop.getBuyPrice(), shop.getSellPrice(), shop.getShopType());
shopUtils.removeShop(shop, true, new Callback(plugin) {
shopUtils.removeShop(shop, true, new Callback<Void>(plugin) {
@Override
public void onResult(Object result) {
public void onResult(Void result) {
newShop.create(true);
shopUtils.addShop(newShop, true);
plugin.debug(String.format("%s extended %s's shop (#%d)", p.getName(), shop.getVendor().getName(), shop.getID()));

View File

@ -32,33 +32,26 @@ public class NotifyPlayerOnJoinListener implements Listener {
}
}
plugin.getShopDatabase().getLastLogout(p, new Callback(plugin) {
plugin.getShopDatabase().getLastLogout(p, new Callback<Long>(plugin) {
@Override
public void onResult(Object result) {
if (result instanceof Long) {
long lastLogout = (long) result;
if (lastLogout < 0) {
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.ERROR_OCCURRED,
new LocalizedMessage.ReplacedPlaceholder(Placeholder.ERROR, "Could not get last time you logged out")));
return;
}
plugin.getShopDatabase().getRevenue(p, lastLogout, new Callback(plugin) {
@Override
public void onResult(Object result) {
if (result instanceof Double) {
double revenue = (double) result;
if (revenue != 0) {
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.REVENUE_WHILE_OFFLINE,
new LocalizedMessage.ReplacedPlaceholder(Placeholder.REVENUE, String.valueOf(revenue))));
}
}
}
});
public void onResult(Long result) {
if (result < 0) {
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.ERROR_OCCURRED,
new LocalizedMessage.ReplacedPlaceholder(Placeholder.ERROR, "Could not get last time you logged out")));
return;
}
plugin.getShopDatabase().getRevenue(p, result, new Callback<Double>(plugin) {
@Override
public void onResult(Double result) {
if (result != 0) {
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.REVENUE_WHILE_OFFLINE,
new LocalizedMessage.ReplacedPlaceholder(Placeholder.REVENUE, String.valueOf(result))));
}
}
});
}
});
}
@EventHandler

View File

@ -59,15 +59,11 @@ public class ShopUpdateListener implements Listener {
public void onWorldLoad(WorldLoadEvent e) {
final String worldName = e.getWorld().getName();
plugin.getShopUtils().reloadShops(false, false, new Callback(plugin) {
plugin.getShopUtils().reloadShops(false, false, new Callback<Integer>(plugin) {
@Override
public void onResult(Object result) {
int amount = -1;
if (result instanceof Integer) {
amount = (int) result;
}
plugin.getLogger().info(String.format("Reloaded %d shops because a new world '%s' was loaded", amount, worldName));
plugin.debug(String.format("Reloaded %d shops because a new world '%s' was loaded", amount, worldName));
public void onResult(Integer result) {
plugin.getLogger().info(String.format("Reloaded %d shops because a new world '%s' was loaded", result, worldName));
plugin.debug(String.format("Reloaded %d shops because a new world '%s' was loaded", result, worldName));
}
});
}

View File

@ -46,7 +46,7 @@ public abstract class Database {
* Creates the table (if doesn't exist) and tests the connection
* @param callback Callback that - if succeeded - returns the amount of shops that were found (as {@code int})
*/
public void connect(final Callback callback) {
public void connect(final Callback<Integer> callback) {
new BukkitRunnable() {
@Override
public void run() {
@ -164,7 +164,7 @@ public abstract class Database {
* @param shop Shop to remove
* @param callback Callback that - if succeeded - returns {@code null}
*/
public void removeShop(final Shop shop, final Callback callback) {
public void removeShop(final Shop shop, final Callback<Void> callback) {
new BukkitRunnable() {
@Override
public void run() {
@ -193,7 +193,7 @@ public abstract class Database {
* @param id ID of the shop
* @param callback Callback that - if succeeded - returns whether a shop with the given ID exists (as {@code boolean})
*/
public void isShop(final int id, final Callback callback) {
public void isShop(final int id, final Callback<Boolean> callback) {
new BukkitRunnable() {
@Override
public void run() {
@ -230,7 +230,7 @@ public abstract class Database {
* @param callback Callback that - if succeeded - returns an array of all shops (as {@code Shop[]})
* @param showConsoleMessages Whether console messages (errors or warnings) should be shown
*/
public void getShops(final boolean showConsoleMessages, final Callback callback) {
public void getShops(final boolean showConsoleMessages, final Callback<Shop[]> callback) {
new BukkitRunnable() {
@Override
public void run() {
@ -296,7 +296,7 @@ public abstract class Database {
* @param shop Shop to add
* @param callback Callback that - if succeeded - returns the ID the shop was given (as {@code int})
*/
public void addShop(final Shop shop, final Callback callback) {
public void addShop(final Shop shop, final Callback<Integer> callback) {
new BukkitRunnable() {
@Override
public void run() {
@ -365,7 +365,7 @@ public abstract class Database {
* @param type Whether the player bought or sold something
* @param callback Callback that - if succeeded - returns {@code null}
*/
public void logEconomy(final Player executor, final ItemStack product, final OfflinePlayer vendor, final ShopType shopType, final Location location, final double price, final ShopBuySellEvent.Type type, final Callback callback) {
public void logEconomy(final Player executor, final ItemStack product, final OfflinePlayer vendor, final ShopType shopType, final Location location, final double price, final ShopBuySellEvent.Type type, final Callback<Void> callback) {
new BukkitRunnable() {
@Override
public void run() {
@ -406,7 +406,7 @@ public abstract class Database {
* @param logoutTime Time in milliseconds when he logged out the last time
* @param callback Callback that - if succeeded - returns the revenue the player made while offline (as {@code double})
*/
public void getRevenue(final Player player, final long logoutTime, final Callback callback) {
public void getRevenue(final Player player, final long logoutTime, final Callback<Double> callback) {
new BukkitRunnable() {
@Override
public void run() {
@ -464,7 +464,7 @@ public abstract class Database {
* @param timestamp Time in milliseconds when the player logged out
* @param callback Callback that - if succeeded - returns {@code null}
*/
public void logLogout(final Player player, final long timestamp, final Callback callback) {
public void logLogout(final Player player, final long timestamp, final Callback<Void> callback) {
new BukkitRunnable() {
@Override
public void run() {
@ -496,7 +496,7 @@ public abstract class Database {
* @param player Player who logged out
* @param callback Callback that - if succeeded - returns the time in milliseconds the player logged out (as {@code long})
*/
public void getLastLogout(final Player player, final Callback callback) {
public void getLastLogout(final Player player, final Callback<Long> callback) {
new BukkitRunnable() {
@Override
public void run() {
@ -517,7 +517,7 @@ public abstract class Database {
}
}
if (callback != null) callback.callSyncResult(-1);
if (callback != null) callback.callSyncResult(-1L);
} catch (SQLException ex) {
if (callback != null) callback.callSyncError(ex);
plugin.getLogger().severe("Failed to access database");

View File

@ -3,18 +3,18 @@ package de.epiceric.shopchest.utils;
import de.epiceric.shopchest.ShopChest;
import org.bukkit.scheduler.BukkitRunnable;
public abstract class Callback {
public abstract class Callback<T> {
private ShopChest plugin;
public Callback(ShopChest plugin) {
this.plugin = plugin;
}
public void onResult(Object result) {}
public void onResult(T result) {}
public void onError(Throwable throwable) {}
public final void callSyncResult(final Object result) {
public final void callSyncResult(final T result) {
new BukkitRunnable() {
@Override
public void run() {

View File

@ -13,7 +13,12 @@ import org.bukkit.inventory.InventoryHolder;
import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.util.Vector;
import java.util.*;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
public class ShopUtils {
@ -62,7 +67,7 @@ public class ShopUtils {
* @param addToDatabase Whether the shop should also be added to the database
* @param callback Callback that - if succeeded - returns the ID the shop had or was given (as {@code int})
*/
public void addShop(Shop shop, boolean addToDatabase, Callback callback) {
public void addShop(Shop shop, boolean addToDatabase, Callback<Integer> callback) {
InventoryHolder ih = shop.getInventoryHolder();
plugin.debug("Adding shop... (#" + shop.getID() + ")");
@ -103,7 +108,7 @@ public class ShopUtils {
* @param removeFromDatabase Whether the shop should also be removed from the database
* @param callback Callback that - if succeeded - returns null
*/
public void removeShop(Shop shop, boolean removeFromDatabase, Callback callback) {
public void removeShop(Shop shop, boolean removeFromDatabase, Callback<Void> callback) {
plugin.debug("Removing shop (#" + shop.getID() + ")");
InventoryHolder ih = shop.getInventoryHolder();
@ -209,7 +214,7 @@ public class ShopUtils {
* @param showConsoleMessages Whether messages about the language file should be shown in the console
* @param callback Callback that - if succeeded - returns the amount of shops that were reloaded (as {@code int})
*/
public void reloadShops(boolean reloadConfig, final boolean showConsoleMessages, final Callback callback) {
public void reloadShops(boolean reloadConfig, final boolean showConsoleMessages, final Callback<Integer> callback) {
plugin.debug("Reloading shops...");
if (reloadConfig) {
@ -218,32 +223,28 @@ public class ShopUtils {
plugin.getUpdater().restart();
}
plugin.getShopDatabase().connect(new Callback(plugin) {
plugin.getShopDatabase().connect(new Callback<Integer>(plugin) {
@Override
public void onResult(Object result) {
public void onResult(Integer result) {
for (Shop shop : getShops()) {
removeShop(shop, false);
plugin.debug("Removed shop (#" + shop.getID() + ")");
}
plugin.getShopDatabase().getShops(showConsoleMessages, new Callback(plugin) {
plugin.getShopDatabase().getShops(showConsoleMessages, new Callback<Shop[]>(plugin) {
@Override
public void onResult(Object result) {
if (result instanceof Shop[]) {
Shop[] shops = (Shop[]) result;
for (Shop shop : shops) {
if (shop.create(showConsoleMessages)) {
addShop(shop, false);
}
public void onResult(Shop[] result) {
for (Shop shop : result) {
if (shop.create(showConsoleMessages)) {
addShop(shop, false);
}
for (Player player : Bukkit.getOnlinePlayers()) {
updateShops(player, true);
}
if (callback != null) callback.callSyncResult(shops.length);
}
for (Player player : Bukkit.getOnlinePlayers()) {
updateShops(player, true);
}
if (callback != null) callback.callSyncResult(result.length);
}
@Override