Skip to content

Commit

Permalink
Fix issues to do with restocking; allow using a bow without closing a…
Browse files Browse the repository at this point in the history
…ny screens
  • Loading branch information
Wide-Cat committed Jan 11, 2025
1 parent e778a66 commit e4ceb6f
Showing 1 changed file with 46 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import net.minecraft.world.EmptyBlockView;
import net.minecraft.world.RaycastContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Range;
import org.joml.Vector3d;

import java.util.ArrayDeque;
Expand Down Expand Up @@ -208,6 +209,16 @@ public enum BlockadeType {
.build()
);

private final Setting<Integer> breakDurability = sgDigging.add(new IntSetting.Builder()
.name("durability-percentage")
.description("The durability percentage at which to stop using a tool.")
.defaultValue(2)
.range(1, 100)
.sliderRange(1, 100)
.visible(dontBreakTools::get)
.build()
);

private final Setting<Integer> savePickaxes = sgDigging.add(new IntSetting.Builder()
.name("save-pickaxes")
.description("How many pickaxes to ensure are saved.")
Expand Down Expand Up @@ -435,7 +446,7 @@ public enum BlockadeType {
private final MBlockPos lastBreakingPos = new MBlockPos();
private boolean displayInfo;
private int placeTimer, breakTimer, count, syncId;
private final RestockTask restockTask = new RestockTask();
private final RestockTask restockTask = new RestockTask(this);
private final ArrayList<EndCrystalEntity> ignoreCrystals = new ArrayList<>();
public DoubleMineBlock normalMining, packetMining;

Expand Down Expand Up @@ -1461,6 +1472,7 @@ protected void tick(HighwayBuilder b) {
breakContainer = true;
}
else {
if (!b.searchShulkers.get()) breakContainer = true;
handleContainerBlock(b, blockPos);
}
}
Expand Down Expand Up @@ -1500,6 +1512,7 @@ protected void tick(HighwayBuilder b) {
breakContainer = true;
}
else {
if (!b.searchEnderChest.get()) breakContainer = true;
handleContainerBlock(b, blockPos);
}
}
Expand Down Expand Up @@ -1668,7 +1681,7 @@ protected void tick(HighwayBuilder b) {
b.destroyCrystalTraps.set(false);
b.warning("No bow found to destroy crystal traps with. Toggling the setting off.");
b.setState(Forward);
b.mc.options.useKey.setPressed(false);
if (b.mc.player.isUsingItem()) b.mc.interactionManager.stopUsingItem(b.mc.player);
return;
}

Expand All @@ -1691,7 +1704,7 @@ protected void tick(HighwayBuilder b) {
if (target == null || target.isRemoved()) {
if (potentialTarget == null) {
b.setState(Forward);
b.mc.options.useKey.setPressed(false);
if (b.mc.player.isUsingItem()) b.mc.interactionManager.stopUsingItem(b.mc.player);
return;
}
else {
Expand All @@ -1704,7 +1717,7 @@ protected void tick(HighwayBuilder b) {
b.ignoreCrystals.add(target);
b.warning("Detected potential hangup on a crystal. Adding it to ignore list and continuing forward.");
b.setState(Forward);
b.mc.options.useKey.setPressed(false);
if (b.mc.player.isUsingItem()) b.mc.interactionManager.stopUsingItem(b.mc.player);
return;
}

Expand All @@ -1716,14 +1729,10 @@ protected void tick(HighwayBuilder b) {

if (BowItem.getPullProgress(b.mc.player.getItemUseTime() - 3) >= 1.0f) {
b.mc.interactionManager.stopUsingItem(b.mc.player);
b.mc.options.useKey.setPressed(false);
cooldown = 20;
shots++;
}
else {
if (b.mc.currentScreen != null) b.mc.currentScreen.close();
b.mc.options.useKey.setPressed(true);
}
else b.mc.interactionManager.interactItem(b.mc.player, Hand.MAIN_HAND);
}

private float aim(HighwayBuilder b, Entity target) {
Expand Down Expand Up @@ -1989,7 +1998,7 @@ protected int findAndMoveBestToolToHotbar(HighwayBuilder b, BlockState blockStat
for (int i = 0; i < b.mc.player.getInventory().main.size(); i++) {
double score = AutoTool.getScore(b.mc.player.getInventory().getStack(i), blockState, false, false, AutoTool.EnchantPreference.None, itemStack -> {
if (noSilkTouch && Utils.hasEnchantment(itemStack, Enchantments.SILK_TOUCH)) return false;
return !b.dontBreakTools.get() || itemStack.getMaxDamage() - itemStack.getDamage() > 1;
return !b.dontBreakTools.get() || itemStack.getMaxDamage() - itemStack.getDamage() > (itemStack.getMaxDamage() * (b.breakDurability.get() / 100));
});

if (score > bestScore) {
Expand All @@ -2000,15 +2009,18 @@ protected int findAndMoveBestToolToHotbar(HighwayBuilder b, BlockState blockStat

if (bestSlot == -1) return b.mc.player.getInventory().selectedSlot;

if (b.mc.player.getInventory().getStack(bestSlot).getItem() instanceof PickaxeItem) {
ItemStack bestStack = b.mc.player.getInventory().getStack(bestSlot);
if (bestStack.getItem() instanceof PickaxeItem) {
int count = countItem(b, stack -> stack.getItem() instanceof PickaxeItem);

if (count <= b.savePickaxes.get()) {
if (b.searchEnderChest.get() || b.searchShulkers.get()) {
// If we are in the process of restocking pickaxes and happen to need one, we should allow using it
// as long as it has enough durability, since we will obtain more shortly thereafter
if (count <= b.savePickaxes.get() && !(b.restockTask.pickaxes && bestStack.getMaxDamage() - bestStack.getDamage() > (bestStack.getMaxDamage() * (b.breakDurability.get() / 100)))) {
if (!b.restockTask.pickaxes && (b.searchEnderChest.get() || b.searchShulkers.get())) {
b.restockTask.setPickaxes();
}
else {
b.error("Found less than the selected amount of pickaxes required: " + count + "/" + (b.savePickaxes.get() + 1));
b.error("Found less than the minimum amount of pickaxes required: " + count + "/" + (b.savePickaxes.get() + 1));
}

return -1;
Expand Down Expand Up @@ -2751,23 +2763,35 @@ private class RestockTask {
public boolean materials;
public boolean pickaxes;
public boolean food;
private final HighwayBuilder b;

public RestockTask(HighwayBuilder b) {
this.b = b;
}

public void setMaterials() {
complete();
materials = true;
setState(State.Restock);
setTask(0);
}

public void setPickaxes() {
complete();
pickaxes = true;
setState(State.Restock);
setTask(1);
}

public void setFood() {
setTask(2);
}

private void setTask(@Range(from = 0, to = 2) int value) {
complete();
food = true;

switch (value) {
case 0 -> materials = true;
case 1 -> pickaxes = true;
case 2 -> food = true;
}

setState(State.Restock);
b.info("Starting new restock task for " + item());
}

public void complete() {
Expand All @@ -2781,7 +2805,7 @@ public boolean tasksInactive() {
}

public String item() {
if (materials) return "materials";
if (materials) return "building materials";
if (pickaxes) return "pickaxes";
if (food) return "food";
return "unknown";
Expand Down

0 comments on commit e4ceb6f

Please sign in to comment.