Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
8 594
src/client/java/com/xpoptimizer/core/XpOptimizerClient.java
package com.xpoptimizer.core;
import com.xpoptimizer.gui.XpOptimizerScreen;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.client.util.InputUtil;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.decoration.ArmorStandEntity;
import net.minecraft.item.Items;
import net.minecraft.registry.Registries;
import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.util.Hand;
import org.lwjgl.glfw.GLFW;
import java.util.concurrent.ThreadLocalRandom;
public final class XpOptimizerClient implements ClientModInitializer {
public static final Settings SETTINGS = Settings.load();
private boolean shiftDown;
private boolean swapDown;
private long due = -1;
private long tick;
private long lastSwap = -100;
private long totemDue = -1;
private boolean pearlDown;
private boolean maceDisableDown;
private boolean fallTriggered;
private int pearlStage;
private long pearlDue;
private int followupStage;
private long followupDue;
@Override
public void onInitializeClient() {
ClientLifecycleEvents.CLIENT_STOPPING.register(client -> SETTINGS.save());
ClientTickEvents.END_CLIENT_TICK.register(client -> {
tick++;
boolean shift = InputUtil.isKeyPressed(client.getWindow(), GLFW.GLFW_KEY_RIGHT_SHIFT);
if (shift && !shiftDown) {
if (client.currentScreen instanceof XpOptimizerScreen) client.setScreen(null);
else if (client.currentScreen == null) client.setScreen(new XpOptimizerScreen());
}
shiftDown = shift;
if (XpOptimizerScreen.bindingSetting != null) {
int captured = captureInput(client);
if (captured != Integer.MIN_VALUE && captured != Integer.MIN_VALUE + 1) {
switch (XpOptimizerScreen.bindingSetting) {
case "spear" -> SETTINGS.spearSwapKey = captured;
case "pearl" -> SETTINGS.pearlKey = captured;
case "mace" -> SETTINGS.maceDisableKey = captured;
}
SETTINGS.save();
XpOptimizerScreen.bindingSetting = null;
client.setScreen(new XpOptimizerScreen(XpOptimizerScreen.bindingPage));
}
}
boolean pressed = inputPressed(client, SETTINGS.spearSwapKey);
boolean edge = pressed && !swapDown;
swapDown = pressed;
boolean pearlPressed = inputPressed(client, SETTINGS.pearlKey);
boolean pearlEdge = pearlPressed && !pearlDown;
pearlDown = pearlPressed;
boolean disablePressed = inputPressed(client, SETTINGS.maceDisableKey);
if (disablePressed && !maceDisableDown) {
SETTINGS.maceEnabled = SETTINGS.maceDisableToggle ? !SETTINGS.maceEnabled : false;
SETTINGS.save();
}
maceDisableDown = disablePressed;
tickCombat(client);
if (client.player == null || client.world == null || client.interactionManager == null
|| client.currentScreen != null || !client.player.isAlive() || client.player.isSpectator()
|| client.player.isUsingItem() || (SETTINGS.skipCreative && client.player.isCreative())
|| !SETTINGS.pearlEnabled) {
pearlStage = 0;
} else {
if (client.player.isOnGround()) fallTriggered = false;
boolean falling = SETTINGS.pearlAutoFall && !fallTriggered && client.player.fallDistance >= SETTINGS.pearlFallDistance;
if (pearlStage == 0 && (pearlEdge || falling)) {
int pearl = -1, wind = -1;
for (int i = 0; i < 9; i++) {
var stack = client.player.getInventory().getStack(i);
if (stack.isOf(Items.ENDER_PEARL)) pearl = i;
if (stack.isOf(Items.WIND_CHARGE)) wind = i;
}
if (pearl >= 0 && (wind >= 0 || !SETTINGS.pearlRequireWind)) {
pearlStage = 1;
pearlDue = System.nanoTime() + ThreadLocalRandom.current().nextLong(50, 151) * 1_000_000L;
if (falling) fallTriggered = true;
}
}
if (pearlStage != 0 && System.nanoTime() >= pearlDue && tick - lastSwap >= SETTINGS.globalCooldown) {
int slot = -1;
for (int i = 0; i < 9; i++) {
var stack = client.player.getInventory().getStack(i);
if (stack.isOf(pearlStage == 1 ? Items.ENDER_PEARL : Items.WIND_CHARGE)) { slot = i; break; }
}
if (slot < 0 || client.player.getItemCooldownManager().isCoolingDown(client.player.getInventory().getStack(slot))) {
pearlStage = 0;
} else {
client.player.getInventory().setSelectedSlot(slot);
client.interactionManager.interactItem(client.player, Hand.MAIN_HAND);
client.player.swingHand(Hand.MAIN_HAND);
lastSwap = tick;
if (pearlStage == 1) {
pearlStage = 2;
int delay = Math.clamp(SETTINGS.pearlWindDelay, 100, 5000);
pearlDue = System.nanoTime() + ThreadLocalRandom.current().nextLong(Math.max(0, delay - 100), delay + 101) * 1_000_000L;
} else pearlStage = 0;
}
}
}
if (client.player != null && client.world != null && client.interactionManager != null
&& client.currentScreen == null && client.player.isAlive() && !client.player.isUsingItem()
&& !client.player.isSpectator() && (!SETTINGS.skipCreative || !client.player.isCreative())
&& SETTINGS.totemEnabled && (!SETTINGS.totemLowHealth || client.player.getHealth() <= SETTINGS.totemHealth)
&& !client.player.getOffHandStack().isOf(Items.TOTEM_OF_UNDYING)
&& client.player.currentScreenHandler == client.player.playerScreenHandler) {
int source = -1;
for (int i = 0; i < 36; i++) {
if (client.player.getInventory().getStack(i).isOf(Items.TOTEM_OF_UNDYING)) {
source = i < 9 ? i + 36 : i;
break;
}
}
if (source < 0) totemDue = -1;
else if (totemDue < 0) totemDue = System.nanoTime() + ThreadLocalRandom.current().nextLong(SETTINGS.totemMinDelay, (long) SETTINGS.totemMaxDelay + 1) * 1_000_000L;
else if (System.nanoTime() >= totemDue && tick - lastSwap >= SETTINGS.globalCooldown) {
client.interactionManager.clickSlot(client.player.currentScreenHandler.syncId, source, 40, SlotActionType.SWAP, client.player);
lastSwap = tick;
totemDue = -1;
}
} else totemDue = -1;
if (!SETTINGS.spearEnabled || client.currentScreen != null || client.player == null || client.world == null
|| !client.player.isAlive() || client.player.isUsingItem()
|| client.player.isSpectator()
|| (SETTINGS.skipCreative && client.player.isCreative())) {
due = -1;
return;
}
if (edge && due < 0) {
due = System.nanoTime() + ThreadLocalRandom.current().nextLong(SETTINGS.minDelay, (long) SETTINGS.maxDelay + 1) * 1_000_000L;
}
if (due < 0 || System.nanoTime() < due || tick - lastSwap < SETTINGS.globalCooldown) return;
due = -1;
for (int slot = 0; slot < 9; slot++) {
var stack = client.player.getInventory().getStack(slot);
var id = Registries.ITEM.getId(stack.getItem());
if (!stack.isEmpty() && id != null && (id.toString().equals(SETTINGS.targetItem)
|| (SETTINGS.targetItem.equals("minecraft:trident") && id.getNamespace().equals("minecraft")
&& id.getPath().endsWith("_spear")))) {
if (client.player.getInventory().getSelectedSlot() != slot) {
client.player.getInventory().setSelectedSlot(slot);
lastSwap = tick;
}
break;
}
}
});
}
private void tickCombat(net.minecraft.client.MinecraftClient client) {
if (client.player == null || client.world == null || client.interactionManager == null
|| client.currentScreen != null || !client.player.isAlive() || client.player.isUsingItem()
|| client.player.isSpectator() || (SETTINGS.skipCreative && client.player.isCreative())) return;
if (!(client.targetedEntity instanceof LivingEntity target) || target instanceof ArmorStandEntity
|| !target.isAlive() || client.player.squaredDistanceTo(target) > SETTINGS.maceRange * SETTINGS.maceRange) return;
if (followupStage == 1) {
if (System.nanoTime() < followupDue) return;
int slot = findHotbar(Items.MACE, client.player);
if (slot < 0) followupStage = 0;
else {
client.player.getInventory().setSelectedSlot(slot);
followupStage = 2;
}
return;
}
if (followupStage == 2) {
if (client.player.getAttackCooldownProgress(0.5F) > 0.9F) {
client.interactionManager.attackEntity(client.player, target);
client.player.swingHand(Hand.MAIN_HAND);
lastSwap = tick;
followupStage = 0;
}
return;
}
boolean attackHeld = org.lwjgl.glfw.GLFW.glfwGetMouseButton(client.getWindow().getHandle(), org.lwjgl.glfw.GLFW.GLFW_MOUSE_BUTTON_1)
== org.lwjgl.glfw.GLFW.GLFW_PRESS;
var heldId = Registries.ITEM.getId(client.player.getMainHandStack().getItem());
boolean spearHeld = heldId != null && (heldId.toString().equals(SETTINGS.targetItem)
|| (SETTINGS.targetItem.equals("minecraft:trident") && heldId.getNamespace().equals("minecraft")
&& heldId.getPath().endsWith("_spear")));
if (SETTINGS.spearToMace && attackHeld && spearHeld && client.player.getAttackCooldownProgress(0.5F) > 0.9F
&& findHotbar(Items.MACE, client.player) >= 0) {
client.interactionManager.attackEntity(client.player, target);
client.player.swingHand(Hand.MAIN_HAND);
followupStage = 1;
followupDue = System.nanoTime() + ThreadLocalRandom.current().nextLong(SETTINGS.minDelay, (long) SETTINGS.maxDelay + 1) * 1_000_000L;
lastSwap = tick;
return;