From 5d3db95a19763b26d51486a6abfb752b0a8c56d6 Mon Sep 17 00:00:00 2001 From: Chris Toph Date: Wed, 26 Feb 2025 04:40:55 -0500 Subject: [PATCH] Add SimplyCompatSwordItem and sword-specific registry for better weapon item management --- .../item/SimplyCompatSwordItem.java | 38 +++++++++++++++++++ .../simplycompat/registry/ItemsRegistry.java | 29 ++++++++------ .../registry/SimplyCompatRegistries.java | 16 ++++++++ 3 files changed, 71 insertions(+), 12 deletions(-) create mode 100644 common/src/main/java/cc/toph/simplycompat/item/SimplyCompatSwordItem.java create mode 100644 common/src/main/java/cc/toph/simplycompat/registry/SimplyCompatRegistries.java diff --git a/common/src/main/java/cc/toph/simplycompat/item/SimplyCompatSwordItem.java b/common/src/main/java/cc/toph/simplycompat/item/SimplyCompatSwordItem.java new file mode 100644 index 0000000..1c68676 --- /dev/null +++ b/common/src/main/java/cc/toph/simplycompat/item/SimplyCompatSwordItem.java @@ -0,0 +1,38 @@ +package cc.toph.simplycompat.item; + +import cc.toph.simplycompat.registry.SimplyCompatRegistries; +import cc.toph.simplycompat.util.ToolMaterials; +import cc.toph.simplycompat.util.WeaponType; +import net.sweenus.simplyswords.item.SimplySwordsSwordItem; + +/** + * Extended Class from Simply Swords to store sword type. + * Storing Sword type will let us avoid all the static mess in registries. + * This class stores all the data we need from the sword, so we can just + * retrieve the object from the registries thanks to {@link SimplyCompatRegistries#SWORD_ITEM} and do whatever we need. + */ +public class SimplyCompatSwordItem extends SimplySwordsSwordItem { + + /** + * The type of the weapon. + */ + public final WeaponType TYPE; + + /** + * Constructor for SimplyCompatSwordItem. + * + * @param toolMaterial The material of the tool, which some of its properties/stats. + * @param type The type of the weapon, which affects its modifiers. + * @param The type of the enum that extends ToolMaterials. + */ + public & ToolMaterials> SimplyCompatSwordItem(ToolMaterials toolMaterial, WeaponType type) { + super( + toolMaterial, + (int) (toolMaterial.getDamageModifier() + type.getPositiveModifier() - type.getNegativeModifier()), + type.getAttackSpeed(), + toolMaterial.getRepairIngredient().toString() + ); + + this.TYPE = type; + } +} \ No newline at end of file diff --git a/common/src/main/java/cc/toph/simplycompat/registry/ItemsRegistry.java b/common/src/main/java/cc/toph/simplycompat/registry/ItemsRegistry.java index 4dc96ac..361b0eb 100644 --- a/common/src/main/java/cc/toph/simplycompat/registry/ItemsRegistry.java +++ b/common/src/main/java/cc/toph/simplycompat/registry/ItemsRegistry.java @@ -1,31 +1,36 @@ package cc.toph.simplycompat.registry; import cc.toph.simplycompat.SimplyCompat; +import cc.toph.simplycompat.item.SimplyCompatSwordItem; import cc.toph.simplycompat.item.SimplyCompatToolMaterials; import cc.toph.simplycompat.util.ToolMaterials; import cc.toph.simplycompat.util.WeaponType; import dev.architectury.registry.registries.DeferredRegister; -import net.minecraft.core.registries.Registries; -import net.minecraft.world.item.Item; -import net.sweenus.simplyswords.item.SimplySwordsSwordItem; public final class ItemsRegistry { - public static final DeferredRegister ITEMS = DeferredRegister.create( + + /** + * Special register for swords only. + */ + public static final DeferredRegister SWORDS = DeferredRegister.create( SimplyCompat.MOD_ID, - Registries.ITEM + SimplyCompatRegistries.SWORD_ITEM ); + /** + * Registers a sword item with the given material and weapon type. + * + * @param material The material of the tool, which some of its properties/stats. + * @param type The type of the weapon, which affects its modifiers. + * @param The type of the enum that extends ToolMaterials. + */ public static & ToolMaterials> void registerSword( ToolMaterials material, WeaponType type ) { - - float finalDamage = material.getDamageModifier() + type.getPositiveModifier() - type.getNegativeModifier(); String name = material.getName() + "_" + type.getWeaponNameSuffix(); - - ITEMS.register(name, () -> - new SimplySwordsSwordItem(material, (int) finalDamage, type.getAttackSpeed(), material.getIdentifier()) - ); + SWORDS.register(name, () -> + new SimplyCompatSwordItem(material, type)); } /** @@ -46,6 +51,6 @@ public final class ItemsRegistry { } CompatRegistry.registerAll(); - ITEMS.register(); + SWORDS.register(); } } diff --git a/common/src/main/java/cc/toph/simplycompat/registry/SimplyCompatRegistries.java b/common/src/main/java/cc/toph/simplycompat/registry/SimplyCompatRegistries.java new file mode 100644 index 0000000..b8253a3 --- /dev/null +++ b/common/src/main/java/cc/toph/simplycompat/registry/SimplyCompatRegistries.java @@ -0,0 +1,16 @@ +package cc.toph.simplycompat.registry; + +import cc.toph.simplycompat.SimplyCompat; +import cc.toph.simplycompat.item.SimplyCompatSwordItem; +import net.minecraft.core.Registry; +import net.minecraft.core.registries.Registries; +import net.minecraft.resources.ResourceKey; +import net.minecraft.resources.ResourceLocation; + +public class SimplyCompatRegistries extends Registries { + + // Defines a Key for Sword Items, enabling the usage of Sword Specific Registries + // In essence allows the usage of the SimplyCompatSwordItem so we can access modded sword data + public static final ResourceKey> SWORD_ITEM = ResourceKey.createRegistryKey(new ResourceLocation(SimplyCompat.MOD_ID, "sword_item")); + +}