From 0f00a1e452e484bf0b9b9b753b2dbc2de2d31a64 Mon Sep 17 00:00:00 2001
From: Ghost_chu <2908803755@qq.com>
Date: Thu, 29 Nov 2018 16:33:46 +0800
Subject: [PATCH] BKCommonLib support for Advanced display-item protection.
Former-commit-id: c0684123ae1c7c4d065a0c6c192f4fec836096d6 [formerly 63482456f7d03fbe69be15961276f7a6bc363ceb]
Former-commit-id: 87c0be8bbd839cef194a85f85de0ff0081ad0455
---
pom.xml | 12 +++-
.../Listeners/BKCommonLibListener.java | 42 +++++++++++++
.../Listeners/DisplayProtectionListener.java | 2 +-
.../org/maxgamer/quickshop/QuickShop.java | 10 +++-
.../maxgamer/quickshop/Shop/DisplayItem.java | 8 ++-
.../quickshop/Shop/DisplayItemStorage.java | 59 +++++++++++++++++++
.../maxgamer/quickshop/Shop/ShopManager.java | 26 ++++----
.../org/maxgamer/quickshop/Util/Util.java | 43 ++++++++++++++
src/main/resources/config.yml | 7 ++-
src/main/resources/plugin.yml | 2 +-
10 files changed, 194 insertions(+), 17 deletions(-)
create mode 100644 src/main/java/org/maxgamer/quickshop/Listeners/BKCommonLibListener.java
create mode 100644 src/main/java/org/maxgamer/quickshop/Shop/DisplayItemStorage.java
diff --git a/pom.xml b/pom.xml
index 383733cbb..37b5e61d3 100644
--- a/pom.xml
+++ b/pom.xml
@@ -13,12 +13,10 @@
repo
-
org.maxgamer.quickshop
Ghost_chu
-
https://github.com/${developer}/${name}/issues
GitHub Issues
@@ -75,6 +73,10 @@
dmulloy2-repo
http://repo.dmulloy2.net/nexus/repository/public/
+
+ MG-Dev Jenkins CI Maven Repository
+ https://ci.mg-dev.eu/plugin/repository/everything
+
@@ -91,6 +93,12 @@
1.6
provided
+
+ com.bergerkiller.bukkit
+ BKCommonLib
+ 1.13.2-v1-SNAPSHOT
+ provided
+
com.onarandombox.multiversecore
Multiverse-Core
diff --git a/src/main/java/org/maxgamer/quickshop/Listeners/BKCommonLibListener.java b/src/main/java/org/maxgamer/quickshop/Listeners/BKCommonLibListener.java
new file mode 100644
index 000000000..d639d8a2a
--- /dev/null
+++ b/src/main/java/org/maxgamer/quickshop/Listeners/BKCommonLibListener.java
@@ -0,0 +1,42 @@
+package org.maxgamer.quickshop.Listeners;
+
+import org.bukkit.Bukkit;
+import org.bukkit.Material;
+import org.bukkit.entity.EntityType;
+import org.bukkit.entity.Item;
+import org.bukkit.event.EventHandler;
+import org.bukkit.event.Listener;
+import org.maxgamer.quickshop.Shop.DisplayItem;
+import org.maxgamer.quickshop.Shop.DisplayItemStorage;
+import org.maxgamer.quickshop.Util.Util;
+
+import com.bergerkiller.bukkit.common.events.EntityMoveEvent;
+
+public class BKCommonLibListener implements Listener {
+ public BKCommonLibListener() {
+ Bukkit.getPluginManager().registerEvents(this,Bukkit.getPluginManager().getPlugin("BKCommonLib"));
+
+ }
+ @EventHandler
+ public void onEntityMove(EntityMoveEvent e) {
+ if(e.getEntityType()!=EntityType.DROPPED_ITEM) { //Only check we need
+ return;
+ }
+ Item item = (Item)e.getEntity();
+ if(DisplayItem.checkShopItem(item.getItemStack())) {
+ if((e.getFromX()==e.getToX())&&(e.getFromZ()==e.getToZ())) //Spam
+ return;
+ if(e.getEntity().getLocation().getBlock().getType()==Material.WATER) {
+ e.getEntity().remove();
+ return;
+ }
+ if(Util.canBeShop(e.getEntity().getLocation().clone().add(0, -1, 0).getBlock())) {
+ e.getEntity().teleport(DisplayItemStorage.getDisplayLocation(e.getEntity().getUniqueId()));
+ return;
+ }else {
+ e.getEntity().remove();
+ return;
+ }
+ }
+ }
+}
diff --git a/src/main/java/org/maxgamer/quickshop/Listeners/DisplayProtectionListener.java b/src/main/java/org/maxgamer/quickshop/Listeners/DisplayProtectionListener.java
index f789e7cfd..274a9233b 100644
--- a/src/main/java/org/maxgamer/quickshop/Listeners/DisplayProtectionListener.java
+++ b/src/main/java/org/maxgamer/quickshop/Listeners/DisplayProtectionListener.java
@@ -28,7 +28,7 @@ public class DisplayProtectionListener implements Listener {
public DisplayProtectionListener(QuickShop plugin) {
this.plugin = plugin;
}
-
+
@EventHandler(ignoreCancelled = true)
public void onInventoryOpen(InventoryOpenEvent event) {
for (int i = 0; i entry : limits.entrySet()) {
@@ -168,11 +169,18 @@ public class QuickShop extends JavaPlugin {
// added for compatibility reasons with OpenInv - see
// https://github.com/KaiKikuchi/QuickShop/issues/139
if(getConfig().getBoolean("plugin.OpenInv")) {
-
this.openInvPlugin = Bukkit.getPluginManager().getPlugin("OpenInv");
if(this.openInvPlugin!=null)
getLogger().info("Successfully loaded OpenInv support!");
}
+ if(getConfig().getBoolean("plugin.BKCommonLib")) {
+
+ this.BKCommonLibPlugin = Bukkit.getPluginManager().getPlugin("BKCommonLib");
+ if(this.BKCommonLibPlugin!=null && Bukkit.getPluginManager().getPlugin("BKCommonLib").isEnabled()) {
+ new BKCommonLibListener();
+ getLogger().info("Successfully loaded BKCommonLib support!");
+ }
+ }
if (getConfig().getBoolean("plugin.ProtocolLib")) {
if (Bukkit.getPluginManager().getPlugin("ProtocolLib") != null) {
protocolLibPlugin = (ProtocolLib) Bukkit.getPluginManager().getPlugin("ProtocolLib");
diff --git a/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java b/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java
index e5d627f6f..58bebb5b6 100644
--- a/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java
+++ b/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java
@@ -183,6 +183,7 @@ public class DisplayItem {
return;
}
this.item = shop.getLocation().getWorld().dropItem(dispLoc, this.iStack);
+ DisplayItemStorage.addDisplayItemShop(item.getUniqueId(), shop);
this.item.setVelocity(new Vector(0, 0.1, 0));
try {
this.safeGuard(this.item);
@@ -210,7 +211,9 @@ public class DisplayItem {
item.setCustomName("QuickShop");
iMeta.setDisplayName("QuickShop");
}
-
+ item.setPortalCooldown(Integer.MAX_VALUE);
+ item.setSilent(true);
+ item.setInvulnerable(true);
java.util.List lore = new ArrayList();
lore.add("QuickShop DisplayItem");
iMeta.setLore(lore);
@@ -260,7 +263,9 @@ public class DisplayItem {
}
}
}
+ DisplayItemStorage.id2ShopMap.clear();
return removed;
+
}
/**
@@ -270,6 +275,7 @@ public class DisplayItem {
if (this.item == null)
return;
this.item.remove();
+ DisplayItemStorage.removeDisplayItemByUUID(this.item.getUniqueId());
this.item = null;
}
diff --git a/src/main/java/org/maxgamer/quickshop/Shop/DisplayItemStorage.java b/src/main/java/org/maxgamer/quickshop/Shop/DisplayItemStorage.java
new file mode 100644
index 000000000..b31086926
--- /dev/null
+++ b/src/main/java/org/maxgamer/quickshop/Shop/DisplayItemStorage.java
@@ -0,0 +1,59 @@
+package org.maxgamer.quickshop.Shop;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.UUID;
+
+import org.bukkit.Location;
+
+public class DisplayItemStorage {
+ static Map id2ShopMap = new HashMap<>();
+ public Shop getDisplayItemShop(UUID uuid) {
+ return id2ShopMap.get(uuid);
+ }
+ public static void addDisplayItemShop(UUID uuid,Shop shop) {
+ id2ShopMap.put(uuid, shop);
+ }
+ public static Location getDisplayLocation(UUID uuid) {
+ Shop shop = id2ShopMap.get(uuid);
+ if(shop==null)
+ return null;
+ return shop.getLocation().clone().add(0.5, 1.2, 0.5);
+ }
+ public static UUID getShopDisplayItem(Shop shop) {
+ Set> set=id2ShopMap.entrySet();
+ Iterator> it=set.iterator();
+ while(it.hasNext()) {
+ Entry entry =(Entry)it.next();
+ if(entry.getValue().equals(shop)) {
+ return entry.getKey();
+ }
+ }
+ return null;
+ }
+
+ public static void removeDisplayItemByShop(Shop shop) {
+ try {
+ Set> set = id2ShopMap.entrySet();
+ Iterator> it = set.iterator();
+ while (it.hasNext()) {
+ Entry entry = (Entry) it.next();
+ if (entry.getValue().equals(shop)) {
+ id2ShopMap.remove(entry.getKey());
+ }
+ }
+ } catch (Exception e) {
+
+ }
+ }
+ public static void removeDisplayItemByUUID(UUID uuid) {
+ try {
+ id2ShopMap.remove(uuid);
+ }catch (Exception e) {
+
+ }
+ }
+}
diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java b/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java
index 1778a36eb..1ada170bf 100644
--- a/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java
+++ b/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java
@@ -17,6 +17,7 @@ import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
+import org.bukkit.block.data.Waterlogged;
import org.bukkit.entity.Player;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
@@ -571,8 +572,8 @@ public class ShopManager {
p.sendMessage(MsgUtil.getMessage("chest-was-removed"));
return;
}
- if (info.getLocation().getWorld().getBlockAt(info.getLocation()).getType()==Material.ENDER_CHEST) {
- if(!p.hasPermission("quickshop.create.enderchest"))
+ if (info.getLocation().getWorld().getBlockAt(info.getLocation()).getType() == Material.ENDER_CHEST) {
+ if (!p.hasPermission("quickshop.create.enderchest"))
return;
}
// Price per item
@@ -593,10 +594,8 @@ public class ShopManager {
// p.sendMessage(ChatColor.RED+"Restricted prices for
// "+info.getItem().getType()+": min "+priceRestriction.getKey()+", max
// "+priceRestriction.getValue());
- p.sendMessage(
- MsgUtil.getMessage("restricted-prices", MsgUtil.getDisplayName(info.getItem()),
- String.valueOf(priceRestriction.getKey()),
- String.valueOf(priceRestriction.getValue())));
+ p.sendMessage(MsgUtil.getMessage("restricted-prices", MsgUtil.getDisplayName(info.getItem()),
+ String.valueOf(priceRestriction.getKey()), String.valueOf(priceRestriction.getValue())));
}
}
@@ -641,8 +640,7 @@ public class ShopManager {
createShop(shop);
Location loc = shop.getLocation();
plugin.log(p.getName() + " created a " + MsgUtil.getDisplayName(shop.getItem()) + " shop at ("
- + loc.getWorld().getName() + " - " + loc.getX() + "," + loc.getY() + "," + loc.getZ()
- + ")");
+ + loc.getWorld().getName() + " - " + loc.getX() + "," + loc.getY() + "," + loc.getZ() + ")");
if (!plugin.getConfig().getBoolean("shop.lock")) {
// Warn them if they haven't been warned since
// reboot
@@ -658,11 +656,19 @@ public class ShopManager {
* NOTICE: When player use /qs create command to create shop, sign will placed
* to wrong facing.
*/
- if (info.getSignBlock() != null && info.getSignBlock().getType() == Material.AIR
- && plugin.getConfig().getBoolean("shop.auto-sign")) {
+ if (info.getSignBlock() != null && plugin.getConfig().getBoolean("shop.auto-sign")) {
+ if(info.getSignBlock().getType() != Material.AIR&&info.getSignBlock().getType() != Material.WATER)
+ return;
+ boolean isWaterLogged = false;
+ if(info.getSignBlock().getType()==Material.WATER)
+ isWaterLogged = true;
final BlockState bs = info.getSignBlock().getState();
final BlockFace bf = info.getLocation().getBlock().getFace(info.getSignBlock());
bs.setType(Material.WALL_SIGN);
+ if(isWaterLogged) {
+ Waterlogged waterable = (Waterlogged)bs.getBlockData();
+ waterable.setWaterlogged(true); //Looks like sign directly put in water
+ }
final Sign sign = (Sign) bs.getData();
sign.setFacingDirection(bf);
bs.setData(sign);
diff --git a/src/main/java/org/maxgamer/quickshop/Util/Util.java b/src/main/java/org/maxgamer/quickshop/Util/Util.java
index 3aece69be..641fa8338 100644
--- a/src/main/java/org/maxgamer/quickshop/Util/Util.java
+++ b/src/main/java/org/maxgamer/quickshop/Util/Util.java
@@ -32,6 +32,8 @@ import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.BlockPosition;
import com.comphenix.protocol.wrappers.WrappedBlockData;
import com.comphenix.protocol.wrappers.WrappedChatComponent;
+
+import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.DecimalFormat;
@@ -50,6 +52,7 @@ public class Util {
private static final EnumMap> restrictedPrices = new EnumMap>(Material.class);
private static QuickShop plugin;
private static Method storageContents;
+ static Map timerMap = new HashMap();
public static void initialize() {
blacklist.clear();
@@ -753,10 +756,50 @@ public class Util {
}
}
+ private static Object serverInstance;
+ private static Field tpsField;
+ public static Double getTPS() {
+ try {
+ serverInstance = getNMSClass("MinecraftServer").getMethod("getServer").invoke(null);
+ tpsField = serverInstance.getClass().getField("recentTps");
+ } catch (NoSuchFieldException | SecurityException | IllegalAccessException | IllegalArgumentException
+ | InvocationTargetException | NoSuchMethodException e) {
+ e.printStackTrace();
+ }
+ try {
+ double[] tps = ((double[]) tpsField.get(serverInstance));
+ return tps[0];
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException(e);
+ }
+
+ }
+ private static Class> getNMSClass(String className) {
+ String name = Bukkit.getServer().getClass().getPackage().getName();
+ String version = name.substring(name.lastIndexOf('.') + 1);
+ try {
+ return Class.forName("net.minecraft.server." + version + "." + className);
+ } catch (ClassNotFoundException e) {
+ throw new RuntimeException(e);
+ }
+ }
public static void debugLog(String logs) {
if(plugin.getConfig().getBoolean("dev-mode")) {
plugin.getLogger().info("[DEBUG]"+logs);
}
}
+ public static UUID setTimer() {
+ UUID random = UUID.randomUUID();
+ timerMap.put(random, System.currentTimeMillis());
+ return random;
+ }
+ public static long getTimer(UUID uuid) {
+ return timerMap.get(uuid);
+ }
+ public static long endTimer(UUID uuid) {
+ long time =System.currentTimeMillis()-timerMap.get(uuid);
+ timerMap.remove(uuid);
+ return time;
+ }
}
diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml
index 43037cf6a..7e176c6a0 100644
--- a/src/main/resources/config.yml
+++ b/src/main/resources/config.yml
@@ -202,6 +202,11 @@ float:
# Enable or disable plugin support.
plugin:
+ #If you have ProtocolLib, QSRR will enable Sign Input GUI not normal chat
ProtocolLib: true
+ #If you have Multiverse-Core, QSRR will try load world when shop's world not exist and throw out exception auto repair the shop.
Multiverse-Core: true
- OpenInv: true
\ No newline at end of file
+ #If you have OpenInv, QSRR will enable EnderChest feature.
+ OpenInv: true
+ #If you have BKCommonLib, QSRR will enable advanced display-item protection.
+ BKCommonLib: true
\ No newline at end of file
diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml
index 17489cfc3..8e2fafc17 100644
--- a/src/main/resources/plugin.yml
+++ b/src/main/resources/plugin.yml
@@ -5,7 +5,7 @@ authors: [Netherfoam, Timtower, KaiNoMood, Ghost_chu, Mgazul]
description: Economy Shops plugin
#softdepend: [Herochat, Vault, Essentials, OpenInv] OLD Remove Herochat support.
-softdepend: [Vault, WorldGuard, Essentials, OpenInv, Multiverse-Core, ProtocolLib, CoreProtect]
+softdepend: [Vault, WorldGuard, Essentials, OpenInv, Multiverse-Core, ProtocolLib, CoreProtect, BKCommonLib]
api-version: 1.13