Skip to content

Commit 45be59b

Browse files
committed
NBTReflection - fix failing on Spigot
- Ref #769
1 parent 84c5c32 commit 45be59b

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

src/main/java/com/shanebeestudios/skbee/api/nbt/NBTApi.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import de.tr7zw.changeme.nbtapi.utils.DataFixerUtil;
2020
import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion;
2121
import org.apache.commons.lang3.ArrayUtils;
22+
import org.bukkit.Bukkit;
2223
import org.bukkit.OfflinePlayer;
2324
import org.bukkit.block.Block;
2425
import org.bukkit.block.BlockState;
@@ -44,6 +45,8 @@ public class NBTApi {
4445
private static boolean ENABLED;
4546
public static final boolean HAS_ITEM_COMPONENTS = Skript.isRunningMinecraft(1, 20, 5);
4647
static final String TAG_NAME = HAS_ITEM_COMPONENTS ? "components" : "tag";
48+
@SuppressWarnings("deprecation")
49+
private static final int DATA_VERSION = Bukkit.getUnsafe().getDataVersion();
4750

4851
/**
4952
* Initialize this NBT API
@@ -866,10 +869,10 @@ public static void addNBTToEntity(Entity entity, NBTCompound compound) {
866869
* @return ItemStack from NBT
867870
*/
868871
public static ItemStack convertNBTtoItem(@NotNull NBTCompound nbtcompound) {
869-
if (!nbtcompound.hasTag("DataVersion") || nbtcompound.getInteger("DataVersion") != NBTReflection.getDataVersion()) {
872+
if (!nbtcompound.hasTag("DataVersion") || nbtcompound.getInteger("DataVersion") != getDataVersion()) {
870873
int dataVersion = nbtcompound.hasTag("DataVersion") ? nbtcompound.getInteger("DataVersion") : DataFixerUtil.VERSION1_20_4;
871874
try {
872-
ReadWriteNBT fixedItemNBT = DataFixerUtil.fixUpItemData(nbtcompound, dataVersion, NBTReflection.getDataVersion());
875+
ReadWriteNBT fixedItemNBT = DataFixerUtil.fixUpItemData(nbtcompound, dataVersion, getDataVersion());
873876
return NBTItem.convertNBTtoItem((NBTCompound) fixedItemNBT);
874877
} catch (NoSuchFieldException | IllegalAccessException e) {
875878
throw new RuntimeException(e);
@@ -878,4 +881,13 @@ public static ItemStack convertNBTtoItem(@NotNull NBTCompound nbtcompound) {
878881
return NBTItem.convertNBTtoItem(nbtcompound);
879882
}
880883

884+
/**
885+
* Get the Minecraft DataVersion
886+
*
887+
* @return DataVersion from MC
888+
*/
889+
public static int getDataVersion() {
890+
return NBTApi.DATA_VERSION;
891+
}
892+
881893
}

src/main/java/com/shanebeestudios/skbee/api/nbt/NBTCustomItemStack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private static NBTCompound getContainer(NBTCompound itemContainer, boolean isFul
4141
if (isFull) {
4242
// TODO temp solution until NBT API handles this
4343
// DataVersion is used for deserializing and running thru DataFixerUpper
44-
itemContainer.setInteger("DataVersion", NBTReflection.getDataVersion());
44+
itemContainer.setInteger("DataVersion", NBTApi.getDataVersion());
4545
// TODO end
4646
return itemContainer;
4747
}

src/main/java/com/shanebeestudios/skbee/api/nbt/NBTReflection.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
@SuppressWarnings({"SequencedCollectionMethodCanBeUsed", "CallToPrintStackTrace", "DataFlowIssue"})
1616
public class NBTReflection {
1717

18-
@SuppressWarnings("deprecation")
19-
private static final int DATA_VERSION = Bukkit.getUnsafe().getDataVersion();
20-
2118
// Classes
2219
private static Class<?> CRAFT_ITEM_STACK_CLASS;
2320

@@ -70,15 +67,6 @@ public class NBTReflection {
7067
}
7168
}
7269

73-
/**
74-
* Get the Minecraft DataVersion
75-
*
76-
* @return DataVersion from MC
77-
*/
78-
public static int getDataVersion() {
79-
return DATA_VERSION;
80-
}
81-
8270
/**
8371
* Get the vanilla version of NBT of an item
8472
* <br>This will show components which don't normally show in NBT

src/main/java/com/shanebeestudios/skbee/elements/nbt/expressions/ExprNbtCompound.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@
8181
@Since("1.6.0")
8282
public class ExprNbtCompound extends PropertyExpression<Object, NBTCompound> {
8383

84+
// My reflection requires Paper/Mojmap. I cant be bothered to re-write this for Spigot
85+
private static final boolean HAS_VANILLA_NBT = Skript.classExists("net.minecraft.server.level.ServerPlayer") && NBTApi.HAS_ITEM_COMPONENTS;
8486
private static final boolean ALLOW_UNSAFE_OPERATIONS = SkBee.getPlugin().getPluginConfig().NBT_ALLOW_UNSAFE_OPERATIONS;
8587

8688
static {
@@ -101,7 +103,11 @@ public boolean init(Expression<?> @NotNull [] exprs, int matchedPattern, @NotNul
101103
Expression<?> expr = LiteralUtils.defendExpression(exprs[0]);
102104
setExpr(expr);
103105
this.isFullItem = parseResult.hasTag("full");
104-
this.isVanilla = parseResult.hasTag("vanilla") && NBTApi.HAS_ITEM_COMPONENTS;
106+
this.isVanilla = parseResult.hasTag("vanilla");
107+
if (this.isVanilla && !HAS_VANILLA_NBT) {
108+
Skript.error("Vanilla NBT requires a PaperMC server (or any other MojMapped server).");
109+
return false;
110+
}
105111
this.isCustom = parseResult.hasTag("custom");
106112
this.isCopy = parseResult.hasTag("copy");
107113
this.isFile = matchedPattern == 1;

0 commit comments

Comments
 (0)