| 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.BoolSetting;
|
| 8 | import com.jellypudding.offlineclient.setting.NumberSetting;
|
| 9 | import com.jellypudding.offlineclient.setting.RegistryListSetting;
|
| 10 | import com.jellypudding.offlineclient.util.ChatUtil;
|
| 11 | import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
|
| 12 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 13 | import net.minecraft.resources.ResourceKey;
|
| 14 | import net.minecraft.world.inventory.AbstractFurnaceMenu;
|
| 15 | import net.minecraft.world.inventory.BlastFurnaceMenu;
|
| 16 | import net.minecraft.world.inventory.ContainerInput;
|
| 17 | import net.minecraft.world.inventory.SmokerMenu;
|
| 18 | import net.minecraft.world.item.Item;
|
| 19 | import net.minecraft.world.item.ItemStack;
|
| 20 | import net.minecraft.world.item.Items;
|
| 21 | import net.minecraft.world.item.crafting.RecipePropertySet;
|
| 22 |
|
| 23 | import java.util.List;
|
| 24 | import java.util.function.Predicate;
|
| 25 |
|
| 26 | // Keeps a furnace fed whilst its screen is open and takes what comes out.
|
| 27 | public final class AutoSmelter extends Module {
|
| 28 |
|
| 29 | private static final int INPUT_SLOT = 0;
|
| 30 | private static final int FUEL_SLOT = 1;
|
| 31 | private static final int OUTPUT_SLOT = 2;
|
| 32 | private static final int FIRST_PLAYER_SLOT = 3;
|
| 33 |
|
| 34 | // Ticks between two rounds of clicks.
|
| 35 | private static final int INTERVAL = 10;
|
| 36 |
|
| 37 | private final RegistryListSetting<Item> fuels = new RegistryListSetting<>("Fuel",
|
| 38 | "Items that may be burnt.", BuiltInRegistries.ITEM,
|
| 39 | List.of(Items.COAL, Items.CHARCOAL, Items.COAL_BLOCK));
|
| 40 | private final NumberSetting fuelPerRefill = new NumberSetting("Fuel per refill",
|
| 41 | "How many fuel items go in each time the fire dies.", 8, 1, 64, 1).max(64);
|
| 42 | private final RegistryListSetting<Item> inputs = new RegistryListSetting<>("Smelt",
|
| 43 | "Items to smelt.", BuiltInRegistries.ITEM,
|
| 44 | List.of(Items.RAW_IRON, Items.RAW_COPPER, Items.RAW_GOLD, Items.IRON_ORE,
|
| 45 | Items.GOLD_ORE, Items.COPPER_ORE, Items.SAND, Items.COBBLESTONE));
|
| 46 | private final BoolSetting stopWhenEmpty = new BoolSetting("Stop when empty",
|
| 47 | "Turns off once you have nothing left to smelt or burn.", true);
|
| 48 | private final BoolSetting closeScreen = new BoolSetting("Close screen",
|
| 49 | "Closes the furnace after every round to let you carry on.", false);
|
| 50 |
|
| 51 | private int taken;
|
| 52 |
|
| 53 | public AutoSmelter() {
|
| 54 | super("AutoSmelter", "Feeds an open furnace from your inventory and empties it.", Category.WORLD);
|
| 55 | addSettings(fuels, fuelPerRefill, inputs, stopWhenEmpty, closeScreen);
|
| 56 | searchTags("furnace", "smelt", "blast furnace", "smoker");
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | public String getSuffix() {
|
| 61 | return taken == 0 ? null : taken + " taken";
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | protected void onEnable() {
|
| 66 | taken = 0;
|
| 67 | }
|
| 68 |
|
| 69 | @Subscribe
|
| 70 | private void onTick(TickEvent event) {
|
| 71 | if (!(mc.gui.screen() instanceof AbstractContainerScreen<?> screen)
|
| 72 | || !(screen.getMenu() instanceof AbstractFurnaceMenu furnace)) {
|
| 73 | return;
|
| 74 | }
|
| 75 | if (mc.player.tickCount % INTERVAL != 0 || !furnace.getCarried().isEmpty()) {
|
| 76 | return;
|
| 77 | }
|
| 78 | takeOutput(furnace);
|
| 79 | if (!isEnabled()) {
|
| 80 | return;
|
| 81 | }
|
| 82 | refuel(furnace);
|
| 83 | if (!isEnabled()) {
|
| 84 | return;
|
| 85 | }
|
| 86 | feed(furnace);
|
| 87 | if (isEnabled() && closeScreen.isOn()) {
|
| 88 | mc.player.closeContainer();
|
| 89 | }
|
| 90 | }
|
| 91 |
|
| 92 | private void takeOutput(AbstractFurnaceMenu furnace) {
|
| 93 | ItemStack output = furnace.slots.get(OUTPUT_SLOT).getItem();
|
| 94 | if (output.isEmpty()) {
|
| 95 | return;
|
| 96 | }
|
| 97 | int count = output.getCount();
|
| 98 | quickMove(furnace, OUTPUT_SLOT);
|
| 99 | if (!furnace.slots.get(OUTPUT_SLOT).getItem().isEmpty()) {
|
| 100 | ChatUtil.error("No room in the inventory for what came out.");
|
| 101 | setEnabled(false);
|
| 102 | return;
|
| 103 | }
|
| 104 | taken += count;
|
| 105 | }
|
| 106 |
|
| 107 | private void refuel(AbstractFurnaceMenu furnace) {
|
| 108 | if (furnace.getLitProgress() > 0 || !furnace.slots.get(FUEL_SLOT).getItem().isEmpty()) {
|
| 109 | return;
|
| 110 | }
|
| 111 | int slot = findPlayerSlot(furnace, stack -> fuels.contains(stack.getItem())
|
| 112 | && mc.level.fuelValues().isFuel(stack));
|
| 113 | if (slot == -1) {
|
| 114 | giveUp("Out of fuel.");
|
| 115 | return;
|
| 116 | }
|
| 117 | ItemStack source = furnace.slots.get(slot).getItem();
|
| 118 | int count = Math.min(fuelPerRefill.getInt(), source.getCount());
|
| 119 | moveSome(furnace, slot, FUEL_SLOT, count);
|
| 120 | }
|
| 121 |
|
| 122 | private void feed(AbstractFurnaceMenu furnace) {
|
| 123 | if (!furnace.slots.get(INPUT_SLOT).getItem().isEmpty()) {
|
| 124 | return;
|
| 125 | }
|
| 126 | ResourceKey<RecipePropertySet> recipes = recipesFor(furnace);
|
| 127 | int slot = findPlayerSlot(furnace, stack -> inputs.contains(stack.getItem())
|
| 128 | && mc.level.recipeAccess().propertySet(recipes).test(stack));
|
| 129 | if (slot == -1) {
|
| 130 | // The last item may still be cooking.
|
| 131 | if (furnace.getBurnProgress() <= 0) {
|
| 132 | giveUp("Nothing left to smelt.");
|
| 133 | }
|
| 134 | return;
|
| 135 | }
|
| 136 | quickMove(furnace, slot);
|
| 137 | }
|
| 138 |
|
| 139 | // Each furnace type takes its own set of inputs.
|
| 140 | private static ResourceKey<RecipePropertySet> recipesFor(AbstractFurnaceMenu furnace) {
|
| 141 | if (furnace instanceof BlastFurnaceMenu) {
|
| 142 | return RecipePropertySet.BLAST_FURNACE_INPUT;
|
| 143 | }
|
| 144 | if (furnace instanceof SmokerMenu) {
|
| 145 | return RecipePropertySet.SMOKER_INPUT;
|
| 146 | }
|
| 147 | return RecipePropertySet.FURNACE_INPUT;
|
| 148 | }
|
| 149 |
|
| 150 | private void giveUp(String reason) {
|
| 151 | if (stopWhenEmpty.isOn()) {
|
| 152 | ChatUtil.error(reason);
|
| 153 | setEnabled(false);
|
| 154 | }
|
| 155 | }
|
| 156 |
|
| 157 | private static int findPlayerSlot(AbstractFurnaceMenu furnace, Predicate<ItemStack> test) {
|
| 158 | for (int i = FIRST_PLAYER_SLOT; i < furnace.slots.size(); i++) {
|
| 159 | if (test.test(furnace.slots.get(i).getItem())) {
|
| 160 | return i;
|
| 161 | }
|
| 162 | }
|
| 163 | return -1;
|
| 164 | }
|
| 165 |
|
| 166 | private void quickMove(AbstractFurnaceMenu furnace, int slot) {
|
| 167 | mc.gameMode.handleContainerInput(furnace.containerId, slot, 0, ContainerInput.QUICK_MOVE, mc.player);
|
| 168 | }
|
| 169 |
|
| 170 | // Picks the stack up and drops items one at a time with right clicks.
|
| 171 | private void moveSome(AbstractFurnaceMenu furnace, int from, int to, int count) {
|
| 172 | mc.gameMode.handleContainerInput(furnace.containerId, from, 0, ContainerInput.PICKUP, mc.player);
|
| 173 | for (int i = 0; i < count && !furnace.getCarried().isEmpty(); i++) {
|
| 174 | mc.gameMode.handleContainerInput(furnace.containerId, to, 1, ContainerInput.PICKUP, mc.player);
|
| 175 | }
|
| 176 | if (!furnace.getCarried().isEmpty()) {
|
| 177 | mc.gameMode.handleContainerInput(furnace.containerId, from, 0, ContainerInput.PICKUP, mc.player);
|
| 178 | }
|
| 179 | }
|
| 180 | }
|
| 181 |
|