Skip to content

Commit

Permalink
- builder task was successful for the first time
Browse files Browse the repository at this point in the history
  • Loading branch information
Meloweh committed Dec 14, 2021
1 parent 3c3c825 commit 393f2cf
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id "fabric-loom" version "0.9-SNAPSHOT"
id "fabric-loom" version "0.10-SNAPSHOT"
id "maven-publish"
id 'com.github.johnrengelman.shadow' version '2.0.4'
}
Expand Down
53 changes: 50 additions & 3 deletions src/main/java/adris/altoclef/tasks/ResourceTask.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package adris.altoclef.tasks;

import adris.altoclef.AltoClef;
import adris.altoclef.Debug;
import adris.altoclef.tasks.chest.PickupFromChestTask;
import adris.altoclef.tasks.movement.DefaultGoToDimensionTask;
import adris.altoclef.tasks.movement.PickupDroppedItemTask;
Expand Down Expand Up @@ -35,7 +36,9 @@ public abstract class ResourceTask extends Task {
private boolean _forceDimension = false;
private Dimension _targetDimension;

private BlockPos _mineLastClosest = null;
//private BlockPos _mineLastClosest = null;
private final Store store = new Store();
public static final String ATTRIBUTE_LAST_CLOSEST = "last_closest";

public ResourceTask(ItemTarget[] itemTargets) {
_itemTargets = itemTargets;
Expand Down Expand Up @@ -119,6 +122,43 @@ protected Task onTick(AltoClef mod) {
}
}

if (Utils.isSet(_mineIfPresent)) {
ArrayList<Block> satisfiedReqs = new ArrayList<>(Arrays.asList(_mineIfPresent));
satisfiedReqs.removeIf(block -> !mod.getInventoryTracker().miningRequirementMet(MiningRequirement.getMinimumRequirementForBlock(block)));
if (!satisfiedReqs.isEmpty()) {
if (mod.getBlockTracker().anyFound(satisfiedReqs.toArray(Block[]::new))) {
BlockPos closest = mod.getBlockTracker().getNearestTracking(mod.getPlayer().getPos(), _mineIfPresent);

//TODO: could this make the bot get stuck between two resources?
if (Utils.isSet(closest)) {
store.setAttribute(ATTRIBUTE_LAST_CLOSEST, closest);
//_mineLastClosest = closest;
}

if (store.hasAttribute(ATTRIBUTE_LAST_CLOSEST)) {
final BlockPos _mineLastClosest = store.fromStorage(ATTRIBUTE_LAST_CLOSEST, BlockPos.class);
final boolean isInChunk = mod.getChunkTracker().isChunkLoaded(_mineLastClosest);
final boolean isMined = !mod.getBlockTracker().blockIsValid(_mineLastClosest, _mineIfPresent);

if (isInChunk && isMined || !Blacklist.isBlacklisted(_mineLastClosest)) {
//TODO so if we set it null here, shouldn't we ensure the next mining target to stick with executing MineAndCollectTask?
//Answer no because if that would be the case then this would count for isBlacklisted too
//_mineLastClosest = null;
if (!store.removeAttribute(ATTRIBUTE_LAST_CLOSEST)) {
Debug.logError(ATTRIBUTE_LAST_CLOSEST + " not present in store");
System.out.println(ATTRIBUTE_LAST_CLOSEST + " not present in store");
store.clearStore();
}
}

if (store.hasAttribute(ATTRIBUTE_LAST_CLOSEST)) {
return new MineAndCollectTask(_itemTargets, _mineIfPresent, MiningRequirement.HAND, store);
}
}
}
}
}

/* TODO
Is it considered in the ResourceTask that the bot could walk away and therefore unload a
tracked mining candidate while going for a side task like getting the right mining tool?
Expand All @@ -130,7 +170,7 @@ protected Task onTick(AltoClef mod) {
Taco: It should have some kind of force condition and mine that block as long as it's valid
(which will only return false if the block is in the chunk and is not the same block as it was before)
*/
if (_mineIfPresent != null) {
/*if (Utils.isSet(_mineIfPresent)) {
ArrayList<Block> satisfiedReqs = new ArrayList<>(Arrays.asList(_mineIfPresent));
satisfiedReqs.removeIf(block -> !mod.getInventoryTracker().miningRequirementMet(MiningRequirement.getMinimumRequirementForBlock(block)));
if (!satisfiedReqs.isEmpty()) {
Expand All @@ -151,13 +191,20 @@ protected Task onTick(AltoClef mod) {
_mineLastClosest = null;
}
// TODO
// I think in ResourceTask it is not ensured that _mineLastClosest will be a target
// for the returned MineAndCollectTask since ResourceTask and MineAndCollectTask call
// getBlockTracker().getNearestTracking seperately, giving it a chance to pick different
// blocks or for closestBlock to be null.
// Consequentially in getClosestTo in MineAndCollectTask the mining goal may be set to infinity
if (Utils.isSet(_mineLastClosest)) {
return new MineAndCollectTask(_itemTargets, _mineIfPresent, MiningRequirement.HAND);
}
}
}
}
}
}*/
/*
// We may just mine if a block is found.
if (_mineIfPresent != null) {
Expand Down
21 changes: 19 additions & 2 deletions src/main/java/adris/altoclef/tasks/SchematicBuildTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.minecraft.block.BlockState;
import net.minecraft.client.MinecraftClient;
import net.minecraft.item.Item;
import net.minecraft.item.Items;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3i;

Expand Down Expand Up @@ -216,7 +217,7 @@ protected Task onTick(AltoClef mod) {
}

if (_moveChecker.check(mod)) {
System.out.println("move checker reset.");
//System.out.println("move checker reset.");
_clickTimer.reset();
}
if (_clickTimer.elapsed()) {
Expand All @@ -236,11 +237,27 @@ protected Task onTick(AltoClef mod) {
}
}

/*
if (BaritoneAPI.getProvider().getPrimaryBaritone().getBuilderProcess().getApproxPlaceable() != null)
BaritoneAPI.getProvider().getPrimaryBaritone().getBuilderProcess().getApproxPlaceable().forEach(e -> {
if (Utils.isSet(e) && e.getBlock().asItem().toString() != "air") {
System.out.println(e.getBlock().getName());
System.out.println(e.getBlock().asItem().getName());
System.out.println(e.getBlock().asItem().toString());
System.out.println(e.getBlock().toString());
System.out.println("(((((((((");
System.out.println(e.getBlock().getDefaultState());
System.out.println(")))))))))");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
});*/
//mod.getInventoryTracker().getItemStackInSlot(mod.getInventoryTracker().getInventorySlotsWithItem(Items.OAK_DOOR).get(0)).getItem().
/*
missing.forEach((k,e) -> {
if (Utils.isSet(k)) {
System.out.println(k);
}
});
});*/

return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import adris.altoclef.tasksystem.Task;
import adris.altoclef.util.ItemTarget;
import adris.altoclef.util.MiningRequirement;
import adris.altoclef.util.Store;
import adris.altoclef.util.Utils;
import adris.altoclef.util.csharpisbetter.TimerGame;
import adris.altoclef.util.helpers.WorldHelper;
import adris.altoclef.util.progresscheck.MovementProgressChecker;
Expand All @@ -30,14 +32,11 @@
import java.util.*;

public class MineAndCollectTask extends ResourceTask {

private final Block[] _blocksToMine;

private final MiningRequirement _requirement;

private final TimerGame _cursorStackTimer = new TimerGame(3);

private final MineOrCollectTask _subtask;
private Store resourceTaskStore;

public MineAndCollectTask(ItemTarget[] itemTargets, Block[] blocksToMine, MiningRequirement requirement) {
super(itemTargets);
Expand All @@ -46,6 +45,14 @@ public MineAndCollectTask(ItemTarget[] itemTargets, Block[] blocksToMine, Mining
_subtask = new MineOrCollectTask(_blocksToMine, _itemTargets);
}

public MineAndCollectTask(ItemTarget[] itemTargets, Block[] blocksToMine, MiningRequirement requirement, final Store resourceTaskStore) {
super(itemTargets);
_requirement = requirement;
_blocksToMine = blocksToMine;
this.resourceTaskStore = resourceTaskStore;
_subtask = new MineOrCollectTask(_blocksToMine, _itemTargets, resourceTaskStore);
}

public MineAndCollectTask(ItemTarget[] blocksToMine, MiningRequirement requirement) {
this(blocksToMine, itemTargetToBlockList(blocksToMine), requirement);
}
Expand Down Expand Up @@ -163,13 +170,21 @@ private static class MineOrCollectTask extends AbstractDoToClosestObjectTask<Obj
private final Task _pickupTask;
private BlockPos _miningPos;
private AltoClef _mod;
private Store resourceTaskStore;

public MineOrCollectTask(Block[] blocks, ItemTarget[] targets) {
_blocks = blocks;
_targets = targets;
_pickupTask = new PickupDroppedItemTask(_targets, true);
}

public MineOrCollectTask(Block[] blocks, ItemTarget[] targets, final Store resourceTaskStore) {
_blocks = blocks;
_targets = targets;
this.resourceTaskStore = resourceTaskStore;
_pickupTask = new PickupDroppedItemTask(_targets, true);
}

@Override
protected Vec3d getPos(AltoClef mod, Object obj) {
if (obj instanceof BlockPos b) {
Expand All @@ -181,21 +196,50 @@ protected Vec3d getPos(AltoClef mod, Object obj) {
throw new UnsupportedOperationException("Shouldn't try to get the position of object " + obj + " of type " + (obj != null ? obj.getClass().toString() : "(null object)"));
}

private final void removeFromStoreIfPresent(final BlockPos check) {
if (Utils.isSet(resourceTaskStore) && resourceTaskStore.hasAttribute(ATTRIBUTE_LAST_CLOSEST)) {
final BlockPos _mineLastClosest = resourceTaskStore.fromStorage(ATTRIBUTE_LAST_CLOSEST, BlockPos.class);
if (_mineLastClosest.compareTo(check) == 0) { //0 = equal to
resourceTaskStore.removeAttribute(ATTRIBUTE_LAST_CLOSEST);
}
}
}

@Override
protected Object getClosestTo(AltoClef mod, Vec3d pos) {
BlockPos closestBlock = null;
if (mod.getBlockTracker().anyFound(_blocks)) {
closestBlock = mod.getBlockTracker().getNearestTracking(pos, check -> {
final boolean isInBlacklist = _blacklist.contains(check);
if (isInBlacklist) {
removeFromStoreIfPresent(check);
return true;
}

// Filter out blocks that will get us into trouble. TODO: Blacklist
final boolean isPlausibleToBreak = MineProcess.plausibleToBreak(new CalculationContext(mod.getClientBaritone()), check);
if (!isPlausibleToBreak) {
removeFromStoreIfPresent(check);
}
return isPlausibleToBreak;
}, _blocks);
}
/*if (mod.getBlockTracker().anyFound(_blocks)) {
closestBlock = mod.getBlockTracker().getNearestTracking(pos, check -> {
if (_blacklist.contains(check)) return false;
// Filter out blocks that will get us into trouble. TODO: Blacklist
return MineProcess.plausibleToBreak(new CalculationContext(mod.getClientBaritone()), check);
}, _blocks);
}
}*/
ItemEntity closestDrop = null;
if (mod.getEntityTracker().itemDropped(_targets)) {
closestDrop = mod.getEntityTracker().getClosestItemDrop(pos, _targets);
}

if (Utils.isNull(closestBlock) && Utils.isSet(resourceTaskStore) && resourceTaskStore.hasAttribute(ATTRIBUTE_LAST_CLOSEST)) {
closestBlock = resourceTaskStore.fromStorage(ATTRIBUTE_LAST_CLOSEST, BlockPos.class);
}

double blockSq = closestBlock == null ? Double.POSITIVE_INFINITY : closestBlock.getSquaredDistance(pos, false);
double dropSq = closestDrop == null ? Double.POSITIVE_INFINITY : closestDrop.squaredDistanceTo(pos);

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/adris/altoclef/util/Store.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ public final boolean hasAttribute(final String name) {
return storage.containsKey(name);
}

public final boolean removeAttribute(final String name) {
return Utils.isSet(storage.remove(name));
}

public final boolean clearStore() {
storage.clear();
return storage.size() < 1;
}

/**
* Returns a casted object from the store.
*
Expand Down

0 comments on commit 393f2cf

Please sign in to comment.