Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scheduled bot remove #274

Merged
merged 3 commits into from
Jul 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 82 additions & 11 deletions patches/server/0010-Fakeplayer-support.patch
Original file line number Diff line number Diff line change
Expand Up @@ -536,10 +536,10 @@ index 22f1ed383313829b8af4badda9ef8dc85cae8fd1..03af280bffbd2070abaf1de4b0c96389
*/
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..b9d4431b8994f79a4569bd267a6f5680b9650bff
index 0000000000000000000000000000000000000000..b64361c4151659d5e3f8884562cb811d60776c38
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/bot/BotCommand.java
@@ -0,0 +1,407 @@
@@ -0,0 +1,472 @@
+package org.leavesmc.leaves.bot;
+
+import org.bukkit.Bukkit;
Expand All @@ -550,6 +550,7 @@ index 0000000000000000000000000000000000000000..b9d4431b8994f79a4569bd267a6f5680
+import org.bukkit.command.CommandSender;
+import org.bukkit.command.ConsoleCommandSender;
+import org.bukkit.craftbukkit.entity.CraftPlayer;
+import org.bukkit.craftbukkit.scheduler.CraftScheduler;
+import org.bukkit.entity.Player;
+import org.bukkit.generator.WorldInfo;
+import org.bukkit.permissions.Permission;
Expand All @@ -566,11 +567,7 @@ index 0000000000000000000000000000000000000000..b9d4431b8994f79a4569bd267a6f5680
+import org.leavesmc.leaves.event.bot.BotCreateEvent;
+import org.leavesmc.leaves.event.bot.BotRemoveEvent;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.*;
+import java.util.stream.Stream;
+
+public class BotCommand extends Command {
Expand Down Expand Up @@ -618,6 +615,7 @@ index 0000000000000000000000000000000000000000..b9d4431b8994f79a4569bd267a6f5680
+ }
+ case "create" -> list.add("<BotSkinName>");
+ case "config" -> list.addAll(acceptConfig);
+ case "remove" -> list.addAll(List.of("cancel", "[hour]"));
+ }
+ }
+
Expand All @@ -633,6 +631,16 @@ index 0000000000000000000000000000000000000000..b9d4431b8994f79a4569bd267a6f5680
+ list.add("false");
+ }
+ }
+ case "remove" -> {
+ if (!Objects.equals(args[3], "cancel")) {
+ list.add("[minute]");
+ }
+ }
+ }
+ }
+ if (args.length == 5 && args[0].equals("remove")) {
+ if (!Objects.equals(args[3], "cancel")) {
+ list.add("[second]");
+ }
+ }
+
Expand Down Expand Up @@ -731,8 +739,8 @@ index 0000000000000000000000000000000000000000..b9d4431b8994f79a4569bd267a6f5680
+ }
+
+ private void onRemove(CommandSender sender, String @NotNull [] args) {
+ if (args.length < 2) {
+ sender.sendMessage(ChatColor.RED + "Use /bot remove <name> to remove a fakeplayer");
+ if (args.length < 2 || args.length > 5) {
+ sender.sendMessage(ChatColor.RED + "Use /bot remove <name> [hour] [minute] [second] to remove a fakeplayer");
+ return;
+ }
+
Expand All @@ -743,6 +751,63 @@ index 0000000000000000000000000000000000000000..b9d4431b8994f79a4569bd267a6f5680
+ return;
+ }
+
+ if (args.length > 2) {
+ if (args[2].equals("cancel")) {
+ if (bot.removeTaskId == -1) {
+ sender.sendMessage(ChatColor.RED + "This fakeplayer is not scheduled to be removed");
+ return;
+ }
+ Bukkit.getScheduler().cancelTask(bot.removeTaskId);
+ bot.removeTaskId = -1;
+ sender.sendMessage("Remove cancel");
+ return;
+ }
+
+ long time = 0;
+ int h; // Preventing out-of-range
+ long s = 0;
+ long m = 0;
+
+ try {
+ h = Integer.parseInt(args[2]);
+ if (h < 0) {
+ throw new NumberFormatException();
+ }
+ time += ((long) h) * 3600 * 20;
+ if (args.length > 3) {
+ m = Long.parseLong(args[3]);
+ if (m > 59 || m < 0) {
+ throw new NumberFormatException();
+ }
+ time += m * 60 * 20;
+ }
+ if (args.length > 4) {
+ s = Long.parseLong(args[4]);
+ if (s > 59 || s < 0) {
+ throw new NumberFormatException();
+ }
+ time += s * 20;
+ }
+ } catch (NumberFormatException e) {
+ sender.sendMessage(ChatColor.RED + "This fakeplayer is not scheduled to be removed");
+ return;
+ }
+
+ boolean isReschedule = bot.removeTaskId != -1;
+
+ if (isReschedule) {
+ Bukkit.getScheduler().cancelTask(bot.removeTaskId);
+ }
+ bot.removeTaskId = Bukkit.getScheduler().runTaskLater(CraftScheduler.MINECRAFT, () -> {
+ bot.removeTaskId = -1;
+ bot.onRemove(BotRemoveEvent.RemoveReason.COMMAND, sender);
Bluemangoo marked this conversation as resolved.
Show resolved Hide resolved
+ }, time).getTaskId();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里schedule是tick计时
我们是否需要考虑tick rate的问题?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不想考虑tick rate了。

+
+ sender.sendMessage("This fakeplayer will be removed in " + h + "h " + m + "m " + s + "s" + (isReschedule ? " (rescheduled)" : ""));
+
+ return;
+ }
+
+ bot.onRemove(BotRemoveEvent.RemoveReason.COMMAND, sender);
+ }
+
Expand Down Expand Up @@ -1426,10 +1491,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..0cb04bf5c5da387b295897997ae0692eb8baab6e
index 0000000000000000000000000000000000000000..c2c88ec8e7ef937af9c014d6511988fce1eb13f5
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/bot/ServerBot.java
@@ -0,0 +1,745 @@
@@ -0,0 +1,751 @@
+package org.leavesmc.leaves.bot;
+
+import com.google.common.collect.Lists;
Expand Down Expand Up @@ -1541,6 +1606,8 @@ index 0000000000000000000000000000000000000000..0cb04bf5c5da387b295897997ae0692e
+ public int notSleepTicks;
+ public boolean alwaysSendData;
+
+ public int removeTaskId = -1;
+
+ private ServerBot(MinecraftServer server, ServerLevel world, GameProfile profile) {
+ super(server, world, profile, ClientInformation.createDefault());
+ this.entityData.set(new EntityDataAccessor<>(16, EntityDataSerializers.INT), 0xFF);
Expand Down Expand Up @@ -1700,6 +1767,10 @@ index 0000000000000000000000000000000000000000..0cb04bf5c5da387b295897997ae0692e
+ if (!new BotRemoveEvent(this.getBukkitPlayer(), reason, remover).callEvent()) {
+ return;
+ }
+ if (this.removeTaskId != -1) {
+ Bukkit.getScheduler().cancelTask(this.removeTaskId);
+ this.removeTaskId = -1;
+ }
+ bots.remove(this);
+ server.getPlayerList().removeBot(this);
+ remove(RemovalReason.DISCARDED);
Expand Down