Continued FastItemStack development

This commit is contained in:
Auxilor
2021-08-03 16:20:05 +01:00
parent a4909453d7
commit 6cf9a53a65
5 changed files with 54 additions and 4 deletions

View File

@@ -9,6 +9,10 @@ import java.util.Map;
/**
* FastItemStack contains methods to modify and read items faster than in default bukkit.
* <p>
* If the ItemStack wrapped is a CraftItemStack, then the instance will be modified, allowing for set methods to work.
* <p>
* Otherwise, the FastItemStack must then be unwrapped to get a bukkit copy.
*/
public interface FastItemStack {
/**
@@ -29,6 +33,22 @@ public interface FastItemStack {
int getLevelOnItem(@NotNull Enchantment enchantment,
boolean checkStored);
/**
* Unwrap an ItemStack.
*
* @return The bukkit ItemStack.
*/
ItemStack unwrap();
/**
* If the FastItemStack modifies the actual ItemStack instance or a copy.
* <p>
* 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.
*/
boolean isModifyingInstance();
/**
* Wrap an ItemStack to create a FastItemStack.
*

View File

@@ -7,6 +7,7 @@ import net.minecraft.server.v1_16_R3.Items;
import net.minecraft.server.v1_16_R3.NBTBase;
import net.minecraft.server.v1_16_R3.NBTTagCompound;
import net.minecraft.server.v1_16_R3.NBTTagList;
import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack;
import org.bukkit.craftbukkit.v1_16_R3.util.CraftNamespacedKey;
import org.bukkit.enchantments.Enchantment;
import org.jetbrains.annotations.NotNull;
@@ -16,9 +17,11 @@ import java.util.Map;
public class EcoFastItemStack implements FastItemStack {
private final ItemStack handle;
private final boolean isCMS;
public EcoFastItemStack(@NotNull final org.bukkit.inventory.ItemStack itemStack) {
this.handle = FastItemStackUtils.getNMSStack(itemStack);
this.isCMS = itemStack instanceof CraftItemStack;
}
@Override
@@ -55,4 +58,14 @@ public class EcoFastItemStack implements FastItemStack {
}
return 0;
}
@Override
public org.bukkit.inventory.ItemStack unwrap() {
return CraftItemStack.asCraftMirror(handle);
}
@Override
public boolean isModifyingInstance() {
return isCMS;
}
}

View File

@@ -1,6 +1,7 @@
package com.willfp.eco.proxy.v1_16_R3.fast;
import lombok.experimental.UtilityClass;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.libs.org.apache.commons.lang3.Validate;
import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack;
import org.bukkit.inventory.ItemStack;
@@ -13,8 +14,9 @@ public class FastItemStackUtils {
private final Field field;
public net.minecraft.server.v1_16_R3.ItemStack getNMSStack(@NotNull final ItemStack itemStack) {
if (itemStack instanceof CraftItemStack) {
throw new IllegalArgumentException("Must be CraftItemStack!");
if (!(itemStack instanceof CraftItemStack)) {
Bukkit.getLogger().warning("Not CraftItemStack - set methods will not work!");
return CraftItemStack.asNMSCopy(itemStack);
}
try {
return (net.minecraft.server.v1_16_R3.ItemStack) field.get(itemStack);

View File

@@ -7,6 +7,7 @@ import net.minecraft.nbt.Tag;
import net.minecraft.world.item.EnchantedBookItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack;
import org.bukkit.craftbukkit.v1_17_R1.util.CraftNamespacedKey;
import org.bukkit.enchantments.Enchantment;
import org.jetbrains.annotations.NotNull;
@@ -16,9 +17,11 @@ import java.util.Map;
public class EcoFastItemStack implements FastItemStack {
private final ItemStack handle;
private final boolean isCMS;
public EcoFastItemStack(@NotNull final org.bukkit.inventory.ItemStack itemStack) {
this.handle = FastItemStackUtils.getNMSStack(itemStack);
this.isCMS = itemStack instanceof CraftItemStack;
}
@Override
@@ -55,4 +58,14 @@ public class EcoFastItemStack implements FastItemStack {
}
return 0;
}
@Override
public org.bukkit.inventory.ItemStack unwrap() {
return CraftItemStack.asCraftMirror(handle);
}
@Override
public boolean isModifyingInstance() {
return isCMS;
}
}

View File

@@ -1,6 +1,7 @@
package com.willfp.eco.proxy.v1_17_R1.fast;
import lombok.experimental.UtilityClass;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.libs.org.apache.commons.lang3.Validate;
import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack;
import org.bukkit.inventory.ItemStack;
@@ -13,8 +14,9 @@ public class FastItemStackUtils {
private final Field field;
public net.minecraft.world.item.ItemStack getNMSStack(@NotNull final ItemStack itemStack) {
if (itemStack instanceof CraftItemStack) {
throw new IllegalArgumentException("Must be CraftItemStack!");
if (!(itemStack instanceof CraftItemStack)) {
Bukkit.getLogger().warning("Not CraftItemStack - set methods will not work!");
return CraftItemStack.asNMSCopy(itemStack);
}
try {
return (net.minecraft.world.item.ItemStack) field.get(itemStack);