Files
src
build.gradle
gradle.properties
LICENSE
settings.gradle
Code
12 395
src/main/java/com/souldrop/SoulDropMod.java
package com.souldrop;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.item.ItemGroups;
import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import static net.minecraft.server.command.CommandManager.literal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SoulDropMod implements ModInitializer {
public static final String MOD_ID = "souldrop";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
@Override
public void onInitialize() {
ModItems.initialize();
ItemGroupEvents.modifyEntriesEvent(ItemGroups.INGREDIENTS)
.register(entries -> {
entries.add(ModItems.PLAYER_SOUL);
});
ServerLivingEntityEvents.AFTER_DEATH.register((entity, damageSource) -> {
if (entity instanceof ServerPlayerEntity player) {
handlePlayerDeath(player, damageSource);
}
});
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
dispatcher.register(literal("withdrawsoul")
.executes(context -> {
ServerPlayerEntity player = context.getSource().getPlayerOrThrow();
return withdrawSoul(player);
}));
});
LOGGER.info("Soul Drop Mod initialized!");
}
private void handlePlayerDeath(ServerPlayerEntity player, DamageSource damageSource) {
ItemStack soul = ModItems.createPlayerSoul(player);
player.dropItem(soul, false);
player.getWorld().playSound(
null,
player.getX(),
player.getY(),
player.getZ(),
SoundEvents.PARTICLE_SOUL_ESCAPE,
SoundCategory.PLAYERS,
1.0f,
0.8f
);
player.getServer().getPlayerManager().broadcast(
Text.literal(player.getName().getString() + "'s soul has been released...")
.formatted(Formatting.DARK_PURPLE, Formatting.ITALIC),
false
);
}
private int withdrawSoul(ServerPlayerEntity player) {
if (player.getHealth() <= 6.0f) {
player.sendMessage(
Text.literal("You are too weak to withdraw your soul!")
.formatted(Formatting.RED),
false
);
return 0;
}
ItemStack soul = ModItems.createPlayerSoul(player);
if (!player.getInventory().insertStack(soul)) {
player.dropItem(soul, false);
}
player.setHealth(player.getHealth() - 6.0f);
player.getWorld().playSound(
null,
player.getX(),
player.getY(),
player.getZ(),
SoundEvents.ENTITY_WARDEN_HEARTBEAT,
SoundCategory.PLAYERS,
2.0f,
0.5f
);
player.getWorld().playSound(
null,
player.getX(),
player.getY(),
player.getZ(),
SoundEvents.PARTICLE_SOUL_ESCAPE,
SoundCategory.PLAYERS,
1.5f,
1.2f
);
SoulParticleSpawner.spawnWithdrawalParticles(player);
player.sendMessage(
Text.literal("You have withdrawn your soul...")
.formatted(Formatting.DARK_PURPLE, Formatting.BOLD),
true
);
return 1;
}
}