Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
7.1 kB
24
Indexable
Never
package com.piggyplugins.WineMaker;
 
import com.example.EthanApiPlugin.Collections.Bank;
import com.example.EthanApiPlugin.Collections.Inventory;
import com.example.EthanApiPlugin.Collections.TileObjects;
import com.example.EthanApiPlugin.Collections.query.TileObjectQuery;
import com.example.EthanApiPlugin.EthanApiPlugin;
import com.example.InteractionApi.BankInteraction;
import com.example.InteractionApi.TileObjectInteraction;
import com.example.PacketUtils.PacketUtilsPlugin;
import com.example.Packets.MousePackets;
import com.example.Packets.MovementPackets;
import com.example.Packets.WidgetPackets;
import com.google.inject.Inject;
import com.google.inject.Provides;
import com.piggyplugins.WineMaker.WineMakerConfig;
import com.piggyplugins.PiggyUtils.PiggyUtilsPlugin;
import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Client;
import net.runelite.api.GameState;
import net.runelite.api.ObjectComposition;
import net.runelite.api.events.GameTick;
import net.runelite.api.widgets.Widget;
import net.runelite.api.widgets.WidgetInfo;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.input.KeyManager;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDependency;
import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.client.util.HotkeyListener;
 
import java.util.Arrays;
 
@PluginDescriptor(
        name = "<html><font color=\"#FF9DF9\">[PP]</font> Wine Maker</html>",
        description = "Automatically banks & combines grapes and jugs of water for you",
        enabledByDefault = false,
        tags = {"hawtin", "wine", "maker"}
)
@PluginDependency(EthanApiPlugin.class)
@PluginDependency(PacketUtilsPlugin.class)
@PluginDependency(PiggyUtilsPlugin.class)
@Slf4j
public class WineMakerPlugin extends Plugin {
    @Inject
    private Client client;
    @Inject
    private MousePackets mousePackets;
    @Inject
    private WidgetPackets widgetPackets;
    @Inject
    private EthanApiPlugin api;
    @Inject
    private WineMakerConfig config;
    @Inject
    private KeyManager keyManager;
    private boolean started;
    private int afkTicks;
    private boolean deposit;
    private boolean isMaking;
 
    @Provides
    private WineMakerConfig getConfig(ConfigManager configManager) {
        return configManager.getConfig(WineMakerConfig.class);
    }
 
    @Override
    protected void startUp() throws Exception {
        keyManager.registerKeyListener(toggle);
    }
 
    @Override
    protected void shutDown() throws Exception {
        keyManager.unregisterKeyListener(toggle);
    }
 
    @Subscribe
    private void onGameTick(GameTick event) {
        if (client.getGameState() != GameState.LOGGED_IN
                || !started
                || EthanApiPlugin.isMoving()
                || client.getLocalPlayer().getAnimation() != -1) {
            afkTicks = 0;
            return;
        }
 
        if (isMaking) {
            if (isDoneMaking()) {
                isMaking = false;
            }
            return;
        }
 
        if (deposit) {
            if (Bank.isOpen()) {
                Widget widget = client.getWidget(WidgetInfo.BANK_DEPOSIT_INVENTORY);
                MousePackets.queueClickPacket();
                WidgetPackets.queueWidgetAction(widget, "Deposit", "Deposit inventory");
                deposit = false;
            } else {
                findBank();
            }
            return;
        }
 
        if (!hasGrapes()) {
            if (!Bank.isOpen()) {
                findBank();
                return;
            }
            withdrawGrapes();
            return;
        }
 
        if (!hasJugsOfWater()) {
            if (!Bank.isOpen()) {
                findBank();
                return;
            }
            withdrawJugsOfWater();
            return;
        }
 
        if (Bank.isOpen()) {
            MousePackets.queueClickPacket();
            MovementPackets.queueMovement(client.getLocalPlayer().getWorldLocation());
            return;
        }
 
        Widget potionWidget = client.getWidget(17694734);
        if (potionWidget != null && !potionWidget.isHidden()) {
            log.info("widget visible");
            MousePackets.queueClickPacket();
            WidgetPackets.queueResumePause(17694734, 14);
            isMaking = true;
            return;
        }
 
        combineGrapesAndWater();
    }
 
    private boolean isDoneMaking() {
        return Inventory.getEmptySlots() == 14;
    }
 
    private void findBank() {
        TileObjects.search()
                .filter(tileObject -> {
                    ObjectComposition objectComposition = TileObjectQuery.getObjectComposition(tileObject);
                    return objectComposition.getName().toLowerCase().contains("bank") ||
                            Arrays.stream(objectComposition.getActions()).anyMatch(action -> action != null && action.toLowerCase().contains("bank"));
                })
                .nearestToPlayer()
                .ifPresent(tileObject -> {
                    TileObjectInteraction.interact(tileObject, "Use", "Bank");
                });
 
        if (!deposit) {
            deposit = true;
        }
    }
 
    private void withdrawGrapes() {
        Bank.search()
                .withName("Grapes")
                .first()
                .ifPresent(item -> BankInteraction.withdrawX(item, 14));
    }
 
    private void withdrawJugsOfWater() {
        Bank.search()
                .withName("Jug of water")
                .first()
                .ifPresent(item -> BankInteraction.withdrawX(item, 14));
    }
 
    private void combineGrapesAndWater() {
        Widget grapes = Inventory.search().filter(item -> item.getName().contains("Grapes")).first().get();
        Widget jugsOfWater = Inventory.search().filter(item -> item.getName().contains("Jug of water")).first().get();
 
        // Check if the inventory is open before combining
        if (Inventory.isOpen()) {
            MousePackets.queueClickPacket();
            MousePackets.queueClickPacket();
            WidgetPackets.queueWidgetOnWidget(grapes, jugsOfWater);
        } else {
            // If inventory is not open, open it before combining
            Inventory.open();
            MousePackets.queueClickPacket();
            MousePackets.queueClickPacket();
            WidgetPackets.queueWidgetOnWidget(grapes, jugsOfWater);
        }
    }
 
    private boolean hasGrapes() {
        return Inventory.search().filter(item -> item.getName().contains("Grapes")).first().isPresent();
    }
 
    private boolean hasJugsOfWater() {
        return Inventory.search().filter(item -> item.getName().contains("Jug of water")).first().isPresent();
    }
 
    private final HotkeyListener toggle = new HotkeyListener(() -> config.toggle()) {
        @Override
        public void hotkeyPressed() {
            toggle();
        }
    };
 
    public void toggle() {
        if (client.getGameState() != GameState.LOGGED_IN) {
            return;
        }
        started = !started;
    }
}