Skip to content

Commit

Permalink
Update bot action
Browse files Browse the repository at this point in the history
  • Loading branch information
Lumine1909 committed Sep 6, 2024
1 parent c3ee619 commit c8f77c3
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 49 deletions.
22 changes: 10 additions & 12 deletions patches/api/0003-Add-fakeplayer-api.patch
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ index 594deedd08c3b3255fe6838471d945759f09a182..6fa638198f75458177af795f00250ce9
}
diff --git a/src/main/java/org/leavesmc/leaves/entity/Bot.java b/src/main/java/org/leavesmc/leaves/entity/Bot.java
new file mode 100644
index 0000000000000000000000000000000000000000..922ca5b27bc0dd443d635646f37f879559cc0252
index 0000000000000000000000000000000000000000..4dd8a64d2e6b82dcef3adfaf8be2d7c09779986f
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/entity/Bot.java
@@ -0,0 +1,51 @@
Expand All @@ -77,20 +77,20 @@ index 0000000000000000000000000000000000000000..922ca5b27bc0dd443d635646f37f8795
+import java.util.UUID;
+
+/**
+ * Represents a fakeplayer
+ * Represent a fakeplayer
+ */
+public interface Bot extends Player {
+
+ /**
+ * Gets the fakeplayer skin
+ * Get the fakeplayer skin
+ *
+ * @return fakeplayer skin name
+ */
+ @Nullable
+ public String getSkinName();
+
+ /**
+ * Gets the fakeplayer name without prefix and suffix
+ * Get the fakeplayer name without prefix and suffix
+ *
+ * @return fakeplayer real name
+ */
Expand All @@ -101,22 +101,22 @@ index 0000000000000000000000000000000000000000..922ca5b27bc0dd443d635646f37f8795
+ public UUID getCreatePlayerUUID();
+
+ /**
+ * Sets the fakeplayer action with args.
+ * Schedule a fakeplayer action with args.
+ *
+ * @param action action name
+ * @param player player who create this action
+ * @param args passed action arguments
+ */
+ public boolean setBotAction(@NotNull String action, @NotNull Player player, @NotNull String[] args);
+ public boolean scheduleBotAction(@NotNull String action, @NotNull Player player, @NotNull String[] args);
+
+ /**
+ * Sets the fakeplayer action with args.
+ * Schedule a fakeplayer action with args.
+ *
+ * @param action leaves bot action
+ * @param player player who create this action
+ * @param args passed action arguments
+ */
+ public boolean setBotAction(@NotNull LeavesBotAction action, @NotNull Player player, @NotNull String[] args);
+ public boolean scheduleBotAction(@NotNull LeavesBotAction action, @NotNull Player player, @NotNull String[] args);
+}
diff --git a/src/main/java/org/leavesmc/leaves/entity/BotManager.java b/src/main/java/org/leavesmc/leaves/entity/BotManager.java
new file mode 100644
Expand Down Expand Up @@ -242,19 +242,17 @@ index 0000000000000000000000000000000000000000..0b1648013d5f03d064c0719c23198108
+}
diff --git a/src/main/java/org/leavesmc/leaves/entity/botaction/LeavesBotAction.java b/src/main/java/org/leavesmc/leaves/entity/botaction/LeavesBotAction.java
new file mode 100644
index 0000000000000000000000000000000000000000..a6fdf8c77318172621494af94a0efb2bd34ca651
index 0000000000000000000000000000000000000000..10e2a51094aee81a9ad8d57b34936c69eee71d47
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/entity/botaction/LeavesBotAction.java
@@ -0,0 +1,36 @@
@@ -0,0 +1,34 @@
+package org.leavesmc.leaves.entity.botaction;
+
+/**
+ * A Leaves bot action enum
+ */
+public enum LeavesBotAction {
+ ATTACK("attack"),
+ @Deprecated(since = "1.21.1", forRemoval = true)
+ ATTACK_SELF("attack_self"),
+ BREAK("break"),
+ DROP("drop"),
+ FISH("fish"),
Expand Down
105 changes: 68 additions & 37 deletions patches/server/0010-Fakeplayer-support.patch
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ index 22f1ed383313829b8af4badda9ef8dc85cae8fd1..1c47e320e464af9651953ff308a2583f
// Paper end
diff --git a/src/main/java/org/leavesmc/leaves/bot/BotCommand.java b/src/main/java/org/leavesmc/leaves/bot/BotCommand.java
new file mode 100644
index 0000000000000000000000000000000000000000..0b2a39dbb4c2bd5c606976df7a2497812c591ace
index 0000000000000000000000000000000000000000..4a07c9f73955e5ddabd5c65f6570248e6ab6004e
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/bot/BotCommand.java
@@ -0,0 +1,427 @@
Expand Down Expand Up @@ -990,7 +990,7 @@ index 0000000000000000000000000000000000000000..0b2a39dbb4c2bd5c606976df7a249781
+ Bukkit.getPluginManager().callEvent(event);
+
+ if (!event.isCancelled()) {
+ bot.setBotAction(newAction);
+ bot.scheduleBotAction(newAction);
+ sender.sendMessage("Action " + action.getName() + " has been issued to " + bot.getName().getString());
+ }
+ }
Expand Down Expand Up @@ -1918,10 +1918,10 @@ index 0000000000000000000000000000000000000000..0db337866c71283464d026a4f230016b
+}
diff --git a/src/main/java/org/leavesmc/leaves/bot/ServerBot.java b/src/main/java/org/leavesmc/leaves/bot/ServerBot.java
new file mode 100644
index 0000000000000000000000000000000000000000..d84b15b6e366e158a9ca99a63c05417f087e7c27
index 0000000000000000000000000000000000000000..e8ec347dc8967f21ad3bb3012124667575031b3d
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/bot/ServerBot.java
@@ -0,0 +1,543 @@
@@ -0,0 +1,525 @@
+package org.leavesmc.leaves.bot;
+
+import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -1969,6 +1969,7 @@ index 0000000000000000000000000000000000000000..d84b15b6e366e158a9ca99a63c05417f
+import org.leavesmc.leaves.LeavesConfig;
+import org.leavesmc.leaves.LeavesLogger;
+import org.leavesmc.leaves.bot.agent.BotAction;
+import org.leavesmc.leaves.bot.agent.BotActionManager;
+import org.leavesmc.leaves.bot.agent.BotConfig;
+import org.leavesmc.leaves.bot.agent.Configs;
+import org.leavesmc.leaves.bot.agent.actions.StopAction;
Expand All @@ -1981,8 +1982,6 @@ index 0000000000000000000000000000000000000000..d84b15b6e366e158a9ca99a63c05417f
+
+import java.util.Collection;
+import java.util.EnumSet;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
Expand All @@ -1993,7 +1992,7 @@ index 0000000000000000000000000000000000000000..d84b15b6e366e158a9ca99a63c05417f
+public class ServerBot extends ServerPlayer {
+
+ private final Map<Configs<?>, BotConfig<?>> configs;
+ private final Map<String, BotAction> actions;
+ private final BotActionManager actions;
+ private final int tracingRange;
+
+ public BotCreateState createState;
Expand All @@ -2014,7 +2013,7 @@ index 0000000000000000000000000000000000000000..d84b15b6e366e158a9ca99a63c05417f
+ this.entityData.set(Player.DATA_PLAYER_MODE_CUSTOMISATION, (byte) -2);
+
+ this.gameMode = new ServerBotGameMode(this);
+ this.actions = new HashMap<>();
+ this.actions = new BotActionManager(this);
+
+ ImmutableMap.Builder<Configs<?>, BotConfig<?>> configBuilder = ImmutableMap.builder();
+ for (Configs<?> config : Configs.getConfigs()) {
Expand Down Expand Up @@ -2162,15 +2161,7 @@ index 0000000000000000000000000000000000000000..d84b15b6e366e158a9ca99a63c05417f
+
+ // this.checkFallDamage(vec3d1.y, this.onGround(), iblockdata, blockposition);
+
+ Iterator<Map.Entry<String, BotAction>> iterator = actions.entrySet().iterator();
+ while (iterator.hasNext()) {
+ Map.Entry<String, BotAction> entry = iterator.next();
+ if (entry.getValue().isCancel()) {
+ iterator.remove();
+ } else {
+ entry.getValue().tryTick(this);
+ }
+ }
+ actions.tick();
+ }
+
+ public Entity getTargetEntity(int maxDistance, Predicate<? super Entity> predicate) {
Expand Down Expand Up @@ -2345,28 +2336,19 @@ index 0000000000000000000000000000000000000000..d84b15b6e366e158a9ca99a63c05417f
+ this.detectEquipmentUpdatesPublic();
+ }
+
+ public void setBotAction(BotAction action) {
+ public void scheduleBotAction(BotAction action) {
+ if (!LeavesConfig.fakeplayerUseAction) {
+ return;
+ }
+ if (action instanceof StopAction) {
+ this.actions.clear();
+ this.actions.stopAll();
+ }
+ action.init();
+ this.actions.put(action.getName(), action);
+ this.actions.addAction(action);
+ }
+
+ public Collection<BotAction> getBotActions() {
+ return actions.values();
+ }
+
+ public BotAction getBotAction(String name) {
+ return actions.get(name);
+ }
+
+ @Deprecated
+ public BotAction getBotAction() {
+ return null;
+ return actions.getActions();
+ }
+
+ @Override
Expand Down Expand Up @@ -2775,10 +2757,10 @@ index 0000000000000000000000000000000000000000..15cbf363f587a27d55f4bc7ec8977871
+}
diff --git a/src/main/java/org/leavesmc/leaves/bot/agent/BotAction.java b/src/main/java/org/leavesmc/leaves/bot/agent/BotAction.java
new file mode 100644
index 0000000000000000000000000000000000000000..9abcb8852ac031abaa991881a7cd6b33bc523b26
index 0000000000000000000000000000000000000000..29a2cf8637bfe797936a936a5e84203d8a2aae45
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/bot/agent/BotAction.java
@@ -0,0 +1,93 @@
@@ -0,0 +1,97 @@
+package org.leavesmc.leaves.bot.agent;
+
+import net.minecraft.server.level.ServerPlayer;
Expand Down Expand Up @@ -2864,6 +2846,10 @@ index 0000000000000000000000000000000000000000..9abcb8852ac031abaa991881a7cd6b33
+ }
+ }
+
+ public boolean isValid() {
+ return this.canDoNumber > 0 && !this.cancel;
+ }
+
+ public CommandArgument getArgument() {
+ return argument;
+ }
Expand All @@ -2872,6 +2858,51 @@ index 0000000000000000000000000000000000000000..9abcb8852ac031abaa991881a7cd6b33
+
+ public abstract boolean doTick(@NotNull ServerBot bot);
+}
diff --git a/src/main/java/org/leavesmc/leaves/bot/agent/BotActionManager.java b/src/main/java/org/leavesmc/leaves/bot/agent/BotActionManager.java
new file mode 100644
index 0000000000000000000000000000000000000000..9b53cff2afebb26b0a761de06f4ef681d9965661
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/bot/agent/BotActionManager.java
@@ -0,0 +1,39 @@
+package org.leavesmc.leaves.bot.agent;
+
+import org.leavesmc.leaves.bot.ServerBot;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+public class BotActionManager {
+
+ private final ServerBot bot;
+ private final List<BotAction> actions = new ArrayList<>();
+
+ public BotActionManager(ServerBot bot) {
+ this.bot = bot;
+ }
+
+ public Collection<BotAction> getActions() {
+ return actions;
+ }
+
+ public void tick() {
+ if (actions.isEmpty()) {
+ return;
+ }
+ for (BotAction action : actions) {
+ action.tryTick(bot);
+ }
+ actions.removeIf(actions -> !actions.isValid());
+ }
+
+ public void addAction(BotAction action) {
+ actions.add(action);
+ }
+
+ public void stopAll() {
+ actions.clear();
+ }
+}
diff --git a/src/main/java/org/leavesmc/leaves/bot/agent/BotConfig.java b/src/main/java/org/leavesmc/leaves/bot/agent/BotConfig.java
new file mode 100644
index 0000000000000000000000000000000000000000..bc40f2ee606d5098b30f6a6bde0a163a939e9a3a
Expand Down Expand Up @@ -4065,7 +4096,7 @@ index 0d6d33e66461dda39c8f0e8395bf3f047ef88cbd..4ca3508475bbd9771768704e300fe12b
}
diff --git a/src/main/java/org/leavesmc/leaves/entity/CraftBot.java b/src/main/java/org/leavesmc/leaves/entity/CraftBot.java
new file mode 100644
index 0000000000000000000000000000000000000000..542a458f99a10167dec898ee2f28465d5ff80b36
index 0000000000000000000000000000000000000000..dbc4444c158e18f83b993063bd2fb991970a7210
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/entity/CraftBot.java
@@ -0,0 +1,67 @@
Expand Down Expand Up @@ -4105,21 +4136,21 @@ index 0000000000000000000000000000000000000000..542a458f99a10167dec898ee2f28465d
+ }
+
+ @Override
+ public boolean setBotAction(@NotNull String action, @NotNull Player player, @NotNull String[] args) {
+ public boolean scheduleBotAction(@NotNull String action, @NotNull Player player, @NotNull String[] args) {
+ BotAction botAction = Actions.getForName(action);
+ if (botAction != null) {
+ BotAction newAction = botAction.getNew(((CraftPlayer) player).getHandle(), botAction.getArgument().parse(0, args));
+ if (newAction != null) {
+ getHandle().setBotAction(newAction);
+ getHandle().scheduleBotAction(newAction);
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public boolean setBotAction(@NotNull LeavesBotAction action, @NotNull Player player, @NotNull String[] args) {
+ return setBotAction(action.getName(), player, args);
+ public boolean scheduleBotAction(@NotNull LeavesBotAction action, @NotNull Player player, @NotNull String[] args) {
+ return scheduleBotAction(action.getName(), player, args);
+ }
+
+ @Override
Expand Down

0 comments on commit c8f77c3

Please sign in to comment.