Skip to content

Commit

Permalink
v1.2.2
Browse files Browse the repository at this point in the history
* Added a system for supporting other mods as well as re-implement support for Trinkets.
* Fixed a bug where having the mod on without trinkets will crash the game.
  • Loading branch information
RedVortexDev committed Mar 15, 2024
1 parent 561c93f commit 559b4ff
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 18 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/powercyphe/coffins/Mod.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,8 +26,6 @@ public class Mod implements ModInitializer {
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
public static final GameRules.Key<GameRules.BooleanRule> 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();
Expand All @@ -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));
}
Expand Down
14 changes: 3 additions & 11 deletions src/main/java/powercyphe/coffins/mixin/PlayerEntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ItemStack> items = DefaultedList.ofSize(54, ItemStack.EMPTY);
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/powercyphe/coffins/modsupport/ModSupportManager.java
Original file line number Diff line number Diff line change
@@ -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<ModSupporter> modSupportClasses = List.of(
// Mod support implementation classes go here.
new TrinketsModSupportImpl()
);

public static List<ItemStack> getAllModSupportedDrops(PlayerEntity player) {
List<ItemStack> 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;
}
}
35 changes: 35 additions & 0 deletions src/main/java/powercyphe/coffins/modsupport/ModSupporter.java
Original file line number Diff line number Diff line change
@@ -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<ItemStack> 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());
}
}
Original file line number Diff line number Diff line change
@@ -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<ItemStack> getDrops(PlayerEntity player) {
List<ItemStack> items = new ArrayList<>();
TrinketsApi.getTrinketComponent(player).ifPresent(trinkets -> trinkets.forEach((ref, stack) -> {
if (!stack.isEmpty()) {
items.add(stack);
}
}));
return items;
}
}

0 comments on commit 559b4ff

Please sign in to comment.