Skip to content

Commit

Permalink
Improve pathfinding command implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Apr 7, 2024
1 parent ee746be commit 20c1e9e
Showing 1 changed file with 24 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ private static String getCurrentUsername() {
}
}

public static void executePathfinding(BotConnection bot, GoalScorer goalScorer) {
PathExecutor.executePathfinding(bot, goalScorer, new CompletableFuture<>());
}

@PostConstruct
public void postConstruct() {
loadCommandHistory();
Expand Down Expand Up @@ -197,24 +193,20 @@ public void postConstruct() {
c -> {
var radius = IntegerArgumentType.getInteger(c, "radius");

return forEveryBot(
c,
bot -> {
var random = ThreadLocalRandom.current();
var pos = bot.sessionDataManager().clientEntity().pos();
var x =
random.nextInt(
pos.getFloorX() - radius,
pos.getFloorX() + radius);
var z =
random.nextInt(
pos.getFloorZ() - radius,
pos.getFloorZ() + radius);

executePathfinding(bot, new XZGoal(x, z));

return Command.SINGLE_SUCCESS;
});
return executePathfinding(c, bot -> {
var random = ThreadLocalRandom.current();
var pos = bot.sessionDataManager().clientEntity().pos();
var x =
random.nextInt(
pos.getFloorX() - radius,
pos.getFloorX() + radius);
var z =
random.nextInt(
pos.getFloorZ() - radius,
pos.getFloorZ() + radius);

return new XZGoal(x, z);
});
}))))
.then(
argument("y", IntegerArgumentType.integer())
Expand All @@ -223,7 +215,7 @@ public void postConstruct() {
"Makes all connected bots walk to the y coordinates",
c -> {
var y = IntegerArgumentType.getInteger(c, "y");
return executePathfinding(c, new YGoal(y));
return executePathfinding(c, bot -> new YGoal(y));
})))
.then(
argument("x", IntegerArgumentType.integer())
Expand All @@ -236,7 +228,7 @@ public void postConstruct() {
var x = IntegerArgumentType.getInteger(c, "x");
var z = IntegerArgumentType.getInteger(c, "z");

return executePathfinding(c, new XZGoal(x, z));
return executePathfinding(c, bot -> new XZGoal(x, z));
}))))
.then(
argument("x", IntegerArgumentType.integer())
Expand All @@ -252,7 +244,7 @@ public void postConstruct() {
var y = IntegerArgumentType.getInteger(c, "y");
var z = IntegerArgumentType.getInteger(c, "z");

return executePathfinding(c, new PosGoal(x, y, z));
return executePathfinding(c, bot -> new PosGoal(x, y, z));
}))))));
dispatcher.register(
literal("collect")
Expand All @@ -276,13 +268,11 @@ public void postConstruct() {
c,
bot -> {
bot.executorManager().newExecutorService(bot, "Pathfinding")
.submit(() -> {
new CollectBlockController(
resolvable.resolve(bot.sessionDataManager().tagsState()),
amount,
searchRadius
).start(bot);
});
.submit(() -> new CollectBlockController(
resolvable.resolve(bot.sessionDataManager().tagsState()),
amount,
searchRadius
).start(bot));

return Command.SINGLE_SUCCESS;
});
Expand Down Expand Up @@ -821,11 +811,11 @@ private int forEveryBot(
printMessages);
}

public int executePathfinding(CommandContext<ConsoleSubject> context, GoalScorer goalScorer) {
public int executePathfinding(CommandContext<ConsoleSubject> context, Function<BotConnection, GoalScorer> goalScorerFactory) {
return forEveryBot(
context,
bot -> {
executePathfinding(bot, goalScorer);
PathExecutor.executePathfinding(bot, goalScorerFactory.apply(bot), new CompletableFuture<>());
return Command.SINGLE_SUCCESS;
});
}
Expand Down

0 comments on commit 20c1e9e

Please sign in to comment.