Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 233
src/main/java/com/kodari/hitvortex/HitVortexMod.java
package com.kodari.hitvortex;
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.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
public class HitVortexMod implements ModInitializer {
private static final Logger LOGGER = LoggerFactory.getLogger("hitvortex");
private static final List<Vortex> ACTIVE_VORTEXES = new ArrayList<>();
private static final Path CONFIG_PATH = Path.of("config", "hitvortex.properties");
private static int durationTicks = 30;
private static double radius = 1.0;
private static int minimumOpLevel = 0;
@Override
public void onInitialize() {
loadConfig();
ServerLivingEntityEvents.AFTER_DAMAGE.register((entity, source, baseDamageTaken, damageTaken, blocked) -> {
if (damageTaken <= 0 || !(entity instanceof PlayerEntity)) {
return;
}
if (!(source.getAttacker() instanceof ServerPlayerEntity attacker)
|| !attacker.hasPermissionLevel(minimumOpLevel)
|| !(entity.getWorld() instanceof ServerWorld world)) {
return;
}
ACTIVE_VORTEXES.add(new Vortex(
world,
entity.getX(),
entity.getY() + entity.getHeight() * 0.55,
entity.getZ(),
durationTicks,
radius
));
});
ServerTickEvents.END_SERVER_TICK.register(server -> tickVortexes());
}
private static void tickVortexes() {
Iterator<Vortex> iterator = ACTIVE_VORTEXES.iterator();
while (iterator.hasNext()) {
Vortex vortex = iterator.next();
if (vortex.age >= vortex.duration) {
iterator.remove();
continue;
}
vortex.spawnParticles();
vortex.age++;
}
}
private static void loadConfig() {
Properties properties = new Properties();
try {
Files.createDirectories(CONFIG_PATH.getParent());
if (Files.exists(CONFIG_PATH)) {
try (var reader = Files.newBufferedReader(CONFIG_PATH)) {
properties.load(reader);
}
}
durationTicks = readInt(properties, "duration-ticks", 30, 1, 1200);
radius = readDouble(properties, "radius", 1.0, 0.1, 8.0);
minimumOpLevel = readInt(properties, "minimum-op-level", 0, 0, 4);
properties.setProperty("duration-ticks", Integer.toString(durationTicks));
properties.setProperty("radius", Double.toString(radius));
properties.setProperty("minimum-op-level", Integer.toString(minimumOpLevel));
try (var writer = Files.newBufferedWriter(CONFIG_PATH)) {
properties.store(writer, "Hit Vortex settings");
}
} catch (IOException exception) {
LOGGER.error("Could not load or save {}", CONFIG_PATH, exception);
}
}
private static int readInt(Properties properties, String key, int fallback, int minimum, int maximum) {
try {
return Math.clamp(Integer.parseInt(properties.getProperty(key, Integer.toString(fallback)).trim()), minimum, maximum);
} catch (NumberFormatException exception) {
LOGGER.warn("Invalid integer for '{}'; using {}", key, fallback);
return fallback;
}
}
private static double readDouble(Properties properties, String key, double fallback, double minimum, double maximum) {
try {
double value = Double.parseDouble(properties.getProperty(key, Double.toString(fallback)).trim());
if (!Double.isFinite(value)) {
throw new NumberFormatException("Value must be finite");
}
return Math.clamp(value, minimum, maximum);
} catch (NumberFormatException exception) {
LOGGER.warn("Invalid number for '{}'; using {}", key, fallback);
return fallback;
}
}
private static final class Vortex {
private final ServerWorld world;
private final double x;
private final double y;
private final double z;
private final int duration;
private final double radius;
private int age;
private Vortex(ServerWorld world, double x, double y, double z, int duration, double radius) {
this.world = world;
this.x = x;
this.y = y;
this.z = z;
this.duration = duration;
this.radius = radius;
}
private void spawnParticles() {
int points = 12;
double rotation = age * 0.32;
for (int strand = 0; strand < 2; strand++) {
for (int point = 0; point < points; point++) {
double progress = point / (double) (points - 1);
double angle = rotation + progress * Math.PI * 3 + strand * Math.PI;
double pointRadius = radius * (0.55 + 0.45 * Math.sin(progress * Math.PI));
double particleX = x + Math.cos(angle) * pointRadius;
double particleY = y + (progress - 0.5) * 1.6;
double particleZ = z + Math.sin(angle) * pointRadius;
world.spawnParticles(
strand == 0 ? ParticleTypes.END_ROD : ParticleTypes.SOUL_FIRE_FLAME,
particleX, particleY, particleZ, 1, 0, 0, 0, 0
);
}
}
}
}
}