From bf559bdb6cedf4fd17b80a9033241b489158bd91 Mon Sep 17 00:00:00 2001 From: Kai Date: Fri, 13 Jan 2017 13:19:36 +0100 Subject: [PATCH] Added ultimate display-item antidupe system Add "display-items-check-ticks" to the config. Check default config for more info. --- .../org/maxgamer/quickshop/QuickShop.java | 43 +++++++++++++------ .../quickshop/Shop/ContainerShop.java | 23 ++++++++++ .../maxgamer/quickshop/Shop/DisplayItem.java | 1 + src/main/resources/config.yml | 3 ++ src/main/resources/plugin.yml | 5 ++- 5 files changed, 62 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/QuickShop.java b/src/main/java/org/maxgamer/quickshop/QuickShop.java index cb327b86f..ed3019f44 100644 --- a/src/main/java/org/maxgamer/quickshop/QuickShop.java +++ b/src/main/java/org/maxgamer/quickshop/QuickShop.java @@ -11,6 +11,7 @@ import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -29,6 +30,7 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionType; +import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitTask; import org.maxgamer.quickshop.Command.QS; import org.maxgamer.quickshop.Database.Database; @@ -43,8 +45,8 @@ import org.maxgamer.quickshop.Economy.Economy_Vault; import org.maxgamer.quickshop.Listeners.BlockListener; import org.maxgamer.quickshop.Listeners.ChatListener; import org.maxgamer.quickshop.Listeners.ChunkListener; -import org.maxgamer.quickshop.Listeners.InventoryListener; import org.maxgamer.quickshop.Listeners.HeroChatListener; +import org.maxgamer.quickshop.Listeners.InventoryListener; import org.maxgamer.quickshop.Listeners.LockListener; import org.maxgamer.quickshop.Listeners.PlayerListener; import org.maxgamer.quickshop.Listeners.WorldListener; @@ -111,6 +113,8 @@ public class QuickShop extends JavaPlugin { public static boolean debug = false; private Map> customItemsName = new HashMap>(); + + private int displayItemCheckTicks; /** The plugin metrics from Hidendra */ // public Metrics getMetrics(){ return metrics; } @@ -410,17 +414,31 @@ public class QuickShop extends JavaPlugin { if (getConfig().getInt("shop.find-distance") > 100) { getLogger().severe("Shop.find-distance is too high! Pick a number under 100!"); } - /** - * If the server has Spout we can get the names of custom items. Latest - * SpoutPlugin http://get.spout.org/1412/SpoutPlugin.jar - * http://build.spout.org/view/Legacy/job/SpoutPlugin/1412/ - */ -// if (Bukkit.getPluginManager().getPlugin("Spout") != null) { -// this.getLogger().info("Found Spout..."); -// this.useSpout = true; -// } else { -// this.useSpout = false; -// } + + if (display && displayItemCheckTicks>0) { + new BukkitRunnable() { + @Override + public void run() { + Iterator it = getShopManager().getShopIterator(); + while (it.hasNext()) { + Shop shop = it.next(); + if (shop instanceof ContainerShop) { + ContainerShop cShop = (ContainerShop) shop; + if (cShop.checkDisplayMoved()) { + log("Display item for "+shop+" is not on the correct location and has been removed. Probably someone is trying to cheat."); + for (Player player : getServer().getOnlinePlayers()) { + if (player.hasPermission("quickshop.alerts")) { + player.sendMessage(ChatColor.RED + "[QuickShop] Display item for "+shop+" is not on the correct location and has been removed. Probably someone is trying to cheat."); + } + } + cShop.getDisplayItem().remove(); + } + } + } + } + }.runTaskTimer(this, 1L, displayItemCheckTicks); + } + getLogger().info("QuickShop loaded!"); } @@ -434,6 +452,7 @@ public class QuickShop extends JavaPlugin { this.sneakCreate = this.getConfig().getBoolean("shop.sneak-to-create"); this.sneakTrade = this.getConfig().getBoolean("shop.sneak-to-trade"); this.priceChangeRequiresFee = this.getConfig().getBoolean("shop.price-change-requires-fee"); + this.displayItemCheckTicks = this.getConfig().getInt("shop.display-items-check-ticks"); MsgUtil.loadCfgMessages(); } diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java b/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java index 37871016d..c2d4f2261 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java @@ -629,6 +629,29 @@ public class ContainerShop implements Shop { } } } + + public boolean checkDisplayMoved() { + // don't check if the plugin doesn't know about the object + if (this.getDisplayItem() == null) { + return false; + } + + Item item = this.getDisplayItem().getItem(); + if (item == null) { + return false; + } + + if (item.isDead()) { + return false; + } + + // don't check if the chunk is not loaded + if (!item.getLocation().getWorld().isChunkLoaded(item.getLocation().getChunk())) { + return false; + } + + return this.getDisplayItem().getDisplayLocation().distanceSquared(item.getLocation()) > 0.2; + } public void onUnload() { if (this.getDisplayItem() != null) { diff --git a/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java b/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java index 50f39e17a..30930045b 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java @@ -101,6 +101,7 @@ public class DisplayItem { if (this.item == null) return; this.item.remove(); + this.item = null; } /** diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 3c76a17ae..e2fd98c80 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -79,6 +79,9 @@ shop: #Should we place display items on the chests? This may cause duped items, especially for modded servers. display-items: false + #The plugin will check if the display item is in a valid position every the specified amount of ticks. 0 to disable. + display-items-check-ticks: 0 + #Should we place item frames on chests? frame-items: false diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 36ada8ede..1341b6dec 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -54,4 +54,7 @@ permissions: default: op quickshop.taxexemption: description: Tax exemption - default: false \ No newline at end of file + default: false + quickshop.alerts: + description: Alert the player when someone tries to cheat with the display item + default: op \ No newline at end of file