From b375f3c7d39af51f7b50783a042620c8ae7cd8f2 Mon Sep 17 00:00:00 2001
From: Ghost_chu <2908803755@qq.com>
Date: Mon, 26 Aug 2019 16:03:05 +0800
Subject: [PATCH] Optimize the item check performance
Former-commit-id: bff5c31b4823a010c1334c80a5a4bc035718aa1b [formerly e53361720c6d92e8d85264edebffff272ada71fb]
Former-commit-id: b6769077a92db94cd48c9fc86668e1dcc38d7aab
---
pom.xml | 1 +
.../maxgamer/quickshop/Shop/DisplayItem.java | 47 ++++++++++++-------
.../quickshop/Shop/ShopProtectionFlag.java | 3 ++
3 files changed, 33 insertions(+), 18 deletions(-)
diff --git a/pom.xml b/pom.xml
index e6ae8bae7..80fc645cb 100644
--- a/pom.xml
+++ b/pom.xml
@@ -245,6 +245,7 @@
LangUtils
2.2-SNAPSHOT
+ provided
me.lucko.luckperms
diff --git a/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java b/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java
index f0ad8df33..cc7567916 100644
--- a/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java
+++ b/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java
@@ -1,6 +1,7 @@
package org.maxgamer.quickshop.Shop;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.List;
import com.google.gson.Gson;
@@ -18,8 +19,10 @@ import org.maxgamer.quickshop.Util.Util;
* cannot be interacted with.
*/
public interface DisplayItem {
+
/**
* Check the itemStack is contains protect flag.
+ *
* @param itemStack Target ItemStack
* @return Contains protect flag.
*/
@@ -36,31 +39,35 @@ public interface DisplayItem {
if (iMeta.hasDisplayName()) {
if (iMeta.getDisplayName().toLowerCase().contains("quickshop displayitem")) {
return true;
- }else{
- return false;
}
}
if (!iMeta.hasLore()) {
return false;
}
- List lores = iMeta.getLore();
- Gson gson = new Gson();
- for (String lore : lores) {
- try {
- ShopProtectionFlag shopProtectionFlag = gson.fromJson(lore, ShopProtectionFlag.class);
- if (shopProtectionFlag == null) {
- continue;
- }
- if (shopProtectionFlag.getMark() == null) {
- continue;
- }
- if ("QuickShop DisplayItem".equals(shopProtectionFlag.getMark())) {
- return true;
- }
- } catch (JsonSyntaxException e) {
- //Ignore
+ List lores = iMeta.getLore();
+ Gson gson = new Gson();
+ for (String lore : lores) {
+ try {
+ if (!lore.startsWith("{")) {
+ continue;
}
+ ShopProtectionFlag shopProtectionFlag = gson.fromJson(lore, ShopProtectionFlag.class);
+ if (shopProtectionFlag == null) {
+ continue;
+ }
+ if (ShopProtectionFlag.getDefaultMark().equals(shopProtectionFlag.getMark())) {
+ return true;
+ }
+ if(shopProtectionFlag.getShopLocation()!= null){
+ return true;
+ }
+ if(shopProtectionFlag.getItemStackString() != null){
+ return true;
+ }
+ } catch (JsonSyntaxException e) {
+ //Ignore
}
+ }
return false;
}
@@ -78,6 +85,9 @@ public interface DisplayItem {
return false;
}
ItemMeta iMeta = itemStack.getItemMeta();
+ if (iMeta == null) {
+ return false;
+ }
if (!iMeta.hasLore()) {
return false;
}
@@ -207,6 +217,7 @@ public interface DisplayItem {
/**
* Get plugin now is using which one DisplayType
+ *
* @return Using displayType.
*/
static DisplayType getNowUsing() {
diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ShopProtectionFlag.java b/src/main/java/org/maxgamer/quickshop/Shop/ShopProtectionFlag.java
index a2920625e..48289784d 100644
--- a/src/main/java/org/maxgamer/quickshop/Shop/ShopProtectionFlag.java
+++ b/src/main/java/org/maxgamer/quickshop/Shop/ShopProtectionFlag.java
@@ -15,4 +15,7 @@ public class ShopProtectionFlag {
this.shopLocation = shopLocation;
this.itemStackString = itemStackString;
}
+ public static String getDefaultMark(){
+ return "QuickShop DisplayItem";
+ }
}