diff --git a/gradle.properties b/gradle.properties index cb8ca2e..b2e9d52 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,7 +9,7 @@ yarn_mappings=1.19.2+build.28 loader_version=0.14.24 # Mod Properties -mod_version=1.2.1+1.19.2 +mod_version=1.2.2+1.19.2 maven_group=powercyphe.coffins archives_base_name=coffins diff --git a/src/main/java/powercyphe/coffins/Mod.java b/src/main/java/powercyphe/coffins/Mod.java index 02a26f5..117179c 100644 --- a/src/main/java/powercyphe/coffins/Mod.java +++ b/src/main/java/powercyphe/coffins/Mod.java @@ -14,8 +14,8 @@ import powercyphe.coffins.block.ModBlocks; import powercyphe.coffins.block.entity.ModBlockEntities; import powercyphe.coffins.event.PlayerRespawnEvent; -import powercyphe.coffins.event.TrinketDropEvent; import powercyphe.coffins.item.ModItems; +import powercyphe.coffins.modsupport.ModSupportManager; import powercyphe.coffins.screen.ModScreenHandlers; import powercyphe.coffins.sound.ModSounds; import powercyphe.coffins.util.ModLootTableModifier; @@ -26,8 +26,6 @@ public class Mod implements ModInitializer { public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); public static final GameRules.Key KEEP_RECOVERY_COMPASS = GameRuleRegistry.register("keepRecoveryCompass", GameRules.Category.PLAYER, GameRuleFactory.createBooleanRule(true)); - public static final boolean isTrinketsLoaded = FabricLoader.getInstance().isModLoaded("trinkets"); - @Override public void onInitialize() { ModItems.registerModItems(); @@ -37,11 +35,13 @@ public void onInitialize() { ModSounds.registerModSounds(); ModLootTableModifier.modifyLootTables(); - if (isTrinketsLoaded) { - TrinketDropEvent.registerCallback(); } - ServerPlayerEvents.AFTER_RESPAWN.register(new PlayerRespawnEvent()); + // Call the init method on mod support classes if the mod is loaded + ModSupportManager.modSupportClasses.forEach(clazz -> { + if (clazz.isModLoaded()) clazz.onInit(); + }); + FabricLoader.getInstance().getModContainer(Mod.MOD_ID).ifPresent(modContainer -> ResourceManagerHelper.registerBuiltinResourcePack(Mod.id("coffins-dark-menu"), modContainer, ResourcePackActivationType.NORMAL)); FabricLoader.getInstance().getModContainer(Mod.MOD_ID).ifPresent(modContainer -> ResourceManagerHelper.registerBuiltinResourcePack(Mod.id("coffins-old-textures"), modContainer, ResourcePackActivationType.NORMAL)); } diff --git a/src/main/java/powercyphe/coffins/mixin/PlayerEntityMixin.java b/src/main/java/powercyphe/coffins/mixin/PlayerEntityMixin.java index dc977c8..b6a7dc8 100644 --- a/src/main/java/powercyphe/coffins/mixin/PlayerEntityMixin.java +++ b/src/main/java/powercyphe/coffins/mixin/PlayerEntityMixin.java @@ -22,6 +22,7 @@ import powercyphe.coffins.Mod; import powercyphe.coffins.block.ModBlocks; import powercyphe.coffins.block.entity.CoffinBlockEntity; +import powercyphe.coffins.modsupport.ModSupportManager; import powercyphe.coffins.util.IEntityDataSaver; import powercyphe.coffins.util.ModTags; import powercyphe.coffins.util.RecoveryCompassData; @@ -54,17 +55,8 @@ private void dropInventory(CallbackInfo ci) { inventory.add(this.getInventory().getStack(i)); } - // If Trinkets is loaded & the player has a trinket component, - // we repeat through all the player's equipped trinkets and add them to the - // inventory (coffin). - - if (Mod.isTrinketsLoaded && TrinketsApi.getTrinketComponent(player).isPresent()) { - TrinketsApi.getTrinketComponent(player).ifPresent(trinkets -> trinkets.forEach((ref, stack) -> { - if (!stack.isEmpty()) { - inventory.add(stack); - } - })); - } + // Add items from all the mods we support + inventory.addAll(ModSupportManager.getAllModSupportedDrops(player)); int itemsAmount = 0; DefaultedList items = DefaultedList.ofSize(54, ItemStack.EMPTY); diff --git a/src/main/java/powercyphe/coffins/modsupport/ModSupportManager.java b/src/main/java/powercyphe/coffins/modsupport/ModSupportManager.java new file mode 100644 index 0000000..6b61994 --- /dev/null +++ b/src/main/java/powercyphe/coffins/modsupport/ModSupportManager.java @@ -0,0 +1,24 @@ +package powercyphe.coffins.modsupport; + +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemStack; +import powercyphe.coffins.modsupport.impl.TrinketsModSupportImpl; + +import java.util.ArrayList; +import java.util.List; + +public class ModSupportManager { + public static final List modSupportClasses = List.of( + // Mod support implementation classes go here. + new TrinketsModSupportImpl() + ); + + public static List getAllModSupportedDrops(PlayerEntity player) { + List modSupportDrops = new ArrayList<>(); + modSupportClasses.forEach(clazz -> { + // Only add the items if the mod is loaded + if (clazz.isModLoaded()) modSupportDrops.addAll(clazz.getDrops(player)); + }); + return modSupportDrops; + } +} diff --git a/src/main/java/powercyphe/coffins/modsupport/ModSupporter.java b/src/main/java/powercyphe/coffins/modsupport/ModSupporter.java new file mode 100644 index 0000000..a4d3f99 --- /dev/null +++ b/src/main/java/powercyphe/coffins/modsupport/ModSupporter.java @@ -0,0 +1,35 @@ +package powercyphe.coffins.modsupport; + +import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemStack; + +import java.util.List; + +public interface ModSupporter { + /** + * Returns the mod ID of the mod the {@link ModSupporter} is supporting. + * @return The supporting mod ID as a string. + */ + String supportingModId(); + + /** + * Called on the {@link powercyphe.coffins.Mod}'s onInitialize method if the mod is loaded. + */ + void onInit(); + + /** + * Returns a list of {@link ItemStack}s that the player has or owns, depending on the mod. + * @param player The player that died. + * @return A list of {@link ItemStack}s representing the player's drops. + */ + List getDrops(PlayerEntity player); + + /** + * Checks if the mod is loaded by the client. + * @return True if the mod is loaded, false otherwise. + */ + default boolean isModLoaded() { + return FabricLoader.getInstance().isModLoaded(supportingModId()); + } +} diff --git a/src/main/java/powercyphe/coffins/modsupport/impl/TrinketsModSupportImpl.java b/src/main/java/powercyphe/coffins/modsupport/impl/TrinketsModSupportImpl.java new file mode 100644 index 0000000..94e0084 --- /dev/null +++ b/src/main/java/powercyphe/coffins/modsupport/impl/TrinketsModSupportImpl.java @@ -0,0 +1,37 @@ +package powercyphe.coffins.modsupport.impl; + +import dev.emi.trinkets.api.TrinketsApi; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemStack; +import powercyphe.coffins.event.TrinketDropEvent; +import powercyphe.coffins.modsupport.ModSupporter; + +import java.util.ArrayList; +import java.util.List; + +/** + * Mod support for Trinkets + * @author redvortex + */ +public class TrinketsModSupportImpl implements ModSupporter { + @Override + public String supportingModId() { + return "trinkets"; + } + + @Override + public void onInit() { + TrinketDropEvent.registerCallback(); + } + + @Override + public List getDrops(PlayerEntity player) { + List items = new ArrayList<>(); + TrinketsApi.getTrinketComponent(player).ifPresent(trinkets -> trinkets.forEach((ref, stack) -> { + if (!stack.isEmpty()) { + items.add(stack); + } + })); + return items; + } +}