diff --git a/src/main/java/org/maxgamer/quickshop/QuickShop.java b/src/main/java/org/maxgamer/quickshop/QuickShop.java index d017d0ce4..a31420953 100644 --- a/src/main/java/org/maxgamer/quickshop/QuickShop.java +++ b/src/main/java/org/maxgamer/quickshop/QuickShop.java @@ -152,6 +152,7 @@ public class QuickShop extends JavaPlugin { * The manager to check permissions. */ private static PermissionManager permissionManager; + private OngoingFeeWatcher ongoingFeeWatcher; /** * Get the Player's Shop limit. @@ -429,6 +430,7 @@ public class QuickShop extends JavaPlugin { displayWatcher = new DisplayWatcher(this); syncTaskWatcher = new SyncTaskWatcher(this); shopVaildWatcher = new ShopVaildWatcher(this); + ongoingFeeWatcher = new OngoingFeeWatcher(this); lockListener = new LockListener(this); Bukkit.getPluginManager().registerEvents(blockListener, this); Bukkit.getPluginManager().registerEvents(playerListener, this); @@ -464,6 +466,11 @@ public class QuickShop extends JavaPlugin { }.runTaskLater(this, 1); Util.debugLog("Registering shop watcher..."); shopVaildWatcher.runTaskTimer(this, 0, 20 * 60); + + if(getConfig().getBoolean("shop.ongoing-fee.enable")){ + getLogger().info("Ongoine fee feature is enabled."); + ongoingFeeWatcher.runTaskTimerAsynchronously(this, getConfig().getInt("shop.ongoing-fee.ticks"), getConfig().getInt("shop.ongoing-fee.ticks")); + } if(this.display){ if(getConfig().getBoolean("shop.display-auto-despawn")){ new BukkitRunnable(){ @@ -1002,7 +1009,14 @@ public class QuickShop extends JavaPlugin { getConfig().set("config-version", 63); selectedVersion = 63; } - + if(selectedVersion == 63){ //Ahhh fuck versions + getConfig().set("shop.ongoing-fee.enable", false); + getConfig().set("shop.ongoing-fee.ticks", 42000); + getConfig().set("shop.ongoing-fee.cost-per-shop", 2); + getConfig().set("shop.ongoing-fee.ignore-unlimited",true); + getConfig().set("config-version", 64); + selectedVersion = 64; + } saveConfig(); reloadConfig(); File file = new File(getDataFolder(), "example.config.yml"); diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java b/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java index d8e1a3644..ef35dded7 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java @@ -2,6 +2,7 @@ package org.maxgamer.quickshop.Shop; import com.lishid.openinv.OpenInv; import lombok.EqualsAndHashCode; +import lombok.ToString; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; @@ -31,6 +32,7 @@ import java.util.logging.Level; * ChestShop core */ @EqualsAndHashCode +@ToString public class ContainerShop implements Shop { private DisplayItem displayItem; @EqualsAndHashCode.Exclude diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java b/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java index ea5d5ceca..276aefae8 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java @@ -849,6 +849,24 @@ public class ShopManager { return new ShopIterator(); } + /** + * Returns all shops in the whole database, include unloaded. + * + * Make sure you have caching this, because this need a while to get all shops + * @return All shop in the database + */ + public Collection getAllShops(){ + //noinspection unchecked + HashMap>> worldsMap = (HashMap>>) getShops() + .clone(); + Collection shops = new ArrayList<>(); + for (HashMap> shopMapData : worldsMap.values()){ + for (HashMap shopData : shopMapData.values()){ + shops.addAll(shopData.values()); + } + } + return shops; + } /** * Returns a hashmap of World - Chunk - Shop * diff --git a/src/main/java/org/maxgamer/quickshop/Util/MsgUtil.java b/src/main/java/org/maxgamer/quickshop/Util/MsgUtil.java index 7b42e2da4..b8fccf38e 100644 --- a/src/main/java/org/maxgamer/quickshop/Util/MsgUtil.java +++ b/src/main/java/org/maxgamer/quickshop/Util/MsgUtil.java @@ -470,13 +470,17 @@ public class MsgUtil { Util.debugLog("Unknown error, send by plain text."); p.getPlayer().sendMessage(msgData[0] + msgData[1] + msgData[2]); } catch (ArrayIndexOutOfBoundsException e2){ - Util.debugLog("Message is legacy format, send as plain message..."); - p.getPlayer().sendMessage(message); + try{ + sendItemholochat(p.getPlayer(), msgData[0], Util.deserialize(msgData[1]),""); + }catch (Exception any){ + //Normal msg + p.getPlayer().sendMessage(message); + } + } } } } - @SneakyThrows public static void sendItemholochat(@NotNull Player player, @NotNull String left, @NotNull ItemStack itemStack, @NotNull String right) { String json = ItemNMS.saveJsonfromNMS(itemStack); @@ -1059,6 +1063,11 @@ public class MsgUtil { setAndUpdate("language-version", 21); selectedVersion = 21; } + if (selectedVersion == 21) { + setAndUpdate("shop-removed-cause-ongoing-fee", "&cYou shop at {0} was removed cause you had no enough money to keep it!"); + setAndUpdate("language-version", 22); + selectedVersion = 22; + } messagei18n.save(messageFile); } diff --git a/src/main/java/org/maxgamer/quickshop/Watcher/OngoingFeeWatcher.java b/src/main/java/org/maxgamer/quickshop/Watcher/OngoingFeeWatcher.java new file mode 100644 index 000000000..3694a04a8 --- /dev/null +++ b/src/main/java/org/maxgamer/quickshop/Watcher/OngoingFeeWatcher.java @@ -0,0 +1,69 @@ +package org.maxgamer.quickshop.Watcher; + +import org.bukkit.Bukkit; +import org.bukkit.scheduler.BukkitRunnable; +import org.jetbrains.annotations.NotNull; +import org.maxgamer.quickshop.QuickShop; +import org.maxgamer.quickshop.Shop.Shop; +import org.maxgamer.quickshop.Util.MsgUtil; +import org.maxgamer.quickshop.Util.Util; + +import java.util.UUID; + +/** + * Check the shops after server booted up, make sure shop can correct self-deleted when container lost. + */ +public class OngoingFeeWatcher extends BukkitRunnable { + private QuickShop plugin; + + public OngoingFeeWatcher(@NotNull QuickShop plugin) { + this.plugin = plugin; + } + + @Override + public void run() { + Util.debugLog("Run task for ongoing fee..."); + if (plugin.getEconomy() == null) { + Util.debugLog("Economy hadn't get ready."); + return; + } + int cost = plugin.getConfig().getInt("shop.ongoing-fee.cost-per-shop"); + boolean allowLoan = plugin.getConfig().getBoolean("shop.allow-economy-loan"); + boolean ignoreUnlimited = plugin.getConfig().getBoolean("shop.ongoing-fee.ignore-unlimited"); + for (Shop shop : plugin.getShopManager().getAllShops()) { + if (!shop.isUnlimited() || !ignoreUnlimited) { + UUID shopOwner = shop.getOwner(); + if (!allowLoan) { + //Disallow loan + if (plugin.getEconomy().getBalance(shopOwner) < cost) { + removeShop(shop); + continue; + } + } + boolean success = plugin.getEconomy().withdraw(shop.getOwner(), cost); + if (!success) { + removeShop(shop); + continue; + } + try { + //noinspection ConstantConditions + plugin.getEconomy().deposit(Bukkit.getOfflinePlayer(plugin.getConfig().getString("tax")).getUniqueId(), cost); + } catch (Exception ignored) { + } + }else{ + Util.debugLog("Shop was ignored for ongoing fee cause it is unlimited and ignoreUnlimited = true : "+shop.toString()); + } + + } + } + + /** + * Remove shop and send alert to shop owner + * + * @param shop The shop was remove cause no enough ongoing fee + */ + public void removeShop(@NotNull Shop shop) { + Bukkit.getScheduler().runTask(plugin, (@NotNull Runnable) shop::delete); + MsgUtil.send(shop.getOwner(), MsgUtil.getMessageOfflinePlayer("shop-removed-cause-ongoing-fee", Bukkit.getOfflinePlayer(shop.getOwner()), "World:" + shop.getLocation().getWorld().getName() + " X:" + shop.getLocation().getBlockX() + " Y:" + shop.getLocation().getBlockY() + " Z:" + shop.getLocation().getBlockZ()), shop.isUnlimited()); + } +} \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index e446198a9..df3423fd6 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,7 +1,7 @@ #This is example config, don't direct modify this file, all changes will revert when you reload the plugin. #Do not touch if you not clear know about this -config-version: 63 +config-version: 64 #Select the language you want to use, (e.g de), use only supported language codes from the list below. #If you use a not exist/not support language, plugin will move to use en_US to keep everything working. @@ -245,6 +245,16 @@ shop: #The word to trade all items in player inventory word-for-trade-all-items: "all" + #The shop ongoing fee, if a shop's owner had no enough money, shop will auto-remove. + ongoing-fee: + enable: false + #How often we will take the ongoing-fee from the players? + ticks: 42000 + #How many money we take from the shop's owner every times? + cost-per-shop: 2 + #Should we ignore the unlimited shop? + ignore-unlimited: true + #List of items that can't be sold in shops. Anyone with quickshop.bypass. can bypass it blacklist: - Bedrock diff --git a/src/main/resources/messages-en.yml b/src/main/resources/messages-en.yml index 7b93d740a..afe54cf9b 100644 --- a/src/main/resources/messages-en.yml +++ b/src/main/resources/messages-en.yml @@ -11,7 +11,7 @@ translation-version: "&c&lSupport Version: &b&lReremake" translation-country: "&c&lLanguage Zone: &b&lEnglish (en_US)" # ====================================================================== -language-version: 20 +language-version: 22 not-looking-at-shop: "&cNo QuickShop was found, you must be looking at one." no-anythings-in-your-hand: "&cThere is nothing in your hand." @@ -226,4 +226,5 @@ updatenotify: qualityverifyed: "[Quality]" github: "[Github]" spigotmc: "[SpigotMC]" - bukkitdev: "[BukkitDev]" \ No newline at end of file + bukkitdev: "[BukkitDev]" +shop-removed-cause-ongoing-fee: "&cYou shop at {0} was removed cause you had no enough money to keep it!" \ No newline at end of file