| 1 | package com.jellypudding.offlineclient.modules.world;
|
| 2 |
|
| 3 | import com.jellypudding.offlineclient.event.Subscribe;
|
| 4 | import com.jellypudding.offlineclient.event.events.TickEvent;
|
| 5 | import com.jellypudding.offlineclient.module.Category;
|
| 6 | import com.jellypudding.offlineclient.module.Module;
|
| 7 | import com.jellypudding.offlineclient.setting.EnumSetting;
|
| 8 | import com.jellypudding.offlineclient.setting.RegistryListSetting;
|
| 9 | import com.jellypudding.offlineclient.util.ChatUtil;
|
| 10 | import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
|
| 11 | import net.minecraft.core.Holder;
|
| 12 | import net.minecraft.core.component.DataComponents;
|
| 13 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 14 | import net.minecraft.world.inventory.BrewingStandMenu;
|
| 15 | import net.minecraft.world.inventory.ContainerInput;
|
| 16 | import net.minecraft.world.item.Item;
|
| 17 | import net.minecraft.world.item.ItemStack;
|
| 18 | import net.minecraft.world.item.Items;
|
| 19 | import net.minecraft.world.item.alchemy.Potion;
|
| 20 | import net.minecraft.world.item.alchemy.PotionBrewing;
|
| 21 | import net.minecraft.world.item.alchemy.PotionContents;
|
| 22 | import net.minecraft.world.item.alchemy.Potions;
|
| 23 |
|
| 24 | import java.util.ArrayDeque;
|
| 25 | import java.util.ArrayList;
|
| 26 | import java.util.Deque;
|
| 27 | import java.util.HashSet;
|
| 28 | import java.util.List;
|
| 29 | import java.util.Set;
|
| 30 | import java.util.function.Predicate;
|
| 31 |
|
| 32 | // Brews the chosen potion whilst a brewing stand is open using the game's own
|
| 33 | // brewing rules. Any potion a server allows can be made.
|
| 34 | public final class AutoBrewer extends Module {
|
| 35 |
|
| 36 | public enum Form { DRINKABLE, SPLASH, LINGERING }
|
| 37 |
|
| 38 | // Stand slots. Everything after these is the player's inventory.
|
| 39 | private static final int BOTTLE_SLOTS = 3;
|
| 40 | private static final int INGREDIENT_SLOT = 3;
|
| 41 | private static final int FUEL_SLOT = 4;
|
| 42 | private static final int FIRST_PLAYER_SLOT = 5;
|
| 43 |
|
| 44 | // Ticks between two actions. The server gets time to answer the last one.
|
| 45 | private static final int SETTLE_TICKS = 5;
|
| 46 |
|
| 47 | // Water goes through at most three ingredients and then the two forms.
|
| 48 | private static final int MAX_STEPS = 6;
|
| 49 |
|
| 50 | private enum Stage { TAKE, FILL, BREW }
|
| 51 |
|
| 52 | private final RegistryListSetting<Potion> potion = new RegistryListSetting<>("Potion",
|
| 53 | "The potion to brew. Only the first one picked is used.",
|
| 54 | BuiltInRegistries.POTION, List.of(Potions.STRENGTH.value()));
|
| 55 | private final EnumSetting<Form> form = new EnumSetting<>("Form",
|
| 56 | "What the finished bottle turns into.", Form.DRINKABLE)
|
| 57 | .describe(Form.DRINKABLE, "A plain bottle to drink.")
|
| 58 | .describe(Form.SPLASH, "Gunpowder goes in last for a throwable bottle.")
|
| 59 | .describe(Form.LINGERING, "Dragon breath follows the gunpowder for a cloud bottle.");
|
| 60 |
|
| 61 | private Stage stage;
|
| 62 | private List<Item> plan;
|
| 63 | private int step;
|
| 64 | private int settle;
|
| 65 | private int brewed;
|
| 66 |
|
| 67 | public AutoBrewer() {
|
| 68 | super("AutoBrewer", "Brews the potion you pick whilst a brewing stand is open.", Category.WORLD);
|
| 69 | addSettings(potion, form);
|
| 70 | searchTags("potion", "brewing stand");
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | public String getSuffix() {
|
| 75 | return brewed == 0 ? null : brewed + " brewed";
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | protected void onEnable() {
|
| 80 | stage = null;
|
| 81 | plan = null;
|
| 82 | brewed = 0;
|
| 83 | }
|
| 84 |
|
| 85 | @Subscribe
|
| 86 | private void onTick(TickEvent event) {
|
| 87 | if (!(mc.gui.screen() instanceof AbstractContainerScreen<?> screen)
|
| 88 | || !(screen.getMenu() instanceof BrewingStandMenu stand)) {
|
| 89 | stage = null;
|
| 90 | return;
|
| 91 | }
|
| 92 | if (stage == null) {
|
| 93 | stage = Stage.TAKE;
|
| 94 | step = 0;
|
| 95 | settle = 0;
|
| 96 | plan = buildPlan();
|
| 97 | if (plan == null) {
|
| 98 | ChatUtil.error("That potion cannot be brewed from water on this server.");
|
| 99 | setEnabled(false);
|
| 100 | return;
|
| 101 | }
|
| 102 | }
|
| 103 | if (settle > 0) {
|
| 104 | settle--;
|
| 105 | return;
|
| 106 | }
|
| 107 | if (stand.getBrewingTicks() != 0) {
|
| 108 | return;
|
| 109 | }
|
| 110 | switch (stage) {
|
| 111 | case TAKE -> takeBottles(stand);
|
| 112 | case FILL -> fillWater(stand);
|
| 113 | case BREW -> brew(stand);
|
| 114 | }
|
| 115 | settle = SETTLE_TICKS;
|
| 116 | }
|
| 117 |
|
| 118 | // Whatever sits in the bottle slots goes to the inventory first.
|
| 119 | private void takeBottles(BrewingStandMenu stand) {
|
| 120 | for (int i = 0; i < BOTTLE_SLOTS; i++) {
|
| 121 | if (stand.slots.get(i).getItem().isEmpty()) {
|
| 122 | continue;
|
| 123 | }
|
| 124 | if (!isWaterBottle(stand.slots.get(i).getItem())) {
|
| 125 | brewed++;
|
| 126 | }
|
| 127 | quickMove(stand, i);
|
| 128 | if (!stand.slots.get(i).getItem().isEmpty()) {
|
| 129 | ChatUtil.error("No room in the inventory for the finished potions.");
|
| 130 | setEnabled(false);
|
| 131 | return;
|
| 132 | }
|
| 133 | }
|
| 134 | stage = Stage.FILL;
|
| 135 | }
|
| 136 |
|
| 137 | private void fillWater(BrewingStandMenu stand) {
|
| 138 | for (int i = 0; i < BOTTLE_SLOTS; i++) {
|
| 139 | if (!stand.slots.get(i).getItem().isEmpty()) {
|
| 140 | continue;
|
| 141 | }
|
| 142 | int slot = findPlayerSlot(stand, AutoBrewer::isWaterBottle);
|
| 143 | if (slot == -1) {
|
| 144 | ChatUtil.error("Out of water bottles.");
|
| 145 | setEnabled(false);
|
| 146 | return;
|
| 147 | }
|
| 148 | moveOne(stand, slot, i);
|
| 149 | }
|
| 150 | step = 0;
|
| 151 | stage = Stage.BREW;
|
| 152 | }
|
| 153 |
|
| 154 | private void brew(BrewingStandMenu stand) {
|
| 155 | // The leftover of the last ingredient goes home before the next one.
|
| 156 | if (!stand.slots.get(INGREDIENT_SLOT).getItem().isEmpty()) {
|
| 157 | quickMove(stand, INGREDIENT_SLOT);
|
| 158 | if (!stand.slots.get(INGREDIENT_SLOT).getItem().isEmpty()) {
|
| 159 | ChatUtil.error("No room in the inventory for the leftover ingredient.");
|
| 160 | setEnabled(false);
|
| 161 | }
|
| 162 | return;
|
| 163 | }
|
| 164 | if (step >= plan.size()) {
|
| 165 | stage = Stage.TAKE;
|
| 166 | return;
|
| 167 | }
|
| 168 | if (stand.getFuel() == 0) {
|
| 169 | int fuel = findPlayerSlot(stand, stack -> stack.is(Items.BLAZE_POWDER));
|
| 170 | if (fuel == -1) {
|
| 171 | ChatUtil.error("Out of blaze powder.");
|
| 172 | setEnabled(false);
|
| 173 | return;
|
| 174 | }
|
| 175 | moveOne(stand, fuel, FUEL_SLOT);
|
| 176 | return;
|
| 177 | }
|
| 178 | Item ingredient = plan.get(step);
|
| 179 | int slot = findPlayerSlot(stand, stack -> stack.is(ingredient));
|
| 180 | if (slot == -1) {
|
| 181 | ChatUtil.error("Out of " + ingredient.getName(ingredient.getDefaultInstance()).getString() + ".");
|
| 182 | setEnabled(false);
|
| 183 | return;
|
| 184 | }
|
| 185 | moveOne(stand, slot, INGREDIENT_SLOT);
|
| 186 | step++;
|
| 187 | }
|
| 188 |
|
| 189 | // Breadth first from a water bottle through every ingredient the brewing rules
|
| 190 | // accept until the wanted potion appears. Null when no chain leads there.
|
| 191 | private List<Item> buildPlan() {
|
| 192 | Potion wanted = potion.resolved().stream().findFirst().orElse(null);
|
| 193 | if (wanted == null || mc.level == null) {
|
| 194 | return null;
|
| 195 | }
|
| 196 | PotionBrewing brewing = mc.level.potionBrewing();
|
| 197 | List<Item> ingredients = new ArrayList<>();
|
| 198 | for (Item item : BuiltInRegistries.ITEM) {
|
| 199 | if (brewing.isPotionIngredient(item.getDefaultInstance())) {
|
| 200 | ingredients.add(item);
|
| 201 | }
|
| 202 | }
|
| 203 |
|
| 204 | record Path(ItemStack potion, List<Item> steps) {
|
| 205 | }
|
| 206 | Deque<Path> queue = new ArrayDeque<>();
|
| 207 | Set<Holder<Potion>> seen = new HashSet<>();
|
| 208 | queue.add(new Path(PotionContents.createItemStack(Items.POTION, Potions.WATER), List.of()));
|
| 209 | seen.add(Potions.WATER);
|
| 210 | while (!queue.isEmpty()) {
|
| 211 | Path path = queue.poll();
|
| 212 | if (contentsOf(path.potion()).is(BuiltInRegistries.POTION.wrapAsHolder(wanted))) {
|
| 213 | return withForm(path.steps());
|
| 214 | }
|
| 215 | if (path.steps().size() >= MAX_STEPS) {
|
| 216 | continue;
|
| 217 | }
|
| 218 | for (Item ingredient : ingredients) {
|
| 219 | ItemStack stack = ingredient.getDefaultInstance();
|
| 220 | if (!brewing.hasPotionMix(path.potion(), stack)) {
|
| 221 | continue;
|
| 222 | }
|
| 223 | ItemStack next = brewing.mix(stack, path.potion());
|
| 224 | Holder<Potion> result = contentsOf(next).potion().orElse(null);
|
| 225 | if (result == null || !seen.add(result)) {
|
| 226 | continue;
|
| 227 | }
|
| 228 | List<Item> steps = new ArrayList<>(path.steps());
|
| 229 | steps.add(ingredient);
|
| 230 | queue.add(new Path(next, steps));
|
| 231 | }
|
| 232 | }
|
| 233 | return null;
|
| 234 | }
|
| 235 |
|
| 236 | private List<Item> withForm(List<Item> steps) {
|
| 237 | List<Item> result = new ArrayList<>(steps);
|
| 238 | if (form.isAny(Form.SPLASH, Form.LINGERING)) {
|
| 239 | result.add(Items.GUNPOWDER);
|
| 240 | }
|
| 241 | if (form.is(Form.LINGERING)) {
|
| 242 | result.add(Items.DRAGON_BREATH);
|
| 243 | }
|
| 244 | return result;
|
| 245 | }
|
| 246 |
|
| 247 | private static PotionContents contentsOf(ItemStack stack) {
|
| 248 | return stack.getOrDefault(DataComponents.POTION_CONTENTS, PotionContents.EMPTY);
|
| 249 | }
|
| 250 |
|
| 251 | private static boolean isWaterBottle(ItemStack stack) {
|
| 252 | return stack.is(Items.POTION) && contentsOf(stack).is(Potions.WATER);
|
| 253 | }
|
| 254 |
|
| 255 | private static int findPlayerSlot(BrewingStandMenu stand, Predicate<ItemStack> test) {
|
| 256 | for (int i = FIRST_PLAYER_SLOT; i < stand.slots.size(); i++) {
|
| 257 | if (test.test(stand.slots.get(i).getItem())) {
|
| 258 | return i;
|
| 259 | }
|
| 260 | }
|
| 261 | return -1;
|
| 262 | }
|
| 263 |
|
| 264 | private void quickMove(BrewingStandMenu stand, int slot) {
|
| 265 | mc.gameMode.handleContainerInput(stand.containerId, slot, 0, ContainerInput.QUICK_MOVE, mc.player);
|
| 266 | }
|
| 267 |
|
| 268 | // Picks the stack up and drops a single item into the target with a right click.
|
| 269 | private void moveOne(BrewingStandMenu stand, int from, int to) {
|
| 270 | if (!stand.getCarried().isEmpty()) {
|
| 271 | return;
|
| 272 | }
|
| 273 | mc.gameMode.handleContainerInput(stand.containerId, from, 0, ContainerInput.PICKUP, mc.player);
|
| 274 | mc.gameMode.handleContainerInput(stand.containerId, to, 1, ContainerInput.PICKUP, mc.player);
|
| 275 | if (!stand.getCarried().isEmpty()) {
|
| 276 | mc.gameMode.handleContainerInput(stand.containerId, from, 0, ContainerInput.PICKUP, mc.player);
|
| 277 | }
|
| 278 | }
|
| 279 | }
|
| 280 |
|