Refactor compatibility system for modular integration

- Migrated mod-specific compatibility logic to a unified `CompatRegistry`, improving modularity and scalability.
- Enhanced `ToolMaterials` interface and enums to support generics and lazy-loaded repair ingredients.
- Simplified and centralized sword registration logic while removing TFMG-specific code.
This commit is contained in:
Chris Toph 2025-02-23 17:39:12 -05:00
parent c20ec9580e
commit 0d8a4e4196
11 changed files with 341 additions and 126 deletions

View file

@ -0,0 +1,55 @@
package cc.toph.simplycompat.compat;
import cc.toph.simplycompat.registry.ItemsRegistry;
import cc.toph.simplycompat.util.ToolMaterials;
import cc.toph.simplycompat.util.WeaponType;
import dev.architectury.platform.Platform;
/**
* The CompatRecord class represents a compatibility record used for managing
* mod integrations and extending functionality based on certain requirements.
* This record checks whether a specific mod is present, its version is compatible,
* and registers tool materials and weapon types if the conditions are met.
*
* @param <E> A type parameter that extends Enum and implements the ToolMaterials
* interface. This defines the supported tool materials for the integration.
* @param modId The unique identifier for the mod to be checked.
* @param requiredVersion The minimum version of the mod that is required for compatibility.
* @param toolMaterialClass The class representing the enumeration of tool materials to register.
*/
public record CompatRecord<E extends Enum<E> & ToolMaterials<E>>(
String modId,
String requiredVersion,
Class<E> toolMaterialClass
) {
/**
* Checks whether the mod's version is loaded
* and passes the required version check.
*/
public boolean passCheck() {
if (Platform.isModLoaded(modId)) {
return Platform.getMod(modId)
.getVersion()
.compareTo(requiredVersion) >= 0;
}
return false;
}
/**
* Registers swords for each enum constant in toolMaterialClass,
* if the material is enabled.
*/
public void register() {
if (!passCheck()) {
return;
}
for (WeaponType type : WeaponType.values()) {
for (E material : toolMaterialClass.getEnumConstants()) {
if (material.isEnabled()) {
ItemsRegistry.registerSword(material, type);
}
}
}
}
}

View file

@ -0,0 +1,107 @@
package cc.toph.simplycompat.compat;
import cc.toph.simplycompat.registry.ConfigRegistry;
import cc.toph.simplycompat.util.ToolMaterials;
import net.minecraft.util.LazyLoadedValue;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.level.ItemLike;
import org.jetbrains.annotations.NotNull;
import java.util.function.Supplier;
public enum CreateToolMaterials implements ToolMaterials<CreateToolMaterials> {
STURDY(
3,
1800,
8.0f,
3.5f,
ConfigRegistry.WEAPON_ATTRIBUTES.STURDY,
14,
ConfigRegistry.WEAPON_ATTRIBUTES.STURDY_ENABLED,
// () -> Ingredient.of(new ItemLike[]{(ItemLike) AllItems.STURDY_SHEET.get()})
() -> Ingredient.of(new ItemLike[]{(ItemLike) Items.STICK})
);
private final int level;
private final int uses;
private final float speed;
private final float attackDamageBonus;
private final float damageModifier;
private final int enchantmentValue;
private final boolean enabled;
private final LazyLoadedValue<Ingredient> repairIngredient;
CreateToolMaterials(
int level,
int uses,
float speed,
float attackDamageBonus,
float damageModifier,
int enchantmentValue,
boolean enabled,
Supplier<Ingredient> repairIngredient
) {
this.level = level;
this.uses = uses;
this.speed = speed;
this.attackDamageBonus = attackDamageBonus;
this.enchantmentValue = enchantmentValue;
this.damageModifier = damageModifier;
this.enabled = enabled;
this.repairIngredient = new LazyLoadedValue(repairIngredient);
}
@Override
public String getName() {
return this.name().toLowerCase();
}
@Override
public int getUses() {
return uses;
}
@Override
public float getSpeed() {
return speed;
}
@Override
public float getAttackDamageBonus() {
return attackDamageBonus;
}
@Override
public float getDamageModifier() {
return damageModifier;
}
@Override
public int getLevel() {
return level;
}
@Override
public int getEnchantmentValue() {
return enchantmentValue;
}
@Override
public boolean isEnabled() {
return enabled;
}
@Override
public @NotNull Ingredient getRepairIngredient() {
return this.repairIngredient.get();
}
@Override
public CreateToolMaterials[] getValues() {
return values();
}
}

View file

@ -1,28 +0,0 @@
package cc.toph.simplycompat.compat;
import cc.toph.simplycompat.registry.ItemsRegistry;
import cc.toph.simplycompat.util.WeaponType;
import dev.architectury.platform.Platform;
public class TFMG {
public static final String MOD_ID = "tfmg";
public static final String requiredVersion = "0.9.3-1.20.1";
public static void register() {
if (!passCheck()) return;
for (WeaponType type : WeaponType.values()) {
for (TFMGToolMaterials material : TFMGToolMaterials.values()) {
if (material.isEnabled()) ItemsRegistry.registerSword(material, type);
}
}
}
public static boolean passCheck() {
if (Platform.isModLoaded(MOD_ID)) {
return Platform.getMod(MOD_ID).getVersion().compareTo(requiredVersion) >= 0;
}
return false;
}
}

View file

@ -3,31 +3,36 @@ package cc.toph.simplycompat.compat;
import cc.toph.simplycompat.registry.ConfigRegistry; import cc.toph.simplycompat.registry.ConfigRegistry;
import cc.toph.simplycompat.util.ToolMaterials; import cc.toph.simplycompat.util.ToolMaterials;
import com.drmangotea.tfmg.registry.TFMGTiers; import com.drmangotea.tfmg.registry.TFMGTiers;
import com.simibubi.create.AllTags; import net.minecraft.util.LazyLoadedValue;
import net.minecraft.world.item.Tier; import net.minecraft.world.item.Items;
import net.minecraft.world.item.crafting.Ingredient; import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.level.ItemLike;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
public enum TFMGToolMaterials implements ToolMaterials, Tier { import java.util.function.Supplier;
public enum TFMGToolMaterials implements ToolMaterials<TFMGToolMaterials> {
ALUMINUM( ALUMINUM(
TFMGTiers.ALUMINUM.getLevel(), TFMGTiers.ALUMINUM.getLevel(),
TFMGTiers.ALUMINUM.getUses(), TFMGTiers.ALUMINUM.getUses(),
TFMGTiers.ALUMINUM.getSpeed(), TFMGTiers.ALUMINUM.getSpeed(),
TFMGTiers.ALUMINUM.getAttackDamageBonus(), TFMGTiers.ALUMINUM.getAttackDamageBonus(),
ConfigRegistry.WEAPON_ATTRIBUTES.STEEL, ConfigRegistry.WEAPON_ATTRIBUTES.ALUMINUM,
TFMGTiers.ALUMINUM.getEnchantmentValue(), TFMGTiers.ALUMINUM.getEnchantmentValue(),
ConfigRegistry.WEAPON_ATTRIBUTES.STEEL_ENABLED, ConfigRegistry.WEAPON_ATTRIBUTES.ALUMINUM_ENABLED,
Ingredient.of(AllTags.AllItemTags.CREATE_INGOTS.tag) // () -> Ingredient.of(new ItemLike[]{(ItemLike) TFMGItems.ALUMINUM_INGOT.get()})
() -> Ingredient.of(new ItemLike[]{(ItemLike) Items.STICK})
), ),
LEAD( LEAD(
TFMGTiers.LEAD.getLevel(), TFMGTiers.LEAD.getLevel(),
TFMGTiers.LEAD.getUses(), TFMGTiers.LEAD.getUses(),
TFMGTiers.LEAD.getSpeed(), TFMGTiers.LEAD.getSpeed(),
TFMGTiers.LEAD.getAttackDamageBonus(), TFMGTiers.LEAD.getAttackDamageBonus(),
ConfigRegistry.WEAPON_ATTRIBUTES.STEEL, ConfigRegistry.WEAPON_ATTRIBUTES.LEAD,
TFMGTiers.LEAD.getEnchantmentValue(), TFMGTiers.LEAD.getEnchantmentValue(),
ConfigRegistry.WEAPON_ATTRIBUTES.STEEL_ENABLED, ConfigRegistry.WEAPON_ATTRIBUTES.LEAD_ENABLED,
Ingredient.of(AllTags.AllItemTags.CREATE_INGOTS.tag) // () -> Ingredient.of(new ItemLike[]{(ItemLike) TFMGItems.LEAD_INGOT.get()})
() -> Ingredient.of(new ItemLike[]{(ItemLike) Items.STICK})
), ),
STEEL( STEEL(
TFMGTiers.STEEL.getLevel(), TFMGTiers.STEEL.getLevel(),
@ -37,7 +42,8 @@ public enum TFMGToolMaterials implements ToolMaterials, Tier {
ConfigRegistry.WEAPON_ATTRIBUTES.STEEL, ConfigRegistry.WEAPON_ATTRIBUTES.STEEL,
TFMGTiers.STEEL.getEnchantmentValue(), TFMGTiers.STEEL.getEnchantmentValue(),
ConfigRegistry.WEAPON_ATTRIBUTES.STEEL_ENABLED, ConfigRegistry.WEAPON_ATTRIBUTES.STEEL_ENABLED,
Ingredient.of(AllTags.AllItemTags.CREATE_INGOTS.tag) // () -> Ingredient.of(new ItemLike[]{(ItemLike) TFMGItems.STEEL_INGOT.get()})
() -> Ingredient.of(new ItemLike[]{(ItemLike) Items.STICK})
); );
private final int level; private final int level;
@ -47,7 +53,7 @@ public enum TFMGToolMaterials implements ToolMaterials, Tier {
private final float damageModifier; private final float damageModifier;
private final int enchantmentValue; private final int enchantmentValue;
private final boolean enabled; private final boolean enabled;
private final Ingredient repairIngredient; private final LazyLoadedValue<Ingredient> repairIngredient;
TFMGToolMaterials( TFMGToolMaterials(
int level, int level,
@ -57,7 +63,7 @@ public enum TFMGToolMaterials implements ToolMaterials, Tier {
float damageModifier, float damageModifier,
int enchantmentValue, int enchantmentValue,
boolean enabled, boolean enabled,
Ingredient repairIngredient Supplier<Ingredient> repairIngredient
) { ) {
this.level = level; this.level = level;
this.uses = uses; this.uses = uses;
@ -66,7 +72,7 @@ public enum TFMGToolMaterials implements ToolMaterials, Tier {
this.enchantmentValue = enchantmentValue; this.enchantmentValue = enchantmentValue;
this.damageModifier = damageModifier; this.damageModifier = damageModifier;
this.enabled = enabled; this.enabled = enabled;
this.repairIngredient = repairIngredient; this.repairIngredient = new LazyLoadedValue(repairIngredient);
} }
@ -113,7 +119,13 @@ public enum TFMGToolMaterials implements ToolMaterials, Tier {
@Override @Override
public @NotNull Ingredient getRepairIngredient() { public @NotNull Ingredient getRepairIngredient() {
return this.repairIngredient; return this.repairIngredient.get();
}
@Override
public TFMGToolMaterials[] getValues() {
return values();
} }
} }

View file

@ -1,9 +1,7 @@
package cc.toph.simplycompat.config; package cc.toph.simplycompat.config;
import cc.toph.simplycompat.SimplyCompat;
public class Config { public class Config {
private SimpleConfig config; private final SimpleConfig config;
private final ConfigProvider provider; private final ConfigProvider provider;
public Config(ConfigProvider provider) { public Config(ConfigProvider provider) {

View file

@ -2,12 +2,15 @@ package cc.toph.simplycompat.item;
import cc.toph.simplycompat.registry.ConfigRegistry.WEAPON_ATTRIBUTES; import cc.toph.simplycompat.registry.ConfigRegistry.WEAPON_ATTRIBUTES;
import cc.toph.simplycompat.util.ToolMaterials; import cc.toph.simplycompat.util.ToolMaterials;
import net.minecraft.tags.ItemTags; import net.minecraft.util.LazyLoadedValue;
import net.minecraft.world.item.Tier; import net.minecraft.world.item.Items;
import net.minecraft.world.item.crafting.Ingredient; import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.level.ItemLike;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
public enum SimplyCompatToolMaterials implements ToolMaterials, Tier { import java.util.function.Supplier;
public enum SimplyCompatToolMaterials implements ToolMaterials<SimplyCompatToolMaterials> {
COPPER( COPPER(
1, 1,
125, 125,
@ -16,7 +19,7 @@ public enum SimplyCompatToolMaterials implements ToolMaterials, Tier {
WEAPON_ATTRIBUTES.COPPER, WEAPON_ATTRIBUTES.COPPER,
8, 8,
WEAPON_ATTRIBUTES.COPPER_ENABLED, WEAPON_ATTRIBUTES.COPPER_ENABLED,
Ingredient.of(ItemTags.COPPER_ORES) () -> Ingredient.of(new ItemLike[]{(ItemLike) Items.COPPER_INGOT})
); );
private final int level; private final int level;
@ -26,7 +29,7 @@ public enum SimplyCompatToolMaterials implements ToolMaterials, Tier {
private final float damageModifier; private final float damageModifier;
private final int enchantmentValue; private final int enchantmentValue;
private final boolean enabled; private final boolean enabled;
private final Ingredient repairIngredient; private final LazyLoadedValue<Ingredient> repairIngredient;
SimplyCompatToolMaterials( SimplyCompatToolMaterials(
int level, int level,
@ -36,7 +39,7 @@ public enum SimplyCompatToolMaterials implements ToolMaterials, Tier {
float damageModifier, float damageModifier,
int enchantmentValue, int enchantmentValue,
boolean enabled, boolean enabled,
Ingredient repairIngredient Supplier<Ingredient> repairIngredient
) { ) {
this.level = level; this.level = level;
this.uses = uses; this.uses = uses;
@ -45,7 +48,7 @@ public enum SimplyCompatToolMaterials implements ToolMaterials, Tier {
this.enchantmentValue = enchantmentValue; this.enchantmentValue = enchantmentValue;
this.damageModifier = damageModifier; this.damageModifier = damageModifier;
this.enabled = enabled; this.enabled = enabled;
this.repairIngredient = repairIngredient; this.repairIngredient = new LazyLoadedValue(repairIngredient);
} }
@ -91,7 +94,12 @@ public enum SimplyCompatToolMaterials implements ToolMaterials, Tier {
@Override @Override
public @NotNull Ingredient getRepairIngredient() { public @NotNull Ingredient getRepairIngredient() {
return this.repairIngredient; return this.repairIngredient.get();
}
@Override
public SimplyCompatToolMaterials[] getValues() {
return values();
} }
} }

View file

@ -0,0 +1,45 @@
package cc.toph.simplycompat.registry;
import cc.toph.simplycompat.compat.CompatRecord;
import cc.toph.simplycompat.compat.CreateToolMaterials;
import cc.toph.simplycompat.compat.TFMGToolMaterials;
import com.drmangotea.tfmg.CreateTFMG;
import com.simibubi.create.Create;
/**
* A registry for compatibility records. Each record represents
* a supported mod's integration setup.
*
* <p>Use {@link #registerAll()} to initialize all integrations.</p>
*/
public final class CompatRegistry {
/**
* Compatibility setup for the Create mod.
*/
public static final CompatRecord<CreateToolMaterials> CREATE = new CompatRecord<>(
Create.ID,
"0.5.1.j-55",
CreateToolMaterials.class
);
/**
* Compatibility setup for the TFMG mod.
*/
public static final CompatRecord<TFMGToolMaterials> TFMG = new CompatRecord<>(
CreateTFMG.MOD_ID,
"0.9.3-1.20.1",
TFMGToolMaterials.class
);
/**
* Initializes all declared compat records.
*/
public static void registerAll() {
CompatRegistry.CREATE.register();
CompatRegistry.TFMG.register();
}
}

View file

@ -1,10 +1,9 @@
package cc.toph.simplycompat.registry; package cc.toph.simplycompat.registry;
import cc.toph.simplycompat.compat.TFMG;
import cc.toph.simplycompat.config.Config; import cc.toph.simplycompat.config.Config;
import cc.toph.simplycompat.config.ConfigProvider; import cc.toph.simplycompat.config.ConfigProvider;
public class ConfigRegistry { public final class ConfigRegistry {
public static final class WEAPON_ATTRIBUTES { public static final class WEAPON_ATTRIBUTES {
public static float COPPER; public static float COPPER;
@ -15,6 +14,8 @@ public class ConfigRegistry {
public static boolean LEAD_ENABLED; public static boolean LEAD_ENABLED;
public static float STEEL; public static float STEEL;
public static boolean STEEL_ENABLED; public static boolean STEEL_ENABLED;
public static float STURDY;
public static boolean STURDY_ENABLED;
public static final ConfigProvider PROVIDER = new ConfigProvider("weapon_attributes"); public static final ConfigProvider PROVIDER = new ConfigProvider("weapon_attributes");
@ -33,7 +34,21 @@ public class ConfigRegistry {
"Enable Copper Swords" "Enable Copper Swords"
); );
if (TFMG.passCheck()) { if (CompatRegistry.CREATE.passCheck()) {
STURDY = common.registerProp(
"sturdy_damageModifier",
4.0f,
"Sturdy Damage Modifier"
);
STURDY_ENABLED = common.registerProp(
"sturdy_enabled",
true,
"Enable Sturdy Swords"
);
}
if (CompatRegistry.TFMG.passCheck()) {
ALUMINUM = common.registerProp( ALUMINUM = common.registerProp(
"aluminum_damageModifier", "aluminum_damageModifier",
3.0f, 3.0f,

View file

@ -1,7 +1,6 @@
package cc.toph.simplycompat.registry; package cc.toph.simplycompat.registry;
import cc.toph.simplycompat.SimplyCompat; import cc.toph.simplycompat.SimplyCompat;
import cc.toph.simplycompat.compat.TFMG;
import cc.toph.simplycompat.item.SimplyCompatToolMaterials; import cc.toph.simplycompat.item.SimplyCompatToolMaterials;
import cc.toph.simplycompat.util.ToolMaterials; import cc.toph.simplycompat.util.ToolMaterials;
import cc.toph.simplycompat.util.WeaponType; import cc.toph.simplycompat.util.WeaponType;
@ -10,14 +9,14 @@ import net.minecraft.core.registries.Registries;
import net.minecraft.world.item.Item; import net.minecraft.world.item.Item;
import net.sweenus.simplyswords.item.SimplySwordsSwordItem; import net.sweenus.simplyswords.item.SimplySwordsSwordItem;
public class ItemsRegistry { public final class ItemsRegistry {
public static final DeferredRegister<Item> ITEMS = DeferredRegister.create( public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(
SimplyCompat.MOD_ID, SimplyCompat.MOD_ID,
Registries.ITEM Registries.ITEM
); );
public static void registerSword( public static <E extends Enum<E> & ToolMaterials<E>> void registerSword(
ToolMaterials material, ToolMaterials<E> material,
WeaponType type WeaponType type
) { ) {
@ -38,6 +37,7 @@ public class ItemsRegistry {
* registered within the item registry for use within the mod. * registered within the item registry for use within the mod.
*/ */
public static void registerSwords() { public static void registerSwords() {
// Register "Vanilla" swords // Register "Vanilla" swords
for (WeaponType type : WeaponType.values()) { for (WeaponType type : WeaponType.values()) {
for (SimplyCompatToolMaterials material : SimplyCompatToolMaterials.values()) { for (SimplyCompatToolMaterials material : SimplyCompatToolMaterials.values()) {
@ -45,7 +45,7 @@ public class ItemsRegistry {
} }
} }
TFMG.register(); CompatRegistry.registerAll();
ITEMS.register(); ITEMS.register();
} }
} }

View file

@ -13,7 +13,8 @@ import net.minecraft.world.item.Tier;
* <p> * <p>
* Suggested implementation with Enums * Suggested implementation with Enums
*/ */
public interface ToolMaterials extends Tier { public interface ToolMaterials<E extends Enum<E> & ToolMaterials<E>>
extends Tier {
/** /**
* Returns the name of the enum constant in lowercase. * Returns the name of the enum constant in lowercase.
@ -59,4 +60,6 @@ public interface ToolMaterials extends Tier {
* @return boolean * @return boolean
*/ */
boolean isEnabled(); boolean isEnabled();
E[] getValues();
} }