Files
src
build.gradle
gradle.properties
settings.gradle
Code
6 215
src/client/java/com/kodari/afksell/AfkSellClient.java
package com.kodari.afksell;
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.gui.screen.GameMenuScreen;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.screen.GenericContainerScreenHandler;
import net.minecraft.screen.slot.Slot;
import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import org.lwjgl.glfw.GLFW;
public class AfkSellClient implements ClientModInitializer {
private static final int INTERVAL = 20 * 60;
private static final int OPEN_TIMEOUT = 20 * 5;
private static final int CLICK_DELAY = 4;
private KeyBinding toggle;
private boolean enabled;
private int cooldown;
private int waitTicks;
private int nextSlot;
private GenericContainerScreenHandler activeHandler;
private boolean waitingForMenu;
private boolean restorePauseMenu;
@Override
public void onInitializeClient() {
KeyBinding.Category category = KeyBinding.Category.create(Identifier.of("afksell", "controls"));
toggle = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.afksell.toggle", GLFW.GLFW_KEY_V, category));
ClientTickEvents.END_CLIENT_TICK.register(this::tick);
}
private void tick(MinecraftClient client) {
while (toggle.wasPressed()) {
enabled = !enabled;
waitingForMenu = false;
activeHandler = null;
restorePauseMenu = false;
cooldown = 0;
if (client.player != null) {
client.player.sendMessage(Text.literal("AFK Sell: " + (enabled ? "enabled" : "disabled")), false);
}
}
if (client.player == null || client.interactionManager == null) {
enabled = false;
waitingForMenu = false;
activeHandler = null;
restorePauseMenu = false;
return;
}
if (!enabled) return;
if (activeHandler != null) {
if (!(client.currentScreen instanceof HandledScreen<?> screen)
|| screen.getScreenHandler() != activeHandler
|| client.player.currentScreenHandler != activeHandler) {
activeHandler = null;
restorePauseMenu = false;
cooldown = INTERVAL;
return;
}
if (!activeHandler.getCursorStack().isEmpty()) {
enabled = false;
activeHandler = null;
restorePauseMenu = false;
client.player.sendMessage(Text.literal("AFK Sell stopped: an item is on the cursor."), false);
return;
}
if (waitTicks-- > 0) return;
while (nextSlot < activeHandler.slots.size()) {
Slot slot = activeHandler.slots.get(nextSlot++);
if (slot.inventory == client.player.getInventory() && slot.hasStack()) {
client.interactionManager.clickSlot(activeHandler.syncId, slot.id, 0, SlotActionType.QUICK_MOVE, client.player);
waitTicks = CLICK_DELAY;
return;
}
}
client.player.closeHandledScreen();
activeHandler = null;
if (restorePauseMenu && client.currentScreen == null) {
client.setScreen(new GameMenuScreen(true));
}
restorePauseMenu = false;
cooldown = INTERVAL;
return;
}
if (waitingForMenu) {
if (client.currentScreen != null && !(client.currentScreen instanceof GameMenuScreen)) {
waitingForMenu = false;
if (client.currentScreen instanceof HandledScreen<?> screen
&& screen.getScreenHandler() instanceof GenericContainerScreenHandler handler
&& client.player.currentScreenHandler == handler
&& handler.getCursorStack().isEmpty()) {
activeHandler = handler;
nextSlot = handler.getRows() * 9;
waitTicks = 10;
} else {
enabled = false;
restorePauseMenu = false;
client.player.sendMessage(Text.literal("AFK Sell stopped: the opened screen is not a compatible container."), false);
}
} else if (--waitTicks <= 0) {
waitingForMenu = false;
enabled = false;
restorePauseMenu = false;
client.player.sendMessage(Text.literal("AFK Sell stopped: /sellgui did not open a container."), false);
}
return;
}
if (cooldown > 0) {
cooldown--;
return;
}
if (client.isPaused() || (client.currentScreen != null && !(client.currentScreen instanceof GameMenuScreen))
|| client.player.currentScreenHandler != client.player.playerScreenHandler) return;
restorePauseMenu = client.currentScreen instanceof GameMenuScreen;
client.player.networkHandler.sendChatCommand("sellgui");
waitingForMenu = true;
waitTicks = OPEN_TIMEOUT;
}
}