Files
src
build.gradle
gradle.properties
settings.gradle
Code
8 251
src/client/java/com/kodari/pearleffects/PearlEffectsClient.java
package com.kodari.pearleffects;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.thrown.EnderPearlEntity;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Hand;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
public final class PearlEffectsClient implements ClientModInitializer {
private static final Random RANDOM = new Random();
private static final Map<Integer, PearlPosition> PEARLS = new HashMap<>();
private static ClientWorld activeWorld;
private static int tick;
private static int ringTicks;
private static double ringX, ringY, ringZ;
private record PearlPosition(double x, double y, double z, int tick) { }
@Override
public void onInitializeClient() {
ClientTickEvents.END_CLIENT_TICK.register(PearlEffectsClient::tick);
}
public static void onThrow(World world, PlayerEntity player, Hand hand) {
MinecraftClient client = MinecraftClient.getInstance();
if (!(world instanceof ClientWorld clientWorld) || player != client.player) return;
Vec3d direction = player.getRotationVec(1.0F);
double side = hand == Hand.MAIN_HAND ? 0.24 : -0.24;
double x = player.getX() + direction.x * 0.45 - direction.z * side;
double y = player.getEyeY() - 0.25 + direction.y * 0.45;
double z = player.getZ() + direction.z * 0.45 + direction.x * side;
clientWorld.playSoundClient(x, y, z, SoundEvents.BLOCK_AMETHYST_BLOCK_CHIME, SoundCategory.PLAYERS, 0.55F, 1.45F, false);
for (int i = 0; i < 12; i++) {
double vx = direction.x * 0.07 + (RANDOM.nextDouble() - 0.5) * 0.1;
double vy = direction.y * 0.07 + (RANDOM.nextDouble() - 0.5) * 0.1;
double vz = direction.z * 0.07 + (RANDOM.nextDouble() - 0.5) * 0.1;
clientWorld.addParticleClient(i % 3 == 0 ? ParticleTypes.END_ROD : ParticleTypes.REVERSE_PORTAL, x, y, z, vx, vy, vz);
}
}
private static void tick(MinecraftClient client) {
ClientWorld world = client.world;
if (world != activeWorld) {
activeWorld = world;
PEARLS.clear();
ringTicks = 0;
tick = 0;
}
if (world == null || client.player == null) return;
tick++;
for (Entity entity : world.getEntities()) {
if (!(entity instanceof EnderPearlEntity pearl)) continue;
if (pearl.getOwner() == client.player) {
PEARLS.put(pearl.getId(), new PearlPosition(pearl.getX(), pearl.getY(), pearl.getZ(), tick));
}
double dx = pearl.getX() - pearl.lastX;
double dy = pearl.getY() - pearl.lastY;
double dz = pearl.getZ() - pearl.lastZ;
for (int i = 0; i < 3; i++) {
double t = (i + 0.5) / 3.0;
double x = pearl.lastX + dx * t + (RANDOM.nextDouble() - 0.5) * 0.08;
double y = pearl.lastY + dy * t + (RANDOM.nextDouble() - 0.5) * 0.08;
double z = pearl.lastZ + dz * t + (RANDOM.nextDouble() - 0.5) * 0.08;
world.addParticleClient(ParticleTypes.REVERSE_PORTAL, x, y, z, 0, 0.005, 0);
}
if (tick % 5 == 0) {
world.addParticleClient(ParticleTypes.END_ROD, pearl.getX(), pearl.getY(), pearl.getZ(), 0, 0.01, 0);
}
}
PEARLS.values().removeIf(position -> tick - position.tick() > 20);
if (ringTicks > 0) {
double radius = 0.3 + (7 - ringTicks) * 0.27;
for (int i = 0; i < 18; i++) {
double angle = 2 * Math.PI * i / 18;
world.addParticleClient(ParticleTypes.END_ROD, ringX + Math.cos(angle) * radius, ringY + 0.08, ringZ + Math.sin(angle) * radius, 0, 0.005, 0);
}
ringTicks--;
}
}
public static void onPositionCorrection() {
MinecraftClient client = MinecraftClient.getInstance();
if (client.world == null || client.player == null || client.world != activeWorld) return;
double x = client.player.getX(), y = client.player.getY(), z = client.player.getZ();
Iterator<PearlPosition> positions = PEARLS.values().iterator();
while (positions.hasNext()) {
PearlPosition position = positions.next();
double dx = position.x() - x, dy = position.y() - y, dz = position.z() - z;
if (tick - position.tick() <= 20 && dx * dx + dy * dy + dz * dz < 36) {
positions.remove();
landing(client.world, x, y, z);
return;
}
}
}
private static void landing(ClientWorld world, double x, double y, double z) {
world.playSoundClient(x, y, z, SoundEvents.BLOCK_ENCHANTMENT_TABLE_USE, SoundCategory.PLAYERS, 0.65F, 1.25F, false);
world.playSoundClient(x, y, z, SoundEvents.BLOCK_AMETHYST_BLOCK_CHIME, SoundCategory.PLAYERS, 0.7F, 0.8F, false);
for (int i = 0; i < 32; i++) {
double angle = RANDOM.nextDouble() * Math.PI * 2;
double speed = 0.03 + RANDOM.nextDouble() * 0.12;
world.addParticleClient(i % 4 == 0 ? ParticleTypes.END_ROD : ParticleTypes.REVERSE_PORTAL,
x, y + 0.8, z, Math.cos(angle) * speed, (RANDOM.nextDouble() - 0.25) * 0.16, Math.sin(angle) * speed);
}
ringX = x;
ringY = y;
ringZ = z;
ringTicks = 6;
}
}