Files
src
build.gradle
gradle.properties
README.md
settings.gradle
Code
6 323
src/main/java/com/kodari/finishers/FinisherMod.java
package com.kodari.finishers;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.entity.event.v1.ServerEntityCombatEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
public final class FinisherMod implements ModInitializer {
private static final Logger LOGGER = Logger.getLogger("KodariFinishers");
private static final Path CONFIG_PATH = Path.of("config", "kodari-finishers.properties");
private static final List<ActiveFinisher> ACTIVE_FINISHERS = new ArrayList<>();
private static final int MAX_ACTIVE_FINISHERS = 12;
private static boolean enabled = true;
@Override
public void onInitialize() {
loadConfig();
ServerEntityCombatEvents.AFTER_KILLED_OTHER_ENTITY.register((world, attacker, killed) -> {
if (!enabled || !(attacker instanceof PlayerEntity player)) {
return;
}
NbtCompound weaponTag = player.getMainHandStack().getNbt();
if (weaponTag == null) {
return;
}
Theme theme = Theme.fromId(weaponTag.getString("kodari_finisher"));
if (theme == null) {
return;
}
if (ACTIVE_FINISHERS.size() >= MAX_ACTIVE_FINISHERS) {
ACTIVE_FINISHERS.remove(0);
}
ActiveFinisher finisher = new ActiveFinisher(world, killed.getX(), killed.getY(), killed.getZ(), theme);
ACTIVE_FINISHERS.add(finisher);
playStinger(world, finisher.x, finisher.y, finisher.z, theme);
});
ServerTickEvents.END_WORLD_TICK.register(world -> {
for (int i = ACTIVE_FINISHERS.size() - 1; i >= 0; i--) {
ActiveFinisher finisher = ACTIVE_FINISHERS.get(i);
if (finisher.world == world && finisher.tick()) {
ACTIVE_FINISHERS.remove(i);
}
}
});
}
private static void loadConfig() {
Properties properties = new Properties();
try {
Files.createDirectories(CONFIG_PATH.getParent());
if (Files.exists(CONFIG_PATH)) {
try (Reader reader = Files.newBufferedReader(CONFIG_PATH)) {
properties.load(reader);
}
} else {
properties.setProperty("enabled", "true");
try (Writer writer = Files.newBufferedWriter(CONFIG_PATH)) {
properties.store(writer, "Enable or disable collection-themed kill finishers");
}
}
enabled = Boolean.parseBoolean(properties.getProperty("enabled", "true"));
} catch (IOException exception) {
enabled = true;
LOGGER.log(Level.WARNING, "Could not read finisher config; finishers remain enabled", exception);
}
}
private static void playStinger(ServerWorld world, double x, double y, double z, Theme theme) {
switch (theme) {
case NEO_FRONTIER -> world.playSound(null, x, y, z, SoundEvents.ENTITY_FIREWORK_ROCKET_TWINKLE, SoundCategory.PLAYERS, 0.65f, 1.1f);
case PRIME -> world.playSound(null, x, y, z, SoundEvents.ENTITY_PLAYER_LEVELUP, SoundCategory.PLAYERS, 0.55f, 1.3f);
case PRIME_2 -> world.playSound(null, x, y, z, SoundEvents.ITEM_TOTEM_USE, SoundCategory.PLAYERS, 0.55f, 1.05f);
case RGX_11Z_PRO -> world.playSound(null, x, y, z, SoundEvents.ITEM_TRIDENT_THUNDER, SoundCategory.PLAYERS, 0.55f, 1.4f);
case KURONAMI -> world.playSound(null, x, y, z, SoundEvents.ENTITY_FIREWORK_ROCKET_TWINKLE, SoundCategory.PLAYERS, 0.55f, 0.8f);
case START_PF_CHAOS -> world.playSound(null, x, y, z, SoundEvents.ENTITY_FIREWORK_ROCKET_LARGE_BLAST, SoundCategory.PLAYERS, 0.55f, 0.75f);
}
}
private enum Theme {
NEO_FRONTIER("neo_frontier"),
PRIME("prime"),
PRIME_2("prime_2"),
RGX_11Z_PRO("rgx_11z_pro"),
KURONAMI("kuronami"),
START_PF_CHAOS("start_pf_chaos");
private final String id;
Theme(String id) {
this.id = id;
}
private static Theme fromId(String id) {
for (Theme theme : values()) {
if (theme.id.equals(id)) {
return theme;
}
}
return null;
}
}
private static final class ActiveFinisher {
private final ServerWorld world;
private final double x;
private final double y;
private final double z;
private final Theme theme;
private int age;
private ActiveFinisher(ServerWorld world, double x, double y, double z, Theme theme) {
this.world = world;
this.x = x;
this.y = y;
this.z = z;
this.theme = theme;
}
private boolean tick() {
if (age >= 21) {
return true;
}
if (age % 3 == 0) {
emitPulse();
}
age++;
return age >= 21;
}
private void emitPulse() {
double radius = 0.35 + age * 0.025;
double rise = 0.35 + age * 0.012;
for (int i = 0; i < 6; i++) {
double angle = age * 0.11 + i * Math.PI / 3.0;
double particleX = x + Math.cos(angle) * radius;
double particleY = y + rise + (i % 2) * 0.12;
double particleZ = z + Math.sin(angle) * radius;
emitThemeParticles(particleX, particleY, particleZ, age / 3, i);
}
}
private void emitThemeParticles(double particleX, double particleY, double particleZ, int pulse, int index) {
switch (theme) {
case NEO_FRONTIER -> {
world.spawnParticles(ParticleTypes.CRIT, particleX, particleY, particleZ, 1, 0.0, 0.0, 0.0, 0.015);
if (index % 2 == 0) {
world.spawnParticles(ParticleTypes.END_ROD, particleX, particleY, particleZ, 1, 0.0, 0.0, 0.0, 0.01);
}
}
case PRIME -> {
world.spawnParticles(ParticleTypes.END_ROD, particleX, particleY, particleZ, 1, 0.0, 0.0, 0.0, 0.01);
if (index % 2 == 0) {
world.spawnParticles(ParticleTypes.GLOW, particleX, particleY, particleZ, 1, 0.0, 0.0, 0.0, 0.01);
}
}
case PRIME_2 -> {
world.spawnParticles(ParticleTypes.END_ROD, particleX, particleY, particleZ, 1, 0.0, 0.0, 0.0, 0.01);
if (index % 2 == 0) {
world.spawnParticles(ParticleTypes.ELECTRIC_SPARK, particleX, particleY, particleZ, 1, 0.0, 0.0, 0.0, 0.02);
}
}
case RGX_11Z_PRO -> {
switch (pulse % 3) {
case 0 -> world.spawnParticles(ParticleTypes.ELECTRIC_SPARK, particleX, particleY, particleZ, 1, 0.0, 0.0, 0.0, 0.025);
case 1 -> world.spawnParticles(ParticleTypes.GLOW, particleX, particleY, particleZ, 1, 0.0, 0.0, 0.0, 0.015);
default -> world.spawnParticles(ParticleTypes.END_ROD, particleX, particleY, particleZ, 1, 0.0, 0.0, 0.0, 0.015);
}
}
case KURONAMI -> {
world.spawnParticles(ParticleTypes.SPLASH, particleX, particleY, particleZ, 1, 0.0, 0.0, 0.0, 0.01);
if (index % 2 == 0) {
world.spawnParticles(ParticleTypes.SOUL_FIRE_FLAME, particleX, particleY, particleZ, 1, 0.0, 0.0, 0.0, 0.01);
}
}
case START_PF_CHAOS -> {
world.spawnParticles(ParticleTypes.ELECTRIC_SPARK, particleX, particleY, particleZ, 1, 0.0, 0.0, 0.0, 0.025);
if ((pulse + index) % 2 == 0) {
world.spawnParticles(ParticleTypes.SOUL_FIRE_FLAME, particleX, particleY, particleZ, 1, 0.0, 0.0, 0.0, 0.02);