-
Notifications
You must be signed in to change notification settings - Fork 166
Support for item model in holograms #341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
1da30cd
a33044a
d194495
ac4aac2
4f123be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,16 @@ | ||
| package eu.decentsoftware.holograms.hook; | ||
|
|
||
| import com.google.gson.Gson; | ||
| import com.google.gson.JsonObject; | ||
| import com.google.gson.JsonParser; | ||
| import de.tr7zw.changeme.nbtapi.NBT; | ||
| import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT; | ||
| import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBTList; | ||
| import de.tr7zw.changeme.nbtapi.utils.DataFixerUtil; | ||
| import eu.decentsoftware.holograms.api.utils.Log; | ||
| import eu.decentsoftware.holograms.api.utils.PAPI; | ||
| import eu.decentsoftware.holograms.api.utils.reflect.Version; | ||
| import lombok.Data; | ||
| import lombok.experimental.UtilityClass; | ||
| import org.bukkit.entity.Player; | ||
| import org.bukkit.inventory.ItemStack; | ||
|
|
@@ -59,19 +63,69 @@ public static ItemStack applyNbtDataToItemStack(ItemStack itemStack, String nbt, | |
| */ | ||
| modifiableNBT.mergeCompound(originalNBT); | ||
|
|
||
| // Load the nbt data | ||
| ItemNbtData nbtData = ItemNbtData.fromJson(nbt); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At line 50, the value of I also don't like how one variable When you separate them, you may not even need the JSON at all. You could just use the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You bring up a good point. This is something I got stuck on as I was trying to think of a good way to integrate this into the current data fixer solution you use for custom model data. Here's what I found: When the I can make a modification to see how this would work without the data fixer. |
||
|
|
||
| /* | ||
| * Since item_model is new, it gets mapped under 'custom_data' in the item. | ||
| * We need to manually re-apply it. | ||
| */ | ||
| if (nbtData.getItemModel() != null) { | ||
| modifiableNBT.getOrCreateCompound("components").setString("minecraft:item_model", nbtData.getItemModel()); | ||
| } | ||
|
|
||
| return NBT.itemStackFromNBT(modifiableNBT); | ||
| } catch (Exception ex) { | ||
| Log.warn("Failed to apply NBT Data to Item: %s", ex, nbt); | ||
| return itemStack; | ||
| } | ||
| } | ||
|
|
||
| public static ItemNbtData readData(ItemStack itemStack) { | ||
| if (!loadedSuccessfully) { | ||
| return ItemNbtData.EMPTY; | ||
| } | ||
|
|
||
| ReadWriteNBT nbt = NBT.itemStackToNBT(itemStack); | ||
| return new ItemNbtData(extractItemModel(nbt), extractCustomModelData(nbt)); | ||
| } | ||
|
|
||
| public static String extractItemModel(ItemStack itemStack) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method is unused, remove it. |
||
| if (!loadedSuccessfully) { | ||
| return null; | ||
| } | ||
|
|
||
| return extractItemModel(NBT.itemStackToNBT(itemStack)); | ||
| } | ||
|
|
||
| public static String extractItemModel(ReadWriteNBT nbtItem) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method can be private |
||
| if (!loadedSuccessfully) { | ||
| return null; | ||
| } | ||
|
|
||
| String itemModel; | ||
| if (Version.afterOrEqual(Version.v1_21_R2)) { | ||
| itemModel = nbtItem.getOrCreateCompound("components") | ||
| .getString("minecraft:item_model"); | ||
| } else { | ||
| itemModel = null; | ||
| } | ||
| return itemModel; | ||
| } | ||
|
|
||
| public static float extractCustomModelData(ItemStack itemStack) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method is unused, so remove it. |
||
| if (!loadedSuccessfully) { | ||
| return 0f; | ||
| } | ||
|
|
||
| ReadWriteNBT nbtItem = NBT.itemStackToNBT(itemStack); | ||
| return extractCustomModelData(NBT.itemStackToNBT(itemStack)); | ||
| } | ||
|
|
||
| public static float extractCustomModelData(ReadWriteNBT nbtItem) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method can be private |
||
| if (!loadedSuccessfully) { | ||
| return 0f; | ||
| } | ||
|
|
||
| float customModelData; | ||
| if (Version.afterOrEqual(Version.v1_21_R3)) { | ||
| // New structure components:{custom_model_data={floats[...]}} since 1.21.4 | ||
|
|
@@ -91,4 +145,51 @@ public static float extractCustomModelData(ItemStack itemStack) { | |
| } | ||
| return customModelData; | ||
| } | ||
|
|
||
| /** | ||
| * Represents the result of reading data from an item stack. | ||
| * This is used to prevent redundancy with NBTAPI. | ||
| */ | ||
| @Data | ||
| public static class ItemNbtData { | ||
| public static final ItemNbtData EMPTY = new ItemNbtData(null, 0f); | ||
|
|
||
| private final String itemModel; | ||
| private final float customModelData; | ||
| private String json; | ||
|
|
||
| public ItemNbtData(String itemModel, float customModelData) { | ||
| this.itemModel = itemModel; | ||
| this.customModelData = customModelData; | ||
| this.json = toJson(); | ||
| } | ||
|
|
||
| /** | ||
| * Converts this result to json. | ||
| * | ||
| * @return the json. | ||
| */ | ||
| private String toJson() { | ||
| if ((this.itemModel == null || this.itemModel.isEmpty()) && this.customModelData == 0f) | ||
| return ""; | ||
|
|
||
| // Use Gson to allow for easier expansion in the future. | ||
| JsonObject object = new JsonObject(); | ||
| if (this.itemModel != null && !this.itemModel.isEmpty()) | ||
| object.addProperty("minecraft:item_model", this.itemModel); | ||
| if (this.customModelData != 0f) | ||
| object.addProperty("CustomModelData", this.customModelData); | ||
| return object.toString(); | ||
| } | ||
|
|
||
| public static ItemNbtData fromJson(String json) { | ||
| if (json == null || json.isEmpty()) | ||
| return EMPTY; | ||
|
|
||
| JsonObject object = new JsonParser().parse(json).getAsJsonObject(); | ||
| String itemModel = object.has("minecraft:item_model") ? object.get("minecraft:item_model").getAsString() : null; | ||
| float customModelData = object.has("CustomModelData") ? object.get("CustomModelData").getAsFloat() : 0f; | ||
| return new ItemNbtData(itemModel, customModelData); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused import