Add support for data components

This commit is contained in:
Josh Roy 2024-05-20 13:52:48 -04:00
parent aec5859c2a
commit e7a4167111
2 changed files with 24 additions and 0 deletions

View File

@ -146,7 +146,13 @@ public class MetaItemStack {
}
public void parseStringMeta(final CommandSource sender, final boolean allowUnsafe, final String[] string, final int fromArg, final IEssentials ess) throws Exception {
final boolean nbtIsKill = VersionUtil.getServerBukkitVersion().isHigherThanOrEqualTo(VersionUtil.v1_20_6_R01);
if (string[fromArg].startsWith("{") && hasMetaPermission(sender, "vanilla", false, true, ess)) {
if (nbtIsKill) {
throw new TranslatableException("noMetaNbtKill");
}
try {
stack = ess.getServer().getUnsafe().modifyItemStack(stack, Joiner.on(' ').join(Arrays.asList(string).subList(fromArg, string.length)));
} catch (final NullPointerException npe) {
@ -158,6 +164,22 @@ public class MetaItemStack {
} catch (final Throwable throwable) {
throw new Exception(throwable.getMessage(), throwable);
}
} else if (string[fromArg].startsWith("[") && hasMetaPermission(sender, "vanilla", false, true, ess)) {
if (!nbtIsKill) {
throw new TranslatableException("noMetaComponents");
}
try {
final String components = Joiner.on(' ').join(Arrays.asList(string).subList(fromArg, string.length));
// modifyItemStack requires that the item namespaced key is prepended to the components for some reason
stack = ess.getServer().getUnsafe().modifyItemStack(stack, stack.getType().getKey() + components);
} catch (final NullPointerException npe) {
if (ess.getSettings().isDebug()) {
ess.getLogger().log(Level.INFO, "Itemstack is invalid", npe);
}
} catch (final Throwable throwable) {
throw new Exception(throwable.getMessage(), throwable);
}
} else {
for (int i = fromArg; i < string.length; i++) {
addStringMeta(sender, allowUnsafe, string[i], ess);

View File

@ -863,8 +863,10 @@ noLocationFound=<dark_red>No valid location found.
noMail=<primary>You do not have any mail.
noMailOther=<secondary>{0} <primary>does not have any mail.
noMatchingPlayers=<primary>No matching players found.
noMetaComponents=Data Components are not supported in this version of Bukkit. Please use JSON NBT metadata.
noMetaFirework=<dark_red>You do not have permission to apply firework meta.
noMetaJson=JSON Metadata is not supported in this version of Bukkit.
noMetaNbtKill=JSON NBT metadata is no longer supported. You must manually convert your defined items to data components. You can convert JSON NBT to data components here: https://docs.papermc.io/misc/tools/item-command-converter
noMetaPerm=<dark_red>You do not have permission to apply <secondary>{0}<dark_red> meta to this item.
none=none
noNewMail=<primary>You have no new mail.