From 48d90c7b1f39886c594ba84b16a0907d615ca838 Mon Sep 17 00:00:00 2001 From: SoSeDiK Date: Wed, 11 Sep 2024 23:24:15 +0300 Subject: [PATCH] Allow using constructed properties in nms items --- patches/api/0015-Data-driven-items.patch | 22 +++++++++++++++-- patches/api/0017-Faking-custom-items.patch | 6 ++--- patches/server/0017-Data-driven-items.patch | 24 ++++++++++++++++--- ...packet-item-faker-for-injected-items.patch | 17 ++++++------- 4 files changed, 53 insertions(+), 16 deletions(-) diff --git a/patches/api/0015-Data-driven-items.patch b/patches/api/0015-Data-driven-items.patch index b20d551..f8c70f0 100644 --- a/patches/api/0015-Data-driven-items.patch +++ b/patches/api/0015-Data-driven-items.patch @@ -21,10 +21,10 @@ index b32ae215e976bcfcdd86b03037de61b3d896f57c..7b12d1fcf5a2f5d4a043f934611fc824 } diff --git a/src/main/java/me/sosedik/kiterino/registry/data/ItemRegistryEntity.java b/src/main/java/me/sosedik/kiterino/registry/data/ItemRegistryEntity.java new file mode 100644 -index 0000000000000000000000000000000000000000..54946c292fda07025e8887aea254e5ad6c4ae89f +index 0000000000000000000000000000000000000000..f1087139b5fa13174ea3bc1699416197022da05f --- /dev/null +++ b/src/main/java/me/sosedik/kiterino/registry/data/ItemRegistryEntity.java -@@ -0,0 +1,91 @@ +@@ -0,0 +1,109 @@ +package me.sosedik.kiterino.registry.data; + +import io.papermc.paper.registry.RegistryBuilder; @@ -35,6 +35,8 @@ index 0000000000000000000000000000000000000000..54946c292fda07025e8887aea254e5ad +import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.Range; + ++import java.util.function.Function; ++ +/** + * A data-centric version-specific registry entry for the {@link ItemType} type. + */ @@ -71,6 +73,13 @@ index 0000000000000000000000000000000000000000..54946c292fda07025e8887aea254e5ad + @Nullable Object nmsItem(); + + /** ++ * Provides the function that takes constructed item properties and provides the item's nms implementation ++ * ++ * @return the item's nms implementation ++ */ ++ @Nullable Function nmsItemFunction(); ++ ++ /** + * A mutable builder for the {@link ItemRegistryEntity} plugins may change in applicable registry events. + */ + @ApiStatus.Experimental @@ -113,6 +122,15 @@ index 0000000000000000000000000000000000000000..54946c292fda07025e8887aea254e5ad + @Contract(value = "_ -> this", mutates = "this") + @NonNull Builder nmsItem(@Nullable Object nmsItem); + ++ /** ++ * Configures the function that takes constructed item properties and provides the item's nms implementation ++ * ++ * @param nmsItemFunction the item's nms implementation ++ * @return this builder ++ */ ++ @Contract(value = "_ -> this", mutates = "this") ++ @NonNull Builder nmsItemFunction(@Nullable Function nmsItemFunction); ++ + } + +} diff --git a/patches/api/0017-Faking-custom-items.patch b/patches/api/0017-Faking-custom-items.patch index d486bbb..958a90d 100644 --- a/patches/api/0017-Faking-custom-items.patch +++ b/patches/api/0017-Faking-custom-items.patch @@ -45,7 +45,7 @@ index 0000000000000000000000000000000000000000..e34e247ae98bf9fe65485f9f2376a61a + +} diff --git a/src/main/java/me/sosedik/kiterino/registry/data/ItemRegistryEntity.java b/src/main/java/me/sosedik/kiterino/registry/data/ItemRegistryEntity.java -index 54946c292fda07025e8887aea254e5ad6c4ae89f..1b69bfc154c3e4675d09c4d7d586a64b924f1ecd 100644 +index f1087139b5fa13174ea3bc1699416197022da05f..982e1d191a7be650aa1305c0d6a51e08bc4fc13c 100644 --- a/src/main/java/me/sosedik/kiterino/registry/data/ItemRegistryEntity.java +++ b/src/main/java/me/sosedik/kiterino/registry/data/ItemRegistryEntity.java @@ -1,5 +1,6 @@ @@ -55,7 +55,7 @@ index 54946c292fda07025e8887aea254e5ad6c4ae89f..1b69bfc154c3e4675d09c4d7d586a64b import io.papermc.paper.registry.RegistryBuilder; import org.bukkit.inventory.ItemType; import org.checkerframework.checker.nullness.qual.NonNull; -@@ -15,6 +16,13 @@ import org.jetbrains.annotations.Range; +@@ -17,6 +18,13 @@ import java.util.function.Function; @ApiStatus.NonExtendable public interface ItemRegistryEntity { @@ -69,7 +69,7 @@ index 54946c292fda07025e8887aea254e5ad6c4ae89f..1b69bfc154c3e4675d09c4d7d586a64b /** * Provides the item's default max stack size * -@@ -50,6 +58,15 @@ public interface ItemRegistryEntity { +@@ -59,6 +67,15 @@ public interface ItemRegistryEntity { @ApiStatus.NonExtendable interface Builder extends ItemRegistryEntity, RegistryBuilder { diff --git a/patches/server/0017-Data-driven-items.patch b/patches/server/0017-Data-driven-items.patch index ac5d21e..b02e4ec 100644 --- a/patches/server/0017-Data-driven-items.patch +++ b/patches/server/0017-Data-driven-items.patch @@ -19,10 +19,10 @@ index 6ec9d9b9acf557aa2ebf39d38a14225b0205fae1..7603395aace52f16453de854b95f2f8c entry(Registries.VILLAGER_PROFESSION, RegistryKey.VILLAGER_PROFESSION, Villager.Profession.class, CraftVillager.CraftProfession::new), diff --git a/src/main/java/me/sosedik/kiterino/registry/data/KiterinoItemRegistryEntity.java b/src/main/java/me/sosedik/kiterino/registry/data/KiterinoItemRegistryEntity.java new file mode 100644 -index 0000000000000000000000000000000000000000..d7fb607dd419d48f16147df96f73a054cb2ad6ee +index 0000000000000000000000000000000000000000..e7ed3e5e40401433269a802aa455cc7dc4042abe --- /dev/null +++ b/src/main/java/me/sosedik/kiterino/registry/data/KiterinoItemRegistryEntity.java -@@ -0,0 +1,107 @@ +@@ -0,0 +1,125 @@ +package me.sosedik.kiterino.registry.data; + +import io.papermc.paper.registry.PaperRegistryBuilder; @@ -34,12 +34,15 @@ index 0000000000000000000000000000000000000000..d7fb607dd419d48f16147df96f73a054 +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + ++import java.util.function.Function; ++ +public class KiterinoItemRegistryEntity implements ItemRegistryEntity { + + protected int maxStackSize; + protected boolean fireResistant; + protected @Nullable Float compostChance; + protected @Nullable Item nmsItem; ++ protected @Nullable Function nmsItemFunction; + + protected final Conversions conversions; + @@ -77,6 +80,11 @@ index 0000000000000000000000000000000000000000..d7fb607dd419d48f16147df96f73a054 + return this.nmsItem; + } + ++ @Override ++ public @Nullable Function nmsItemFunction() { ++ return this.nmsItemFunction; ++ } ++ + protected void parseValues(@NonNull Item item) { + this.maxStackSize = item.getDefaultMaxStackSize(); + } @@ -115,13 +123,23 @@ index 0000000000000000000000000000000000000000..d7fb607dd419d48f16147df96f73a054 + } + + @Override ++ public @NonNull Builder nmsItemFunction(@Nullable Function nmsItemFunction) { ++ this.nmsItemFunction = nmsItemFunction; ++ return this; ++ } ++ ++ @Override + public @NonNull Item build() { + Item item = this.nmsItem; + if (item == null) { + var properties = new Item.Properties() + .stacksTo(this.maxStackSize); + if (this.fireResistant) properties = properties.fireResistant(); -+ item = new Item(properties); ++ if (this.nmsItemFunction == null) { ++ item = new Item(properties); ++ } else { ++ item = (Item) this.nmsItemFunction.apply(properties); ++ } + } + if (this.compostChance != null) ComposterBlock.add(this.compostChance, item); + return item; diff --git a/patches/server/0019-Implement-packet-item-faker-for-injected-items.patch b/patches/server/0019-Implement-packet-item-faker-for-injected-items.patch index 19223b5..5dc5b67 100644 --- a/patches/server/0019-Implement-packet-item-faker-for-injected-items.patch +++ b/patches/server/0019-Implement-packet-item-faker-for-injected-items.patch @@ -67,7 +67,7 @@ index 0000000000000000000000000000000000000000..16242986d124173007c4e18dd6219e5d + +} diff --git a/src/main/java/me/sosedik/kiterino/registry/data/KiterinoItemRegistryEntity.java b/src/main/java/me/sosedik/kiterino/registry/data/KiterinoItemRegistryEntity.java -index d7fb607dd419d48f16147df96f73a054cb2ad6ee..9e6db3fe451a015c77ad367974daea30329abcba 100644 +index e7ed3e5e40401433269a802aa455cc7dc4042abe..f8cebc380a565ecb8f56f8f4bebf562f21ae155d 100644 --- a/src/main/java/me/sosedik/kiterino/registry/data/KiterinoItemRegistryEntity.java +++ b/src/main/java/me/sosedik/kiterino/registry/data/KiterinoItemRegistryEntity.java @@ -1,5 +1,8 @@ @@ -79,7 +79,7 @@ index d7fb607dd419d48f16147df96f73a054cb2ad6ee..9e6db3fe451a015c77ad367974daea30 import io.papermc.paper.registry.PaperRegistryBuilder; import io.papermc.paper.registry.TypedKey; import io.papermc.paper.registry.data.util.Conversions; -@@ -11,12 +14,14 @@ import org.checkerframework.checker.nullness.qual.Nullable; +@@ -13,6 +16,7 @@ import java.util.function.Function; public class KiterinoItemRegistryEntity implements ItemRegistryEntity { @@ -87,14 +87,15 @@ index d7fb607dd419d48f16147df96f73a054cb2ad6ee..9e6db3fe451a015c77ad367974daea30 protected int maxStackSize; protected boolean fireResistant; protected @Nullable Float compostChance; - protected @Nullable Item nmsItem; +@@ -20,6 +24,7 @@ public class KiterinoItemRegistryEntity implements ItemRegistryEntity { + protected @Nullable Function nmsItemFunction; protected final Conversions conversions; + protected final TypedKey itemKey; public KiterinoItemRegistryEntity( final @NonNull Conversions conversions, -@@ -24,6 +29,7 @@ public class KiterinoItemRegistryEntity implements ItemRegistryEntity { +@@ -27,6 +32,7 @@ public class KiterinoItemRegistryEntity implements ItemRegistryEntity { final @Nullable Item internal ) { this.conversions = conversions; @@ -102,7 +103,7 @@ index d7fb607dd419d48f16147df96f73a054cb2ad6ee..9e6db3fe451a015c77ad367974daea30 if (internal == null) { this.maxStackSize = 64; return; -@@ -32,6 +38,11 @@ public class KiterinoItemRegistryEntity implements ItemRegistryEntity { +@@ -35,6 +41,11 @@ public class KiterinoItemRegistryEntity implements ItemRegistryEntity { parseValues(internal); } @@ -114,7 +115,7 @@ index d7fb607dd419d48f16147df96f73a054cb2ad6ee..9e6db3fe451a015c77ad367974daea30 @Override public int maxStackSize() { return this.maxStackSize; -@@ -63,6 +74,12 @@ public class KiterinoItemRegistryEntity implements ItemRegistryEntity { +@@ -71,6 +82,12 @@ public class KiterinoItemRegistryEntity implements ItemRegistryEntity { super(conversions, key, internal); } @@ -127,7 +128,7 @@ index d7fb607dd419d48f16147df96f73a054cb2ad6ee..9e6db3fe451a015c77ad367974daea30 @Override public @NonNull Builder maxStackSize(int size) { this.maxStackSize = size; -@@ -91,6 +108,7 @@ public class KiterinoItemRegistryEntity implements ItemRegistryEntity { +@@ -105,6 +122,7 @@ public class KiterinoItemRegistryEntity implements ItemRegistryEntity { @Override public @NonNull Item build() { @@ -135,7 +136,7 @@ index d7fb607dd419d48f16147df96f73a054cb2ad6ee..9e6db3fe451a015c77ad367974daea30 Item item = this.nmsItem; if (item == null) { var properties = new Item.Properties() -@@ -102,6 +120,14 @@ public class KiterinoItemRegistryEntity implements ItemRegistryEntity { +@@ -120,6 +138,14 @@ public class KiterinoItemRegistryEntity implements ItemRegistryEntity { return item; }