aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/chrisoft/trashyaddon/modules/AutoTrade.java
blob: a062b380c0fdc2783c0cc9fe34827aae9a1644b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package org.chrisoft.trashyaddon.modules;

import meteordevelopment.meteorclient.events.game.OpenScreenEvent;
import meteordevelopment.meteorclient.events.packets.PacketEvent;
import meteordevelopment.meteorclient.events.world.TickEvent;
import meteordevelopment.meteorclient.settings.*;
import meteordevelopment.meteorclient.systems.modules.Categories;
import meteordevelopment.meteorclient.systems.modules.Module;
import meteordevelopment.meteorclient.utils.player.FindItemResult;
import meteordevelopment.meteorclient.utils.player.InvUtils;
import meteordevelopment.orbit.EventHandler;
import net.minecraft.client.gui.screen.ingame.MerchantScreen;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.network.packet.s2c.play.ScreenHandlerSlotUpdateS2CPacket;
import net.minecraft.network.packet.s2c.play.SetTradeOffersS2CPacket;
import net.minecraft.village.TradeOffer;
import net.minecraft.village.TradeOfferList;
import org.chrisoft.trashyaddon.mixin.MerchantScreenAccessor;

import java.util.*;

public class AutoTrade extends Module {
    private final SettingGroup sgGeneral = settings.getDefaultGroup();
    public static final List<Item> allSellItems = List.of(
        Items.COAL,
        Items.CHICKEN,
        Items.PORKCHOP,
        Items.RABBIT,
        Items.MUTTON,
        Items.BEEF,
        Items.DRIED_KELP_BLOCK,
        Items.SWEET_BERRIES,
        Items.IRON_INGOT,
        Items.DIAMOND,
        Items.PAPER,
        Items.GLASS_PANE,
        Items.ROTTEN_FLESH,
        Items.GOLD_INGOT,
        Items.RABBIT_FOOT,
        Items.TURTLE_SCUTE,
        Items.GLASS_BOTTLE,
        Items.NETHER_WART,
        Items.WHEAT,
        Items.POTATO,
        Items.CARROT,
        Items.BEETROOT,
        Items.PUMPKIN,
        Items.MELON,
        Items.STRING,
        Items.COD,
        Items.SALMON,
        Items.TROPICAL_FISH,
        Items.PUFFERFISH,
        Items.STICK,
        Items.FLINT,
        Items.FEATHER,
        Items.TRIPWIRE_HOOK,
        Items.LEATHER,
        Items.RABBIT_HIDE,
        Items.BOOK,
        Items.INK_SAC,
        Items.CLAY_BALL,
        Items.STONE,
        Items.GRANITE,
        Items.ANDESITE,
        Items.DIORITE,
        Items.QUARTZ
    );

    public static final List<Item> allBuyItems = List.of(
        Items.BELL,
        Items.COOKED_PORKCHOP,
        Items.COOKED_CHICKEN,
        Items.MAP,
        Items.ITEM_FRAME,
        Items.REDSTONE,
        Items.LAPIS_LAZULI,
        Items.GLOWSTONE,
        Items.ENDER_PEARL,
        Items.EXPERIENCE_BOTTLE,
        Items.BREAD,
        Items.PUMPKIN_PIE,
        Items.APPLE,
        Items.COOKIE,
        Items.GOLDEN_CARROT,
        Items.GLISTERING_MELON_SLICE,
        Items.CAMPFIRE,
        Items.ARROW,
        Items.BOOKSHELF,
        Items.LANTERN,
        Items.GLASS,
        Items.CLOCK,
        Items.COMPASS,
        Items.NAME_TAG,
        Items.BRICK,
        Items.CHISELED_STONE_BRICKS,
        Items.DRIPSTONE_BLOCK,
        Items.POLISHED_ANDESITE,
        Items.POLISHED_DIORITE,
        Items.POLISHED_GRANITE,
        Items.QUARTZ_PILLAR,
        Items.QUARTZ_BLOCK,
        Items.PAINTING
    );

    private final Setting<Boolean> sellingEnabled = sgGeneral.add(new BoolSetting.Builder()
        .name("Enable Selling")
        .build()
    );
    private final Setting<List<Item>> sellsSetting = sgGeneral.add(new ItemListSetting.Builder()
        .name("Sells")
        .description("Items to automatically SELL TO villagers.")
        .filter((item) -> allSellItems.contains(item))
        .build()
    );
    private final Setting<Boolean> buyingEnabled = sgGeneral.add(new BoolSetting.Builder()
        .name("Enable Buying")
        .build()
    );
    private final Setting<List<Item>> buysSetting = sgGeneral.add(new ItemListSetting.Builder()
        .name("Buys")
        .description("Items to automatically BUY FROM villagers.")
        .filter((item) -> allBuyItems.contains(item))
        .build()
    );
    private  final Setting<AcceptablePrices> acceptablePricesSetting = sgGeneral.add(new GenericSetting.Builder<AcceptablePrices>()
        .name("acceptable-prices")
        .description("Configure maximum acceptable price for each item.")
        .defaultValue(new AcceptablePrices(new HashMap<>()))
        .build()
    );
    private final Setting<Integer> interactionRate = sgGeneral.add(new IntSetting.Builder()
        .name("Interaction Rate")
        .description("Number of ticks between interactions.")
        .min(0)
        .max(20)
        .build()
    );
    private final Setting<Boolean> autoClose = sgGeneral.add(new BoolSetting.Builder()
        .name("Auto Close")
        .description("Close trading screen after any successful trades.")
        .build()
    );
    private final Setting<Boolean> logSummary = sgGeneral.add(new BoolSetting.Builder()
        .name("Log Summary")
        .description("Give a summary of what has been traded once trading is complete.")
        .build()
    );

    private TradeOfferList offers;
    private MerchantScreen screen;
    private int currentOffer;
    private int ticksRemaining;
    private int countBefore;
    private int countAfter;

    private enum WaitState {
        None,
        WaitingForOutput,
        WaitingForInventoryUpdate,
        WaitingForFinalize
    };
    private WaitState waitState;
    private ArrayList<ItemStack> itemsSold;
    private ArrayList<ItemStack> itemsBought;


    public AutoTrade() {
        super(Categories.World, "Auto Trade", "Help prevent getting arthritis from excessively trading with villagers.");
        itemsSold = new ArrayList<>();
        itemsBought = new ArrayList<>();
        waitState = WaitState.None;
    }

    /*
    @Override
    public String getInfoString() {
        return waitState.toString() + " " + ticksRemaining;
    }
     */

    private boolean isOfferEligible(TradeOffer o) {
        // I don't give a SHIT to trades that use the second slot
        // well actually I do, but I don't need THAT many enchanted books...
        if (o.getSecondBuyItem().isPresent() && !o.getSecondBuyItem().get().equals(Items.AIR))
            return false;
        if (o.isDisabled())
            return false;
        ItemStack mbuy = o.getDisplayedFirstBuyItem();
        ItemStack msell = o.getSellItem();
        List<Item> sells = sellingEnabled.get() ? this.sellsSetting.get() : List.of();
        List<Item> buys = buyingEnabled.get() ? this.buysSetting.get() : List.of();
        Item interestedItem = null;
        if (sells.contains(mbuy.getItem()))
            interestedItem = mbuy.getItem();
        if (buys.contains(msell.getItem()))
            interestedItem = msell.getItem();
        if (interestedItem == null)
            return false;
        Integer maxPrice = acceptablePricesSetting.get().getMaxPriceForItem(interestedItem);
        if (maxPrice != null && mbuy.getCount() > maxPrice) {
            return false;
        }
        FindItemResult rs = InvUtils.find((stack) -> stack.getItem().equals(mbuy.getItem()) && stack.getCount() >= mbuy.getCount());
        ItemStack s0is = screen.getScreenHandler().slots.get(0).getStack();
        boolean remainingFirstSlotSufficient = s0is.getItem().equals(mbuy.getItem()) && s0is.getCount() >= mbuy.getCount();
        if (!rs.found() && !remainingFirstSlotSufficient)
            return false;
        return true;
    }

    private void selectTrade() {
        while (currentOffer < offers.size() && !isOfferEligible(offers.get(currentOffer)))
            ++currentOffer;
        if (currentOffer >= offers.size()) {
            ticksRemaining = interactionRate.get();
            waitState = WaitState.WaitingForFinalize;
            return;
        }
        waitState = WaitState.WaitingForOutput;
        ticksRemaining = -0xdead;
        TradeOffer o = offers.get(currentOffer);
        Item tradedItem = o.getSellItem().getItem().equals(Items.EMERALD) ?
            o.getDisplayedFirstBuyItem().getItem() : o.getSellItem().getItem();
        countBefore = screen.getScreenHandler().slots.stream()
            .map(slot -> slot.getStack())
            .filter(s -> s.getItem().equals(tradedItem))
            .map(s -> s.getCount())
            .reduce(0, Integer::sum);
        ((MerchantScreenAccessor)screen).setSelectedIndex(currentOffer);
        ((MerchantScreenAccessor)screen).invokeSyncRecipeIndex();
    }

    private void finalizeCurrentTrade() {
        TradeOffer o = offers.get(currentOffer);
        Item tradedItem = o.getSellItem().getItem().equals(Items.EMERALD) ?
            o.getDisplayedFirstBuyItem().getItem() : o.getSellItem().getItem();
        countAfter = screen.getScreenHandler().slots.stream()
            .map(slot -> slot.getStack())
            .filter(s -> s.getItem().equals(tradedItem))
            .map(s -> s.getCount())
            .reduce(0, Integer::sum);
        ArrayList<ItemStack> itemsTraded = countAfter < countBefore ? itemsSold : itemsBought;
        int count = java.lang.Math.abs(countAfter - countBefore);
        Optional<ItemStack> t = itemsTraded.stream().filter(s -> s.getItem().equals(tradedItem)).findFirst();
        if (t.isPresent()) {
            t.get().setCount(t.get().getCount() + count);
        }
        else {
            ItemStack s = new ItemStack(tradedItem, count);
            itemsTraded.add(s);
        }
    }

    private void endTrading() {
        if (logSummary.get()) {
            if (!itemsSold.isEmpty()) {
                Formatter f = new Formatter();
                f.format("Item(s) sold:");
                boolean first = true;
                for (ItemStack i : itemsSold) {
                    f.format("%s%s*%d", first ? " " : ", ", i.getName().getString(), i.getCount());
                    first = false;
                }
                info(f.toString());
                f.close();
            }
            if (!itemsBought.isEmpty()) {
                Formatter f = new Formatter();
                f.format("Item(s) bought:");
                boolean first = true;
                for (ItemStack i : itemsBought) {
                    f.format("%s%s*%d", first ? " " : ", ", i.getName().getString(), i.getCount());
                    first = false;
                }
                info(f.toString());
                f.close();
            }
        }
        if (autoClose.get() && (!itemsBought.isEmpty() || !itemsSold.isEmpty()))
            screen.close();
        screen = null;
    }
    @EventHandler
    private void onTick(TickEvent.Pre event) {
        if (ticksRemaining > 0) {
            --ticksRemaining;
            return;
        }
        if (offers != null && screen != null) {
            if (waitState == WaitState.WaitingForFinalize) {
                endTrading();
                return;
            }
            if (waitState != WaitState.None && currentOffer < offers.size() && (offers.get(currentOffer).isDisabled() ||
                screen.getScreenHandler().slots.get(0).getStack().getCount() < offers.get(currentOffer).getDisplayedFirstBuyItem().getCount())) {
                // the WaitingForInventoryUpdate state is mostly useless because ScreenHandlerSlotUpdateS2CPacket
                // isn't sent after the shift-click...
                finalizeCurrentTrade();
                waitState = WaitState.None;
                ticksRemaining = interactionRate.get();
                return;
            }
            if (ticksRemaining == -0xdead)
                return;
            switch (waitState) {
                case None: selectTrade(); break;
                case WaitingForOutput:
                    InvUtils.shiftClick().slotId(2);
                    waitState = WaitState.WaitingForInventoryUpdate;
                    ticksRemaining = -0xdead;
                break;
                case WaitingForInventoryUpdate:
                    finalizeCurrentTrade();
                    waitState = WaitState.None;
                    ticksRemaining = interactionRate.get();
                break;
            }
        }
    }

    @EventHandler
    private void onReceivePacket(PacketEvent.Receive e) {
        if (e.packet instanceof SetTradeOffersS2CPacket p) {
            this.offers = p.getOffers();
            currentOffer = 0;
            waitState = WaitState.None;
            ticksRemaining = 0;
            itemsSold.clear();
            itemsBought.clear();
        } else if (e.packet instanceof ScreenHandlerSlotUpdateS2CPacket p) {
            if (screen == null || p.getSyncId() != screen.getScreenHandler().syncId || p.getSlot() != 2)
                return;
            if (waitState == WaitState.WaitingForOutput) {
                ticksRemaining = interactionRate.get();
            }
            if (waitState == WaitState.WaitingForInventoryUpdate) {
                if (!p.getStack().isEmpty())
                    waitState = WaitState.WaitingForOutput;
                ticksRemaining = interactionRate.get();
            }
        }
    }

    @EventHandler
    private void onOpenScreen(OpenScreenEvent e) {
        if (e.screen == null) {
            this.screen = null;
            this.offers = null;
        }
        else if (e.screen instanceof MerchantScreen s) {
            this.screen = s;
        }
    }
}