Skip to content

Commit 3d32183

Browse files
committed
example wander module
1 parent 55ed40a commit 3d32183

5 files changed

Lines changed: 176 additions & 1 deletion

File tree

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.1-bin.zip
44
networkTimeout=10000
55
validateDistributionUrl=true
66
zipStoreBase=GRADLE_USER_HOME

src/main/java/org/example/ExampleConfig.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,11 @@ public static class ExampleModuleConfig {
1717
public boolean enabled = true;
1818
public int delayTicks = 250;
1919
}
20+
21+
public final ExampleWanderConfig wander = new ExampleWanderConfig();
22+
public static final class ExampleWanderConfig {
23+
public boolean enabled = false;
24+
public int radius = 2000;
25+
public int minRadius = 100;
26+
}
2027
}

src/main/java/org/example/ExamplePlugin.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import net.kyori.adventure.text.logger.slf4j.ComponentLogger;
77
import org.example.command.ExampleCommand;
88
import org.example.command.ExampleESPCommand;
9+
import org.example.command.ExampleWanderCommand;
910
import org.example.module.ExampleESPModule;
1011
import org.example.module.ExampleModule;
12+
import org.example.module.ExampleWanderModule;
1113

1214
@Plugin(
1315
id = "example-plugin",
@@ -32,8 +34,10 @@ public void onLoad(PluginAPI pluginAPI) {
3234
PLUGIN_CONFIG = pluginAPI.registerConfig("example-plugin", ExampleConfig.class);
3335
pluginAPI.registerModule(new ExampleModule());
3436
pluginAPI.registerModule(new ExampleESPModule());
37+
pluginAPI.registerModule(new ExampleWanderModule());
3538
pluginAPI.registerCommand(new ExampleCommand());
3639
pluginAPI.registerCommand(new ExampleESPCommand());
40+
pluginAPI.registerCommand(new ExampleWanderCommand());
3741
LOG.info("Example Plugin loaded!");
3842
}
3943
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package org.example.command;
2+
3+
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
4+
import com.zenith.command.api.Command;
5+
import com.zenith.command.api.CommandCategory;
6+
import com.zenith.command.api.CommandContext;
7+
import com.zenith.command.api.CommandUsage;
8+
import com.zenith.discord.Embed;
9+
import org.example.module.ExampleWanderModule;
10+
11+
import static com.mojang.brigadier.arguments.IntegerArgumentType.getInteger;
12+
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
13+
import static com.zenith.Globals.MODULE;
14+
import static com.zenith.command.brigadier.ToggleArgumentType.getToggle;
15+
import static com.zenith.command.brigadier.ToggleArgumentType.toggle;
16+
import static org.example.ExamplePlugin.PLUGIN_CONFIG;
17+
18+
public class ExampleWanderCommand extends Command {
19+
@Override
20+
public CommandUsage commandUsage() {
21+
return CommandUsage.builder()
22+
.name("wander")
23+
.category(CommandCategory.MODULE)
24+
.description("""
25+
Randomly moves the player around in the world
26+
""")
27+
.usageLines(
28+
"on/off",
29+
"radius <blocks>",
30+
"minRadius <blocks"
31+
)
32+
.build();
33+
}
34+
35+
@Override
36+
public LiteralArgumentBuilder<CommandContext> register() {
37+
return command("wander")
38+
.then(argument("toggle", toggle()).executes(c -> {
39+
PLUGIN_CONFIG.wander.enabled = getToggle(c, "toggle");
40+
c.getSource().getEmbed()
41+
.title("Wander " + toggleStrCaps(PLUGIN_CONFIG.wander.enabled));
42+
MODULE.get(ExampleWanderModule.class).syncEnabledFromConfig();
43+
}))
44+
.then(literal("radius").then(argument("blocks", integer(1)).executes(c -> {
45+
int radius = getInteger(c, "blocks");
46+
if (radius < PLUGIN_CONFIG.wander.minRadius) {
47+
c.getSource().getEmbed()
48+
.title("Error")
49+
.description("Radius must be greater than minRadius");
50+
return ERROR;
51+
}
52+
PLUGIN_CONFIG.wander.radius = radius;
53+
c.getSource().getEmbed()
54+
.title("Radius Set");
55+
return OK;
56+
})))
57+
.then(literal("minRadius").then(argument("blocks", integer(1)).executes(c -> {
58+
int radius = getInteger(c, "blocks");
59+
if (radius > PLUGIN_CONFIG.wander.radius) {
60+
c.getSource().getEmbed()
61+
.title("Error")
62+
.description("Min Radius must be less than radius");
63+
return ERROR;
64+
}
65+
PLUGIN_CONFIG.wander.minRadius = radius;
66+
c.getSource().getEmbed()
67+
.title("Min Radius Set");
68+
return OK;
69+
})));
70+
71+
}
72+
73+
@Override
74+
public void defaultEmbed(Embed embed) {
75+
embed
76+
.addField("Wander", toggleStr(PLUGIN_CONFIG.wander.enabled))
77+
.addField("Radius", PLUGIN_CONFIG.wander.radius)
78+
.addField("Min Radius", PLUGIN_CONFIG.wander.minRadius)
79+
.primaryColor();
80+
}
81+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package org.example.module;
2+
3+
import com.github.rfresh2.EventConsumer;
4+
import com.zenith.event.client.ClientBotTick;
5+
import com.zenith.feature.pathfinder.goals.GoalXZ;
6+
import com.zenith.module.api.Module;
7+
import com.zenith.util.math.MathHelper;
8+
import com.zenith.util.timer.Timer;
9+
import com.zenith.util.timer.Timers;
10+
11+
import java.util.List;
12+
import java.util.concurrent.ThreadLocalRandom;
13+
import java.util.concurrent.TimeUnit;
14+
15+
import static com.github.rfresh2.EventConsumer.of;
16+
import static com.zenith.Globals.BARITONE;
17+
import static com.zenith.Globals.CACHE;
18+
import static org.example.ExamplePlugin.PLUGIN_CONFIG;
19+
20+
public class ExampleWanderModule extends Module {
21+
private final Timer pathTimer = Timers.tickTimer();
22+
private long lastPathTime = 0L;
23+
private long lastStuckWarning = 0L;
24+
public GoalXZ goal = new GoalXZ(0, 0);
25+
26+
@Override
27+
public List<EventConsumer<?>> registerEvents() {
28+
return List.of(
29+
of(ClientBotTick.class, this::handleBotTick),
30+
of(ClientBotTick.Starting.class, this::handleBotTickStarting)
31+
);
32+
}
33+
34+
@Override
35+
public boolean enabledSetting() {
36+
return PLUGIN_CONFIG.wander.enabled;
37+
}
38+
39+
@Override
40+
public void onDisable() {
41+
if (BARITONE.isGoalActive(goal)) {
42+
debug("Stopping active pathing goal");
43+
BARITONE.stop();
44+
}
45+
lastPathTime = 0L;
46+
lastStuckWarning = 0L;
47+
}
48+
49+
private void handleBotTickStarting(ClientBotTick.Starting event) {
50+
lastPathTime = 0L;
51+
lastStuckWarning = 0L;
52+
}
53+
54+
private void handleBotTick(ClientBotTick event) {
55+
if (!BARITONE.isActive() && pathTimer.tick(20L)) {
56+
if (System.currentTimeMillis() - lastPathTime < TimeUnit.MINUTES.toMillis(1)) {
57+
if (System.currentTimeMillis() - lastStuckWarning > TimeUnit.MINUTES.toMillis(5)) {
58+
warn("we are likely stuck :(");
59+
lastStuckWarning = System.currentTimeMillis();
60+
}
61+
return;
62+
}
63+
64+
int currentX = MathHelper.floorI(CACHE.getPlayerCache().getX());
65+
int currentZ = MathHelper.floorI(CACHE.getPlayerCache().getZ());
66+
int radius = PLUGIN_CONFIG.wander.radius;
67+
int minRadius = PLUGIN_CONFIG.wander.minRadius;
68+
int bound = radius - minRadius;
69+
int goalX = ThreadLocalRandom.current().nextInt(currentX - bound, currentX + bound);
70+
// shift goalX to be within the bounds of the active radius (area between radius and minRadius)
71+
goalX += goalX < currentX ? -minRadius : minRadius;
72+
int goalZ = ThreadLocalRandom.current().nextInt(currentZ - bound, currentZ + bound);
73+
goalZ += goalZ < currentZ ? -minRadius : minRadius;
74+
goal = new GoalXZ(goalX, goalZ);
75+
info("Pathing to goal: [{}, {}]", goalX, goalZ);
76+
BARITONE.pathTo(goal).addExecutedListener(f -> {
77+
info("Reached wander goal! [{}, {}]", goal.x(), goal.z());
78+
pathTimer.skip();
79+
});
80+
lastPathTime = System.currentTimeMillis();
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)