Skip to content

Commit

Permalink
Allow using constructed properties in nms items
Browse files Browse the repository at this point in the history
  • Loading branch information
SoSeDiK committed Sep 11, 2024
1 parent dbaa732 commit 48d90c7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 16 deletions.
22 changes: 20 additions & 2 deletions patches/api/0015-Data-driven-items.patch
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
+ */
Expand Down Expand Up @@ -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<Object, Object> nmsItemFunction();
+
+ /**
+ * A mutable builder for the {@link ItemRegistryEntity} plugins may change in applicable registry events.
+ */
+ @ApiStatus.Experimental
Expand Down Expand Up @@ -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<Object, Object> nmsItemFunction);
+
+ }
+
+}
6 changes: 3 additions & 3 deletions patches/api/0017-Faking-custom-items.patch
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@
Expand All @@ -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 {

Expand All @@ -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<ItemType> {

Expand Down
24 changes: 21 additions & 3 deletions patches/server/0017-Data-driven-items.patch
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Object, Object> nmsItemFunction;
+
+ protected final Conversions conversions;
+
Expand Down Expand Up @@ -77,6 +80,11 @@ index 0000000000000000000000000000000000000000..d7fb607dd419d48f16147df96f73a054
+ return this.nmsItem;
+ }
+
+ @Override
+ public @Nullable Function<Object, Object> nmsItemFunction() {
+ return this.nmsItemFunction;
+ }
+
+ protected void parseValues(@NonNull Item item) {
+ this.maxStackSize = item.getDefaultMaxStackSize();
+ }
Expand Down Expand Up @@ -115,13 +123,23 @@ index 0000000000000000000000000000000000000000..d7fb607dd419d48f16147df96f73a054
+ }
+
+ @Override
+ public @NonNull Builder nmsItemFunction(@Nullable Function<Object, Object> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@
Expand All @@ -79,30 +79,31 @@ 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 {

+ protected @Nullable KiterinoItemModifier modifier;
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<Object, Object> nmsItemFunction;

protected final Conversions conversions;
+ protected final TypedKey<ItemType> 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;
+ this.itemKey = ignoredKey;
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);
}

Expand All @@ -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);
}

Expand All @@ -127,15 +128,15 @@ 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() {
+ if (this.modifier != null) KiterinoItemModifierImpl.MODIFIER_IMPL.add(new org.bukkit.NamespacedKey(this.itemKey.key().namespace(), this.itemKey.key().value()), buildModifier());
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;
}

Expand Down

0 comments on commit 48d90c7

Please sign in to comment.