From 23c47a3ccee96b28257f51f304fc5879f7d7f7dd Mon Sep 17 00:00:00 2001 From: MC_XiaoHei Date: Wed, 18 Sep 2024 21:05:41 +0800 Subject: [PATCH] Enhance bot api, fix somethings (#341) --- patches/api/0003-Add-fakeplayer-api.patch | 82 +++++++++++++------- patches/api/0009-Replay-Mod-API.patch | 9 --- patches/server/0010-Fakeplayer-support.patch | 69 ++++++++++------ 3 files changed, 99 insertions(+), 61 deletions(-) diff --git a/patches/api/0003-Add-fakeplayer-api.patch b/patches/api/0003-Add-fakeplayer-api.patch index 725ba330..9e9e1386 100644 --- a/patches/api/0003-Add-fakeplayer-api.patch +++ b/patches/api/0003-Add-fakeplayer-api.patch @@ -4,15 +4,6 @@ Date: Wed, 27 Jul 2022 15:30:34 +0800 Subject: [PATCH] Add fakeplayer api -diff --git a/.gitignore b/.gitignore -index 97e78e27ee0eea2c8b24886eeb19164d552323fe..9764fa643039f215627c20a33ca70c9e36b2d599 100644 ---- a/.gitignore -+++ b/.gitignore -@@ -38,3 +38,4 @@ - # vs code - /.vscode - /.factorypath -+ diff --git a/src/main/java/org/bukkit/Bukkit.java b/src/main/java/org/bukkit/Bukkit.java index e20e4239a5a1f952e1c70e899549989d5e42f73c..5f6078bc80e20c9482a96d2bf1095bb32fbfc28c 100644 --- a/src/main/java/org/bukkit/Bukkit.java @@ -149,10 +140,10 @@ index 0000000000000000000000000000000000000000..7a1ee45d571687317883b896f3ec0a83 +} diff --git a/src/main/java/org/leavesmc/leaves/entity/BotCreator.java b/src/main/java/org/leavesmc/leaves/entity/BotCreator.java new file mode 100644 -index 0000000000000000000000000000000000000000..98c7e87854eae9760a6f4427c6b052b192df2b45 +index 0000000000000000000000000000000000000000..3c0d985c3e293144f4fef0e447f9bd981bf7912f --- /dev/null +++ b/src/main/java/org/leavesmc/leaves/entity/BotCreator.java -@@ -0,0 +1,52 @@ +@@ -0,0 +1,53 @@ +package org.leavesmc.leaves.entity; + +import org.bukkit.Bukkit; @@ -197,7 +188,8 @@ index 0000000000000000000000000000000000000000..98c7e87854eae9760a6f4427c6b052b1 + public Bot spawn(); + + /** -+ * Create a bot and apply skin ++ * Create a bot and apply skin of player names `skinName` from MojangAPI ++ * just like `mojangAPISkin().spawn()`, but async + *

+ * you can not get the bot instance instantly because get skin in on async thread + * @@ -542,15 +534,17 @@ index 0000000000000000000000000000000000000000..69a99679d407f974ef0e414945d3bcc7 + diff --git a/src/main/java/org/leavesmc/leaves/event/bot/BotActionScheduleEvent.java b/src/main/java/org/leavesmc/leaves/event/bot/BotActionScheduleEvent.java new file mode 100644 -index 0000000000000000000000000000000000000000..7189649e608d41511d4213c1c3938996361290df +index 0000000000000000000000000000000000000000..f1123d55712cc90a1d0bb79b7832c90abb77ae90 --- /dev/null +++ b/src/main/java/org/leavesmc/leaves/event/bot/BotActionScheduleEvent.java -@@ -0,0 +1,39 @@ +@@ -0,0 +1,48 @@ +package org.leavesmc.leaves.event.bot; + ++import org.bukkit.command.CommandSender; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; ++import org.jetbrains.annotations.Nullable; +import org.leavesmc.leaves.entity.Bot; + +import java.util.UUID; @@ -560,9 +554,11 @@ index 0000000000000000000000000000000000000000..7189649e608d41511d4213c1c3938996 + private static final HandlerList handlers = new HandlerList(); + + private boolean cancel = false; ++ private final CommandSender sender; + -+ public BotActionScheduleEvent(@NotNull Bot who, String actionName, UUID actionUUID) { ++ public BotActionScheduleEvent(@NotNull Bot who, String actionName, UUID actionUUID, CommandSender sender) { + super(who, actionName, actionUUID); ++ this.sender = sender; + } + + @Override @@ -575,6 +571,11 @@ index 0000000000000000000000000000000000000000..7189649e608d41511d4213c1c3938996 + this.cancel = cancel; + } + ++ @Nullable ++ public CommandSender getSender() { ++ return sender; ++ } ++ + @Override + @NotNull + public HandlerList getHandlers() { @@ -587,21 +588,25 @@ index 0000000000000000000000000000000000000000..7189649e608d41511d4213c1c3938996 +} diff --git a/src/main/java/org/leavesmc/leaves/event/bot/BotActionStopEvent.java b/src/main/java/org/leavesmc/leaves/event/bot/BotActionStopEvent.java new file mode 100644 -index 0000000000000000000000000000000000000000..e8de7b19d72b3dfd6e4423096573b3a7ef737803 +index 0000000000000000000000000000000000000000..56822a944a13394f635a7640d76c66b4cc98698a --- /dev/null +++ b/src/main/java/org/leavesmc/leaves/event/bot/BotActionStopEvent.java -@@ -0,0 +1,36 @@ +@@ -0,0 +1,56 @@ +package org.leavesmc.leaves.event.bot; + ++import org.bukkit.command.CommandSender; ++import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; ++import org.jetbrains.annotations.Nullable; +import org.leavesmc.leaves.entity.Bot; + +import java.util.UUID; + -+public class BotActionStopEvent extends BotActionEvent { -+ ++public class BotActionStopEvent extends BotActionEvent implements Cancellable{ + private static final HandlerList handlers = new HandlerList(); ++ private boolean cancel = false; ++ private final CommandSender sender; + + public enum Reason { + DONE, COMMAND, PLUGIN, INTERNAL @@ -609,9 +614,10 @@ index 0000000000000000000000000000000000000000..e8de7b19d72b3dfd6e4423096573b3a7 + + private final Reason reason; + -+ public BotActionStopEvent(@NotNull Bot who, String actionName, UUID actionUUID, Reason stopReason) { ++ public BotActionStopEvent(@NotNull Bot who, String actionName, UUID actionUUID, Reason stopReason, CommandSender sender) { + super(who, actionName, actionUUID); + this.reason = stopReason; ++ this.sender = sender; + } + + public Reason getReason() { @@ -626,15 +632,31 @@ index 0000000000000000000000000000000000000000..e8de7b19d72b3dfd6e4423096573b3a7 + public static HandlerList getHandlerList() { + return handlers; + } ++ ++ @Override ++ public boolean isCancelled() { ++ return cancel; ++ } ++ ++ @Override ++ public void setCancelled(boolean cancel) { ++ this.cancel = cancel; ++ } ++ ++ @Nullable ++ public CommandSender getSender() { ++ return sender; ++ } +} diff --git a/src/main/java/org/leavesmc/leaves/event/bot/BotConfigModifyEvent.java b/src/main/java/org/leavesmc/leaves/event/bot/BotConfigModifyEvent.java new file mode 100644 -index 0000000000000000000000000000000000000000..053be37cb250d77b1c9f4c1bbd83a49da93027a7 +index 0000000000000000000000000000000000000000..5afa2df3566dd038b9f262ce8bc1f01c688b45a5 --- /dev/null +++ b/src/main/java/org/leavesmc/leaves/event/bot/BotConfigModifyEvent.java -@@ -0,0 +1,50 @@ +@@ -0,0 +1,53 @@ +package org.leavesmc.leaves.event.bot; + ++import org.bukkit.command.CommandSender; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; @@ -647,11 +669,13 @@ index 0000000000000000000000000000000000000000..053be37cb250d77b1c9f4c1bbd83a49d + private final String configName; + private final String[] configValue; + private boolean cancel; ++ private final CommandSender sender; + -+ public BotConfigModifyEvent(@NotNull Bot who, String configName, String[] configValue) { ++ public BotConfigModifyEvent(@NotNull Bot who, String configName, String[] configValue, CommandSender sender) { + super(who); + this.configName = configName; + this.configValue = configValue; ++ this.sender = sender; + } + + @NotNull @@ -685,7 +709,7 @@ index 0000000000000000000000000000000000000000..053be37cb250d77b1c9f4c1bbd83a49d +} diff --git a/src/main/java/org/leavesmc/leaves/event/bot/BotCreateEvent.java b/src/main/java/org/leavesmc/leaves/event/bot/BotCreateEvent.java new file mode 100644 -index 0000000000000000000000000000000000000000..08e382120feec65c2a842134a1643f236a120bbd +index 0000000000000000000000000000000000000000..11e55a99b91e4ea11640d3fe02bc7f0106d7b91c --- /dev/null +++ b/src/main/java/org/leavesmc/leaves/event/bot/BotCreateEvent.java @@ -0,0 +1,119 @@ @@ -700,7 +724,7 @@ index 0000000000000000000000000000000000000000..08e382120feec65c2a842134a1643f23 +import org.jetbrains.annotations.Nullable; + +/** -+ * Call when a fakeplayer creates a server ++ * Call when a fakeplayer created + */ +public class BotCreateEvent extends Event implements Cancellable { + @@ -1048,7 +1072,7 @@ index 0000000000000000000000000000000000000000..24e5f4d833897000e0378d4d3c3ff75c +} diff --git a/src/main/java/org/leavesmc/leaves/event/bot/BotLoadEvent.java b/src/main/java/org/leavesmc/leaves/event/bot/BotLoadEvent.java new file mode 100644 -index 0000000000000000000000000000000000000000..d4472675af540cdeeebf428144c70b9a5c3f34ce +index 0000000000000000000000000000000000000000..b5295bddb627985ff35ca4de55253bbd68593655 --- /dev/null +++ b/src/main/java/org/leavesmc/leaves/event/bot/BotLoadEvent.java @@ -0,0 +1,59 @@ @@ -1062,7 +1086,7 @@ index 0000000000000000000000000000000000000000..d4472675af540cdeeebf428144c70b9a +import java.util.UUID; + +/** -+ * Call when a fakeplayer loading a server ++ * Call when a fakeplayer loaded + */ +public class BotLoadEvent extends Event implements Cancellable { + @@ -1113,7 +1137,7 @@ index 0000000000000000000000000000000000000000..d4472675af540cdeeebf428144c70b9a +} diff --git a/src/main/java/org/leavesmc/leaves/event/bot/BotRemoveEvent.java b/src/main/java/org/leavesmc/leaves/event/bot/BotRemoveEvent.java new file mode 100644 -index 0000000000000000000000000000000000000000..408a7e39ee1923d595fb8ac3f91d60e14a2c446c +index 0000000000000000000000000000000000000000..76909d03f2b23602335b91f3b168e17f20256d34 --- /dev/null +++ b/src/main/java/org/leavesmc/leaves/event/bot/BotRemoveEvent.java @@ -0,0 +1,106 @@ @@ -1129,7 +1153,7 @@ index 0000000000000000000000000000000000000000..408a7e39ee1923d595fb8ac3f91d60e1 +import org.leavesmc.leaves.entity.Bot; + +/** -+ * Call when a fakeplayer creates a server ++ * Call when a fakeplayer removed + */ +public class BotRemoveEvent extends BotEvent implements Cancellable { + diff --git a/patches/api/0009-Replay-Mod-API.patch b/patches/api/0009-Replay-Mod-API.patch index a09b0308..ebb9b1f0 100644 --- a/patches/api/0009-Replay-Mod-API.patch +++ b/patches/api/0009-Replay-Mod-API.patch @@ -4,15 +4,6 @@ Date: Sat, 5 Aug 2023 09:10:59 +0800 Subject: [PATCH] Replay Mod API -diff --git a/.gitignore b/.gitignore -index 9764fa643039f215627c20a33ca70c9e36b2d599..97e78e27ee0eea2c8b24886eeb19164d552323fe 100644 ---- a/.gitignore -+++ b/.gitignore -@@ -38,4 +38,3 @@ - # vs code - /.vscode - /.factorypath -- diff --git a/src/main/java/org/bukkit/Bukkit.java b/src/main/java/org/bukkit/Bukkit.java index 5f6078bc80e20c9482a96d2bf1095bb32fbfc28c..d0a9b96f4ab200892d589a68b27585a08780f7ac 100644 --- a/src/main/java/org/bukkit/Bukkit.java diff --git a/patches/server/0010-Fakeplayer-support.patch b/patches/server/0010-Fakeplayer-support.patch index 028a08a7..dbbfdf58 100644 --- a/patches/server/0010-Fakeplayer-support.patch +++ b/patches/server/0010-Fakeplayer-support.patch @@ -626,10 +626,10 @@ index 4632eb883e9f5efde520ee543bcad25827c0da2c..d710803137a325f34e0628405d5ddfd0 return event; 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..a06f24253a2d941c8fd265f50dcaec356b5cfbcc +index 0000000000000000000000000000000000000000..5a1054414f5313b59d38d89fb84987cad397ff12 --- /dev/null +++ b/src/main/java/org/leavesmc/leaves/bot/BotCommand.java -@@ -0,0 +1,522 @@ +@@ -0,0 +1,543 @@ +package org.leavesmc.leaves.bot; + +import io.papermc.paper.command.CommandUtil; @@ -657,6 +657,7 @@ index 0000000000000000000000000000000000000000..a06f24253a2d941c8fd265f50dcaec35 +import org.leavesmc.leaves.bot.agent.actions.CraftCustomBotAction; +import org.leavesmc.leaves.command.CommandArgumentResult; +import org.leavesmc.leaves.entity.Bot; ++import org.leavesmc.leaves.event.bot.BotActionStopEvent; +import org.leavesmc.leaves.event.bot.BotConfigModifyEvent; +import org.leavesmc.leaves.event.bot.BotCreateEvent; +import org.leavesmc.leaves.event.bot.BotRemoveEvent; @@ -957,7 +958,18 @@ index 0000000000000000000000000000000000000000..a06f24253a2d941c8fd265f50dcaec35 + + String index = args[3]; + if (index.equals("all")) { -+ bot.getBotActions().clear(); ++ Set> forRemoval = new HashSet<>(); ++ for (int i = 0; i < bot.getBotActions().size(); i++) { ++ BotAction action = bot.getBotActions().get(i); ++ BotActionStopEvent event = new BotActionStopEvent( ++ bot.getBukkitEntity(), action.getName(), action.getUUID(), BotActionStopEvent.Reason.COMMAND, sender ++ ); ++ event.callEvent(); ++ if (!event.isCancelled()) { ++ forRemoval.add(action); ++ } ++ } ++ bot.getBotActions().removeAll(forRemoval); + sender.sendMessage(bot.getScoreboardName() + "'s action list cleared."); + } else { + try { @@ -967,8 +979,16 @@ index 0000000000000000000000000000000000000000..a06f24253a2d941c8fd265f50dcaec35 + return; + } + -+ BotAction action = bot.getBotActions().remove(i); -+ sender.sendMessage(bot.getScoreboardName() + "'s " + action.getName() + " stopped."); ++ BotAction action = bot.getBotActions().get(i); ++ BotActionStopEvent event = new BotActionStopEvent( ++ bot.getBukkitEntity(), action.getName(), action.getUUID(), BotActionStopEvent.Reason.COMMAND, sender ++ ); ++ event.callEvent(); ++ if (!event.isCancelled()) { ++ bot.getBotActions().remove(i); ++ sender.sendMessage(bot.getScoreboardName() + "'s " + action.getName() + " stopped."); ++ ++ } + } catch (NumberFormatException e) { + sender.sendMessage(text("Invalid index", NamedTextColor.RED)); + } @@ -1010,7 +1030,7 @@ index 0000000000000000000000000000000000000000..a06f24253a2d941c8fd265f50dcaec35 + return; + } + -+ if (bot.addBotAction(newAction)) { ++ if (bot.addBotAction(newAction, sender)) { + sender.sendMessage("Action " + action.getName() + " has been issued to " + bot.getName().getString()); + } + } @@ -1045,7 +1065,7 @@ index 0000000000000000000000000000000000000000..a06f24253a2d941c8fd265f50dcaec35 + String[] realArgs = new String[args.length - 3]; + System.arraycopy(args, 3, realArgs, 0, realArgs.length); + -+ BotConfigModifyEvent event = new BotConfigModifyEvent(bot.getBukkitEntity(), config.getName(), realArgs); ++ BotConfigModifyEvent event = new BotConfigModifyEvent(bot.getBukkitEntity(), config.getName(), realArgs, sender); + Bukkit.getPluginManager().callEvent(event); + + if (event.isCancelled()) { @@ -1080,8 +1100,9 @@ index 0000000000000000000000000000000000000000..a06f24253a2d941c8fd265f50dcaec35 + return; + } + -+ botList.removeBot(bot, BotRemoveEvent.RemoveReason.COMMAND, sender, true); -+ sender.sendMessage(bot.getScoreboardName() + " saved to " + bot.createState.realName()); ++ if (botList.removeBot(bot, BotRemoveEvent.RemoveReason.COMMAND, sender, true)) { ++ sender.sendMessage(bot.getScoreboardName() + " saved to " + bot.createState.realName()); ++ } + } + + private void onLoad(CommandSender sender, String @NotNull [] args) { @@ -1604,10 +1625,10 @@ index 0000000000000000000000000000000000000000..4f5e6e5c1b9d8bd38c98e97fd31b3833 +} diff --git a/src/main/java/org/leavesmc/leaves/bot/BotList.java b/src/main/java/org/leavesmc/leaves/bot/BotList.java new file mode 100644 -index 0000000000000000000000000000000000000000..003f1e01ba56ea2fa9ff311675c58b1d0a38d53b +index 0000000000000000000000000000000000000000..33deb7eeda2c4cda396ac4ed7f05e6a851e89050 --- /dev/null +++ b/src/main/java/org/leavesmc/leaves/bot/BotList.java -@@ -0,0 +1,338 @@ +@@ -0,0 +1,339 @@ +package org.leavesmc.leaves.bot; + +import com.google.common.collect.Maps; @@ -1813,16 +1834,16 @@ index 0000000000000000000000000000000000000000..003f1e01ba56ea2fa9ff311675c58b1d + } + } + -+ public void removeBot(@NotNull ServerBot bot, @NotNull BotRemoveEvent.RemoveReason reason, @Nullable CommandSender remover, boolean saved) { -+ this.removeBot(bot, reason, remover, saved, this.dataStorage); ++ public boolean removeBot(@NotNull ServerBot bot, @NotNull BotRemoveEvent.RemoveReason reason, @Nullable CommandSender remover, boolean saved) { ++ return this.removeBot(bot, reason, remover, saved, this.dataStorage); + } + -+ public void removeBot(@NotNull ServerBot bot, @NotNull BotRemoveEvent.RemoveReason reason, @Nullable CommandSender remover, boolean saved, IPlayerDataStorage playerIO) { ++ public boolean removeBot(@NotNull ServerBot bot, @NotNull BotRemoveEvent.RemoveReason reason, @Nullable CommandSender remover, boolean saved, IPlayerDataStorage playerIO) { + BotRemoveEvent event = new BotRemoveEvent(bot.getBukkitEntity(), reason, remover, PaperAdventure.asAdventure(Component.translatable("multiplayer.player.left", bot.getDisplayName())).style(Style.style(NamedTextColor.YELLOW)), saved); + this.server.server.getPluginManager().callEvent(event); + + if (event.isCancelled() && event.getReason() != BotRemoveEvent.RemoveReason.INTERNAL) { -+ return; ++ return event.isCancelled(); + } + + if (bot.removeTaskId != -1) { @@ -1878,6 +1899,7 @@ index 0000000000000000000000000000000000000000..003f1e01ba56ea2fa9ff311675c58b1d + if (removeMessage != null && !removeMessage.equals(net.kyori.adventure.text.Component.empty())) { + this.server.getPlayerList().broadcastSystemMessage(PaperAdventure.asVanilla(removeMessage), false); + } ++ return true; + } + + public void removeAll() { @@ -2133,10 +2155,10 @@ index 0000000000000000000000000000000000000000..517e3321b866abe9d17a6fe9e919528b +} 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..18cf5182c33c43690d7329d6bd435ebc8ee517e5 +index 0000000000000000000000000000000000000000..bab7790db401135cb9ea5b05f749dca5e7d0b95f --- /dev/null +++ b/src/main/java/org/leavesmc/leaves/bot/ServerBot.java -@@ -0,0 +1,543 @@ +@@ -0,0 +1,544 @@ +package org.leavesmc.leaves.bot; + +import com.google.common.collect.ImmutableMap; @@ -2181,6 +2203,7 @@ index 0000000000000000000000000000000000000000..18cf5182c33c43690d7329d6bd435ebc +import net.minecraft.world.phys.Vec3; +import org.bukkit.Bukkit; +import org.bukkit.Location; ++import org.bukkit.command.CommandSender; +import org.bukkit.util.Vector; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; @@ -2642,12 +2665,12 @@ index 0000000000000000000000000000000000000000..18cf5182c33c43690d7329d6bd435ebc + } + } + -+ public boolean addBotAction(BotAction action) { ++ public boolean addBotAction(BotAction action, CommandSender sender) { + if (!LeavesConfig.fakeplayerUseAction) { + return false; + } + -+ if (!new BotActionScheduleEvent(this.getBukkitEntity(), action.getName(), action.getUUID()).callEvent()) { ++ if (!new BotActionScheduleEvent(this.getBukkitEntity(), action.getName(), action.getUUID(), sender).callEvent()) { + return false; + } + @@ -2990,7 +3013,7 @@ index 0000000000000000000000000000000000000000..a37513e1ba8443c702ab0c01fbe5e052 +} 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..3ad5484f26e3dc7fb45c5d2ee0687604e6974404 +index 0000000000000000000000000000000000000000..3bd512b436b32f240466a406a101a051b4b07817 --- /dev/null +++ b/src/main/java/org/leavesmc/leaves/bot/agent/BotAction.java @@ -0,0 +1,163 @@ @@ -3082,7 +3105,7 @@ index 0000000000000000000000000000000000000000..3ad5484f26e3dc7fb45c5d2ee0687604 + } + + public void stop(@NotNull ServerBot bot, BotActionStopEvent.Reason reason) { -+ new BotActionStopEvent(bot.getBukkitEntity(), this.name, this.uuid, reason).callEvent(); ++ new BotActionStopEvent(bot.getBukkitEntity(), this.name, this.uuid, reason, null).callEvent(); + this.setCancelled(true); + } + @@ -4356,7 +4379,7 @@ index 0000000000000000000000000000000000000000..a3f978318a67c3c5e147a50eb2b6c01c +} 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..744f5533859bfe81267b3fd5133597e80e0fdc73 +index 0000000000000000000000000000000000000000..46aec2f954919f487e22ab953062b6889fe3e58b --- /dev/null +++ b/src/main/java/org/leavesmc/leaves/entity/CraftBot.java @@ -0,0 +1,84 @@ @@ -4399,7 +4422,7 @@ index 0000000000000000000000000000000000000000..744f5533859bfe81267b3fd5133597e8 + + @Override + public void addAction(@NotNull LeavesBotAction action) { -+ this.getHandle().addBotAction(CraftBotAction.asInternalCopy(action)); ++ this.getHandle().addBotAction(CraftBotAction.asInternalCopy(action), null); + } + + @Override