Files
src
build.gradle
gradle.properties
settings.gradle
Code
15 682
src/main/java/com/kodari/lightningspear/LightningSpearMod.java
package com.kodari.lightningspear;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import net.fabricmc.api.ModInitializer;
import net.minecraft.datafixer.DataFixTypes;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LightningEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.damage.DamageTypes;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.mob.HostileEntity;
import net.minecraft.entity.player.HungerManager;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.tooltip.TooltipType;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.RegistryWrapper;
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.Formatting;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.PersistentState;
import net.minecraft.world.World;
public final class LightningSpearMod implements ModInitializer {
public static final String MOD_ID = "lightningspear";
private static final RegistryKey<Item> SPEAR_KEY = RegistryKey.of(RegistryKeys.ITEM, Identifier.of(MOD_ID, "lightning_spear"));
public static final Item LIGHTNING_SPEAR = Registry.register(
Registries.ITEM,
SPEAR_KEY,
new LightningSpearItem(new Item.Settings().registryKey(SPEAR_KEY).maxCount(1))
);
private static final PersistentState.Type<QuestState> QUEST_STATE_TYPE = new PersistentState.Type<>(
QuestState::new,
QuestState::readNbt,
DataFixTypes.SAVED_DATA_COMMAND_STORAGE
);
private static final Map<UUID, Integer> ATTACK_LOCKS = new ConcurrentHashMap<>();
private static final Map<UUID, Integer> INVISIBILITY_REFRESH = new ConcurrentHashMap<>();
private static final Map<UUID, Aura> ACTIVE_AURAS = new ConcurrentHashMap<>();
@Override
public void onInitialize() {
}
public static boolean handleSpearAttack(ServerPlayerEntity player, Entity target) {
if (!player.getMainHandStack().isOf(LIGHTNING_SPEAR)) {
return false;
}
if (player.isSpectator()) {
return true;
}
if (!(player.getWorld() instanceof ServerWorld serverWorld)) {
return true;
}
UUID playerId = player.getUuid();
if (ATTACK_LOCKS.getOrDefault(playerId, 0) > 0) {
return true;
}
Progress progress = progressFor(getQuestState(serverWorld), playerId);
ATTACK_LOCKS.put(playerId, Math.max(3, 10 - progress.tier()));
if (target instanceof PlayerEntity defendingPlayer && defendingPlayer.isBlocking()) {
ItemStack blockingStack = defendingPlayer.getBlockingItem();
if (blockingStack != null) {
defendingPlayer.disableShield(blockingStack);
}
}
Vec3d velocity = player.getVelocity();
float speedMultiplier = 1.0F + (float) velocity.horizontalLength() * 0.65F;
float tierMultiplier = 1.0F + progress.tier() * 0.2F;
float damage = 8.0F * speedMultiplier * tierMultiplier;
boolean damaged = target.damage(serverWorld, player.getDamageSources().playerAttack(player), damage);
if (damaged) {
LightningEntity lightning = new LightningEntity(EntityType.LIGHTNING_BOLT, serverWorld);
lightning.setPosition(target.getX(), target.getY(), target.getZ());
serverWorld.spawnEntity(lightning);
player.resetLastAttackedTicks();
}
return true;
}
public static boolean shouldHideEquipment(PlayerEntity player) {
if (!player.hasStatusEffect(StatusEffects.INVISIBILITY) || !isHoldingSpear(player)) {
return false;
}
return Math.max(progressFromStack(player.getMainHandStack()).tier(), progressFromStack(player.getOffHandStack()).tier()) >= 2;
}
public static boolean shouldCancelFallDamage(LivingEntity entity, DamageSource source) {
if (!(entity instanceof PlayerEntity player)
|| !source.isOf(DamageTypes.FALL)
|| !isHoldingSpear(player)) {
return false;
}
if (entity.fallDistance > 10.0F && entity.getWorld() instanceof ServerWorld serverWorld) {
triggerSafeKnockback(player, serverWorld);
}
return true;
}
public static void recordPlayerKill(LivingEntity entity, DamageSource source) {
if (entity.getWorld() instanceof ServerWorld serverWorld
&& source.getAttacker() instanceof ServerPlayerEntity player) {
QuestState state = getQuestState(serverWorld);
Progress progress = state.recordKill(player.getUuid(), entity.getType());
syncProgress(player.getInventory(), progress);
}
}
public static void onServerPlayerTick(ServerPlayerEntity player) {
UUID playerId = player.getUuid();
ATTACK_LOCKS.computeIfPresent(playerId, (id, ticks) -> ticks <= 1 ? null : ticks - 1);
boolean holdingSpear = isHoldingSpear(player);
if (holdingSpear) {
HungerManager hunger = player.getHungerManager();
hunger.setFoodLevel(20);
hunger.setSaturationLevel(20.0F);
}
Progress progress = progressFor(getQuestState((ServerWorld) player.getWorld()), playerId);
syncProgress(player.getInventory(), progress);
if (holdingSpear && progress.tier() >= 2) {
refreshInvisibility(player, playerId);
} else {
INVISIBILITY_REFRESH.remove(playerId);
}
Aura aura = ACTIVE_AURAS.get(playerId);
if (aura == null) {
return;
}
if (!player.isAlive() || aura.ticksRemaining() <= 0) {
ACTIVE_AURAS.remove(playerId, aura);
return;
}
ServerWorld world = (ServerWorld) player.getWorld();
for (int i = 0; i < 16; i++) {
double angle = Math.PI * 2.0 * i / 16.0;
world.spawnParticles(
ParticleTypes.ELECTRIC_SPARK,
player.getX() + Math.cos(angle) * 2.5,
player.getY() + 1.0,
player.getZ() + Math.sin(angle) * 2.5,
1, 0.0, 0.0, 0.0, 0.0
);
}
int elapsed = aura.elapsedTicks() + 1;
if (elapsed % 20 == 0) {
for (HostileEntity hostile : world.getEntitiesByClass(
HostileEntity.class,
new Box(player.getX() - 5.0, player.getY() - 5.0, player.getZ() - 5.0,
player.getX() + 5.0, player.getY() + 5.0, player.getZ() + 5.0),
entity -> entity.squaredDistanceTo(player) <= 25.0
)) {
hostile.damage(world, player.getDamageSources().magic(), 3.0F);
}
}
Aura next = new Aura(aura.ticksRemaining() - 1, elapsed);
if (next.ticksRemaining() <= 0) {
ACTIVE_AURAS.remove(playerId, aura);
} else {
ACTIVE_AURAS.replace(playerId, aura, next);
}
}
private static void refreshInvisibility(ServerPlayerEntity player, UUID playerId) {
int elapsed = INVISIBILITY_REFRESH.getOrDefault(playerId, 0);
if (elapsed == 0 || elapsed >= 20 || !player.hasStatusEffect(StatusEffects.INVISIBILITY)) {
player.addStatusEffect(new StatusEffectInstance(StatusEffects.INVISIBILITY, 40, 0, false, false, false));
INVISIBILITY_REFRESH.put(playerId, 1);
} else {
INVISIBILITY_REFRESH.put(playerId, elapsed + 1);
}
}
private static void triggerSafeKnockback(PlayerEntity player, ServerWorld world) {