Files
src
build.gradle
gradle.properties
LICENSE
settings.gradle
Code
33 1,092
src/main/java/com/kodari/soulbound/SoulboundSwords.java
package com.kodari.soulbound;
import com.mojang.brigadier.CommandDispatcher;
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.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.event.player.AttackEntityCallback;
import net.fabricmc.fabric.api.event.player.UseItemCallback;
import net.minecraft.entity.Entity;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.LightningEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.player.HungerManager;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
public final class SoulboundSwords implements ModInitializer {
public static final String MOD_ID = "soulbound_swords";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
private static final String[] CLASS_NAMES = {
"Flame", "Gambling", "Burst", "Sonic", "Mind", "Brute",
"Phantom", "Lightning", "Gravity", "Nature", "Music", "Water"
};
private static final int[] ABILITY_1_THRESHOLDS = {20, 15, 0, 30, 1, 0, 1, 40, 1, 50, 35, 40};
private static final int[] ABILITY_2_THRESHOLDS = {10, 20, 0, 50, 1, 40, 40, 50, 50, 30, 50, 60};
@Override
public void onInitialize() {
SwordItems.initialize();
AttackEntityCallback.EVENT.register(SoulboundSwords::onAttack);
UseItemCallback.EVENT.register(SoulboundSwords::onUseItem);
ServerLivingEntityEvents.AFTER_DEATH.register(SoulboundSwords::onDeath);
ServerTickEvents.END_SERVER_TICK.register(SoulboundSwords::onServerTick);
CommandRegistrationCallback.EVENT.register(SoulboundSwords::registerCommands);
}
private static ActionResult onAttack(PlayerEntity player, World world, Hand hand, Entity entity, net.minecraft.util.hit.EntityHitResult hitResult) {
if (world.isClient() || !(player instanceof ServerPlayerEntity serverPlayer) || !isSword(player.getMainHandStack())) {
return ActionResult.PASS;
}
SwordProgression progression = SwordComponents.PROGRESSION.get(serverPlayer);
ensureStarted(progression, player.getMainHandStack());
progression.setHits(progression.getHits() + 1);
updateTier(progression, serverPlayer);
chargeAbilities(progression);
if (progression.getClassId() == 2 && progression.isDoubleDamageReady() && entity instanceof LivingEntity target && target.isAlive()) {
target.damage(serverPlayer.getEntityWorld(), serverPlayer.getEntityWorld().getDamageSources().playerAttack(serverPlayer), 8.0F);
progression.setDoubleDamageReady(false);
}
return ActionResult.PASS;
}
private static ActionResult onUseItem(PlayerEntity player, World world, Hand hand) {
if (world.isClient() || !(player instanceof ServerPlayerEntity serverPlayer)) {
return ActionResult.PASS;
}
ItemStack stack = player.getStackInHand(hand);
SwordProgression progression = SwordComponents.PROGRESSION.get(serverPlayer);
if (stack.isOf(SwordItems.ABILITY_SLOT_TWO)) {
if (progression.getTier() < 3) {
serverPlayer.sendMessage(Text.literal("Reach tier 3 before using the Ability Slot 2 item."), true);
} else {
progression.setSlot2(true);
stack.decrement(1);
serverPlayer.sendMessage(Text.literal("Ability Slot 2 unlocked."), true);
}
return ActionResult.SUCCESS;
}
if (stack.isOf(SwordItems.REROLL_TOKEN)) {
int next = serverPlayer.getEntityWorld().getRandom().nextInt(CLASS_NAMES.length) + 1;
progression.setClassId(next);
stack.decrement(1);
serverPlayer.sendMessage(Text.literal("Your sword class is now " + CLASS_NAMES[next - 1] + "."), true);
return ActionResult.SUCCESS;
}
return ActionResult.PASS;
}
private static void onDeath(LivingEntity entity, DamageSource source) {
if (!(entity instanceof ServerPlayerEntity player)) {
return;
}
SwordProgression progression = SwordComponents.PROGRESSION.get(player);
if (progression.getTier() > 0) {
progression.setTier(progression.getTier() - 1);
}
Entity attacker = source.getAttacker();
if (attacker instanceof ServerPlayerEntity killer) {
SwordProgression killerProgression = SwordComponents.PROGRESSION.get(killer);
if (killerProgression.getTier() > 0 && isSword(killer.getMainHandStack())) {
incrementProgress(killerProgression, killer);
}
if (progression.hasSlot2()) {
killerProgression.setSlot2(true);
killer.sendMessage(Text.literal("You stole Ability Slot 2 from " + player.getName().getString() + "."), true);
}
}
}
private static void onServerTick(MinecraftServer server) {
for (ServerPlayerEntity player : server.getPlayerManager().getPlayerList()) {
normalizeInventory(player);
SwordProgression progression = SwordComponents.PROGRESSION.get(player);
ItemStack sword = findSword(player);
if (!sword.isEmpty()) {
ensureStarted(progression, sword);
applyPassive(player, progression);
}
}
for (ServerWorld world : server.getWorlds()) {
for (Entity entity : world.iterateEntities()) {
if (entity instanceof ItemEntity item && isSword(item.getStack())) {
item.discard();
}
}
}
}
private static void normalizeInventory(ServerPlayerEntity player) {
boolean found = false;
for (int slot = 0; slot < player.getInventory().size(); slot++) {
ItemStack stack = player.getInventory().getStack(slot);
if (!isSword(stack)) {
continue;
}
if (!found) {
found = true;
} else {
player.getInventory().setStack(slot, ItemStack.EMPTY);
}
}
SwordProgression progression = SwordComponents.PROGRESSION.get(player);
if (!found && progression.getTier() > 0) {
ItemStack restored = new ItemStack(materialItem(progression.getMaterial()));
for (int slot = 0; slot < player.getInventory().size(); slot++) {
if (player.getInventory().getStack(slot).isEmpty()) {
player.getInventory().setStack(slot, restored);
return;
}
}
player.getInventory().setStack(0, restored);
}
}
private static ItemStack findSword(PlayerEntity player) {
for (int slot = 0; slot < player.getInventory().size(); slot++) {
ItemStack stack = player.getInventory().getStack(slot);
if (isSword(stack)) {
return stack;
}
}
return ItemStack.EMPTY;
}
private static boolean isSword(ItemStack stack) {
return stack.isOf(SwordItems.WOOD_SWORD) || stack.isOf(SwordItems.IRON_SWORD) || stack.isOf(SwordItems.DIAMOND_SWORD);
}
private static net.minecraft.item.Item materialItem(String material) {
return switch (material) {
case "iron" -> SwordItems.IRON_SWORD;
case "diamond" -> SwordItems.DIAMOND_SWORD;
default -> SwordItems.WOOD_SWORD;
};
}
private static void ensureStarted(SwordProgression progression, ItemStack sword) {
if (progression.getTier() == 0) {
progression.setTier(1);
progression.setMaterial(sword.isOf(SwordItems.IRON_SWORD) ? "iron" : sword.isOf(SwordItems.DIAMOND_SWORD) ? "diamond" : "wood");
}
}