Add SimplyCompatSwordItem and sword-specific registry for better weapon item management
This commit is contained in:
parent
e6a6314f6b
commit
5d3db95a19
3 changed files with 71 additions and 12 deletions
|
@ -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 <E> The type of the enum that extends ToolMaterials.
|
||||
*/
|
||||
public <E extends Enum<E> & ToolMaterials<E>> SimplyCompatSwordItem(ToolMaterials<E> toolMaterial, WeaponType type) {
|
||||
super(
|
||||
toolMaterial,
|
||||
(int) (toolMaterial.getDamageModifier() + type.getPositiveModifier() - type.getNegativeModifier()),
|
||||
type.getAttackSpeed(),
|
||||
toolMaterial.getRepairIngredient().toString()
|
||||
);
|
||||
|
||||
this.TYPE = type;
|
||||
}
|
||||
}
|
|
@ -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<Item> ITEMS = DeferredRegister.create(
|
||||
|
||||
/**
|
||||
* Special register for swords only.
|
||||
*/
|
||||
public static final DeferredRegister<SimplyCompatSwordItem> 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 <E> The type of the enum that extends ToolMaterials.
|
||||
*/
|
||||
public static <E extends Enum<E> & ToolMaterials<E>> void registerSword(
|
||||
ToolMaterials<E> 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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Registry<SimplyCompatSwordItem>> SWORD_ITEM = ResourceKey.createRegistryKey(new ResourceLocation(SimplyCompat.MOD_ID, "sword_item"));
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue