Skip to content
This repository has been archived by the owner on Jun 28, 2024. It is now read-only.

Added travel using an elytra feature, the RepairToolTask and some edits around that #194

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
14e3389
Start of the movement with elytra task, and added firework_rocket int…
badpiggies007 Jan 22, 2022
a0227b5
Fix fireworks in TaskCatalogue (bad item output number, was 3 instead…
badpiggies007 Jan 22, 2022
6bdfcee
Added a new state named 'disableDefence' inside BotBehaviour to disab…
badpiggies007 Jan 23, 2022
fe20c5f
Added a command for the GetToXZWithElytraTask, and removed the elytra…
badpiggies007 Jan 23, 2022
819f334
Edited the elytra command usage in usage.md
badpiggies007 Jan 23, 2022
ee1acad
Made the 'moving to the surface' thing more reliable to find a place …
badpiggies007 Jan 23, 2022
60403d2
Merge branch 'main' into elytra-test
badpiggies007 Jan 28, 2022
5819a0f
Some elytra edit : added checking elytra durability to avoid falling …
badpiggies007 Jan 28, 2022
0cd915f
Edited some comments and optimised import in the elytra travel task
badpiggies007 Jan 29, 2022
58f66ae
Added the RepairToolTask, that can repair items that have mending on …
badpiggies007 Jan 30, 2022
2db2c72
Now using a better way to detect when landed in the elytra travel task
badpiggies007 Jan 30, 2022
17596dd
Merge remote-tracking branch 'origin' into elytra-test
badpiggies007 Feb 3, 2022
0c54576
Renamed import of the TimeTimer in RepairToolTask and in GetToXZWithE…
badpiggies007 Feb 3, 2022
cecc968
Now using Worldhelper.getGroundHeight in the elytra travel task
badpiggies007 Feb 3, 2022
548ef61
Removed code duplicate (isGrounded)
badpiggies007 Feb 3, 2022
f754aac
Using PlayerSlot.ARMOR_CHESTPLATE_SLOT instead of PlayerSlot(6)
badpiggies007 Feb 3, 2022
72c4454
Removed the _isFinished bool in the elytra task
badpiggies007 Feb 3, 2022
f169977
Removed 'magic numbers' from the elytra travel task
badpiggies007 Feb 3, 2022
1b23d97
Renamed 'disableDefence' to 'defence' and to 'isDefenseDisabled'
badpiggies007 Feb 3, 2022
ee90ed5
Some changes to the repairToolTask
badpiggies007 Feb 4, 2022
6a2c7d8
Edited the layers where the bot need to fly to y=325
badpiggies007 Feb 4, 2022
eb50987
Forgot to rename SlotRepair to slotRepair in the repair tool task
badpiggies007 Feb 4, 2022
2ea8832
Code style/minor adjustments in `RepairToolTask`
TacoTechnica Feb 4, 2022
ccbdafa
Elytra flying renaming + `WorldHelper` fixes and adjustments
TacoTechnica Feb 4, 2022
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
1 change: 1 addition & 0 deletions CataloguedResources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ fence_gate
fermented_spider_eye
fern
fire_charge
firework_rocket
fishing_rod
fletching_table
flint
Expand Down
1 change: 1 addition & 0 deletions src/main/java/adris/altoclef/AltoClefCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public AltoClefCommands() throws CommandException {
new GiveCommand(),
new EquipCommand(),
new GotoCommand(),
new GotoWithElytraCommand(),
new IdleCommand(),
new CoordsCommand(),
new StatusCommand(),
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/adris/altoclef/BotBehaviour.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ public void setAllowWalkThroughFlowingWater(boolean value) {
current().applyState();
}

//Way of disabling MLG and MobDefense from a task
public boolean disableDefence() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nitpick but the convention we're using to spell this word is defense

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I'd change this to something like isDefenseDisabled

return current().disableDefence; //get value with mod.getBehaviour().disableDefence()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider making mob defence enabled/disabled an interface. We can talk about how to integrate this later. Discord me if you are interested in learning about this,


public void disableDefence(boolean value) { //and set value with mod.getBehaviour().disableDefence(true|false)
current().disableDefence = value;
current().applyState();
}

public void setPauseOnLostFocus(boolean pauseOnLostFocus) {
current().pauseOnLostFocus = pauseOnLostFocus;
current().applyState();
Expand Down Expand Up @@ -246,6 +256,7 @@ class State {
// Alto Clef params
public boolean exclusivelyMineLogs;
public boolean forceFieldPlayers;
public boolean disableDefence;
public List<Predicate<Entity>> avoidDodgingProjectile = new ArrayList<>();

public List<Predicate<Entity>> excludeFromForceField = new ArrayList<>();
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/adris/altoclef/Playground.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import adris.altoclef.tasks.examples.ExampleTask2;
import adris.altoclef.tasks.misc.EquipArmorTask;
import adris.altoclef.tasks.misc.PlaceBedAndSetSpawnTask;
import adris.altoclef.tasks.misc.RepairToolTask;
import adris.altoclef.tasks.construction.PlaceSignTask;
import adris.altoclef.tasks.speedrun.*;
import adris.altoclef.tasks.stupid.BeeMovieTask;
Expand Down Expand Up @@ -188,6 +189,10 @@ public static void TEMP_TEST_FUNCTION(AltoClef mod, String arg) {
ItemTarget material = new ItemTarget("iron_ore", 4);
mod.runUserTask(new SmeltInFurnaceTask(new SmeltTarget(target, material)));
break;
case "repair":
//mod.runUserTask(new RepairToolTask(new ItemTarget("leather_chestplate", 1), new ItemTarget("golden_boots", 1) , new ItemTarget("iron_sword", 1)));
mod.runUserTask(new RepairToolTask());
break;
case "avoid":
// Test block break predicate
mod.getBehaviour().avoidBlockBreaking((BlockPos b) -> (-1000 < b.getX() && b.getX() < 1000)
Expand Down
1 change: 1 addition & 0 deletions src/main/java/adris/altoclef/TaskCatalogue.java
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ public class TaskCatalogue {
alias("eye_of_ender", "ender_eye");
shapedRecipe2x2("fermented_spider_eye", Items.FERMENTED_SPIDER_EYE, 1, "brown_mushroom", "sugar", o, "spider_eye");
shapedRecipe3x3("fire_charge", Items.FIRE_CHARGE, 3, o, "blaze_powder", o, o, "coal", o, o, "gunpowder", o);
shapedRecipe2x2("firework_rocket", Items.FIREWORK_ROCKET, 3, "paper", "gunpowder", o, o);
shapedRecipe2x2("flower_banner_pattern", Items.FLOWER_BANNER_PATTERN, 1, "paper", "oxeye_daisy", o, o);
simple("magma_cream", Items.MAGMA_CREAM, CollectMagmaCreamTask::new);
// Slabs + Stairs + Walls
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public boolean isActive() {
}

public boolean isFallingOhNo(AltoClef mod) {
if (!mod.getModSettings().shouldAutoMLGBucket()) {
if (!mod.getModSettings().shouldAutoMLGBucket() || mod.getBehaviour().disableDefence()) {
return false;
}
if (mod.getPlayer().isSwimming() || mod.getPlayer().isTouchingWater() || mod.getPlayer().isOnGround() || mod.getPlayer().isClimbing()) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/adris/altoclef/chains/MobDefenseChain.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private float getPriorityInner(AltoClef mod) {
return Float.NEGATIVE_INFINITY;
}

if (!mod.getModSettings().isMobDefense()) {
if (!mod.getModSettings().isMobDefense() || mod.getBehaviour().disableDefence()) {
return Float.NEGATIVE_INFINITY;
}

Expand Down
21 changes: 21 additions & 0 deletions src/main/java/adris/altoclef/commands/GotoWithElytraCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package adris.altoclef.commands;

import adris.altoclef.AltoClef;
import adris.altoclef.commandsystem.Arg;
import adris.altoclef.commandsystem.ArgParser;
import adris.altoclef.commandsystem.Command;
import adris.altoclef.commandsystem.CommandException;
import adris.altoclef.tasks.movement.GetToXZWithElytraTask;

public class GotoWithElytraCommand extends Command {
public GotoWithElytraCommand() throws CommandException {
super("elytra", "Tell bot to travel to a set of coordinates using Elytra", new Arg(Integer.class, "x"), new Arg(Integer.class, "z"));
}

@Override
protected void call(AltoClef mod, ArgParser parser) throws CommandException {
int x = parser.get(Integer.class);
int z = parser.get(Integer.class);
mod.runUserTask(new GetToXZWithElytraTask(x,z), this::finish);
}
}
197 changes: 197 additions & 0 deletions src/main/java/adris/altoclef/tasks/misc/RepairToolTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
package adris.altoclef.tasks.misc;

import adris.altoclef.AltoClef;
import adris.altoclef.Debug;
import adris.altoclef.tasks.entity.KillEntityTask;
import adris.altoclef.tasks.movement.GetToBlockTask;
import adris.altoclef.tasks.movement.TimeoutWanderTask;
import adris.altoclef.tasksystem.Task;
import adris.altoclef.util.ItemTarget;
import adris.altoclef.util.helpers.StorageHelper;
import adris.altoclef.util.slots.Slot;
import adris.altoclef.util.slots.PlayerSlot;
import adris.altoclef.util.helpers.ItemHelper;
import net.minecraft.item.Items;
import baritone.api.utils.input.Input;
import net.minecraft.entity.mob.ZombieEntity;
import net.minecraft.entity.ExperienceOrbEntity;
import adris.altoclef.util.csharpisbetter.TimerGame;
import adris.altoclef.util.helpers.LookHelper;
import baritone.api.utils.Rotation;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.item.ItemStack;
import java.util.List;
import java.util.Optional;
import java.util.Arrays;

public class RepairToolTask extends Task {

private ItemTarget[] _toRepair;
private boolean _finished;
private final TimerGame _throwTimer = new TimerGame(0.5);

public RepairToolTask(ItemTarget... toRepair) {
_toRepair = toRepair;
}
public RepairToolTask() { //If this task is called without itemtarget, repair anything we can
this(
new ItemTarget(ItemHelper.NETHERITE_ARMORS),
new ItemTarget(ItemHelper.NETHERITE_TOOLS),
new ItemTarget(Items.ELYTRA),
new ItemTarget(ItemHelper.DIAMOND_ARMORS),
new ItemTarget(ItemHelper.DIAMOND_TOOLS),
new ItemTarget(ItemHelper.IRON_ARMORS),
new ItemTarget(ItemHelper.IRON_TOOLS),
new ItemTarget(ItemHelper.GOLDEN_ARMORS),
new ItemTarget(ItemHelper.GOLDEN_TOOLS),
new ItemTarget(ItemHelper.STONE_TOOLS),
new ItemTarget(ItemHelper.LEATHER_ARMORS),
new ItemTarget(ItemHelper.WOODEN_TOOLS),
new ItemTarget(Items.FISHING_ROD),
new ItemTarget(Items.FLINT_AND_STEEL),
new ItemTarget(Items.CARROT_ON_A_STICK),
new ItemTarget(Items.SHEARS),
new ItemTarget(Items.BOW),
new ItemTarget(Items.SHIELD),
new ItemTarget(Items.TRIDENT),
new ItemTarget(Items.CROSSBOW),
new ItemTarget(Items.WARPED_FUNGUS_ON_A_STICK),
new ItemTarget(Items.BOW)
);
}
@Override
protected void onStart(AltoClef mod) {
_throwTimer.reset();
_toRepair = Arrays.stream(_toRepair).filter(target -> needRepair(mod, target)).toArray(ItemTarget[]::new); //Filter out items type we can't repair
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is rerun in onTick so no need for that here

Optional<ItemTarget> ItemTargetOPTRepair = Arrays.stream(_toRepair).findFirst(); //Get the first element in our list
if (!ItemTargetOPTRepair.isPresent()) {
Debug.logWarning("There is nothing to repair");
}
}

@Override
protected Task onTick(AltoClef mod) {
//We start this task by filtering out every item type that we can't repair :
//All items without mending or with no damage
_toRepair = Arrays.stream(_toRepair).filter(target -> needRepair(mod, target)).toArray(ItemTarget[]::new);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd make _toRepair final and make a local variable ItemTarget[] shouldRepair here or something. Instance variables should be final if they're compared to in isEqual, because otherwise the task may continuously re-run onStart


//After that, we get the first item type to repair on the list
Optional<ItemTarget> ItemTargetOPTRepair = Arrays.stream(_toRepair).findFirst();

if (ItemTargetOPTRepair.isPresent()) { //If the list is not empty
ItemTarget ItemTargetRepair = ItemTargetOPTRepair.get(); //We get the (real) first item on the list

List<Slot> SlotRepair = mod.getItemStorage().getSlotsWithItemPlayerInventory(false, ItemTargetRepair.getMatches()); //And we get a list of every slot with that item
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Java uses lowerCamelCase for local variables (slotRepair, not SlotRepair)


Slot SlotRepairTarget = new PlayerSlot(-1); //Placeholder slot, will never get used if not replaced
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use null as a placeholder instead? -1 can be mistaken for the cursor slot.

boolean FoundSomethingToRepair = false; //Will be set if we find the slot with the item to repair
for (int i = 0; i < SlotRepair.size(); ++i) { //For every item slot that is on our list
if (!FoundSomethingToRepair & StorageHelper.getItemStackInSlot(SlotRepair.get(i)).getDamage() != 0) { //if we can repair it
if (haveMending(mod,StorageHelper.getItemStackInSlot(SlotRepair.get(i)))) { //and it have mending
SlotRepairTarget = SlotRepair.get(i); //Replace the placeholder slot with the slot we found
FoundSomethingToRepair = true;
}
}
}
if (FoundSomethingToRepair) { //If we found our slot, we can now repair the item !
setDebugState("Repairing "+StorageHelper.getItemStackInSlot(SlotRepairTarget).getName().getString());
if (!_throwTimer.elapsed()){ //If we just used a experience bottle, get the item in our hand to repair
mod.getSlotHandler().forceEquipSlot(SlotRepairTarget);
return null;
}
//Get the nearest experience orb
Optional<Entity> expentityOPT = mod.getEntityTracker().getClosestEntity(mod.getPlayer().getPos(), ExperienceOrbEntity.class);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an annoying gotcha, but avoid always gunning for the closest entity/block. There could be a ping-pong pattern of the bot getting stuck between two targets (https://youtu.be/uROEqwyzn3o?t=6946)

Using DoToClosest ... Task can fix this, as it checks for this bug.

return new DoToClosestEntityTask(entity -> {
    // Make sure our repair item is equipped when we're about to collect the orb!
    if (entity.isInRange(mod.getPlayer(), 3)) {
        mod.getSlotHandler().forceEquipSlot(SlotRepairTarget);
    }
    return new GetToEntityTask(entity);
});

Copy link

@Bluscream Bluscream Feb 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting ... maybe this is why my @pickup task likes to shit itself sometimes

if (expentityOPT.isPresent()) { //if there is one
Entity expentity = expentityOPT.get(); //get it
if (expentity.getBlockPos().isWithinDistance(mod.getPlayer().getPos(), 3)) { //and if the orb is near the player
mod.getSlotHandler().forceEquipSlot(SlotRepairTarget); //get the item in our hand to repair the item
};
return new GetToBlockTask(expentity.getBlockPos()); //go to the orb too
}
if (mod.getItemStorage().hasItem(Items.EXPERIENCE_BOTTLE)) { //if we have some experience bottle
if (_throwTimer.elapsed()) { //the timer for throwing a experience bottle
if (!LookHelper.isLookingAt(mod, new Rotation(0, 90))) {
LookHelper.lookAt(mod, new Rotation(0, 90)); //Look at our feet
}
mod.getSlotHandler().forceEquipItem(Items.EXPERIENCE_BOTTLE); //equip it
mod.getInputControls().tryPress(Input.CLICK_RIGHT); //and throw it
_throwTimer.reset();
}
return null;
}

//So we don't have any experience bottle, we will kill some zombies to get XP
Optional<Entity> entity = mod.getEntityTracker().getClosestEntity(mod.getPlayer().getPos(), ZombieEntity.class); //Find a zombie
if (entity.isPresent()) {
return new KillEntityTask(entity.get()); //And kill it
}
return new TimeoutWanderTask(); //If there is no zombie, wander
}
} //If there is no items in the list of itemtype to repair, it means there is nothing to repair :)
_finished = true;
return null;
}

@Override
public boolean isFinished(AltoClef mod) {
return _finished;
}
//Check if a type of item can be repaired.
public static boolean needRepair(AltoClef mod, ItemTarget target) {
List<Slot> SlotRepair = mod.getItemStorage().getSlotsWithItemPlayerInventory(false, target.getMatches());
boolean FoundSomethingToRepair = false;
for (int i = 0; i < SlotRepair.size(); ++i) {
if (!FoundSomethingToRepair & StorageHelper.getItemStackInSlot(SlotRepair.get(i)).getDamage() != 0) {
if (haveMending(mod,StorageHelper.getItemStackInSlot(SlotRepair.get(i)))) {
FoundSomethingToRepair = true;
}
}
}
return FoundSomethingToRepair;
}
//Will get the durability of an item in accordance of the ItemTarget.
//Return the durability of one of the item, or -1 if all targeted items is repaired or doesn't have the targeted item
public static int getDurabilityOfRepairableItem(AltoClef mod, ItemTarget target) {
List<Slot> SlotRepair = mod.getItemStorage().getSlotsWithItemPlayerInventory(false, target.getMatches());
for (int i = 0; i < SlotRepair.size(); ++i) {
if (StorageHelper.getItemStackInSlot(SlotRepair.get(i)).getDamage() != 0) {
if (haveMending(mod,StorageHelper.getItemStackInSlot(SlotRepair.get(i)))) {
return StorageHelper.getItemStackInSlot(SlotRepair.get(i)).getMaxDamage()-StorageHelper.getItemStackInSlot(SlotRepair.get(i)).getDamage();
}
}
}
return -1;
}

//Test if the itemstack have mending on it
public static boolean haveMending(AltoClef mod, ItemStack target) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EnchantmentHelper.get(itemStack).containsKey(Enchantments.MENDING);

boolean canRepair = false;
for (NbtElement elm : target.getEnchantments()) {
NbtCompound comp = (NbtCompound) elm;
if (comp.getString("id").equals("minecraft:mending")) {
canRepair = true;
}
}
return canRepair;
}
@Override
protected void onStop(AltoClef mod, Task interruptTask) {

}

@Override
protected boolean isEqual(Task other) {
if (other instanceof RepairToolTask task) {
return Arrays.equals(task._toRepair, _toRepair);
}
return false;
}

@Override
protected String toDebugString() {
return "Repairing an item";
}

}
Loading