Skip to content

Commit

Permalink
use Item Component factory in 1.20.5+
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Jun 8, 2024
1 parent a3cf27b commit 116e3bf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.12.2-R0.1-SNAPSHOT</version>
<version>1.18.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>

Expand Down
26 changes: 22 additions & 4 deletions src/main/java/me/hsgamer/bettergui/modifier/NBTModifier.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package me.hsgamer.bettergui.modifier;

import com.google.gson.Gson;
import me.hsgamer.hscore.bukkit.utils.VersionUtils;
import me.hsgamer.hscore.common.StringReplacer;
import me.hsgamer.hscore.common.Validate;
import me.hsgamer.hscore.minecraft.item.ItemModifier;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

Expand All @@ -15,6 +18,7 @@

public class NBTModifier implements ItemModifier<ItemStack> {
private static final Gson GSON = new Gson();
private static final boolean USE_ITEM_COMPONENT = VersionUtils.isAtLeast(20, 5);
private String nbtData = "";

@SuppressWarnings("deprecation")
Expand All @@ -23,10 +27,24 @@ public class NBTModifier implements ItemModifier<ItemStack> {
if (Validate.isNullOrEmpty(nbtData)) {
return original;
}
try {
return Bukkit.getUnsafe().modifyItemStack(original, StringReplacer.replace(nbtData, uuid, stringReplacers));
} catch (Throwable throwable) {
return original;

String replacedNbtData = StringReplacer.replace(nbtData, uuid, stringReplacers);

if (USE_ITEM_COMPONENT) {
Material material = original.getType();
NamespacedKey materialKey = material.getKey();
String materialName = materialKey.toString();
try {
return Bukkit.getItemFactory().createItemStack(materialName + replacedNbtData);
} catch (Throwable throwable) {
return original;
}
} else {
try {
return Bukkit.getUnsafe().modifyItemStack(original, replacedNbtData);
} catch (Throwable throwable) {
return original;
}
}
}

Expand Down

0 comments on commit 116e3bf

Please sign in to comment.