Files
src
build.gradle
gradle.properties
settings.gradle
Code
9 315
src/main/java/com/kodari/sandstorm/SandstormMod.java
package com.kodari.sandstorm;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.NbtComponent;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.particle.DustParticleEffect;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Hand;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3d;
public class SandstormMod implements ModInitializer {
public static final String MOD_ID = "sandstorm";
private static final long HIT_WINDOW_MILLIS = 10_000L;
private static final long DAMAGE_INTERVAL_MILLIS = 500L;
private static final int STORM_DURATION_TICKS = 20 * 20;
private static final int MAX_GROWTH = 8;
private static final int SAND_COLOR = 0xD6B875;
private static final Map<UUID, Long> RECENT_HITS = new HashMap<>();
private static final Map<UUID, StormState> ACTIVE_STORMS = new HashMap<>();
@Override
public void onInitialize() {
ServerLivingEntityEvents.AFTER_DAMAGE.register(SandstormMod::afterDamage);
ServerTickEvents.END_WORLD_TICK.register(SandstormMod::tickWorld);
}
private static void afterDamage(LivingEntity entity, DamageSource source, float baseDamageTaken, float damageTaken, boolean blocked) {
if (damageTaken <= 0.0F || !(source.getAttacker() instanceof ServerPlayerEntity player)) {
return;
}
UUID playerId = player.getUuid();
StormState storm = ACTIVE_STORMS.get(playerId);
if (storm != null) {
if (!storm.dealingStormDamage) {
storm.grow();
}
return;
}
ItemStack heldStack = player.getStackInHand(Hand.MAIN_HAND);
if (isSandstormSword(heldStack)) {
RECENT_HITS.put(playerId, System.currentTimeMillis());
}
}
private static boolean isSandstormSword(ItemStack stack) {
if (!stack.isOf(Items.DIAMOND_SWORD)) {
return false;
}
NbtComponent customData = stack.get(DataComponentTypes.CUSTOM_DATA);
return customData != null && customData.copyNbt().getBoolean("sandstorm", false);
}
private static void tickWorld(ServerWorld world) {
long now = System.currentTimeMillis();
for (ServerPlayerEntity player : world.getPlayers()) {
UUID playerId = player.getUuid();
StormState storm = ACTIVE_STORMS.get(playerId);
if (storm == null) {
Long lastHit = RECENT_HITS.get(playerId);
if (lastHit == null) {
continue;
}
if (now - lastHit > HIT_WINDOW_MILLIS) {
RECENT_HITS.remove(playerId);
continue;
}
if (!player.isSneaking()) {
continue;
}
storm = new StormState(player, now);
ACTIVE_STORMS.put(playerId, storm);
RECENT_HITS.remove(playerId);
}
if (!player.isAlive() || storm.ticksRemaining <= 0) {
ACTIVE_STORMS.remove(playerId);
if (storm.appliedInvisibility) {
player.removeStatusEffect(StatusEffects.INVISIBILITY);
}
continue;
}
tickStorm(world, player, storm, now);
}
}
private static void tickStorm(ServerWorld world, ServerPlayerEntity player, StormState storm, long now) {
storm.ticksRemaining--;
if (player.getStatusEffect(StatusEffects.INVISIBILITY) == null) {
storm.appliedInvisibility |= player.addStatusEffect(
new StatusEffectInstance(StatusEffects.INVISIBILITY, 3, 0, true, false, false)
);
}
double size = 2.0 + storm.growth * 0.5;
double radius = size / 2.0;
double height = size;
Vec3d position = player.getEntityPos();
boolean moved = storm.lastPosition.squaredDistanceTo(position) > 0.0025;
boolean turned = angleDifference(storm.lastYaw, player.getYaw()) >= 8.0F
|| angleDifference(storm.lastPitch, player.getPitch()) >= 8.0F;
storm.lastPosition = position;
storm.lastYaw = player.getYaw();
storm.lastPitch = player.getPitch();
double rotation = (STORM_DURATION_TICKS - storm.ticksRemaining) * 0.18;
for (int i = 0; i < 8; i++) {
double fraction = i / 8.0;
double angle = rotation + fraction * Math.PI * 2.0;
double verticalFraction = (fraction + rotation / (Math.PI * 2.0) * 0.35) % 1.0;
double spiralRadius = radius * (0.45 + 0.55 * Math.sin(verticalFraction * Math.PI));
int color = switch (i % 3) {
case 0 -> 0xF1D9A2;
case 1 -> SAND_COLOR;
default -> 0xB38B4D;
};
world.spawnParticles(
new DustParticleEffect(color, 1.15F + storm.growth * 0.1F),
player.getX() + Math.cos(angle) * spiralRadius,
player.getY() + 0.15 + verticalFraction * (height - 0.3),
player.getZ() + Math.sin(angle) * spiralRadius,
2, 0.08, 0.12, 0.08, 0.01
);
}
world.spawnParticles(
new DustParticleEffect(SAND_COLOR, 1.2F + storm.growth * 0.08F),
player.getX(), player.getY() + height / 2.0, player.getZ(),
8 + storm.growth, radius * 0.55, height / 2.0, radius * 0.55, 0.025
);
if ((moved || turned) && now >= storm.nextDamageAt) {
damageNearbyEntities(world, player, storm, radius, height);
storm.nextDamageAt = now + DAMAGE_INTERVAL_MILLIS;
}
}
private static void damageNearbyEntities(ServerWorld world, ServerPlayerEntity player, StormState storm, double radius, double height) {
double verticalExpansion = Math.max(0.2, (height - 1.8) / 2.0);
Box area = player.getBoundingBox().expand(radius, verticalExpansion, radius);
float damage = 2.0F + storm.growth * 0.5F;
storm.dealingStormDamage = true;
try {
for (LivingEntity target : world.getEntitiesByClass(LivingEntity.class, area, entity -> entity != player && entity.isAlive())) {
double verticalDistance = target.getY() - player.getY();
if (Math.abs(target.getX() - player.getX()) <= radius
&& Math.abs(target.getZ() - player.getZ()) <= radius
&& verticalDistance >= -0.5
&& verticalDistance <= height) {
target.damage(world, player.getDamageSources().playerAttack(player), damage);
}
}
} finally {
storm.dealingStormDamage = false;
}
}
private static float angleDifference(float first, float second) {
return Math.abs((first - second + 540.0F) % 360.0F - 180.0F);
}
private static final class StormState {
private int growth;
private int ticksRemaining = STORM_DURATION_TICKS;
private long nextDamageAt;
private Vec3d lastPosition;
private float lastYaw;
private float lastPitch;
private boolean dealingStormDamage;
private boolean appliedInvisibility;
private StormState(ServerPlayerEntity player, long now) {
this.nextDamageAt = now;
this.lastPosition = player.getEntityPos();
this.lastYaw = player.getYaw();
this.lastPitch = player.getPitch();
}