a start on inventory movement
This commit is contained in:
parent
0ef08142fd
commit
299540725e
@ -28,6 +28,9 @@ import net.minecraft.inventory.ClickType;
|
|||||||
import net.minecraft.item.*;
|
import net.minecraft.item.*;
|
||||||
import net.minecraft.util.NonNullList;
|
import net.minecraft.util.NonNullList;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.OptionalInt;
|
||||||
|
import java.util.Random;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
public class InventoryBehavior extends Behavior {
|
public class InventoryBehavior extends Behavior {
|
||||||
@ -56,6 +59,34 @@ public class InventoryBehavior extends Behavior {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void attemptToPutOnHotbar(int inMainInvy, Predicate<Integer> disallowedHotbar) {
|
||||||
|
OptionalInt destination = getTempHotbarSlot(disallowedHotbar);
|
||||||
|
if (destination.isPresent()) {
|
||||||
|
swapWithHotBar(inMainInvy, destination.getAsInt());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public OptionalInt getTempHotbarSlot(Predicate<Integer> disallowedHotbar) {
|
||||||
|
// we're using 0 and 8 for pickaxe and throwaway
|
||||||
|
ArrayList<Integer> candidates = new ArrayList<>();
|
||||||
|
for (int i = 1; i < 8; i++) {
|
||||||
|
if (ctx.player().inventory.mainInventory.get(i).isEmpty() && !disallowedHotbar.test(i)) {
|
||||||
|
candidates.add(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (candidates.isEmpty()) {
|
||||||
|
for (int i = 1; i < 8; i++) {
|
||||||
|
if (!disallowedHotbar.test(i)) {
|
||||||
|
candidates.add(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (candidates.isEmpty()) {
|
||||||
|
return OptionalInt.empty();
|
||||||
|
}
|
||||||
|
return OptionalInt.of(candidates.get(new Random().nextInt(candidates.size())));
|
||||||
|
}
|
||||||
|
|
||||||
private void swapWithHotBar(int inInventory, int inHotbar) {
|
private void swapWithHotBar(int inInventory, int inHotbar) {
|
||||||
ctx.playerController().windowClick(ctx.player().inventoryContainer.windowId, inInventory < 9 ? inInventory + 36 : inInventory, inHotbar, ClickType.SWAP, ctx.player());
|
ctx.playerController().windowClick(ctx.player().inventoryContainer.windowId, inInventory < 9 ? inInventory + 36 : inInventory, inHotbar, ClickType.SWAP, ctx.player());
|
||||||
}
|
}
|
||||||
|
@ -158,7 +158,7 @@ public class BuilderProcess extends BaritoneProcessHelper implements IBuilderPro
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Placement> searchForPlacables(BuilderCalculationContext bcc) {
|
public Optional<Placement> searchForPlacables(BuilderCalculationContext bcc, List<IBlockState> desirableOnHotbar) {
|
||||||
BetterBlockPos center = ctx.playerFeet();
|
BetterBlockPos center = ctx.playerFeet();
|
||||||
for (int dx = -5; dx <= 5; dx++) {
|
for (int dx = -5; dx <= 5; dx++) {
|
||||||
for (int dy = -5; dy <= 1; dy++) {
|
for (int dy = -5; dy <= 1; dy++) {
|
||||||
@ -175,6 +175,7 @@ public class BuilderProcess extends BaritoneProcessHelper implements IBuilderPro
|
|||||||
if (dy == 1 && bcc.bsi.get0(x, y + 1, z).getBlock() == Blocks.AIR) {
|
if (dy == 1 && bcc.bsi.get0(x, y + 1, z).getBlock() == Blocks.AIR) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
desirableOnHotbar.add(desired);
|
||||||
Optional<Placement> opt = possibleToPlace(desired, x, y, z, bcc.bsi);
|
Optional<Placement> opt = possibleToPlace(desired, x, y, z, bcc.bsi);
|
||||||
if (opt.isPresent()) {
|
if (opt.isPresent()) {
|
||||||
return opt;
|
return opt;
|
||||||
@ -298,7 +299,8 @@ public class BuilderProcess extends BaritoneProcessHelper implements IBuilderPro
|
|||||||
}
|
}
|
||||||
return new PathingCommand(null, PathingCommandType.REQUEST_PAUSE);
|
return new PathingCommand(null, PathingCommandType.REQUEST_PAUSE);
|
||||||
}
|
}
|
||||||
Optional<Placement> toPlace = searchForPlacables(bcc);
|
List<IBlockState> desirableOnHotbar = new ArrayList<>();
|
||||||
|
Optional<Placement> toPlace = searchForPlacables(bcc, desirableOnHotbar);
|
||||||
if (toPlace.isPresent() && isSafeToCancel && ctx.player().onGround && ticks <= 0) {
|
if (toPlace.isPresent() && isSafeToCancel && ctx.player().onGround && ticks <= 0) {
|
||||||
Rotation rot = toPlace.get().rot;
|
Rotation rot = toPlace.get().rot;
|
||||||
baritone.getLookBehavior().updateTarget(rot, true);
|
baritone.getLookBehavior().updateTarget(rot, true);
|
||||||
@ -310,12 +312,42 @@ public class BuilderProcess extends BaritoneProcessHelper implements IBuilderPro
|
|||||||
return new PathingCommand(null, PathingCommandType.REQUEST_PAUSE);
|
return new PathingCommand(null, PathingCommandType.REQUEST_PAUSE);
|
||||||
}
|
}
|
||||||
|
|
||||||
Goal goal = assemble(bcc);
|
List<IBlockState> approxPlacable = placable(36);
|
||||||
|
if (Baritone.settings().allowInventory.get()) {
|
||||||
|
ArrayList<Integer> usefulSlots = new ArrayList<>();
|
||||||
|
List<IBlockState> noValidHotbarOption = new ArrayList<>();
|
||||||
|
outer:
|
||||||
|
for (IBlockState desired : desirableOnHotbar) {
|
||||||
|
for (int i = 0; i < 9; i++) {
|
||||||
|
if (valid(approxPlacable.get(i), desired)) {
|
||||||
|
usefulSlots.add(i);
|
||||||
|
continue outer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
noValidHotbarOption.add(desired);
|
||||||
|
}
|
||||||
|
|
||||||
|
outer:
|
||||||
|
for (int i = 9; i < 36; i++) {
|
||||||
|
for (IBlockState desired : noValidHotbarOption) {
|
||||||
|
if (valid(approxPlacable.get(i), desired)) {
|
||||||
|
baritone.getInventoryBehavior().attemptToPutOnHotbar(i, usefulSlots::contains);
|
||||||
|
break outer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Goal goal = assemble(bcc, approxPlacable.subList(0, 9));
|
||||||
|
if (goal == null) {
|
||||||
|
goal = assemble(bcc, approxPlacable); // we're far away, so assume that we have our whole inventory to recalculate placable properly
|
||||||
if (goal == null) {
|
if (goal == null) {
|
||||||
logDirect("Unable to do it =(");
|
logDirect("Unable to do it =(");
|
||||||
onLostControl();
|
onLostControl();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return new PathingCommandContext(goal, PathingCommandType.FORCE_REVALIDATE_GOAL_AND_PATH, bcc);
|
return new PathingCommandContext(goal, PathingCommandType.FORCE_REVALIDATE_GOAL_AND_PATH, bcc);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -371,8 +403,7 @@ public class BuilderProcess extends BaritoneProcessHelper implements IBuilderPro
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Goal assemble(BuilderCalculationContext bcc) {
|
private Goal assemble(BuilderCalculationContext bcc, List<IBlockState> approxPlacable) {
|
||||||
List<IBlockState> approxPlacable = placable();
|
|
||||||
List<BetterBlockPos> placable = incorrectPositions.stream().filter(pos -> bcc.bsi.get0(pos).getBlock() == Blocks.AIR && approxPlacable.contains(bcc.getSchematic(pos.x, pos.y, pos.z))).collect(Collectors.toList());
|
List<BetterBlockPos> placable = incorrectPositions.stream().filter(pos -> bcc.bsi.get0(pos).getBlock() == Blocks.AIR && approxPlacable.contains(bcc.getSchematic(pos.x, pos.y, pos.z))).collect(Collectors.toList());
|
||||||
Goal[] toBreak = incorrectPositions.stream().filter(pos -> bcc.bsi.get0(pos).getBlock() != Blocks.AIR).map(GoalBreak::new).toArray(Goal[]::new);
|
Goal[] toBreak = incorrectPositions.stream().filter(pos -> bcc.bsi.get0(pos).getBlock() != Blocks.AIR).map(GoalBreak::new).toArray(Goal[]::new);
|
||||||
Goal[] toPlace = placable.stream().filter(pos -> !placable.contains(pos.down()) && !placable.contains(pos.down(2))).map(pos -> placementgoal(pos, bcc)).toArray(Goal[]::new);
|
Goal[] toPlace = placable.stream().filter(pos -> !placable.contains(pos.down()) && !placable.contains(pos.down(2))).map(pos -> placementgoal(pos, bcc)).toArray(Goal[]::new);
|
||||||
@ -484,16 +515,9 @@ public class BuilderProcess extends BaritoneProcessHelper implements IBuilderPro
|
|||||||
return "Building " + name;
|
return "Building " + name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public List<IBlockState> placable(int size) {
|
||||||
* Hotbar contents, if they were placed
|
|
||||||
* <p>
|
|
||||||
* Always length nine, empty slots become Blocks.AIR.getDefaultState()
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public List<IBlockState> placable() {
|
|
||||||
List<IBlockState> result = new ArrayList<>();
|
List<IBlockState> result = new ArrayList<>();
|
||||||
for (int i = 0; i < 9; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
ItemStack stack = ctx.player().inventory.mainInventory.get(i);
|
ItemStack stack = ctx.player().inventory.mainInventory.get(i);
|
||||||
if (stack.isEmpty() || !(stack.getItem() instanceof ItemBlock)) {
|
if (stack.isEmpty() || !(stack.getItem() instanceof ItemBlock)) {
|
||||||
result.add(Blocks.AIR.getDefaultState());
|
result.add(Blocks.AIR.getDefaultState());
|
||||||
@ -520,7 +544,7 @@ public class BuilderProcess extends BaritoneProcessHelper implements IBuilderPro
|
|||||||
|
|
||||||
public BuilderCalculationContext() {
|
public BuilderCalculationContext() {
|
||||||
super(BuilderProcess.this.baritone, true); // wew lad
|
super(BuilderProcess.this.baritone, true); // wew lad
|
||||||
this.placable = placable();
|
this.placable = placable(9);
|
||||||
this.schematic = BuilderProcess.this.schematic;
|
this.schematic = BuilderProcess.this.schematic;
|
||||||
this.originX = origin.getX();
|
this.originX = origin.getX();
|
||||||
this.originY = origin.getY();
|
this.originY = origin.getY();
|
||||||
|
@ -258,7 +258,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
|
|||||||
String file;
|
String file;
|
||||||
BlockPos origin;
|
BlockPos origin;
|
||||||
try {
|
try {
|
||||||
String[] coords = msg.substring("build".length()).split(" ");
|
String[] coords = msg.substring("build".length()).trim().split(" ");
|
||||||
file = coords[0] + ".schematic";
|
file = coords[0] + ".schematic";
|
||||||
origin = new BlockPos(Integer.parseInt(coords[1]), Integer.parseInt(coords[2]), Integer.parseInt(coords[3]));
|
origin = new BlockPos(Integer.parseInt(coords[1]), Integer.parseInt(coords[2]), Integer.parseInt(coords[3]));
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user