Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
11 600
src/client/java/com/kodari/pearloptimizer/PearlOptimizerClient.java
package com.kodari.pearloptimizer;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientEntityEvents;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.event.player.UseItemCallback;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.entity.Entity;
import net.minecraft.entity.projectile.thrown.EnderPearlEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.util.ActionResult;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class PearlOptimizerClient implements ClientModInitializer {
private static final List<Prediction> PREDICTIONS = new ArrayList<>();
private static int nextPredictionId = Integer.MIN_VALUE;
private static ClientPlayNetworkHandler lastNetworkHandler;
private static boolean settingsDirty;
private static int syncDelay;
@Override
public void onInitializeClient() {
UseItemCallback.EVENT.register((player, world, hand) -> {
if (!world.isClient() || !PearlOptimizerConfig.isClientOptimizationEnabled()
|| !(player instanceof ClientPlayerEntity clientPlayer) || clientPlayer.isSpectator()) {
return ActionResult.PASS;
}
ItemStack stack = clientPlayer.getStackInHand(hand);
if (!stack.isOf(Items.ENDER_PEARL) || clientPlayer.getItemCooldownManager().isCoolingDown(stack)) {
return ActionResult.PASS;
}
ClientWorld clientWorld = (ClientWorld) world;
EnderPearlEntity prediction = new EnderPearlEntity(clientWorld, clientPlayer, stack);
prediction.setVelocity(clientPlayer, clientPlayer.getPitch(), clientPlayer.getYaw(), 0.0F, 1.5F, 1.0F);
while (clientWorld.getEntityById(nextPredictionId) != null) {
nextPredictionId++;
}
prediction.setId(nextPredictionId++);
PREDICTIONS.add(new Prediction(clientWorld, prediction));
clientWorld.addEntity(prediction);
return ActionResult.PASS;
});
ClientEntityEvents.ENTITY_LOAD.register(PearlOptimizerClient::onEntityLoad);
ClientTickEvents.END_CLIENT_TICK.register(PearlOptimizerClient::cleanupPredictions);
}
public static void settingsChanged() {
settingsDirty = true;
syncDelay = 5;
}
private static void onEntityLoad(Entity entity, ClientWorld world) {
MinecraftClient client = MinecraftClient.getInstance();
if (!(entity instanceof EnderPearlEntity authoritativePearl)
|| authoritativePearl.getOwner() != client.player) {
return;
}
Prediction match = null;
double closestDistance = 4.0;
for (Prediction prediction : PREDICTIONS) {
if (prediction.world != world || prediction.pearl == authoritativePearl || prediction.removeNextTick) {
continue;
}
double distance = authoritativePearl.squaredDistanceTo(
prediction.startX, prediction.startY, prediction.startZ);
if (distance <= closestDistance) {
closestDistance = distance;
match = prediction;
}
}
if (match != null) {
authoritativePearl.setPosition(match.pearl.getX(), match.pearl.getY(), match.pearl.getZ());
authoritativePearl.setVelocity(match.pearl.getVelocity());
match.removeNextTick = true;
}
}
private static void cleanupPredictions(MinecraftClient client) {
Iterator<Prediction> iterator = PREDICTIONS.iterator();
while (iterator.hasNext()) {
Prediction prediction = iterator.next();
if (prediction.removeNextTick || prediction.pearl.isRemoved() || prediction.pearl.age >= 40
|| prediction.world != client.world) {
if (!prediction.pearl.isRemoved() && prediction.world == client.world) {
prediction.pearl.discard();
}
iterator.remove();
}
}
ClientPlayNetworkHandler networkHandler = client.getNetworkHandler();
if (networkHandler == null) {
lastNetworkHandler = null;
return;
}
if (networkHandler != lastNetworkHandler) {
lastNetworkHandler = networkHandler;
if (PearlOptimizerConfig.hasClientSettings()) {
settingsDirty = true;
syncDelay = 0;
}
}
if (!settingsDirty) {
return;
}
if (syncDelay > 0) {
syncDelay--;
return;
}
networkHandler.sendChatCommand("pearloptimizer set "
+ PearlOptimizerConfig.isClientOptimizationEnabled() + " "
+ PearlOptimizerConfig.getDistance() + " " + PearlOptimizerConfig.getSpeed());
settingsDirty = false;
}
private static final class Prediction {
private final ClientWorld world;
private final EnderPearlEntity pearl;
private final double startX;
private final double startY;
private final double startZ;
private boolean removeNextTick;
private Prediction(ClientWorld world, EnderPearlEntity pearl) {
this.world = world;
this.pearl = pearl;
this.startX = pearl.getX();
this.startY = pearl.getY();
this.startZ = pearl.getZ();
}
}
}