Skip to content

Commit

Permalink
Customizing item durability and rarity
Browse files Browse the repository at this point in the history
  • Loading branch information
SoSeDiK committed Sep 11, 2024
1 parent 48d90c7 commit bd2a6c8
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 70 deletions.
41 changes: 37 additions & 4 deletions patches/api/0015-Data-driven-items.patch
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ 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..f1087139b5fa13174ea3bc1699416197022da05f
index 0000000000000000000000000000000000000000..1f61fe3836de8d89a23e36efc4c5d65fd56ae838
--- /dev/null
+++ b/src/main/java/me/sosedik/kiterino/registry/data/ItemRegistryEntity.java
@@ -0,0 +1,109 @@
@@ -0,0 +1,142 @@
+package me.sosedik.kiterino.registry.data;
+
+import io.papermc.paper.registry.RegistryBuilder;
+import org.bukkit.inventory.ItemRarity;
+import org.bukkit.inventory.ItemType;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.jetbrains.annotations.ApiStatus;
Expand All @@ -52,13 +53,27 @@ index 0000000000000000000000000000000000000000..f1087139b5fa13174ea3bc1699416197
+ int maxStackSize();
+
+ /**
+ * Provides the item's default durability
+ *
+ * @return the item's default durability
+ */
+ @Nullable Integer durability();
+
+ /**
+ * Provides the item's fire resistance state
+ *
+ * @return the item's fire resistance state
+ */
+ boolean fireResistant();
+
+ /**
+ * Provides the item's default rarity
+ *
+ * @return the item's default rarity
+ */
+ @Nullable ItemRarity rarity();
+
+ /**
+ * Provides the item's compost chance
+ *
+ * @return the item's compost chance
Expand Down Expand Up @@ -89,11 +104,20 @@ index 0000000000000000000000000000000000000000..f1087139b5fa13174ea3bc1699416197
+ /**
+ * Configures the item's default max stack size
+ *
+ * @param size the item's default max stack size
+ * @param maxStackSize the item's default max stack size
+ * @return this builder
+ */
+ @Contract(value = "_ -> this", mutates = "this")
+ @NonNull Builder maxStackSize(@Range(from = 1L, to = 99L) int maxStackSize);
+
+ /**
+ * Configures the item's default durability
+ *
+ * @param durability the item's default durability
+ * @return this builder
+ */
+ @Contract(value = "_ -> this", mutates = "this")
+ @NonNull Builder maxStackSize(@Range(from = 1L, to = 99L) int size);
+ @NonNull Builder durability(@Range(from = 1L, to = Integer.MAX_VALUE) @Nullable Integer durability);
+
+ /**
+ * Configures the item's fire resistance state
Expand All @@ -105,6 +129,15 @@ index 0000000000000000000000000000000000000000..f1087139b5fa13174ea3bc1699416197
+ @NonNull Builder fireResistant(boolean fireResistant);
+
+ /**
+ * Configures the item's default rarity
+ *
+ * @param rarity the item's default rarity
+ * @return this builder
+ */
+ @Contract(value = "_ -> this", mutates = "this")
+ @NonNull Builder rarity(@Nullable ItemRarity rarity);
+
+ /**
+ * Configures the item's compost chance
+ *
+ * @param levelIncreaseChance the item's compost chance
Expand Down
8 changes: 4 additions & 4 deletions patches/api/0017-Faking-custom-items.patch
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ 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 f1087139b5fa13174ea3bc1699416197022da05f..982e1d191a7be650aa1305c0d6a51e08bc4fc13c 100644
index 1f61fe3836de8d89a23e36efc4c5d65fd56ae838..998405409ec613e60a13f1a235a477ababa6364e 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 @@
package me.sosedik.kiterino.registry.data;

+import me.sosedik.kiterino.modifier.item.KiterinoItemModifier;
import io.papermc.paper.registry.RegistryBuilder;
import org.bukkit.inventory.ItemRarity;
import org.bukkit.inventory.ItemType;
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -17,6 +18,13 @@ import java.util.function.Function;
@@ -18,6 +19,13 @@ import java.util.function.Function;
@ApiStatus.NonExtendable
public interface ItemRegistryEntity {

Expand All @@ -69,7 +69,7 @@ index f1087139b5fa13174ea3bc1699416197022da05f..982e1d191a7be650aa1305c0d6a51e08
/**
* Provides the item's default max stack size
*
@@ -59,6 +67,15 @@ public interface ItemRegistryEntity {
@@ -74,6 +82,15 @@ public interface ItemRegistryEntity {
@ApiStatus.NonExtendable
interface Builder extends ItemRegistryEntity, RegistryBuilder<ItemType> {

Expand Down
80 changes: 73 additions & 7 deletions patches/server/0017-Data-driven-items.patch
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,46 @@ index 6ec9d9b9acf557aa2ebf39d38a14225b0205fae1..7603395aace52f16453de854b95f2f8c
entry(Registries.CAT_VARIANT, RegistryKey.CAT_VARIANT, Cat.Type.class, CraftCat.CraftType::new),
entry(Registries.FROG_VARIANT, RegistryKey.FROG_VARIANT, Frog.Variant.class, CraftFrog.CraftVariant::new),
entry(Registries.VILLAGER_PROFESSION, RegistryKey.VILLAGER_PROFESSION, Villager.Profession.class, CraftVillager.CraftProfession::new),
diff --git a/src/main/java/io/papermc/paper/registry/data/util/Conversions.java b/src/main/java/io/papermc/paper/registry/data/util/Conversions.java
index eda5cc7d45ef59ccc1c9c7e027c1f044f1dcc86b..48de73884cdece86ff19e36d930e3990066de3bf 100644
--- a/src/main/java/io/papermc/paper/registry/data/util/Conversions.java
+++ b/src/main/java/io/papermc/paper/registry/data/util/Conversions.java
@@ -33,4 +33,24 @@ public class Conversions {
public Component asAdventure(final net.minecraft.network.chat.@Nullable Component vanilla) {
return vanilla == null ? Component.empty() : this.serializer.deserialize(vanilla);
}
+
+ // Kiterino start - Data-driven items
+ public net.minecraft.world.item.Rarity asVanilla(final org.bukkit.inventory.ItemRarity rarity) {
+ return switch (rarity) {
+ case COMMON -> net.minecraft.world.item.Rarity.COMMON;
+ case UNCOMMON -> net.minecraft.world.item.Rarity.UNCOMMON;
+ case RARE -> net.minecraft.world.item.Rarity.RARE;
+ case EPIC -> net.minecraft.world.item.Rarity.EPIC;
+ };
+ }
+
+ public org.bukkit.inventory.ItemRarity asBukkit(final net.minecraft.world.item.Rarity rarity) {
+ return switch (rarity) {
+ case COMMON -> org.bukkit.inventory.ItemRarity.COMMON;
+ case UNCOMMON -> org.bukkit.inventory.ItemRarity.UNCOMMON;
+ case RARE -> org.bukkit.inventory.ItemRarity.RARE;
+ case EPIC -> org.bukkit.inventory.ItemRarity.EPIC;
+ };
+ }
+ // Kiterino end - Data-driven items
}
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..e7ed3e5e40401433269a802aa455cc7dc4042abe
index 0000000000000000000000000000000000000000..60403dd5ce27c84057e6c9e59c810b3e318e1d98
--- /dev/null
+++ b/src/main/java/me/sosedik/kiterino/registry/data/KiterinoItemRegistryEntity.java
@@ -0,0 +1,125 @@
@@ -0,0 +1,162 @@
+package me.sosedik.kiterino.registry.data;
+
+import net.minecraft.core.component.DataComponentMap;
+import net.minecraft.core.component.DataComponents;
+import org.bukkit.inventory.ItemRarity;
+import io.papermc.paper.registry.PaperRegistryBuilder;
+import io.papermc.paper.registry.TypedKey;
+import io.papermc.paper.registry.data.util.Conversions;
Expand All @@ -33,25 +65,31 @@ index 0000000000000000000000000000000000000000..e7ed3e5e40401433269a802aa455cc7d
+import org.bukkit.inventory.ItemType;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.jetbrains.annotations.Range;
+
+import java.util.Objects;
+import java.util.function.Function;
+
+public class KiterinoItemRegistryEntity implements ItemRegistryEntity {
+
+ protected int maxStackSize;
+ protected @Nullable Integer durability;
+ protected boolean fireResistant;
+ protected @Nullable ItemRarity rarity;
+ protected @Nullable Float compostChance;
+ protected @Nullable Item nmsItem;
+ protected @Nullable Function<Object, Object> nmsItemFunction;
+
+ protected final Conversions conversions;
+ protected final TypedKey<ItemType> itemKey;
+
+ public KiterinoItemRegistryEntity(
+ final @NonNull Conversions conversions,
+ final @NonNull TypedKey<ItemType> ignoredKey,
+ final @Nullable Item internal
+ ) {
+ this.conversions = conversions;
+ this.itemKey = ignoredKey;
+ if (internal == null) {
+ this.maxStackSize = 64;
+ return;
Expand All @@ -66,11 +104,21 @@ index 0000000000000000000000000000000000000000..e7ed3e5e40401433269a802aa455cc7d
+ }
+
+ @Override
+ public @Nullable Integer durability() {
+ return this.durability;
+ }
+
+ @Override
+ public boolean fireResistant() {
+ return this.fireResistant;
+ }
+
+ @Override
+ public @Nullable ItemRarity rarity() {
+ return this.rarity;
+ }
+
+ @Override
+ public @Nullable Float compostChance() {
+ return this.compostChance;
+ }
Expand All @@ -86,7 +134,11 @@ index 0000000000000000000000000000000000000000..e7ed3e5e40401433269a802aa455cc7d
+ }
+
+ protected void parseValues(@NonNull Item item) {
+ DataComponentMap components = item.components();
+ this.maxStackSize = item.getDefaultMaxStackSize();
+ this.durability = components.get(DataComponents.MAX_DAMAGE);
+ this.fireResistant = components.has(DataComponents.FIRE_RESISTANT);
+ this.rarity = components.has(DataComponents.RARITY) ? conversions.asBukkit(Objects.requireNonNull(components.get(DataComponents.RARITY))) : null;
+ }
+
+ public static final class KiterinoBuilder extends KiterinoItemRegistryEntity implements Builder,
Expand All @@ -97,8 +149,14 @@ index 0000000000000000000000000000000000000000..e7ed3e5e40401433269a802aa455cc7d
+ }
+
+ @Override
+ public @NonNull Builder maxStackSize(int size) {
+ this.maxStackSize = size;
+ public @NonNull Builder maxStackSize(int maxStackSize) {
+ this.maxStackSize = maxStackSize;
+ return this;
+ }
+
+ @Override
+ public @NonNull Builder durability(@Range(from = 1L, to = Integer.MAX_VALUE) @Nullable Integer durability) {
+ this.durability = durability;
+ return this;
+ }
+
Expand All @@ -109,6 +167,12 @@ index 0000000000000000000000000000000000000000..e7ed3e5e40401433269a802aa455cc7d
+ }
+
+ @Override
+ public @NonNull Builder rarity(@Nullable ItemRarity rarity) {
+ this.rarity = rarity;
+ return this;
+ }
+
+ @Override
+ public @NonNull Builder compostChance(@Nullable Float levelIncreaseChance) {
+ this.compostChance = levelIncreaseChance;
+ return this;
Expand All @@ -132,9 +196,11 @@ index 0000000000000000000000000000000000000000..e7ed3e5e40401433269a802aa455cc7d
+ 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();
+ var properties = new Item.Properties();
+ if (this.durability != null) properties.durability(this.durability);
+ properties.stacksTo(this.maxStackSize);
+ if (this.fireResistant) properties.fireResistant();
+ if (this.rarity != null) properties.rarity(conversions.asVanilla(this.rarity));
+ if (this.nmsItemFunction == null) {
+ item = new Item(properties);
+ } else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,56 +67,41 @@ 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 e7ed3e5e40401433269a802aa455cc7dc4042abe..f8cebc380a565ecb8f56f8f4bebf562f21ae155d 100644
index 60403dd5ce27c84057e6c9e59c810b3e318e1d98..fd737493e557ff468bc31a9814eced0a07702ac4 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 @@
@@ -1,5 +1,9 @@
package me.sosedik.kiterino.registry.data;

+import me.sosedik.kiterino.modifier.item.KiterinoItemModifier;
+import me.sosedik.kiterino.modifier.item.KiterinoItemModifierImpl;
+import org.bukkit.inventory.meta.ItemMeta;
import io.papermc.paper.registry.PaperRegistryBuilder;
import io.papermc.paper.registry.TypedKey;
import io.papermc.paper.registry.data.util.Conversions;
@@ -13,6 +16,7 @@ import java.util.function.Function;

public class KiterinoItemRegistryEntity implements ItemRegistryEntity {

+ protected @Nullable KiterinoItemModifier modifier;
protected int maxStackSize;
protected boolean fireResistant;
+
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.core.component.DataComponents;
import org.bukkit.inventory.ItemRarity;
@@ -25,6 +29,7 @@ public class KiterinoItemRegistryEntity implements ItemRegistryEntity {
protected @Nullable Float compostChance;
@@ -20,6 +24,7 @@ public class KiterinoItemRegistryEntity implements ItemRegistryEntity {
protected @Nullable Item nmsItem;
protected @Nullable Function<Object, Object> nmsItemFunction;
+ protected @Nullable KiterinoItemModifier modifier;

protected final Conversions conversions;
+ protected final TypedKey<ItemType> itemKey;

public KiterinoItemRegistryEntity(
final @NonNull Conversions conversions,
@@ -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;
@@ -35,6 +41,11 @@ public class KiterinoItemRegistryEntity implements ItemRegistryEntity {
parseValues(internal);
protected final TypedKey<ItemType> itemKey;
@@ -79,6 +84,11 @@ public class KiterinoItemRegistryEntity implements ItemRegistryEntity {
return this.nmsItemFunction;
}

+ @Override
+ public @Nullable KiterinoItemModifier modifier() {
+ return this.modifier;
+ }
+
@Override
public int maxStackSize() {
return this.maxStackSize;
@@ -71,6 +82,12 @@ public class KiterinoItemRegistryEntity implements ItemRegistryEntity {
super(conversions, key, internal);
protected void parseValues(@NonNull Item item) {
DataComponentMap components = item.components();
this.maxStackSize = item.getDefaultMaxStackSize();
@@ -138,8 +148,23 @@ public class KiterinoItemRegistryEntity implements ItemRegistryEntity {
return this;
}

+ @Override
Expand All @@ -125,21 +110,6 @@ index e7ed3e5e40401433269a802aa455cc7dc4042abe..f8cebc380a565ecb8f56f8f4bebf562f
+ return this;
+ }
+
@Override
public @NonNull Builder maxStackSize(int size) {
this.maxStackSize = size;
@@ -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()
@@ -120,6 +138,14 @@ public class KiterinoItemRegistryEntity implements ItemRegistryEntity {
return item;
}

+ private @NonNull KiterinoItemModifier buildModifier() {
+ return box -> {
+ ItemMeta meta = box.getMeta();
Expand All @@ -148,9 +118,12 @@ index e7ed3e5e40401433269a802aa455cc7dc4042abe..f8cebc380a565ecb8f56f8f4bebf562f
+ };
+ }
+
}

}
@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();
diff --git a/src/main/java/me/sosedik/kiterino/util/KiterinoBootstrapMaterialInjectorImpl.java b/src/main/java/me/sosedik/kiterino/util/KiterinoBootstrapMaterialInjectorImpl.java
index c8714a9e3d382827a61c02f61f05b1449d0fa8ea..5d8b3b44da3403245157aeadf14dff35cce3362d 100644
--- a/src/main/java/me/sosedik/kiterino/util/KiterinoBootstrapMaterialInjectorImpl.java
Expand Down
Loading

0 comments on commit bd2a6c8

Please sign in to comment.