Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AutoFarm enhancements #846

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 106 additions & 5 deletions src/main/java/net/wurstclient/hacks/AutoFarmHack.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@

import net.minecraft.block.*;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.enchantment.Enchantments;
import net.minecraft.item.AxeItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.network.packet.c2s.play.HandSwingC2SPacket;
import net.minecraft.registry.tag.BlockTags;
Expand All @@ -27,6 +31,7 @@
import net.wurstclient.events.UpdateListener;
import net.wurstclient.hack.Hack;
import net.wurstclient.hacks.autofarm.AutoFarmRenderer;
import net.wurstclient.settings.BlockListSetting;
import net.wurstclient.settings.CheckboxSetting;
import net.wurstclient.settings.SliderSetting;
import net.wurstclient.settings.SliderSetting.ValueDisplay;
Expand All @@ -48,6 +53,26 @@ public final class AutoFarmHack extends Hack
private final CheckboxSetting replant =
new CheckboxSetting("Replant", true);

private final CheckboxSetting harvestFirst = new CheckboxSetting(
"Harvest first", "Harvest all crops first before replanting.", false);

private final CheckboxSetting checkLOS = new CheckboxSetting(
"Check line of sight",
"Makes sure that you don't reach through walls when breaking or replanting.",
false);

private final CheckboxSetting fortune =
new CheckboxSetting("Choose fortune tool",
"Chooses a fortune tool to harvest crops.", false);

private final CheckboxSetting silkTouch = new CheckboxSetting(
"Choose silk touch tool",
"Chooses a silk touch tool to harvest melons. Axes will be prioritized.",
false);

private final BlockListSetting excluded = new BlockListSetting(
"Excluded Crops", "List of crops that will not be harvested.");

private final HashMap<Block, Item> seeds = new HashMap<>();
{
seeds.put(Blocks.WHEAT, Items.WHEAT_SEEDS);
Expand All @@ -60,6 +85,16 @@ public final class AutoFarmHack extends Hack
seeds.put(Blocks.COCOA, Items.COCOA_BEANS);
}

private final HashSet<Block> fortuneBlocks = new HashSet<>();
{
fortuneBlocks.add(Blocks.WHEAT);
fortuneBlocks.add(Blocks.CARROTS);
fortuneBlocks.add(Blocks.POTATOES);
fortuneBlocks.add(Blocks.BEETROOTS);
fortuneBlocks.add(Blocks.NETHER_WART);
fortuneBlocks.add(Blocks.MELON);
}

private final HashMap<BlockPos, Item> plants = new HashMap<>();
private final ArrayDeque<Set<BlockPos>> prevBlocks = new ArrayDeque<>();
private BlockPos currentlyHarvesting;
Expand All @@ -76,6 +111,11 @@ public AutoFarmHack()
setCategory(Category.BLOCKS);
addSetting(range);
addSetting(replant);
addSetting(harvestFirst);
addSetting(checkLOS);
addSetting(fortune);
addSetting(silkTouch);
addSetting(excluded);
}

@Override
Expand Down Expand Up @@ -141,11 +181,15 @@ public void onUpdate()
getBlocksToReplant(eyesVec, eyesBlock, rangeSq, blockRange);
}

// first, try to replant
boolean replanting = replant(blocksToReplant);
// replant and harvest
if(harvestFirst.isChecked())
harvest(blocksToHarvest);

boolean replanting = false;
if(currentlyHarvesting == null)
replanting = replant(blocksToReplant);

// if we can't replant, harvest instead
if(!replanting)
if(!harvestFirst.isChecked() && !replanting)
harvest(blocksToHarvest);

// upate busy state
Expand Down Expand Up @@ -197,6 +241,10 @@ private boolean shouldBeHarvested(BlockPos pos)
Block block = BlockUtils.getBlock(pos);
BlockState state = BlockUtils.getState(pos);

if(Collections.binarySearch(excluded.getBlockNames(),
BlockUtils.getName(pos)) >= 0)
return false;
ThisTestUser marked this conversation as resolved.
Show resolved Hide resolved

if(block instanceof CropBlock)
return ((CropBlock)block).isMature(state);

Expand Down Expand Up @@ -294,6 +342,9 @@ private boolean replant(List<BlockPos> blocksToReplant)
if(params == null || params.distanceSq() > range.getValueSq())
continue;

if(checkLOS.isChecked() && !params.lineOfSight())
continue;

// face block
WURST.getRotationFaker().faceVectorPacket(params.hitVec());

Expand Down Expand Up @@ -353,11 +404,61 @@ private void harvest(List<BlockPos> blocksToHarvest)
}

for(BlockPos pos : blocksToHarvest)
if(BlockBreaker.breakOneBlock(pos))
{
boolean findSilkTouch = silkTouch.isChecked()
&& BlockUtils.getBlock(pos) == Blocks.MELON;
boolean findFortune = fortune.isChecked()
&& fortuneBlocks.contains(BlockUtils.getBlock(pos));
ItemStack held = MC.player.getMainHandStack();
if(findSilkTouch)
{
if(EnchantmentHelper.getLevel(Enchantments.SILK_TOUCH,
held) == 0 || !(held.getItem() instanceof AxeItem))
{
int slot = InventoryUtils
.indexOf(stack -> stack.getItem() instanceof AxeItem
&& EnchantmentHelper
.getLevel(Enchantments.SILK_TOUCH, stack) > 0);
if(slot == -1)
slot = InventoryUtils.indexOf(stack -> EnchantmentHelper
.getLevel(Enchantments.SILK_TOUCH, stack) > 0);
InventoryUtils.selectItem(slot);
}
}else if(findFortune)
{
int[] slots =
InventoryUtils
.indicesOf(
stack -> EnchantmentHelper
.getLevel(Enchantments.SILK_TOUCH, stack) == 0
&& EnchantmentHelper
.getLevel(Enchantments.FORTUNE, stack) > 0,
36, false);

int selected = -1;
int level = EnchantmentHelper.getLevel(Enchantments.SILK_TOUCH,
held) > 0 ? 0
: EnchantmentHelper.getLevel(Enchantments.FORTUNE,
held);
for(int slot : slots)
{
int curLevel =
EnchantmentHelper.getLevel(Enchantments.FORTUNE,
MC.player.getInventory().getStack(slot));
if(curLevel > level)
{
selected = slot;
level = curLevel;
}
}
InventoryUtils.selectItem(selected);
}
if(BlockBreaker.breakOneBlock(pos, checkLOS.isChecked()))
{
currentlyHarvesting = pos;
break;
}
}

if(currentlyHarvesting == null)
MC.interactionManager.cancelBlockBreaking();
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/net/wurstclient/util/BlockBreaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ public enum BlockBreaker
private static final MinecraftClient MC = WurstClient.MC;

public static boolean breakOneBlock(BlockPos pos)
{
return breakOneBlock(pos, false);
}

public static boolean breakOneBlock(BlockPos pos, boolean checkLOS)
{
BlockBreakingParams params = getBlockBreakingParams(pos);
if(params == null)
if(params == null || (checkLOS && !params.lineOfSight))
return false;

// face block
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/net/wurstclient/util/InventoryUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,35 @@ public static int indexOf(Predicate<ItemStack> predicate, int maxInvSlot,
return slot;
}

/**
* Searches the player's inventory from slot 0 to {@code maxInvSlot-1} for
* all items that matches the given predicate.
*
* @param predicate
* checks if an item is the one you want
* @param maxInvSlot
* the maximum slot to search (exclusive), usually 9 for the
* hotbar or 36 for the whole inventory
* @param includeOffhand
* also search the offhand (slot 40), even if maxInvSlot is lower
* @return
* all the slots the item was found on as an array
*/
public static int[] indicesOf(Predicate<ItemStack> predicate,
int maxInvSlot, boolean includeOffhand)
{
PlayerInventory inventory = MC.player.getInventory();

// create a stream of all slots that we want to search
IntStream stream = IntStream.range(0, maxInvSlot);
if(includeOffhand)
stream = IntStream.concat(stream, IntStream.of(40));

// find the slots of the item we want
return stream.filter(i -> predicate.test(inventory.getStack(i)))
.toArray();
}

public static boolean selectItem(Item item)
{
return selectItem(stack -> stack.isOf(item), 36, false);
Expand Down