From faeacb80b17c479fa1fe39ddc59d83cdd630aa28 Mon Sep 17 00:00:00 2001 From: codist <50346006+ImCodist@users.noreply.github.com> Date: Thu, 9 May 2024 14:30:26 -0400 Subject: [PATCH] Reimplement Custom Model Data. --- .../xyz/imcodist/data/ActionButtonData.java | 21 ++---- .../java/xyz/imcodist/ui/ActionEditorUI.java | 69 ++++++++----------- .../xyz/imcodist/ui/popups/ItemPickerUI.java | 8 +-- 3 files changed, 34 insertions(+), 64 deletions(-) diff --git a/src/main/java/xyz/imcodist/data/ActionButtonData.java b/src/main/java/xyz/imcodist/data/ActionButtonData.java index e1074bb..6f3361e 100644 --- a/src/main/java/xyz/imcodist/data/ActionButtonData.java +++ b/src/main/java/xyz/imcodist/data/ActionButtonData.java @@ -2,9 +2,9 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.client.util.InputUtil; +import net.minecraft.component.DataComponentTypes; +import net.minecraft.component.type.CustomModelDataComponent; import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NbtCompound; -import net.minecraft.nbt.NbtElement; import net.minecraft.registry.Registries; import net.minecraft.text.Text; import net.minecraft.util.Identifier; @@ -45,14 +45,7 @@ public ActionButtonDataJSON toJSON() { jsonData.icon = icon.getRegistryEntry().getKey().get().getValue().toString(); } -// if (icon.getNbt() != null) { -// NbtElement nbtElement = icon.getNbt().get("CustomModelData"); -// if (nbtElement != null) { -// jsonData.customModelData = Integer.parseInt(nbtElement.toString()); -// } -// } - - //jsonData.customModelData = icon.getOr(new NbtKey<>("CustomModelData", NbtKey.Type.INT), null); + jsonData.customModelData = icon.getOrDefault(DataComponentTypes.CUSTOM_MODEL_DATA, CustomModelDataComponent.DEFAULT).value(); } return jsonData; @@ -73,11 +66,7 @@ public static ActionButtonData fromJSON(ActionButtonDataJSON json) { if (json.icon != null) { data.icon = new ItemStack(Registries.ITEM.get(new Identifier(json.icon))); - -// try { -// NbtCompound nbt = data.icon.getOrCreateNbt(); -// nbt.putInt("CustomModelData", json.customModelData); -// } catch (NumberFormatException ignored) {} + data.icon.set(DataComponentTypes.CUSTOM_MODEL_DATA, new CustomModelDataComponent(json.customModelData)); } return data; @@ -123,8 +112,6 @@ public void run(boolean isKeybind) { } } - - // Run the buttons action. actions.forEach(BaseActionData::run); } diff --git a/src/main/java/xyz/imcodist/ui/ActionEditorUI.java b/src/main/java/xyz/imcodist/ui/ActionEditorUI.java index d11f6ea..16c8ab5 100644 --- a/src/main/java/xyz/imcodist/ui/ActionEditorUI.java +++ b/src/main/java/xyz/imcodist/ui/ActionEditorUI.java @@ -12,9 +12,9 @@ import io.wispforest.owo.ui.core.*; import net.minecraft.client.MinecraftClient; import net.minecraft.client.util.InputUtil; +import net.minecraft.component.DataComponentTypes; +import net.minecraft.component.type.CustomModelDataComponent; import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NbtCompound; -import net.minecraft.nbt.NbtElement; import net.minecraft.text.MutableText; import net.minecraft.text.Text; import net.minecraft.util.Formatting; @@ -156,27 +156,26 @@ protected void build(FlowLayout rootComponent) { updateKeybindButton(); - // REMOVE CUSTOM MODEL DATA TEMP -// FlowLayout customModelDataProperty = createNewProperty("custommodeldata", false); -// advancedLayout.child(customModelDataProperty); -// -// Integer customModelData = getCustomModelData(iconButton.itemIcon); -// String cmdText = customModelData != null ? customModelData.toString() : ""; -// -// customModelDataTextBox = Components.textBox(Sizing.fixed(75), cmdText); -// customModelDataTextBox.cursorStyle(CursorStyle.TEXT); -// -// customModelDataTextBox.onChanged().subscribe((text) -> { -// customModelDataTextBox.setText(text.replaceAll("^0+|\\D", "")); -// updateCustomModelData(iconButton.itemIcon); -// }); -// -// customModelDataProperty.child(customModelDataTextBox); + FlowLayout customModelDataProperty = createNewProperty("custommodeldata", false); + advancedLayout.child(customModelDataProperty); + + Integer customModelData = getCustomModelData(iconButton.itemIcon); + String cmdText = customModelData != 0 ? customModelData.toString() : ""; + + customModelDataTextBox = Components.textBox(Sizing.fixed(75), cmdText); + customModelDataTextBox.cursorStyle(CursorStyle.TEXT); + + customModelDataTextBox.onChanged().subscribe((text) -> { + customModelDataTextBox.setText(text.replaceAll("^0+|\\D", "")); + updateCustomModelData(iconButton.itemIcon); + }); + + customModelDataProperty.child(customModelDataTextBox); propertiesLayout.child(advancedLayout); // Add padding to the last property in the advanced layout. -// customModelDataProperty.padding(customModelDataProperty.padding().get().add(0, 6, 0, 0)); + customModelDataProperty.padding(customModelDataProperty.padding().get().add(0, 6, 0, 0)); // Set up the editor buttons. FlowLayout buttonsLayout = Containers.horizontalFlow(Sizing.content(), Sizing.content()); @@ -216,20 +215,8 @@ protected void build(FlowLayout rootComponent) { } private Integer getCustomModelData(ItemStack item) { - Integer existingCustomModelData = null; - if (item != null) { -// if (item.getNbt() != null) { -// NbtElement nbtElement = item.getNbt().get("CustomModelData"); -// if (nbtElement != null) { -// existingCustomModelData = Integer.parseInt(nbtElement.toString()); -// if (existingCustomModelData == 0) existingCustomModelData = null; -// } -// } - - //existingCustomModelData = item.getOr(new NbtKey<>("CustomModelData", NbtKey.Type.INT), null); - } - - return existingCustomModelData; + if (item == null) return CustomModelDataComponent.DEFAULT.value(); + return item.getOrDefault(DataComponentTypes.CUSTOM_MODEL_DATA, CustomModelDataComponent.DEFAULT).value(); } private void updateCustomModelData(ItemStack itemStack) { @@ -238,15 +225,13 @@ private void updateCustomModelData(ItemStack itemStack) { if (itemStack == null) return; -// try { -// NbtCompound nbt = itemStack.getOrCreateNbt(); -// -// if (!text.equals("")) { -// nbt.putInt("CustomModelData", Integer.parseInt(text)); -// } else { -// nbt.remove("CustomModelData"); -// } -// } catch (NumberFormatException ignored) {} + try { + if (!text.equals("")) { + itemStack.set(DataComponentTypes.CUSTOM_MODEL_DATA, new CustomModelDataComponent(Integer.parseInt(text))); + } else { + itemStack.remove(DataComponentTypes.CUSTOM_MODEL_DATA); + } + } catch (NumberFormatException ignored) {} } public FlowLayout createNewProperty(String name) { diff --git a/src/main/java/xyz/imcodist/ui/popups/ItemPickerUI.java b/src/main/java/xyz/imcodist/ui/popups/ItemPickerUI.java index 928adf8..51fb58d 100644 --- a/src/main/java/xyz/imcodist/ui/popups/ItemPickerUI.java +++ b/src/main/java/xyz/imcodist/ui/popups/ItemPickerUI.java @@ -9,9 +9,10 @@ import io.wispforest.owo.ui.container.OverlayContainer; import io.wispforest.owo.ui.container.ScrollContainer; import io.wispforest.owo.ui.core.*; +import net.minecraft.component.DataComponentTypes; +import net.minecraft.component.type.CustomModelDataComponent; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NbtCompound; import net.minecraft.registry.Registries; import net.minecraft.util.Identifier; import xyz.imcodist.ui.components.QuickMenuButton; @@ -103,10 +104,7 @@ public void createItemButtons(FlowLayout parent, String search) { ItemStack item = items.get(curItem).getDefaultStack(); if (customModelData != null) { -// try { -// NbtCompound nbt = item.getOrCreateNbt(); -// nbt.putInt("CustomModelData", customModelData); -// } catch (NumberFormatException ignored) {} + item.set(DataComponentTypes.CUSTOM_MODEL_DATA, new CustomModelDataComponent(customModelData)); } ButtonComponent button = new QuickMenuButton(item, (buttonComponent) -> {