Implement new CustomModelData

This commit is contained in:
Owen1212055 2024-12-04 00:26:31 -05:00
parent 606d8d99f8
commit 0eefdfb97a
2 changed files with 180 additions and 14 deletions

View file

@ -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<Integer> colors();
+ List<Color> colors();
+
+ /**
+ * Builder for {@link CustomModelData}.
+ */
+ @ApiStatus.Experimental
+ @ApiStatus.NonExtendable
+ interface Builder extends DataComponentBuilder<CustomModelData> {
+
+ /**
+ * 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<Float> 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<Boolean> 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<String> 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<Color> 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);
+

View file

@ -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<Float> floats() {
+ return this.impl.floats();
+ return Collections.unmodifiableList(this.impl.floats());
+ }
+
+ @Override
+ public List<Boolean> flags() {
+ return this.impl.flags();
+ return Collections.unmodifiableList(this.impl.flags());
+ }
+
+ @Override
+ public List<String> strings() {
+ return this.impl.strings();
+ return Collections.unmodifiableList(this.impl.strings());
+ }
+
+ @Override
+ public List<Integer> colors() {
+ return this.impl.colors();
+ public List<Color> colors() {
+ return MCUtil.transformUnmodifiable(this.impl.colors(), Color::fromRGB);
+ }
+
+ static final class BuilderImpl implements CustomModelData.Builder {
+
+ private final List<Float> floats = new ArrayList<>();
+ private final List<Boolean> flags = new ArrayList<>();
+ private final List<String> strings = new ArrayList<>();
+ private final List<Integer> colors = new ArrayList<>();
+
+ @Override
+ public Builder addFloat(final float num) {
+ this.floats.add(num);
+ return this;
+ }
+
+ @Override
+ public Builder addFloats(final List<Float> 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<Boolean> 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<String> 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<Color> 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