Skip to content

Commit

Permalink
Reimplement Custom Model Data.
Browse files Browse the repository at this point in the history
  • Loading branch information
ImCodist committed May 9, 2024
1 parent 5d2b06e commit faeacb8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 64 deletions.
21 changes: 4 additions & 17 deletions src/main/java/xyz/imcodist/data/ActionButtonData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -123,8 +112,6 @@ public void run(boolean isKeybind) {
}
}



// Run the buttons action.
actions.forEach(BaseActionData::run);
}
Expand Down
69 changes: 27 additions & 42 deletions src/main/java/xyz/imcodist/ui/ActionEditorUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/xyz/imcodist/ui/popups/ItemPickerUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) -> {
Expand Down

0 comments on commit faeacb8

Please sign in to comment.