diff --git a/pom.xml b/pom.xml index 0ca23c5..47ff1dc 100644 --- a/pom.xml +++ b/pom.xml @@ -132,7 +132,7 @@ org.bukkit bukkit - 1.12-pre2-SNAPSHOT + 1.12-pre5-SNAPSHOT provided diff --git a/src/main/java/de/epiceric/shopchest/ShopChest.java b/src/main/java/de/epiceric/shopchest/ShopChest.java index bd6707a..43b52bd 100644 --- a/src/main/java/de/epiceric/shopchest/ShopChest.java +++ b/src/main/java/de/epiceric/shopchest/ShopChest.java @@ -30,6 +30,7 @@ import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scheduler.BukkitRunnable; import pl.islandworld.IslandWorld; import us.talabrek.ultimateskyblock.api.uSkyBlockAPI; @@ -148,6 +149,54 @@ public class ShopChest extends JavaPlugin { getLogger().warning("Plugin may still work, but more errors are expected!"); } + loadExternalPlugins(); + + debug("Loading utils and extras..."); + LanguageUtils.load(); + saveResource("item_names.txt", true); + + loadMetrics(); + checkForUpdates(); + + shopUtils = new ShopUtils(this); + shopCommand = new ShopCommand(this); + + registerListeners(); + initializeShops(); + + updater = new ShopUpdater(this); + updater.start(); + } + + @Override + public void onDisable() { + debug("Disabling ShopChest..."); + + if (updater != null) { + debug("Stopping updater"); + updater.cancel(); + } + + if (database != null) { + for (Shop shop : shopUtils.getShops()) { + shopUtils.removeShop(shop, false, true); + debug("Removed shop (#" + shop.getID() + ")"); + } + + database.disconnect(); + } + + if (fw != null && config.enable_debug_log) { + try { + fw.close(); + } catch (IOException e) { + getLogger().severe("Failed to close FileWriter"); + e.printStackTrace(); + } + } + } + + private void loadExternalPlugins() { if (worldGuard != null && !WorldGuardShopFlag.isLoaded()) { WorldGuardShopFlag.register(this, false); @@ -201,14 +250,9 @@ public class ShopChest extends JavaPlugin { if (hasPlotSquared()) { new PlotSquaredShopFlag().register(this); } + } - debug("Loading utils and extras..."); - - LanguageUtils.load(); - saveResource("item_names.txt", true); - - shopUtils = new ShopUtils(this); - + private void loadMetrics() { debug("Initializing Metrics..."); Metrics metrics = new Metrics(this); @@ -256,8 +300,10 @@ public class ShopChest extends JavaPlugin { }, config.database_mysql_ping_interval * 20L, config.database_mysql_ping_interval * 20L); } } + } - Bukkit.getScheduler().runTaskAsynchronously(this, new Runnable() { + private void checkForUpdates() { + new BukkitRunnable() { @Override public void run() { UpdateChecker uc = new UpdateChecker(ShopChest.this); @@ -289,10 +335,10 @@ public class ShopChest extends JavaPlugin { Bukkit.getConsoleSender().sendMessage("[ShopChest] " + LanguageUtils.getMessage(LocalizedMessage.Message.UPDATE_ERROR)); } } - }); - - shopCommand = new ShopCommand(this); + }.runTaskAsynchronously(this); + } + private void registerListeners() { debug("Registering listeners..."); getServer().getPluginManager().registerEvents(new ShopUpdateListener(this), this); getServer().getPluginManager().registerEvents(new ShopItemListener(this), this); @@ -311,39 +357,23 @@ public class ShopChest extends JavaPlugin { getServer().getPluginManager().registerEvents(new AreaShopListener(this), this); } } - - initializeShops(); - - updater = new ShopUpdater(this); - updater.start(); } - @Override - public void onDisable() { - debug("Disabling ShopChest..."); - - if (updater != null) { - debug("Stopping updater"); - updater.cancel(); - } - - if (database != null) { - for (Shop shop : shopUtils.getShops()) { - shopUtils.removeShop(shop, false, true); - debug("Removed shop (#" + shop.getID() + ")"); + /** + * Initializes the shops + */ + private void initializeShops() { + debug("Initializing Shops..."); + shopUtils.reloadShops(false, true, new Callback(this) { + @Override + public void onResult(Object result) { + if (result instanceof Integer) { + int count = (int) result; + getLogger().info("Initialized " + count + " Shops"); + debug("Initialized " + count + " Shops"); + } } - - database.disconnect(); - } - - if (fw != null && config.enable_debug_log) { - try { - fw.close(); - } catch (IOException e) { - getLogger().severe("Failed to close FileWriter"); - e.printStackTrace(); - } - } + }); } /** @@ -376,24 +406,6 @@ public class ShopChest extends JavaPlugin { } } - /** - * Initializes the shops - */ - private void initializeShops() { - debug("Initializing Shops..."); - shopUtils.reloadShops(false, true, new Callback(this) { - @Override - public void onResult(Object result) { - if (result instanceof Integer) { - int count = (int) result; - getLogger().info("Initialized " + count + " Shops"); - debug("Initialized " + count + " Shops"); - } - } - }); - - } - /** * @return The {@link ShopCommand} */ @@ -422,13 +434,6 @@ public class ShopChest extends JavaPlugin { return areaShop != null && areaShop.isEnabled(); } - /** - * @return An instance of {@link AreaShop} or {@code null} if AreaShop is not enabled - */ - public AreaShop getAreaShop() { - return areaShop; - } - /** * @return Whether the plugin 'GriefPrevention' is enabled */ @@ -449,14 +454,6 @@ public class ShopChest extends JavaPlugin { public boolean hasIslandWorld() { return islandWorld != null && islandWorld.isEnabled(); } - - /** - * @return An instance of {@link IslandWorld} or {@code null} if IslandWorld is not enabled - */ - public IslandWorld getIslandWorld() { - return islandWorld; - } - /** * @return Whether the plugin 'ASkyBlock' is enabled */ @@ -464,13 +461,6 @@ public class ShopChest extends JavaPlugin { return aSkyBlock != null && aSkyBlock.isEnabled(); } - /** - * @return An instance of {@link ASkyBlock} or {@code null} if ASkyBlock is not enabled - */ - public ASkyBlock getASkyBlock() { - return aSkyBlock; - } - /** * @return Whether the plugin 'uSkyBlock' is enabled */ @@ -499,14 +489,6 @@ public class ShopChest extends JavaPlugin { public boolean hasAuthMe() { return authMe != null && authMe.isEnabled(); } - - /** - * @return An instance of {@link AuthMe} or {@code null} if AuthMe is not enabled - */ - public AuthMe getAuthMe() { - return authMe; - } - /** * @return Whether the plugin 'Towny' is enabled */ @@ -514,13 +496,6 @@ public class ShopChest extends JavaPlugin { return towny != null && towny.isEnabled(); } - /** - * @return An instance of {@link Towny} or {@code null} if Towny is not enabled - */ - public Towny getTowny() { - return towny; - } - /** * @return Whether the plugin 'WorldGuard' is enabled */ @@ -602,20 +577,9 @@ public class ShopChest extends JavaPlugin { } /** - * @return The {@link Config} of ShopChset + * @return The {@link Config} of ShopChest */ public Config getShopChestConfig() { return config; } - - /** - *

Provides a reader for a text file located inside the jar.

- * The returned reader will read text with the UTF-8 charset. - * @param file the filename of the resource to load - * @return null if {@link #getResource(String)} returns null - * @throws IllegalArgumentException if file is null - */ - public Reader _getTextResource(String file) throws IllegalArgumentException { - return getTextResource(file); - } } diff --git a/src/main/java/de/epiceric/shopchest/ShopCommand.java b/src/main/java/de/epiceric/shopchest/ShopCommand.java index 8cac67c..7212bb1 100644 --- a/src/main/java/de/epiceric/shopchest/ShopCommand.java +++ b/src/main/java/de/epiceric/shopchest/ShopCommand.java @@ -55,7 +55,6 @@ class ShopCommand implements CommandExecutor { cmd.setDescription("Manage players' shops or this plugin."); cmd.setUsage("/" + name); cmd.setExecutor(this); - cmd.setPermission(Permissions.BUY); cmd.setTabCompleter(new ShopTabCompleter(plugin)); return cmd; diff --git a/src/main/java/de/epiceric/shopchest/config/Config.java b/src/main/java/de/epiceric/shopchest/config/Config.java index b1a156e..82e9c32 100644 --- a/src/main/java/de/epiceric/shopchest/config/Config.java +++ b/src/main/java/de/epiceric/shopchest/config/Config.java @@ -10,6 +10,7 @@ import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.Reader; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -405,6 +406,18 @@ public class Config { return langConfig; } + private Reader getTextResource(String file, boolean showMessages) { + try { + return (Reader) plugin.getClass().getDeclaredMethod("getTextResource", String.class).invoke(plugin, file); + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + if (showMessages) plugin.getLogger().severe("Failed to get file from jar: " + file); + plugin.debug("Failed to get file from jar: " + file); + plugin.debug(e); + } + + return null; + } + private void loadLanguageConfig(boolean showMessages) { langConfig = new LanguageConfiguration(plugin, showMessages); File langFolder = new File(plugin.getDataFolder(), "lang"); @@ -421,15 +434,20 @@ public class Config { if (!langConfigFile.exists()) { if (!langDefaultFile.exists()) { try { - Reader r = plugin._getTextResource("lang/" + langConfigFile.getName()); + Reader r = getTextResource("lang/" + langConfigFile.getName(), showMessages); if (r == null) { - r = plugin._getTextResource("lang/en_US.lang"); + r = getTextResource("lang/en_US.lang", showMessages); if (showMessages) plugin.getLogger().info("Using locale \"en_US\" (Streamed from jar file)"); } else { if (showMessages) plugin.getLogger().info("Using locale \"" + langConfigFile.getName().substring(0, langConfigFile.getName().length() - 5) + "\" (Streamed from jar file)"); } + if (r == null) { + if (showMessages) plugin.getLogger().warning("Using default language values"); + plugin.debug("Using default language values (#1)"); + } + BufferedReader br = new BufferedReader(r); StringBuilder sb = new StringBuilder(); @@ -447,7 +465,7 @@ public class Config { plugin.getLogger().warning("Using default language values"); } - plugin.debug("Using default language values (#1)"); + plugin.debug("Using default language values (#2)"); plugin.debug(e); } } else { @@ -459,7 +477,7 @@ public class Config { plugin.getLogger().warning("Using default language values"); } - plugin.debug("Using default language values (#2)"); + plugin.debug("Using default language values (#3)"); plugin.debug(e); } } @@ -472,7 +490,7 @@ public class Config { plugin.getLogger().warning("Using default language values"); } - plugin.debug("Using default language values (#3)"); + plugin.debug("Using default language values (#4)"); plugin.debug(e); } } diff --git a/src/main/java/de/epiceric/shopchest/shop/Shop.java b/src/main/java/de/epiceric/shopchest/shop/Shop.java index f717893..b5ce79e 100644 --- a/src/main/java/de/epiceric/shopchest/shop/Shop.java +++ b/src/main/java/de/epiceric/shopchest/shop/Shop.java @@ -1,6 +1,7 @@ package de.epiceric.shopchest.shop; import de.epiceric.shopchest.ShopChest; +import de.epiceric.shopchest.config.Config; import de.epiceric.shopchest.config.Regex; import de.epiceric.shopchest.exceptions.ChestNotFoundException; import de.epiceric.shopchest.exceptions.NotEnoughSpaceException; @@ -11,6 +12,7 @@ import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.OfflinePlayer; +import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.Chest; @@ -32,6 +34,7 @@ public class Shop { private double buyPrice; private double sellPrice; private ShopType shopType; + private Config config; public Shop(int id, ShopChest plugin, OfflinePlayer vendor, ItemStack product, Location location, double buyPrice, double sellPrice, ShopType shopType) { this.id = id; @@ -42,6 +45,7 @@ public class Shop { this.buyPrice = buyPrice; this.sellPrice = sellPrice; this.shopType = shopType; + this.config = plugin.getShopChestConfig(); } public Shop(ShopChest plugin, OfflinePlayer vendor, ItemStack product, Location location, double buyPrice, double sellPrice, ShopType shopType) { @@ -56,14 +60,14 @@ public class Shop { Block b = location.getBlock(); if (b.getType() != Material.CHEST && b.getType() != Material.TRAPPED_CHEST) { ChestNotFoundException ex = new ChestNotFoundException(String.format("No Chest found in world '%s' at location: %d; %d; %d", b.getWorld().getName(), b.getX(), b.getY(), b.getZ())); - plugin.getShopUtils().removeShop(this, plugin.getShopChestConfig().remove_shop_on_error); + plugin.getShopUtils().removeShop(this, config.remove_shop_on_error); if (showConsoleMessages) plugin.getLogger().severe(ex.getMessage()); plugin.debug("Failed to create shop (#" + id + ")"); plugin.debug(ex); return false; - } else if ((b.getRelative(BlockFace.UP).getType() != Material.AIR) && plugin.getShopChestConfig().show_shop_items) { + } else if ((b.getRelative(BlockFace.UP).getType() != Material.AIR) && config.show_shop_items) { NotEnoughSpaceException ex = new NotEnoughSpaceException(String.format("No space above chest in world '%s' at location: %d; %d; %d", b.getWorld().getName(), b.getX(), b.getY(), b.getZ())); - plugin.getShopUtils().removeShop(this, plugin.getShopChestConfig().remove_shop_on_error); + plugin.getShopUtils().removeShop(this, config.remove_shop_on_error); if (showConsoleMessages) plugin.getLogger().severe(ex.getMessage()); plugin.debug("Failed to create shop (#" + id + ")"); plugin.debug(ex); @@ -114,7 +118,7 @@ public class Shop { * Call this after {@link #createHologram()}, because it depends on the hologram's location */ private void createItem() { - if (plugin.getShopChestConfig().show_shop_items) { + if (config.show_shop_items) { plugin.debug("Creating item (#" + id + ")"); Location itemLocation; @@ -138,86 +142,108 @@ public class Shop { private void createHologram() { plugin.debug("Creating hologram (#" + id + ")"); - boolean doubleChest; - - Chest[] chests = new Chest[2]; - Block b = location.getBlock(); InventoryHolder ih = getInventoryHolder(); if (ih == null) return; + Chest[] chests = new Chest[2]; + boolean doubleChest; + if (ih instanceof DoubleChest) { DoubleChest dc = (DoubleChest) ih; - Chest r = (Chest) dc.getRightSide(); Chest l = (Chest) dc.getLeftSide(); chests[0] = r; chests[1] = l; - doubleChest = true; - } else { - doubleChest = false; chests[0] = (Chest) ih; + doubleChest = false; } + boolean twoLinePrices = config.two_line_prices; + + String[] holoText = getHologramText(twoLinePrices ? 3 : 2); + Location holoLocation = getHologramLocation(doubleChest, chests, holoText); + + hologram = new Hologram(plugin, holoText, holoLocation); + } + + private String[] getHologramText(int length) { + String[] holoText = new String[length]; + + holoText[0] = LanguageUtils.getMessage(LocalizedMessage.Message.HOLOGRAM_FORMAT, + new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(product.getAmount())), + new LocalizedMessage.ReplacedRegex(Regex.ITEM_NAME, LanguageUtils.getItemName(product))); + + if ((buyPrice <= 0) && (sellPrice > 0)) { + holoText[1] = LanguageUtils.getMessage(LocalizedMessage.Message.HOLOGRAM_SELL, + new LocalizedMessage.ReplacedRegex(Regex.SELL_PRICE, String.valueOf(sellPrice))); + } else if ((buyPrice > 0) && (sellPrice <= 0)) { + holoText[1] = LanguageUtils.getMessage(LocalizedMessage.Message.HOLOGRAM_BUY, + new LocalizedMessage.ReplacedRegex(Regex.BUY_PRICE, String.valueOf(buyPrice))); + } else { + if (length == 2) { + holoText[1] = LanguageUtils.getMessage(LocalizedMessage.Message.HOLOGRAM_BUY_SELL, + new LocalizedMessage.ReplacedRegex(Regex.BUY_PRICE, String.valueOf(buyPrice)), + new LocalizedMessage.ReplacedRegex(Regex.SELL_PRICE, String.valueOf(sellPrice))); + } else { + holoText[1] = LanguageUtils.getMessage(LocalizedMessage.Message.HOLOGRAM_BUY, + new LocalizedMessage.ReplacedRegex(Regex.BUY_PRICE, String.valueOf(buyPrice))); + holoText[2] = LanguageUtils.getMessage(LocalizedMessage.Message.HOLOGRAM_SELL, + new LocalizedMessage.ReplacedRegex(Regex.SELL_PRICE, String.valueOf(sellPrice))); + } + } + + return holoText; + } + + private Location getHologramLocation(boolean doubleChest, Chest[] chests, String[] holoText) { + Block b = location.getBlock(); Location holoLocation; - String[] holoText = new String[plugin.getShopChestConfig().two_line_prices ? 3 : 2]; + + World w = b.getWorld(); + int x = b.getX(); + int y = b.getY(); + int z = b.getZ(); if (doubleChest) { - Chest r = chests[0]; Chest l = chests[1]; if (b.getLocation().equals(r.getLocation())) { - - if (r.getX() != l.getX()) - holoLocation = new Location(b.getWorld(), b.getX(), b.getY() - 0.6, b.getZ() + 0.5); - else if (r.getZ() != l.getZ()) - holoLocation = new Location(b.getWorld(), b.getX() + 0.5, b.getY() - 0.6, b.getZ()); - else holoLocation = new Location(b.getWorld(), b.getX() + 0.5, b.getY() - 0.6, b.getZ() + 0.5); - + if (r.getX() != l.getX()) { + holoLocation = new Location(w, x, y - 0.6, z + 0.5); + } else if (r.getZ() != l.getZ()) { + holoLocation = new Location(w, x + 0.5, y - 0.6, z); + } else { + holoLocation = new Location(w, x + 0.5, y - 0.6, z + 0.5); + } } else { - - if (r.getX() != l.getX()) - holoLocation = new Location(b.getWorld(), b.getX() + 1, b.getY() - 0.6, b.getZ() + 0.5); - else if (r.getZ() != l.getZ()) - holoLocation = new Location(b.getWorld(), b.getX() + 0.5, b.getY() - 0.6, b.getZ() + 1); - else holoLocation = new Location(b.getWorld(), b.getX() + 0.5, b.getY() - 0.6, b.getZ() + 0.5); - + if (r.getX() != l.getX()) { + holoLocation = new Location(w, x + 1, y - 0.6, z + 0.5); + } else if (r.getZ() != l.getZ()) { + holoLocation = new Location(w, x + 0.5, y - 0.6, z + 1); + } else { + holoLocation = new Location(w, x + 0.5, y - 0.6, z + 0.5); + } } - - } else holoLocation = new Location(b.getWorld(), b.getX() + 0.5, b.getY() - 0.6, b.getZ() + 0.5); - - holoText[0] = LanguageUtils.getMessage(LocalizedMessage.Message.HOLOGRAM_FORMAT, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(product.getAmount())), - new LocalizedMessage.ReplacedRegex(Regex.ITEM_NAME, LanguageUtils.getItemName(product))); - - if ((buyPrice <= 0) && (sellPrice > 0)) { - holoText[1] = LanguageUtils.getMessage(LocalizedMessage.Message.HOLOGRAM_SELL, new LocalizedMessage.ReplacedRegex(Regex.SELL_PRICE, String.valueOf(sellPrice))); - } else if ((buyPrice > 0) && (sellPrice <= 0)) { - holoText[1] = LanguageUtils.getMessage(LocalizedMessage.Message.HOLOGRAM_BUY, new LocalizedMessage.ReplacedRegex(Regex.BUY_PRICE, String.valueOf(buyPrice))); } else { - if (holoText.length == 2) { - holoText[1] = LanguageUtils.getMessage(LocalizedMessage.Message.HOLOGRAM_BUY_SELL, new LocalizedMessage.ReplacedRegex(Regex.BUY_PRICE, String.valueOf(buyPrice)), - new LocalizedMessage.ReplacedRegex(Regex.SELL_PRICE, String.valueOf(sellPrice))); - } else { - holoText[1] = LanguageUtils.getMessage(LocalizedMessage.Message.HOLOGRAM_BUY, new LocalizedMessage.ReplacedRegex(Regex.BUY_PRICE, String.valueOf(buyPrice))); - holoText[2] = LanguageUtils.getMessage(LocalizedMessage.Message.HOLOGRAM_SELL, new LocalizedMessage.ReplacedRegex(Regex.SELL_PRICE, String.valueOf(sellPrice))); - } + holoLocation = new Location(w, x + 0.5, y - 0.6, z + 0.5); } - holoLocation.add(0, plugin.getShopChestConfig().hologram_lift, 0); + holoLocation.add(0, config.hologram_lift, 0); - if (plugin.getShopChestConfig().two_line_prices) { + if (config.two_line_prices) { if (holoText.length == 3 && holoText[2] != null) { - holoLocation.add(0, plugin.getShopChestConfig().two_line_hologram_lift, 0); + holoLocation.add(0, config.two_line_hologram_lift, 0); } else { - holoLocation.add(0, plugin.getShopChestConfig().one_line_hologram_lift, 0); + holoLocation.add(0, config.one_line_hologram_lift, 0); } } - hologram = new Hologram(plugin, holoText, holoLocation); + return holoLocation; } /**