Skip to content

Commit

Permalink
Add support for data components
Browse files Browse the repository at this point in the history
  • Loading branch information
JRoy committed May 27, 2024
1 parent aec5859 commit e7a4167
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,13 @@ public boolean canSpawn(final IEssentials ess) {
}

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) {
Expand All @@ -158,6 +164,22 @@ public void parseStringMeta(final CommandSource sender, final boolean allowUnsaf
} 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);
Expand Down
2 changes: 2 additions & 0 deletions Essentials/src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit e7a4167

Please sign in to comment.