From 0eefdfb97a54649def325304da5eee8b6ec13c7a Mon Sep 17 00:00:00 2001 From: Owen1212055 <23108066+Owen1212055@users.noreply.github.com> Date: Wed, 4 Dec 2024 00:26:31 -0500 Subject: [PATCH] Implement new CustomModelData --- patches/api/DataComponent-API.patch | 99 +++++++++++++++++++++++++- patches/server/DataComponent-API.patch | 95 +++++++++++++++++++++--- 2 files changed, 180 insertions(+), 14 deletions(-) diff --git a/patches/api/DataComponent-API.patch b/patches/api/DataComponent-API.patch index 4dbbadf86b..219aae3035 100644 --- a/patches/api/DataComponent-API.patch +++ b/patches/api/DataComponent-API.patch @@ -860,6 +860,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 +package io.papermc.paper.datacomponent.item; + +import java.util.List; ++import io.papermc.paper.datacomponent.DataComponentBuilder; ++import org.bukkit.Color; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Contract; +import org.jspecify.annotations.NullMarked; @@ -874,7 +876,10 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 +@ApiStatus.NonExtendable +public interface CustomModelData { + -+ // TODO ++ @Contract(value = "-> new", pure = true) ++ static CustomModelData.Builder customModelData() { ++ return ItemComponentTypesBridge.bridge().customModelData(); ++ } + + /** + * Gets the custom model data float values. @@ -906,7 +911,95 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + * @return the color values + */ + @Contract(pure = true) -+ List colors(); ++ List colors(); ++ ++ /** ++ * Builder for {@link CustomModelData}. ++ */ ++ @ApiStatus.Experimental ++ @ApiStatus.NonExtendable ++ interface Builder extends DataComponentBuilder { ++ ++ /** ++ * Adds a float to this custom model data. ++ * ++ * @param num the float ++ * @return the builder for chaining ++ * @see #floats() ++ */ ++ @Contract(value = "_ -> this", mutates = "this") ++ CustomModelData.Builder addFloat(float num); ++ ++ /** ++ * Adds multiple floats to this custom model data. ++ * ++ * @param nums the floats ++ * @return the builder for chaining ++ * @see #floats() ++ */ ++ @Contract(value = "_ -> this", mutates = "this") ++ CustomModelData.Builder addFloats(List nums); ++ ++ /** ++ * Adds a flag to this custom model data. ++ * ++ * @param flag the flag ++ * @return the builder for chaining ++ * @see #floats() ++ */ ++ @Contract(value = "_ -> this", mutates = "this") ++ CustomModelData.Builder addFlag(boolean flag); ++ ++ /** ++ * Adds multiple flags to this custom model data. ++ * ++ * @param flags the flags ++ * @return the builder for chaining ++ * @see #flags() ++ */ ++ @Contract(value = "_ -> this", mutates = "this") ++ CustomModelData.Builder addFlags(List flags); ++ ++ /** ++ * Adds a string to this custom model data. ++ * ++ * @param string the string ++ * @return the builder for chaining ++ * @see #strings() ++ */ ++ @Contract(value = "_ -> this", mutates = "this") ++ CustomModelData.Builder addString(String string); ++ ++ /** ++ * Adds multiple strings to this custom model data. ++ * ++ * @param strings the strings ++ * @return the builder for chaining ++ * @see #strings() ++ */ ++ @Contract(value = "_ -> this", mutates = "this") ++ CustomModelData.Builder addStrings(List strings); ++ ++ /** ++ * Adds a color to this custom model data. ++ * ++ * @param color the float ++ * @return the builder for chaining ++ * @see #colors() ++ */ ++ @Contract(value = "_ -> this", mutates = "this") ++ CustomModelData.Builder addColor(Color color); ++ ++ /** ++ * Adds multiple colors to this custom model data. ++ * ++ * @param colors the colors ++ * @return the builder for chaining ++ * @see #colors() ++ */ ++ @Contract(value = "_ -> this", mutates = "this") ++ CustomModelData.Builder addColors(List colors); ++ } +} diff --git a/src/main/java/io/papermc/paper/datacomponent/item/DamageResistant.java b/src/main/java/io/papermc/paper/datacomponent/item/DamageResistant.java new file mode 100644 @@ -1778,7 +1871,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + + ItemAdventurePredicate.Builder itemAdventurePredicate(); + -+ CustomModelData customModelData(int id); ++ CustomModelData.Builder customModelData(); + + MapId mapId(int id); + diff --git a/patches/server/DataComponent-API.patch b/patches/server/DataComponent-API.patch index 39274920ea..b85b132c85 100644 --- a/patches/server/DataComponent-API.patch +++ b/patches/server/DataComponent-API.patch @@ -577,9 +577,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + } + + @Override -+ public CustomModelData customModelData(final int id) { -+ throw new UnsupportedOperationException("Not implemented yet"); -+ //return new PaperCustomModelData(new net.minecraft.world.item.component.CustomModelData(id)); ++ public CustomModelData.Builder customModelData() { ++ return new PaperCustomModelData.BuilderImpl(); + } + + @Override @@ -970,7 +969,11 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 @@ -0,0 +0,0 @@ +package io.papermc.paper.datacomponent.item; + ++import java.util.ArrayList; ++import java.util.Collections; +import java.util.List; ++import io.papermc.paper.util.MCUtil; ++import org.bukkit.Color; +import org.bukkit.craftbukkit.util.Handleable; + +public record PaperCustomModelData( @@ -984,22 +987,93 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + + @Override + public List floats() { -+ return this.impl.floats(); ++ return Collections.unmodifiableList(this.impl.floats()); + } + + @Override + public List flags() { -+ return this.impl.flags(); ++ return Collections.unmodifiableList(this.impl.flags()); + } + + @Override + public List strings() { -+ return this.impl.strings(); ++ return Collections.unmodifiableList(this.impl.strings()); + } + + @Override -+ public List colors() { -+ return this.impl.colors(); ++ public List colors() { ++ return MCUtil.transformUnmodifiable(this.impl.colors(), Color::fromRGB); ++ } ++ ++ static final class BuilderImpl implements CustomModelData.Builder { ++ ++ private final List floats = new ArrayList<>(); ++ private final List flags = new ArrayList<>(); ++ private final List strings = new ArrayList<>(); ++ private final List colors = new ArrayList<>(); ++ ++ @Override ++ public Builder addFloat(final float num) { ++ this.floats.add(num); ++ return this; ++ } ++ ++ @Override ++ public Builder addFloats(final List nums) { ++ this.floats.addAll(nums); ++ return this; ++ } ++ ++ @Override ++ public Builder addFlag(final boolean flag) { ++ this.flags.add(flag); ++ return this; ++ } ++ ++ @Override ++ public Builder addFlags(final List flags) { ++ this.flags.addAll(flags); ++ return this; ++ } ++ ++ @Override ++ public Builder addString(final String string) { ++ this.strings.add(string); ++ return this; ++ } ++ ++ @Override ++ public Builder addStrings(final List strings) { ++ this.strings.addAll(strings); ++ return this; ++ } ++ ++ @Override ++ public Builder addColor(final Color color) { ++ this.colors.add(color.asRGB()); ++ return this; ++ } ++ ++ @Override ++ public Builder addColors(final List colors) { ++ for (Color color : colors) { ++ this.addColor(color); ++ } ++ return this; ++ } ++ ++ @Override ++ public CustomModelData build() { ++ return new PaperCustomModelData( ++ new net.minecraft.world.item.component.CustomModelData( ++ this.floats, ++ this.flags, ++ this.strings, ++ this.colors ++ ) ++ ); ++ } ++ + } +} diff --git a/src/main/java/io/papermc/paper/datacomponent/item/PaperDamageResistant.java b/src/main/java/io/papermc/paper/datacomponent/item/PaperDamageResistant.java @@ -4276,9 +4350,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + } + + @Test -+ void testCustomModelData() { -+ // TODO -+ //testWithMeta(new ItemStack(Material.STONE), DataComponentTypes.CUSTOM_MODEL_DATA, CustomModelData.customModelData(1), CustomModelData::id, ItemMeta.class, ItemMeta::getCustomModelData, ItemMeta::setCustomModelData); ++ void testLegacyCustomModelData() { ++ testWithMeta(new ItemStack(Material.STONE), DataComponentTypes.CUSTOM_MODEL_DATA, CustomModelData.customModelData().addFloat(1).build(), customModelData -> customModelData.floats().get(0).intValue(), ItemMeta.class, ItemMeta::getCustomModelData, ItemMeta::setCustomModelData); + } + + @Test