From 86f80e2a625052407cdb6a389c894332b7008a6f Mon Sep 17 00:00:00 2001 From: lbenav8095 Date: Wed, 13 Dec 2023 00:07:41 -0600 Subject: [PATCH] Adds static method TranslatableItem#localize to quickly localize ItemStacks --- ci-pom.xml | 2 +- local-pom.xml | 2 +- pom.xml | 2 +- .../translatable/TranslatableItem.java | 31 +++++++++++++++++++ 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/ci-pom.xml b/ci-pom.xml index 36dfbd0..e50f28b 100644 --- a/ci-pom.xml +++ b/ci-pom.xml @@ -7,7 +7,7 @@ us.mytheria BlobLib - 1.697.40 + 1.697.41 pom.xml bloblib diff --git a/local-pom.xml b/local-pom.xml index 44b93ee..a7741cc 100644 --- a/local-pom.xml +++ b/local-pom.xml @@ -5,7 +5,7 @@ us.mytheria BlobLib - 1.697.40 + 1.697.41 pom.xml bloblib diff --git a/pom.xml b/pom.xml index cc5849e..704e92c 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 us.mytheria BlobLib - 1.697.40 + 1.697.41 pom diff --git a/src/main/java/us/mytheria/bloblib/entities/translatable/TranslatableItem.java b/src/main/java/us/mytheria/bloblib/entities/translatable/TranslatableItem.java index c4d2160..db16d7e 100644 --- a/src/main/java/us/mytheria/bloblib/entities/translatable/TranslatableItem.java +++ b/src/main/java/us/mytheria/bloblib/entities/translatable/TranslatableItem.java @@ -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)