Files
src
build.gradle
gradle.properties
settings.gradle
Code
8 594
src/client/java/com/kodari/potionautosell/PotionAutoSellClient.java
package com.kodari.potionautosell;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.PotionContentsComponent;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.potion.Potions;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.text.MutableText;
import net.minecraft.text.PlainTextContent;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import org.lwjgl.glfw.GLFW;
public final class PotionAutoSellClient implements ClientModInitializer {
private static final int INVENTORY_SIZE = 36;
private static final int HOTBAR_SIZE = 9;
private static final int HOTBAR_HANDLER_START = 36;
private static final int SALE_TIMEOUT_TICKS = 200;
private static final int SALE_COOLDOWN_TICKS = 10;
private static final int SALES_PER_COLLECTION = 5;
private static final int COLLECTION_DELAY_TICKS = 400;
private static final int AH_COLLECTION_DELAY_TICKS = 80;
private static final int AH_MENU_CLOSE_DELAY_TICKS = 40;
private static final int AH_MENU_TIMEOUT_TICKS = 200;
private static final int AH_SLOT_46_SETTLE_DELAY_TICKS = 20;
private static final int AH_SLOT_ZERO_CLICK_INTERVAL_TICKS = 5;
private static final int AH_SLOT_ZERO_CLICKS = 7;
private static KeyBinding toggleKey;
private static boolean running;
private static Phase phase = Phase.IDLE;
private static int phaseTicks;
private static int cooldownTicks;
private static int destinationSlot = -1;
private static int successfulSales;
private static int ahSlotZeroClicks;
@Override
public void onInitializeClient() {
ZeonClientCommands.register();
toggleKey = KeyBindingHelper.registerKeyBinding(new KeyBinding(
"key.potionautosell.toggle",
GLFW.GLFW_KEY_K,
KeyBinding.Category.create(Identifier.of("potionautosell", "main"))
));
ClientTickEvents.END_CLIENT_TICK.register(PotionAutoSellClient::onClientTick);
}
private static void onClientTick(MinecraftClient client) {
ZeonClientCommands.tick(client);
while (toggleKey.wasPressed()) {
running = !running;
if (client.player != null) {
sendStatus(client.player, running
? "Продажа зелий невидимости запущена."
: "Автопродажа остановлена.");
}
}
if (!running) {
return;
}
ClientPlayerEntity player = client.player;
if (player == null || client.interactionManager == null) {
running = false;
phase = Phase.IDLE;
phaseTicks = 0;
successfulSales = 0;
return;
}
if (phase == Phase.WAITING_FOR_AH_DELAY) {
if (++phaseTicks < COLLECTION_DELAY_TICKS
|| player.currentScreenHandler != player.playerScreenHandler) {
return;
}
player.networkHandler.sendChatCommand("ah");
phase = Phase.WAITING_FOR_AH_MENU;
phaseTicks = 0;
return;
}
if (phase == Phase.WAITING_FOR_AH_MENU) {
if (player.currentScreenHandler != player.playerScreenHandler) {
phase = Phase.WAITING_FOR_COLLECTION_DELAY;
phaseTicks = 0;
return;
}
if (++phaseTicks >= AH_MENU_TIMEOUT_TICKS) {
stop(player, "Меню /ah не открылось за 10 секунд; автопродажа остановлена.");
}
return;
}
if (phase == Phase.WAITING_FOR_COLLECTION_DELAY) {
if (player.currentScreenHandler == player.playerScreenHandler) {
phase = Phase.WAITING_FOR_AH_MENU;
phaseTicks = 0;
return;
}
if (++phaseTicks >= AH_COLLECTION_DELAY_TICKS) {
ScreenHandler handler = player.currentScreenHandler;
if (handler.slots.size() <= 46) {
stop(player, "В меню /ah нет слота 46; автопродажа остановлена.");
return;
}
client.interactionManager.clickSlot(handler.syncId, 46, 0, SlotActionType.PICKUP, player);
ahSlotZeroClicks = 0;
phase = Phase.WAITING_FOR_AH_SLOT_46;
phaseTicks = 0;
}
return;
}
if (phase == Phase.WAITING_FOR_AH_SLOT_46) {
if (player.currentScreenHandler == player.playerScreenHandler) {
stop(player, "Меню /ah закрылось после слота 46; сбор зелий не выполнен.");
return;
}
if (++phaseTicks >= AH_SLOT_46_SETTLE_DELAY_TICKS) {
phase = Phase.CLICKING_AH_SLOT_ZERO;
phaseTicks = 0;
}
return;
}
if (phase == Phase.CLICKING_AH_SLOT_ZERO) {
ScreenHandler handler = player.currentScreenHandler;
if (handler == player.playerScreenHandler) {
stop(player, "Меню /ah закрылось до сбора зелий; автопродажа остановлена.");
return;
}
if (++phaseTicks >= AH_SLOT_ZERO_CLICK_INTERVAL_TICKS) {
if (handler.slots.isEmpty()) {
stop(player, "В меню /ah нет слота 0; автопродажа остановлена.");
return;
}
client.interactionManager.clickSlot(handler.syncId, 0, 0, SlotActionType.PICKUP, player);
ahSlotZeroClicks++;
phaseTicks = 0;
if (ahSlotZeroClicks >= AH_SLOT_ZERO_CLICKS) {
phase = Phase.WAITING_TO_CLOSE_AH_MENU;
}
}
return;
}
if (phase == Phase.WAITING_TO_CLOSE_AH_MENU) {
if (++phaseTicks >= AH_MENU_CLOSE_DELAY_TICKS) {
if (player.currentScreenHandler != player.playerScreenHandler) {
player.closeHandledScreen();
}
successfulSales = 0;
phase = Phase.IDLE;
phaseTicks = 0;
cooldownTicks = SALE_COOLDOWN_TICKS;
}
return;
}
if (player.currentScreenHandler != player.playerScreenHandler) {
return;
}
PlayerInventory inventory = player.getInventory();
if (phase == Phase.PREPARING_SALE) {
if (++phaseTicks < 2) {
return;
}
ItemStack heldStack = inventory.getStack(destinationSlot);
if (heldStack.isEmpty() || heldStack.getCount() != 1 || !isInvisibilityPotion(heldStack)) {
stop(player, "Не удалось подготовить одно зелье для продажи.");
return;
}
player.networkHandler.sendChatCommand("ah sell " + ZeonClientCommands.salePrice());
phase = Phase.WAITING_FOR_SALE;
phaseTicks = 0;
return;
}
if (phase == Phase.WAITING_FOR_SALE) {