Skip to content

Commit

Permalink
Adds static method TranslatableItem#localize to quickly localize Item…
Browse files Browse the repository at this point in the history
…Stacks
  • Loading branch information
anjoismysign committed Dec 13, 2023
1 parent c0175c9 commit 86f80e2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion ci-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.697.40</version>
<version>1.697.41</version>
<relativePath>pom.xml</relativePath>
</parent>
<artifactId>bloblib</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion local-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.697.40</version>
<version>1.697.41</version>
<relativePath>pom.xml</relativePath>
</parent>
<artifactId>bloblib</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.697.40</version>
<version>1.697.41</version>
<packaging>pom</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,37 @@ static TranslatableItem by(@NotNull String key) {
return BlobLibTranslatableAPI.getInstance().getTranslatableItem(key);
}

/**
* Will attempt to localize a specific ItemStack while preserving
* their NBTs.
*
* @param itemStack The ItemStack to process. If null, nothing happens.
* @param locale The target locale.
*/
static void localize(@Nullable ItemStack itemStack,
@NotNull String locale) {
TranslatableItem translatableItem = TranslatableItem.isInstance(itemStack);
if (translatableItem == null)
return;
TranslatableItem localized = translatableItem.localize(locale);
ItemStack toStack = localized.getClone();
ItemMeta from = itemStack.getItemMeta();
Objects.requireNonNull(from);
ItemMeta to = toStack.getItemMeta();
Objects.requireNonNull(to);
if (to.hasDisplayName())
from.setDisplayName(to.getDisplayName());
else
from.setDisplayName(null);
if (to.hasLore())
from.setLore(to.getLore());
else
from.setLore(null);
PersistentDataContainer container = from.getPersistentDataContainer();
container.set(BlobTranslatableItem.localeKey, PersistentDataType.STRING, localized.getLocale());
itemStack.setItemMeta(from);
}

@Nullable
static TranslatableItem isInstance(@Nullable ItemStack item) {
if (item == null)
Expand Down

0 comments on commit 86f80e2

Please sign in to comment.