Skip to content

Commit

Permalink
Add scheduled bot remove
Browse files Browse the repository at this point in the history
  • Loading branch information
Bluemangoo committed Jul 25, 2024
1 parent 6a3a5cd commit 3a87199
Showing 1 changed file with 77 additions and 11 deletions.
88 changes: 77 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..d3c7d6bbbe687a9bbfdba7658f831cdf63d5e547
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/bot/BotCommand.java
@@ -0,0 +1,407 @@
@@ -0,0 +1,471 @@
+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,62 @@ 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.die(bot.damageSources().fellOutOfWorld());
+ }, time).getTaskId();
+
+ 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 +1490,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..a4d5ffb41fb347848058f032e06ef19427507138
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/bot/ServerBot.java
@@ -0,0 +1,745 @@
@@ -0,0 +1,747 @@
+package org.leavesmc.leaves.bot;
+
+import com.google.common.collect.Lists;
Expand Down Expand Up @@ -1541,6 +1605,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

0 comments on commit 3a87199

Please sign in to comment.