From 3ffbb861d1be5fd3331bdfcd1e5ef4ebdbd2a0ca Mon Sep 17 00:00:00 2001 From: Auxilor Date: Tue, 3 Aug 2021 17:00:49 +0100 Subject: [PATCH] More FastItemStack changes --- .../com/willfp/eco/core/display/Display.java | 4 ---- .../willfp/eco/core/fast/FastItemStack.java | 23 ++++++++----------- .../proxy/v1_16_R3/fast/EcoFastItemStack.java | 17 ++++++-------- .../proxy/v1_17_R1/fast/EcoFastItemStack.java | 17 ++++++-------- 4 files changed, 24 insertions(+), 37 deletions(-) diff --git a/eco-api/src/main/java/com/willfp/eco/core/display/Display.java b/eco-api/src/main/java/com/willfp/eco/core/display/Display.java index c1f46bae..b7fa8cad 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/display/Display.java +++ b/eco-api/src/main/java/com/willfp/eco/core/display/Display.java @@ -1,9 +1,7 @@ package com.willfp.eco.core.display; -import com.willfp.eco.core.fast.FastItemStack; import lombok.experimental.UtilityClass; import org.apache.commons.lang.Validate; -import org.bukkit.Bukkit; import org.bukkit.NamespacedKey; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -77,8 +75,6 @@ public class Display { return itemStack; // return early if there's no customization of the item } - Bukkit.getLogger().info((FastItemStack.wrap(itemStack).isModifyingInstance() == (FastItemStack.wrap(itemStack).unwrap() == itemStack)) + " amogus?"); - Map pluginVarArgs = new HashMap<>(); for (DisplayPriority priority : DisplayPriority.values()) { diff --git a/eco-api/src/main/java/com/willfp/eco/core/fast/FastItemStack.java b/eco-api/src/main/java/com/willfp/eco/core/fast/FastItemStack.java index 27caace2..42bce47f 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/fast/FastItemStack.java +++ b/eco-api/src/main/java/com/willfp/eco/core/fast/FastItemStack.java @@ -12,7 +12,9 @@ import java.util.Map; *

* If the ItemStack wrapped is a CraftItemStack, then the instance will be modified, allowing for set methods to work. *

- * Otherwise, the FastItemStack must then be unwrapped to get a bukkit copy. + * Otherwise, apply() must be called in order to apply the changes. + *

+ * apply() will call getItemMeta and setItemMeta which will hurt performance, however this will still be faster. */ public interface FastItemStack { /** @@ -34,20 +36,15 @@ public interface FastItemStack { boolean checkStored); /** - * Unwrap an ItemStack. - * - * @return The bukkit ItemStack. - */ - ItemStack unwrap(); - - /** - * If the FastItemStack modifies the actual ItemStack instance or a copy. + * Apply the changes made in FastItemStack. *

- * If a copy, then {@link FastItemStack#unwrap()} must be called in order to obtain the modified Bukkit ItemStack. - * - * @return If the ItemStack wrapped is a CraftItemStack, allowing for direct modification. + * If the ItemStack was a CraftItemStack, then no code will run - the changes are automatically applied. + *

+ * If the ItemStack wasn't a CraftItemStack, then the unwrapped ItemStack's ItemMeta will be applied to the original ItemStack. + *

+ * You should always call apply() if you have used any set methods. */ - boolean isModifyingInstance(); + void apply(); /** * Wrap an ItemStack to create a FastItemStack. diff --git a/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/eco/proxy/v1_16_R3/fast/EcoFastItemStack.java b/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/eco/proxy/v1_16_R3/fast/EcoFastItemStack.java index 9a37ce8a..67214bcb 100644 --- a/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/eco/proxy/v1_16_R3/fast/EcoFastItemStack.java +++ b/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/eco/proxy/v1_16_R3/fast/EcoFastItemStack.java @@ -18,16 +18,16 @@ import java.util.Map; public class EcoFastItemStack implements FastItemStack { private final ItemStack handle; private final boolean isCIS; - private final CraftItemStack cis; + private final org.bukkit.inventory.ItemStack bukkit; public EcoFastItemStack(@NotNull final org.bukkit.inventory.ItemStack itemStack) { this.handle = FastItemStackUtils.getNMSStack(itemStack); if (itemStack instanceof CraftItemStack craftItemStack) { this.isCIS = true; - this.cis = craftItemStack; + this.bukkit = craftItemStack; } else { this.isCIS = false; - this.cis = null; + this.bukkit = itemStack; } } @@ -67,12 +67,9 @@ public class EcoFastItemStack implements FastItemStack { } @Override - public org.bukkit.inventory.ItemStack unwrap() { - return this.isCIS ? cis : CraftItemStack.asCraftMirror(handle); - } - - @Override - public boolean isModifyingInstance() { - return isCIS; + public void apply() { + if (!this.isCIS) { + bukkit.setItemMeta(CraftItemStack.asCraftMirror(handle).getItemMeta()); + } } } diff --git a/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/fast/EcoFastItemStack.java b/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/fast/EcoFastItemStack.java index 8beae5da..61272b95 100644 --- a/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/fast/EcoFastItemStack.java +++ b/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/fast/EcoFastItemStack.java @@ -18,16 +18,16 @@ import java.util.Map; public class EcoFastItemStack implements FastItemStack { private final ItemStack handle; private final boolean isCIS; - private final CraftItemStack cis; + private final org.bukkit.inventory.ItemStack bukkit; public EcoFastItemStack(@NotNull final org.bukkit.inventory.ItemStack itemStack) { this.handle = FastItemStackUtils.getNMSStack(itemStack); if (itemStack instanceof CraftItemStack craftItemStack) { this.isCIS = true; - this.cis = craftItemStack; + this.bukkit = craftItemStack; } else { this.isCIS = false; - this.cis = null; + this.bukkit = itemStack; } } @@ -67,12 +67,9 @@ public class EcoFastItemStack implements FastItemStack { } @Override - public org.bukkit.inventory.ItemStack unwrap() { - return this.isCIS ? cis : CraftItemStack.asCraftMirror(handle); - } - - @Override - public boolean isModifyingInstance() { - return isCIS; + public void apply() { + if (!this.isCIS) { + bukkit.setItemMeta(CraftItemStack.asCraftMirror(handle).getItemMeta()); + } } }