diff --git a/build.gradle b/build.gradle index 53e7ac2..ea2afa0 100644 --- a/build.gradle +++ b/build.gradle @@ -11,11 +11,14 @@ base { } repositories { - // Add repositories to retrieve artifacts from in here. - // You should only use this when depending on other mods because - // Loom adds the essential maven repositories to download Minecraft and libraries from automatically. - // See https://docs.gradle.org/current/userguide/declaring_repositories.html - // for more information about repositories. + maven { + name = "TerraformersMC" + url = "https://maven.terraformersmc.com/" + } + maven { + name = "Ladysnake Libs" + url = 'https://maven.ladysnake.org/releases' + } } loom { @@ -44,18 +47,18 @@ sourceSets { } } dependencies { - // To change the versions see the gradle.properties file + // Versions can be changed in the gradle.properties file minecraft "com.mojang:minecraft:${project.minecraft_version}" mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" - // Fabric API. This is technically optional, but you probably want it anyway. + // Fabric API modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" - - // Uncomment the following line to enable the deprecated Fabric API modules. - // These are included in the Fabric API production distribution and allow you to update your mod to the latest modules at a later more convenient time. - // modImplementation "net.fabricmc.fabric-api:fabric-api-deprecated:${project.fabric_version}" + // Trinkets API + modImplementation "dev.emi:trinkets:${project.trinkets_version}" + modImplementation include("dev.onyxstudios.cardinal-components-api:cardinal-components-base:${project.cca_version}") + modImplementation include("dev.onyxstudios.cardinal-components-api:cardinal-components-entity:${project.cca_version}") } processResources { diff --git a/gradle.properties b/gradle.properties index 80108e1..2de8824 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,9 +9,11 @@ yarn_mappings=1.19.2+build.28 loader_version=0.14.24 # Mod Properties -mod_version=1.1.0+1.19.2 +mod_version=1.2.0+1.19.2 maven_group=powercyphe.coffins archives_base_name=coffins # Dependencies -fabric_version=0.76.1+1.19.2 \ No newline at end of file +fabric_version=0.76.1+1.19.2 +trinkets_version=3.4.1 +cca_version=5.0.2 \ No newline at end of file diff --git a/src/main/java/powercyphe/coffins/Mod.java b/src/main/java/powercyphe/coffins/Mod.java index b42738b..20735dd 100644 --- a/src/main/java/powercyphe/coffins/Mod.java +++ b/src/main/java/powercyphe/coffins/Mod.java @@ -7,7 +7,6 @@ import net.fabricmc.fabric.api.resource.ResourceManagerHelper; import net.fabricmc.fabric.api.resource.ResourcePackActivationType; import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.resource.ResourceType; import net.minecraft.util.Identifier; import net.minecraft.world.GameRules; import org.slf4j.Logger; @@ -15,6 +14,7 @@ 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.screen.ModScreenHandlers; import powercyphe.coffins.sound.ModSounds; @@ -26,6 +26,8 @@ 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(); @@ -35,6 +37,8 @@ public void onInitialize() { ModSounds.registerModSounds(); ModLootTableModifier.modifyLootTables(); + if (isTrinketsLoaded) TrinketDropEvent.registerCallback(); + ServerPlayerEvents.AFTER_RESPAWN.register(new PlayerRespawnEvent()); FabricLoader.getInstance().getModContainer(Mod.MOD_ID).ifPresent(modContainer -> ResourceManagerHelper.registerBuiltinResourcePack(Mod.id("coffins-dark-menu"), modContainer, ResourcePackActivationType.NORMAL)); diff --git a/src/main/java/powercyphe/coffins/event/TrinketDropEvent.java b/src/main/java/powercyphe/coffins/event/TrinketDropEvent.java new file mode 100644 index 0000000..08a5717 --- /dev/null +++ b/src/main/java/powercyphe/coffins/event/TrinketDropEvent.java @@ -0,0 +1,21 @@ +package powercyphe.coffins.event; + + +import dev.emi.trinkets.api.TrinketEnums; +import dev.emi.trinkets.api.event.TrinketDropCallback; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.world.GameRules; + +public class TrinketDropEvent { + + public static void registerCallback() { + TrinketDropCallback.EVENT.register((rule, stack, ref, entity) -> { + + // If the trinkets were dropped by a player and keep inventory is off, destroy the trinket + if (!(entity instanceof PlayerEntity player)) return rule; + if (player.getWorld().getGameRules().getBoolean(GameRules.KEEP_INVENTORY)) return rule; + + return TrinketEnums.DropRule.DESTROY; + }); + } +} diff --git a/src/main/java/powercyphe/coffins/mixin/PlayerEntityMixin.java b/src/main/java/powercyphe/coffins/mixin/PlayerEntityMixin.java index db2cbf0..dc977c8 100644 --- a/src/main/java/powercyphe/coffins/mixin/PlayerEntityMixin.java +++ b/src/main/java/powercyphe/coffins/mixin/PlayerEntityMixin.java @@ -1,29 +1,27 @@ package powercyphe.coffins.mixin; +import dev.emi.trinkets.api.TrinketsApi; import net.minecraft.block.BlockState; import net.minecraft.block.entity.BlockEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerInventory; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; -import net.minecraft.nbt.NbtCompound; -import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; import net.minecraft.util.ItemScatterer; import net.minecraft.util.collection.DefaultedList; import net.minecraft.util.math.BlockPos; import net.minecraft.world.GameRules; import net.minecraft.world.World; +import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import powercyphe.coffins.Mod; import powercyphe.coffins.block.ModBlocks; import powercyphe.coffins.block.entity.CoffinBlockEntity; -import powercyphe.coffins.event.PlayerEventHandler; import powercyphe.coffins.util.IEntityDataSaver; import powercyphe.coffins.util.ModTags; import powercyphe.coffins.util.RecoveryCompassData; @@ -40,111 +38,120 @@ public abstract class PlayerEntityMixin implements IEntityDataSaver { @Shadow protected abstract void vanishCursedItems(); + @Shadow @Final private PlayerInventory inventory; + @Inject(method = "dropInventory", at = @At("HEAD")) private void dropInventory(CallbackInfo ci) { PlayerEntity player = ((PlayerEntity) (Object) this); - if (!player.getWorld().isClient) { - RecoveryCompassData.setNbt((IEntityDataSaver) player, null); - RecoveryCompassData.setCount((IEntityDataSaver) player, 0); - if (!player.getWorld().getGameRules().getBoolean(GameRules.KEEP_INVENTORY)) { - this.vanishCursedItems(); - - ArrayList inventory = new ArrayList<>(); - for (int i = 0; i < this.getInventory().size(); i++) { - inventory.add(this.getInventory().getStack(i)); - } + if (player.getWorld().isClient) return; + RecoveryCompassData.setNbt((IEntityDataSaver) player, null); + RecoveryCompassData.setCount((IEntityDataSaver) player, 0); + if (player.getWorld().getGameRules().getBoolean(GameRules.KEEP_INVENTORY)) return; + this.vanishCursedItems(); + + ArrayList inventory = new ArrayList<>(); + for (int i = 0; i < this.getInventory().size(); i++) { + inventory.add(this.getInventory().getStack(i)); + } - int itemsAmount = 0; - DefaultedList items = DefaultedList.ofSize(54, ItemStack.EMPTY); - boolean hasRecoveryCompass = false; - for (int i = 0; i < inventory.size(); i++) { - ItemStack stack = inventory.get(i); - if (!stack.isEmpty() && !stack.isOf(Items.AIR) && stack != Items.AIR.getDefaultStack() && itemsAmount < items.size()) { - boolean keepRecoveryCompass = false; - if (player.getWorld().getGameRules().getBoolean(Mod.KEEP_RECOVERY_COMPASS)) { - if (ItemStack.areItemsEqual(stack, Items.RECOVERY_COMPASS.getDefaultStack())) { - if (!hasRecoveryCompass) { - keepRecoveryCompass = true; - } - } - } + // 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); + } + })); + } - if (!keepRecoveryCompass) { - this.getInventory().removeOne(stack); - itemsAmount = itemsAmount + 1; - items.set(i, stack); - } else { - this.getInventory().removeOne(stack); - RecoveryCompassData.setNbt((IEntityDataSaver) player, stack); - RecoveryCompassData.setCount((IEntityDataSaver) player, stack.getCount()); - hasRecoveryCompass = true; + int itemsAmount = 0; + DefaultedList items = DefaultedList.ofSize(54, ItemStack.EMPTY); + boolean hasRecoveryCompass = false; + for (int i = 0; i < inventory.size(); i++) { + ItemStack stack = inventory.get(i); + if (!stack.isEmpty() && !stack.isOf(Items.AIR) && stack != Items.AIR.getDefaultStack() && itemsAmount < items.size()) { + boolean keepRecoveryCompass = false; + if (player.getWorld().getGameRules().getBoolean(Mod.KEEP_RECOVERY_COMPASS)) { + if (ItemStack.areItemsEqual(stack, Items.RECOVERY_COMPASS.getDefaultStack())) { + if (!hasRecoveryCompass) { + keepRecoveryCompass = true; } } } - if (!items.isEmpty() && itemsAmount > 0) { - TreeMap distanceMap = new TreeMap<>(); - World world = player.getWorld(); + this.getInventory().removeOne(stack); + if (!keepRecoveryCompass) { + itemsAmount = itemsAmount + 1; + items.set(i, stack); + } else { + RecoveryCompassData.setNbt((IEntityDataSaver) player, stack); + RecoveryCompassData.setCount((IEntityDataSaver) player, stack.getCount()); + hasRecoveryCompass = true; + } + } + } - BlockPos playerPos = player.getBlockPos(); - boolean shift = true; - while (shift) { - BlockPos shiftPos = playerPos.down(1); - if ( - player.getWorld().getBlockState(shiftPos).isIn(ModTags.Blocks.COFFIN_REPLACEABLE) - ) { - playerPos = shiftPos; - } else { - shift = false; - } + if (!items.isEmpty() && itemsAmount > 0) { + TreeMap distanceMap = new TreeMap<>(); + World world = player.getWorld(); + + BlockPos playerPos = player.getBlockPos(); + boolean shift = true; + while (shift) { + BlockPos shiftPos = playerPos.down(1); + if (player.getWorld().getBlockState(shiftPos).isIn(ModTags.Blocks.COFFIN_REPLACEABLE)) { + playerPos = shiftPos; + } else { + shift = false; + } - } + } + + int xPlayer = playerPos.getX(); + int yPlayer = playerPos.getY(); + int zPlayer = playerPos.getZ(); - int xPlayer = playerPos.getX(); - int yPlayer = playerPos.getY(); - int zPlayer = playerPos.getZ(); - - int xMin = playerPos.getX() - 16; - int xMax = playerPos.getX() + 16; - int zMin = playerPos.getZ() - 16; - int zMax = playerPos.getZ() + 16; - - for (int x = xMin; x < xMax; x++) { - for (int z = zMin; z < zMax; z++) { - for (int y = -64; y < 255; y++) { - BlockPos blockPos = new BlockPos(x, y, z); - int distance = 0; - if ( - player.getWorld().getBlockState(blockPos).isIn(ModTags.Blocks.COFFIN_REPLACEABLE) && - player.getWorld().getWorldBorder().contains(x, z) - ) { - distance = distance + Math.abs(x - xPlayer) + Math.abs(y - yPlayer) + Math.abs(z - zPlayer); - distanceMap.put(blockPos, distance); - } - } + int xMin = playerPos.getX() - 16; + int xMax = playerPos.getX() + 16; + int zMin = playerPos.getZ() - 16; + int zMax = playerPos.getZ() + 16; + + for (int x = xMin; x < xMax; x++) { + for (int z = zMin; z < zMax; z++) { + for (int y = -64; y < 255; y++) { + BlockPos blockPos = new BlockPos(x, y, z); + int distance = 0; + if ( + player.getWorld().getBlockState(blockPos).isIn(ModTags.Blocks.COFFIN_REPLACEABLE) && + player.getWorld().getWorldBorder().contains(x, z) + ) { + distance = distance + Math.abs(x - xPlayer) + Math.abs(y - yPlayer) + Math.abs(z - zPlayer); + distanceMap.put(blockPos, distance); } } - List> entryList = new ArrayList<>(distanceMap.entrySet()); - entryList.sort(Map.Entry.comparingByValue()); - int sizeCheck = entryList.size(); - if (sizeCheck > 0) { - BlockPos blockPos = entryList.get(1).getKey(); - BlockState blockState = ModBlocks.COFFIN.getDefaultState(); + } + } + List> entryList = new ArrayList<>(distanceMap.entrySet()); + entryList.sort(Map.Entry.comparingByValue()); + int sizeCheck = entryList.size(); + if (sizeCheck > 0) { + BlockPos blockPos = entryList.get(1).getKey(); + BlockState blockState = ModBlocks.COFFIN.getDefaultState(); // player.sendMessage(Text.literal("Your Items were dropped at " + "[" + blockPos.getX() + " " + blockPos.getY() + " " + blockPos.getZ() + "]")); - world.setBlockState(blockPos, blockState); - BlockEntity blockEntity = world.getBlockEntity(blockPos); - if (blockEntity instanceof CoffinBlockEntity coffinBlockEntity) { - coffinBlockEntity.setItems(items); - coffinBlockEntity.setOwner(player.getEntityName()); - coffinBlockEntity.setFragile(true); - world.updateListeners(blockPos, blockState, blockState, 3); - } - } else { -// player.sendMessage(Text.literal("Your Items were dropped at " + "[" + player.getBlockPos().getX() + " " + player.getBlockPos().getY() + " " + player.getBlockPos().getZ() + "]")); - ItemScatterer.spawn(world, player.getBlockPos(), items); - } + world.setBlockState(blockPos, blockState); + BlockEntity blockEntity = world.getBlockEntity(blockPos); + if (blockEntity instanceof CoffinBlockEntity coffinBlockEntity) { + coffinBlockEntity.setItems(items); + coffinBlockEntity.setOwner(player.getEntityName()); + coffinBlockEntity.setFragile(true); + world.updateListeners(blockPos, blockState, blockState, 3); } + } else { +// player.sendMessage(Text.literal("Your Items were dropped at " + "[" + player.getBlockPos().getX() + " " + player.getBlockPos().getY() + " " + player.getBlockPos().getZ() + "]")); + ItemScatterer.spawn(world, player.getBlockPos(), items); } } } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 8791dc5..382a80d 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -37,11 +37,8 @@ ], "depends": { "fabricloader": ">=0.14.24", - "minecraft": "~1.19.2", + "minecraft": "1.19.2", "java": ">=17", "fabric-api": "*" - }, - "suggests": { - "another-mod": "*" } } \ No newline at end of file