ShopUtils no longer contains static methods

This commit is contained in:
Eric 2016-07-13 15:03:54 +02:00
parent 225c59c2ea
commit dc40e30301
9 changed files with 85 additions and 57 deletions

View File

@ -34,11 +34,13 @@ public class Commands extends BukkitCommand {
private ShopChest plugin;
private Permission perm;
private ShopUtils shopUtils;
public Commands(ShopChest plugin, String name, String description, String usageMessage, List<String> aliases) {
super(name, description, usageMessage, aliases);
this.plugin = plugin;
this.perm = plugin.getPermission();
this.shopUtils = plugin.getShopUtils();
}
/**
@ -118,8 +120,8 @@ public class Commands extends BukkitCommand {
} else if (args[0].equalsIgnoreCase("limits")) {
if (perm.has(p, "shopchest.limits")) {
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.OCCUPIED_SHOP_SLOTS,
new LocalizedMessage.ReplacedRegex(Regex.LIMIT, String.valueOf(ShopUtils.getShopLimit(p))),
new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(ShopUtils.getShopAmount(p)))));
new LocalizedMessage.ReplacedRegex(Regex.LIMIT, String.valueOf(shopUtils.getShopLimit(p))),
new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(shopUtils.getShopAmount(p)))));
return true;
} else {
@ -232,7 +234,7 @@ public class Commands extends BukkitCommand {
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) return;
player.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.RELOADED_SHOPS, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(ShopUtils.reloadShops(true)))));
player.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.RELOADED_SHOPS, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(shopUtils.reloadShops(true)))));
}
/**
@ -245,10 +247,10 @@ public class Commands extends BukkitCommand {
int amount;
double buyPrice, sellPrice;
int limit = ShopUtils.getShopLimit(p);
int limit = shopUtils.getShopLimit(p);
if (limit != -1) {
if (ShopUtils.getShopAmount(p) >= limit) {
if (shopUtils.getShopAmount(p) >= limit) {
if (shopType != ShopType.ADMIN || !plugin.getShopChestConfig().exclude_admin_shops) {
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SHOP_LIMIT_REACHED, new LocalizedMessage.ReplacedRegex(Regex.LIMIT, String.valueOf(limit))));
return;

View File

@ -46,6 +46,7 @@ public class ShopChest extends JavaPlugin {
private boolean isUpdateNeeded = false;
private String latestVersion = "";
private String downloadLink = "";
private ShopUtils shopUtils;
/**
* @return An instance of ShopChest
@ -118,6 +119,8 @@ public class ShopChest extends JavaPlugin {
LanguageUtils.load();
saveResource("item_names.txt", true);
shopUtils = new ShopUtils(this);
try {
Metrics metrics = new Metrics(this);
Graph shopType = metrics.createGraph("Shop Type");
@ -127,7 +130,7 @@ public class ShopChest extends JavaPlugin {
public int getValue() {
int value = 0;
for (Shop shop : ShopUtils.getShops()) {
for (Shop shop : shopUtils.getShops()) {
if (shop.getShopType() == ShopType.NORMAL) value++;
}
@ -142,7 +145,7 @@ public class ShopChest extends JavaPlugin {
public int getValue() {
int value = 0;
for (Shop shop : ShopUtils.getShops()) {
for (Shop shop : shopUtils.getShops()) {
if (shop.getShopType() == ShopType.ADMIN) value++;
}
@ -196,7 +199,7 @@ public class ShopChest extends JavaPlugin {
ShopReloadEvent event = new ShopReloadEvent(Bukkit.getConsoleSender());
Bukkit.getServer().getPluginManager().callEvent(event);
if (!event.isCancelled()) getLogger().info(LanguageUtils.getMessage(LocalizedMessage.Message.RELOADED_SHOPS, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(ShopUtils.reloadShops(true)))));
if (!event.isCancelled()) getLogger().info(LanguageUtils.getMessage(LocalizedMessage.Message.RELOADED_SHOPS, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(shopUtils.reloadShops(true)))));
}
}, config.auto_reload_time * 20, config.auto_reload_time * 20);
}
@ -269,7 +272,7 @@ public class ShopChest extends JavaPlugin {
initializeShops();
getServer().getPluginManager().registerEvents(new HologramUpdateListener(this), this);
getServer().getPluginManager().registerEvents(new ItemProtectListener(), this);
getServer().getPluginManager().registerEvents(new ItemProtectListener(this), this);
getServer().getPluginManager().registerEvents(new ShopInteractListener(this), this);
getServer().getPluginManager().registerEvents(new NotifyUpdateOnJoinListener(this), this);
getServer().getPluginManager().registerEvents(new ChestProtectListener(this), this);
@ -284,8 +287,8 @@ public class ShopChest extends JavaPlugin {
@Override
public void onDisable() {
for (Shop shop : ShopUtils.getShops()) {
ShopUtils.removeShop(shop, false);
for (Shop shop : shopUtils.getShops()) {
shopUtils.removeShop(shop, false);
}
for (World world : Bukkit.getWorlds()) {
@ -301,10 +304,17 @@ public class ShopChest extends JavaPlugin {
* Initializes the shops
*/
private void initializeShops() {
int count = ShopUtils.reloadShops(false);
int count = shopUtils.reloadShops(false);
getLogger().info("Initialized " + String.valueOf(count) + " Shops");
}
/**
* @return ShopChest's {@link ShopUtils} containing some important methods
*/
public ShopUtils getShopUtils() {
return shopUtils;
}
/**
* @return Registered Economy of Vault
*/

View File

@ -26,14 +26,16 @@ import java.util.ArrayList;
public class ChestProtectListener implements Listener {
private ShopChest plugin;
private ShopUtils shopUtils;
public ChestProtectListener(ShopChest plugin) {
this.plugin = plugin;
this.shopUtils = plugin.getShopUtils();
}
@EventHandler
public void onBlockBreak(BlockBreakEvent e) {
if (ShopUtils.isShop(e.getBlock().getLocation())) {
if (shopUtils.isShop(e.getBlock().getLocation())) {
e.setCancelled(true);
e.getPlayer().sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CANNOT_BREAK_SHOP));
}
@ -45,7 +47,7 @@ public class ChestProtectListener implements Listener {
ArrayList<Block> bl = new ArrayList<>(e.blockList());
for (Block b : bl) {
if (b.getType().equals(Material.CHEST) || b.getType().equals(Material.TRAPPED_CHEST)) {
if (ShopUtils.isShop(b.getLocation())) e.blockList().remove(b);
if (shopUtils.isShop(b.getLocation())) e.blockList().remove(b);
}
}
}
@ -57,7 +59,7 @@ public class ChestProtectListener implements Listener {
ArrayList<Block> bl = new ArrayList<>(e.blockList());
for (Block b : bl) {
if (b.getType().equals(Material.CHEST) || b.getType().equals(Material.TRAPPED_CHEST)) {
if (ShopUtils.isShop(b.getLocation())) e.blockList().remove(b);
if (shopUtils.isShop(b.getLocation())) e.blockList().remove(b);
}
}
}
@ -76,22 +78,22 @@ public class ChestProtectListener implements Listener {
Chest r = (Chest) dc.getRightSide();
Chest l = (Chest) dc.getLeftSide();
if (ShopUtils.isShop(r.getLocation()) || ShopUtils.isShop(l.getLocation())) {
if (shopUtils.isShop(r.getLocation()) || shopUtils.isShop(l.getLocation())) {
if (b.getRelative(BlockFace.UP).getType() == Material.AIR) {
Shop shop;
if (b.getLocation().equals(r.getLocation())) {
shop = ShopUtils.getShop(l.getLocation());
shop = shopUtils.getShop(l.getLocation());
} else if (b.getLocation().equals(l.getLocation())) {
shop = ShopUtils.getShop(r.getLocation());
shop = shopUtils.getShop(r.getLocation());
} else {
return;
}
ShopUtils.removeShop(shop, true);
shopUtils.removeShop(shop, true);
Shop newShop = new Shop(shop.getID(), ShopChest.getInstance(), shop.getVendor(), shop.getProduct(), shop.getLocation(), shop.getBuyPrice(), shop.getSellPrice(), shop.getShopType());
ShopUtils.addShop(newShop, true);
shopUtils.addShop(newShop, true);
} else {
e.setCancelled(true);
e.getPlayer().sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CHEST_BLOCKED));
@ -113,12 +115,12 @@ public class ChestProtectListener implements Listener {
Chest r = (Chest) dc.getRightSide();
Chest l = (Chest) dc.getLeftSide();
if (ShopUtils.isShop(r.getLocation()) || ShopUtils.isShop(l.getLocation())) e.setCancelled(true);
if (shopUtils.isShop(r.getLocation()) || shopUtils.isShop(l.getLocation())) e.setCancelled(true);
} else if (e.getSource().getHolder() instanceof Chest) {
Chest c = (Chest) e.getSource().getHolder();
if (ShopUtils.isShop(c.getLocation())) e.setCancelled(true);
if (shopUtils.isShop(c.getLocation())) e.setCancelled(true);
}
}

View File

@ -23,7 +23,7 @@ public class HologramUpdateListener implements Listener {
Player p = e.getPlayer();
Location playerLocation = p.getLocation();
for (Shop shop : ShopUtils.getShops()) {
for (Shop shop : plugin.getShopUtils().getShops()) {
if (shop.getHologram() != null) {

View File

@ -1,6 +1,7 @@
package de.epiceric.shopchest.listeners;
import de.epiceric.shopchest.ShopChest;
import de.epiceric.shopchest.utils.ShopUtils;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
@ -18,6 +19,12 @@ import org.bukkit.event.player.PlayerPickupItemEvent;
public class ItemProtectListener implements Listener {
private ShopUtils shopUtils;
public ItemProtectListener(ShopChest plugin) {
this.shopUtils = plugin.getShopUtils();
}
@EventHandler(priority = EventPriority.HIGH)
public void onItemDespawn(ItemDespawnEvent e) {
Item item = e.getEntity();
@ -41,14 +48,14 @@ public class ItemProtectListener implements Listener {
Block b = e.getBlockPlaced();
Block below = b.getRelative(BlockFace.DOWN);
if (ShopUtils.isShop(below.getLocation())) e.setCancelled(true);
if (shopUtils.isShop(below.getLocation())) e.setCancelled(true);
}
@EventHandler(priority = EventPriority.HIGH)
public void onMultiBlockPlace(BlockMultiPlaceEvent e) {
for (BlockState blockState : e.getReplacedBlockStates()) {
Block below = blockState.getBlock().getRelative(BlockFace.DOWN);
if (ShopUtils.isShop(below.getLocation())) e.setCancelled(true);
if (shopUtils.isShop(below.getLocation())) e.setCancelled(true);
}
}
@ -57,7 +64,7 @@ public class ItemProtectListener implements Listener {
// If the piston would only move itself
Block airAfterPiston = e.getBlock().getRelative(e.getDirection());
Block belowAir = airAfterPiston.getRelative(BlockFace.DOWN);
if (ShopUtils.isShop(belowAir.getLocation())) {
if (shopUtils.isShop(belowAir.getLocation())) {
e.setCancelled(true);
return;
}
@ -65,7 +72,7 @@ public class ItemProtectListener implements Listener {
for (Block b : e.getBlocks()) {
Block newBlock = b.getRelative(e.getDirection());
Block belowNewBlock = newBlock.getRelative(BlockFace.DOWN);
if (ShopUtils.isShop(belowNewBlock.getLocation())) e.setCancelled(true);
if (shopUtils.isShop(belowNewBlock.getLocation())) e.setCancelled(true);
}
}
@ -74,7 +81,7 @@ public class ItemProtectListener implements Listener {
for (Block b : e.getBlocks()) {
Block newBlock = b.getRelative(e.getDirection());
Block belowNewBlock = newBlock.getRelative(BlockFace.DOWN);
if (ShopUtils.isShop(belowNewBlock.getLocation())) e.setCancelled(true);
if (shopUtils.isShop(belowNewBlock.getLocation())) e.setCancelled(true);
}
}
@ -83,14 +90,14 @@ public class ItemProtectListener implements Listener {
Block b = e.getToBlock();
Block below = b.getRelative(BlockFace.DOWN);
if (ShopUtils.isShop(below.getLocation())) e.setCancelled(true);
if (shopUtils.isShop(below.getLocation())) e.setCancelled(true);
}
@EventHandler(priority = EventPriority.HIGH)
public void onBucketEmpty(PlayerBucketEmptyEvent e) {
Block b = e.getBlockClicked();
if (ShopUtils.isShop(b.getLocation())) e.setCancelled(true);
if (shopUtils.isShop(b.getLocation())) e.setCancelled(true);
}
}

View File

@ -47,12 +47,14 @@ public class ShopInteractListener implements Listener {
private Permission perm;
private Economy econ;
private Database database;
private ShopUtils shopUtils;
public ShopInteractListener(ShopChest plugin) {
this.plugin = plugin;
this.perm = plugin.getPermission();
this.econ = plugin.getEconomy();
this.database = plugin.getShopDatabase();
this.shopUtils = plugin.getShopUtils();
}
@EventHandler
@ -95,7 +97,7 @@ public class ShopInteractListener implements Listener {
}
if (!ShopUtils.isShop(b.getLocation())) {
if (!shopUtils.isShop(b.getLocation())) {
if (b.getRelative(BlockFace.UP).getType() == Material.AIR) {
ClickType clickType = ClickType.getPlayerClickType(p);
ItemStack product = clickType.getProduct();
@ -117,9 +119,9 @@ public class ShopInteractListener implements Listener {
case INFO:
e.setCancelled(true);
if (ShopUtils.isShop(b.getLocation())) {
if (shopUtils.isShop(b.getLocation())) {
Shop shop = ShopUtils.getShop(b.getLocation());
Shop shop = shopUtils.getShop(b.getLocation());
info(p, shop);
} else {
@ -132,9 +134,9 @@ public class ShopInteractListener implements Listener {
case REMOVE:
e.setCancelled(true);
if (ShopUtils.isShop(b.getLocation())) {
if (shopUtils.isShop(b.getLocation())) {
Shop shop = ShopUtils.getShop(b.getLocation());
Shop shop = shopUtils.getShop(b.getLocation());
if (shop.getVendor().getUniqueId().equals(p.getUniqueId()) || perm.has(p, "shopchest.removeOther")) {
remove(p, shop);
@ -153,9 +155,9 @@ public class ShopInteractListener implements Listener {
} else {
if (ShopUtils.isShop(b.getLocation())) {
if (shopUtils.isShop(b.getLocation())) {
e.setCancelled(true);
Shop shop = ShopUtils.getShop(b.getLocation());
Shop shop = shopUtils.getShop(b.getLocation());
if (p.isSneaking()) {
if (!shop.getVendor().getUniqueId().equals(p.getUniqueId())) {
@ -199,9 +201,9 @@ public class ShopInteractListener implements Listener {
} else if (e.getAction() == Action.LEFT_CLICK_BLOCK) {
if (ShopUtils.isShop(b.getLocation())) {
if (shopUtils.isShop(b.getLocation())) {
e.setCancelled(true);
Shop shop = ShopUtils.getShop(b.getLocation());
Shop shop = shopUtils.getShop(b.getLocation());
if ((shop.getShopType() == ShopType.ADMIN) || (!shop.getVendor().getUniqueId().equals(p.getUniqueId()))) {
if (shop.getSellPrice() > 0) {
@ -265,7 +267,7 @@ public class ShopInteractListener implements Listener {
Shop shop = new Shop(id, plugin, executor, product, location, buyPrice, sellPrice, shopType);
ShopUtils.addShop(shop, true);
shopUtils.addShop(shop, true);
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SHOP_CREATED));
for (Player p : Bukkit.getOnlinePlayers()) {
@ -284,7 +286,7 @@ public class ShopInteractListener implements Listener {
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) return;
ShopUtils.removeShop(shop, true);
shopUtils.removeShop(shop, true);
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SHOP_REMOVED));
}

View File

@ -54,7 +54,7 @@ public class Shop {
} else {
try {
if (plugin.getShopChestConfig().remove_shop_on_error)
ShopUtils.removeShop(this, true);
plugin.getShopUtils().removeShop(this, true);
throw new Exception("No Chest found at specified Location: " + b.getX() + "; " + b.getY() + "; " + b.getZ());
} catch (Exception ex) {

View File

@ -186,7 +186,7 @@ public abstract class Database {
switch (shopInfo) {
case SHOP:
Shop shop = ShopUtils.getShop((Location) get(id, ShopInfo.LOCATION, attempts));
Shop shop = plugin.getShopUtils().getShop((Location) get(id, ShopInfo.LOCATION, attempts));
if (shop != null)
return shop;
else {

View File

@ -22,8 +22,12 @@ import java.util.UUID;
public class ShopUtils {
private static HashMap<Location, Shop> shopLocation = new HashMap<>();
private static ShopChest plugin = ShopChest.getInstance();
private HashMap<Location, Shop> shopLocation = new HashMap<>();
private ShopChest plugin;
public ShopUtils(ShopChest plugin) {
this.plugin = plugin;
}
/**
* Get the shop at a given location
@ -31,7 +35,7 @@ public class ShopUtils {
* @param location Location of the shop
* @return Shop at the given location or <b>null</b> if no shop is found there
*/
public static Shop getShop(Location location) {
public Shop getShop(Location location) {
Location newLocation = new Location(location.getWorld(), location.getX(), location.getY(), location.getZ());
return shopLocation.get(newLocation);
@ -42,7 +46,7 @@ public class ShopUtils {
* @param location Location to check
* @return Whether there is a shop at the given location
*/
public static boolean isShop(Location location) {
public boolean isShop(Location location) {
Location newLocation = new Location(location.getWorld(), location.getX(), location.getY(), location.getZ());
return shopLocation.containsKey(newLocation);
}
@ -51,7 +55,7 @@ public class ShopUtils {
* Get all Shops
* @return Array of all Shops
*/
public static Shop[] getShops() {
public Shop[] getShops() {
ArrayList<Shop> shops = new ArrayList<>();
for (Shop shop : shopLocation.values()) {
@ -66,7 +70,7 @@ public class ShopUtils {
* @param shop Shop to add
* @param addToDatabase Whether the shop should also be added to the database
*/
public static void addShop(Shop shop, boolean addToDatabase) {
public void addShop(Shop shop, boolean addToDatabase) {
InventoryHolder ih = shop.getChest().getInventory().getHolder();
if (ih instanceof DoubleChest) {
@ -90,7 +94,7 @@ public class ShopUtils {
* @param shop Shop to remove
* @param removeFromDatabase Whether the shop should also be removed from the database
*/
public static void removeShop(Shop shop, boolean removeFromDatabase) {
public void removeShop(Shop shop, boolean removeFromDatabase) {
InventoryHolder ih = shop.getChest().getInventory().getHolder();
if (ih instanceof DoubleChest) {
@ -116,7 +120,7 @@ public class ShopUtils {
* @param p Player, whose shop limits should be returned
* @return The shop limits of the given player
*/
public static int getShopLimit(Player p) {
public int getShopLimit(Player p) {
int limit = plugin.getShopChestConfig().default_limit;
if (plugin.getPermission().hasGroupSupport()) {
@ -174,10 +178,10 @@ public class ShopUtils {
* @param p Player, whose shops should be counted
* @return The amount of a shops a player has (if {@link Config#exclude_admin_shops} is true, admin shops won't be counted)
*/
public static int getShopAmount(OfflinePlayer p) {
public int getShopAmount(OfflinePlayer p) {
float shopCount = 0;
for (Shop shop : ShopUtils.getShops()) {
for (Shop shop : getShops()) {
if (shop.getVendor().equals(p)) {
if (shop.getShopType() != Shop.ShopType.ADMIN || !plugin.getShopChestConfig().exclude_admin_shops) {
shopCount++;
@ -193,13 +197,14 @@ public class ShopUtils {
/**
* Reload the shops
* @param reloadConfig Whether the configuration should also be reloaded
* @return Amount of shops, which were reloaded
*/
public static int reloadShops(boolean reloadConfig) {
public int reloadShops(boolean reloadConfig) {
if (reloadConfig) plugin.getShopChestConfig().reload(false, true);
for (Shop shop : ShopUtils.getShops()) {
ShopUtils.removeShop(shop, false);
for (Shop shop : getShops()) {
removeShop(shop, false);
}
for (World world : Bukkit.getWorlds()) {
@ -218,7 +223,7 @@ public class ShopUtils {
try {
Shop shop = (Shop) plugin.getShopDatabase().get(id, Database.ShopInfo.SHOP, plugin.getShopChestConfig().database_reconnect_attempts);
ShopUtils.addShop(shop, false);
addShop(shop, false);
} catch (NullPointerException e) {
continue;
}