Files
src
build.gradle
gradle.properties
settings.gradle
Code
6 277
src/client/java/com/kodari/nukefishing/NukeFishingClient.java
package com.kodari.nukefishing;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.enchantment.Enchantments;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.particle.DustParticleEffect;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
public final class NukeFishingClient implements ClientModInitializer {
private static final int RING_COUNT = 10;
private static final int MAX_AGE = 52;
private static final DustParticleEffect CORE_DUST = new DustParticleEffect(0xFF3B0A, 1.15F);
private static final DustParticleEffect RING_DUST = new DustParticleEffect(0xFFB300, 0.85F);
private final List<NukeShot> shots = new ArrayList<>();
private boolean useKeyWasDown;
@Override
public void onInitializeClient() {
ClientTickEvents.END_CLIENT_TICK.register(this::tick);
}
private void tick(MinecraftClient client) {
if (client.player == null || client.world == null) {
shots.clear();
useKeyWasDown = false;
return;
}
boolean useKeyDown = client.options.useKey.isPressed();
if (useKeyDown && !useKeyWasDown) {
ItemStack rod = client.player.getStackInHand(Hand.MAIN_HAND);
if (rod.isOf(Items.FISHING_ROD) && hasInfinity(client, rod)) {
shots.add(new NukeShot(client.player));
client.player.playSound(SoundEvents.ENTITY_FIREWORK_ROCKET_LAUNCH, 1.3F, 0.55F);
}
}
useKeyWasDown = useKeyDown;
Iterator<NukeShot> iterator = shots.iterator();
while (iterator.hasNext()) {
if (iterator.next().tick(client)) {
iterator.remove();
}
}
}
private static boolean hasInfinity(MinecraftClient client, ItemStack stack) {
var enchantments = stack.get(DataComponentTypes.ENCHANTMENTS);
if (enchantments == null) {
return false;
}
return enchantments.getLevel(
client.world.getRegistryManager()
.getOrThrow(RegistryKeys.ENCHANTMENT)
.getOrThrow(Enchantments.INFINITY)) > 0;
}
private static final class NukeShot {
private final Vec3d direction;
private final Vec3d right;
private final Vec3d up;
private Vec3d position;
private int age;
private NukeShot(PlayerEntity player) {
this.position = player.getCameraPosVec(1.0F).add(player.getRotationVec(1.0F).multiply(0.8));
this.direction = player.getRotationVec(1.0F).normalize();
Vec3d referenceUp = Math.abs(direction.y) > 0.9
? new Vec3d(1.0, 0.0, 0.0)
: new Vec3d(0.0, 1.0, 0.0);
this.right = direction.crossProduct(referenceUp).normalize();
this.up = right.crossProduct(direction).normalize();
}
private boolean tick(MinecraftClient client) {
age++;
position = position.add(direction.multiply(1.35));
spawnTrail(client);
spawnRings(client);
BlockPos blockPos = BlockPos.ofFloored(position);
boolean hitBlock = age > 2 && !client.world.getBlockState(blockPos).isAir();
if (hitBlock || age >= MAX_AGE) {
spawnDetonation(client);
return true;
}
return false;
}
private void spawnTrail(MinecraftClient client) {
client.world.addParticleClient(ParticleTypes.END_ROD,
position.x, position.y, position.z, 0.0, 0.0, 0.0);
client.world.addParticleClient(ParticleTypes.FLAME,
position.x, position.y, position.z, 0.0, 0.015, 0.0);
if (age % 2 == 0) {
client.world.addParticleClient(ParticleTypes.SMOKE,
position.x, position.y, position.z, 0.0, 0.02, 0.0);
}
}
private void spawnRings(MinecraftClient client) {
for (int ring = 0; ring < RING_COUNT; ring++) {
int ringAge = age - 2 - ring * 4;
if (ringAge < 0 || ringAge > 18) {
continue;
}
double radius = 0.18 + ringAge * 0.23;
for (int point = 0; point < 40; point++) {
double angle = point * Math.PI * 2.0 / 40.0;
Vec3d offset = right.multiply(Math.cos(angle) * radius)
.add(up.multiply(Math.sin(angle) * radius));
Vec3d ringPoint = position.add(offset);
client.world.addParticleClient(RING_DUST,
ringPoint.x, ringPoint.y, ringPoint.z, 0.0, 0.0, 0.0);
}
}
}
private void spawnDetonation(MinecraftClient client) {
client.player.playSound(SoundEvents.ENTITY_GENERIC_EXPLODE.value(), 2.5F, 0.55F);
for (int ring = 0; ring < RING_COUNT; ring++) {
double radius = 0.7 + ring * 0.42;
for (int point = 0; point < 48; point++) {
double angle = point * Math.PI * 2.0 / 48.0;
double y = Math.sin(angle) * radius;
double horizontal = Math.cos(angle) * radius;
client.world.addParticleClient(CORE_DUST,
position.x + horizontal, position.y + y, position.z,
0.0, 0.02, 0.0);
client.world.addParticleClient(CORE_DUST,
position.x, position.y + y, position.z + horizontal,
0.0, 0.02, 0.0);
}
}
for (int particle = 0; particle < 90; particle++) {
double theta = particle * 0.71;
double phi = particle * 1.17;
double spread = 0.25 + (particle % 9) * 0.12;
Vec3d velocity = new Vec3d(
Math.cos(theta) * Math.sin(phi),
Math.cos(phi),
Math.sin(theta) * Math.sin(phi)).multiply(spread);
client.world.addParticleClient(ParticleTypes.EXPLOSION,
position.x, position.y, position.z,
velocity.x, velocity.y, velocity.z);
client.world.addParticleClient(ParticleTypes.FLAME,
position.x, position.y, position.z,
velocity.x, velocity.y, velocity.z);
}
}
}
}