More FastItemStack changes

This commit is contained in:
Auxilor
2021-08-03 17:00:49 +01:00
parent f8fad15f0b
commit 3ffbb861d1
4 changed files with 24 additions and 37 deletions

View File

@@ -1,9 +1,7 @@
package com.willfp.eco.core.display; package com.willfp.eco.core.display;
import com.willfp.eco.core.fast.FastItemStack;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
import org.apache.commons.lang.Validate; import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@@ -77,8 +75,6 @@ public class Display {
return itemStack; // return early if there's no customization of the item 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<String, Object[]> pluginVarArgs = new HashMap<>(); Map<String, Object[]> pluginVarArgs = new HashMap<>();
for (DisplayPriority priority : DisplayPriority.values()) { for (DisplayPriority priority : DisplayPriority.values()) {

View File

@@ -12,7 +12,9 @@ import java.util.Map;
* <p> * <p>
* If the ItemStack wrapped is a CraftItemStack, then the instance will be modified, allowing for set methods to work. * If the ItemStack wrapped is a CraftItemStack, then the instance will be modified, allowing for set methods to work.
* <p> * <p>
* Otherwise, the FastItemStack must then be unwrapped to get a bukkit copy. * Otherwise, apply() must be called in order to apply the changes.
* <p>
* apply() <b>will</b> call getItemMeta and setItemMeta which will hurt performance, however this will still be faster.
*/ */
public interface FastItemStack { public interface FastItemStack {
/** /**
@@ -34,20 +36,15 @@ public interface FastItemStack {
boolean checkStored); boolean checkStored);
/** /**
* Unwrap an ItemStack. * Apply the changes made in FastItemStack.
*
* @return The bukkit ItemStack.
*/
ItemStack unwrap();
/**
* If the FastItemStack modifies the actual ItemStack instance or a copy.
* <p> * <p>
* If a copy, then {@link FastItemStack#unwrap()} must be called in order to obtain the modified Bukkit ItemStack. * If the ItemStack was a CraftItemStack, then no code will run - the changes are automatically applied.
* * <p>
* @return If the ItemStack wrapped is a CraftItemStack, allowing for direct modification. * If the ItemStack wasn't a CraftItemStack, then the unwrapped ItemStack's ItemMeta will be applied to the original ItemStack.
* <p>
* You should <b>always</b> call apply() if you have used any set methods.
*/ */
boolean isModifyingInstance(); void apply();
/** /**
* Wrap an ItemStack to create a FastItemStack. * Wrap an ItemStack to create a FastItemStack.

View File

@@ -18,16 +18,16 @@ import java.util.Map;
public class EcoFastItemStack implements FastItemStack { public class EcoFastItemStack implements FastItemStack {
private final ItemStack handle; private final ItemStack handle;
private final boolean isCIS; private final boolean isCIS;
private final CraftItemStack cis; private final org.bukkit.inventory.ItemStack bukkit;
public EcoFastItemStack(@NotNull final org.bukkit.inventory.ItemStack itemStack) { public EcoFastItemStack(@NotNull final org.bukkit.inventory.ItemStack itemStack) {
this.handle = FastItemStackUtils.getNMSStack(itemStack); this.handle = FastItemStackUtils.getNMSStack(itemStack);
if (itemStack instanceof CraftItemStack craftItemStack) { if (itemStack instanceof CraftItemStack craftItemStack) {
this.isCIS = true; this.isCIS = true;
this.cis = craftItemStack; this.bukkit = craftItemStack;
} else { } else {
this.isCIS = false; this.isCIS = false;
this.cis = null; this.bukkit = itemStack;
} }
} }
@@ -67,12 +67,9 @@ public class EcoFastItemStack implements FastItemStack {
} }
@Override @Override
public org.bukkit.inventory.ItemStack unwrap() { public void apply() {
return this.isCIS ? cis : CraftItemStack.asCraftMirror(handle); if (!this.isCIS) {
} bukkit.setItemMeta(CraftItemStack.asCraftMirror(handle).getItemMeta());
}
@Override
public boolean isModifyingInstance() {
return isCIS;
} }
} }

View File

@@ -18,16 +18,16 @@ import java.util.Map;
public class EcoFastItemStack implements FastItemStack { public class EcoFastItemStack implements FastItemStack {
private final ItemStack handle; private final ItemStack handle;
private final boolean isCIS; private final boolean isCIS;
private final CraftItemStack cis; private final org.bukkit.inventory.ItemStack bukkit;
public EcoFastItemStack(@NotNull final org.bukkit.inventory.ItemStack itemStack) { public EcoFastItemStack(@NotNull final org.bukkit.inventory.ItemStack itemStack) {
this.handle = FastItemStackUtils.getNMSStack(itemStack); this.handle = FastItemStackUtils.getNMSStack(itemStack);
if (itemStack instanceof CraftItemStack craftItemStack) { if (itemStack instanceof CraftItemStack craftItemStack) {
this.isCIS = true; this.isCIS = true;
this.cis = craftItemStack; this.bukkit = craftItemStack;
} else { } else {
this.isCIS = false; this.isCIS = false;
this.cis = null; this.bukkit = itemStack;
} }
} }
@@ -67,12 +67,9 @@ public class EcoFastItemStack implements FastItemStack {
} }
@Override @Override
public org.bukkit.inventory.ItemStack unwrap() { public void apply() {
return this.isCIS ? cis : CraftItemStack.asCraftMirror(handle); if (!this.isCIS) {
} bukkit.setItemMeta(CraftItemStack.asCraftMirror(handle).getItemMeta());
}
@Override
public boolean isModifyingInstance() {
return isCIS;
} }
} }