Skip to content

Commit

Permalink
Fix and modify Botapi (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lumine1909 authored Jul 3, 2024
1 parent f329632 commit a26fd68
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 34 deletions.
30 changes: 23 additions & 7 deletions patches/api/0003-Add-fakeplayer-api.patch
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,20 @@ index 0000000000000000000000000000000000000000..922ca5b27bc0dd443d635646f37f8795
+}
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
index 0000000000000000000000000000000000000000..7662b8bb1bb47f7a85705709548e00a3918d0502
index 0000000000000000000000000000000000000000..ee6848b8990c516aa5c5490546dd16ae5c909740
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/entity/BotManager.java
@@ -0,0 +1,107 @@
@@ -0,0 +1,124 @@
+package org.leavesmc.leaves.entity;
+
+import org.bukkit.Location;
+import org.bukkit.util.Consumer;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.leavesmc.leaves.entity.botaction.CustomBotAction;
+
+import java.util.Collection;
+import java.util.UUID;
+import java.util.function.Consumer;
+
+/**
+ * Simple fakeplayer manager
Expand Down Expand Up @@ -170,11 +170,28 @@ index 0000000000000000000000000000000000000000..7662b8bb1bb47f7a85705709548e00a3
+ * @param location a location will create fakeplayer
+ * @return a fakeplayer if success, null otherwise
+ */
+ @Deprecated(since = "1.21")
+ @Nullable
+ public Bot createBot(@NotNull String name, @NotNull String realName, @Nullable String[] skin, @Nullable String skinName, @NotNull Location location);
+
+ /**
+ * Creates a fakeplayer with given param.
+ * <p>
+ * prefix and suffix will not be added.
+ *
+ * @param name fakeplayer name
+ * @param realName fakeplayer real name
+ * @param skin fakeplayer skin arr
+ * @param skinName fakeplayer skin name
+ * @param location a location will create fakeplayer
+ * @param consumer a consumer after create fakeplayer success
+ * @return a fakeplayer if you support skin arr and the creation is success, null otherwise
+ */
+ @Nullable
+ public Bot createBot(@NotNull String name, @NotNull String realName, @NotNull String[] skin, @Nullable String skinName, @NotNull Location location, @Nullable Consumer<Bot> consumer);
+
+ /**
+ * Creates a fakeplayer with given param.
+ *
+ * @param name fakeplayer name
+ * @param skinName fakeplayer skin name
Expand Down Expand Up @@ -439,10 +456,10 @@ index 0000000000000000000000000000000000000000..5e55759fd3d7891e8e1d5d6a306dc814
+}
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..2ce7e91bf86df8630ba928409f0fc2ef5d359376
index 0000000000000000000000000000000000000000..be510d565c5942efea3423190b06c01873a7abd2
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/event/bot/BotCreateEvent.java
@@ -0,0 +1,119 @@
@@ -0,0 +1,118 @@
+package org.leavesmc.leaves.event.bot;
+
+import org.bukkit.Location;
Expand Down Expand Up @@ -473,8 +490,7 @@ index 0000000000000000000000000000000000000000..2ce7e91bf86df8630ba928409f0fc2ef
+ private Location createLocation;
+ private boolean cancel = false;
+
+ public BotCreateEvent(@NotNull final String who, @NotNull final String skin, @NotNull final Location createLocation, @NotNull CreateReason reason, @Nullable CommandSender creator, boolean async) {
+ super(async);
+ public BotCreateEvent(@NotNull final String who, @NotNull final String skin, @NotNull final Location createLocation, @NotNull CreateReason reason, @Nullable CommandSender creator) {
+ this.bot = who;
+ this.skin = skin;
+ this.createLocation = createLocation;
Expand Down
61 changes: 34 additions & 27 deletions patches/server/0010-Fakeplayer-support.patch
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ index bb9383f1a457433f9db3e78d7913616280925200..55b41ca7630db143d70137324a9de871
*/
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..ee53eb3bc9a0299f8438d8a650ce39b80f35192b
index 0000000000000000000000000000000000000000..b9d4431b8994f79a4569bd267a6f5680b9650bff
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/bot/BotCommand.java
@@ -0,0 +1,407 @@
Expand Down Expand Up @@ -524,7 +524,7 @@ index 0000000000000000000000000000000000000000..ee53eb3bc9a0299f8438d8a650ce39b8
+
+ if (canCreate(sender, args[1])) {
+ if (sender instanceof Player player) {
+ new ServerBot.BotCreateState(player.getLocation(), args[1], args.length < 3 ? args[1] : args[2], BotCreateEvent.CreateReason.COMMAND, player).createAsync(bot -> bot.createPlayer = player.getUniqueId());
+ new ServerBot.BotCreateState(player.getLocation(), args[1], args.length < 3 ? args[1] : args[2], BotCreateEvent.CreateReason.COMMAND, player).create(bot -> bot.createPlayer = player.getUniqueId());
+ } else if (sender instanceof ConsoleCommandSender csender) {
+ if (args.length < 6) {
+ sender.sendMessage(ChatColor.RED + "Use /bot create <name> <skin_name> <bukkit_world_name> <x> <y> <z> to create a fakeplayer");
Expand All @@ -538,7 +538,7 @@ index 0000000000000000000000000000000000000000..ee53eb3bc9a0299f8438d8a650ce39b8
+ double z = Double.parseDouble(args[6]);
+
+ if (world != null) {
+ new ServerBot.BotCreateState(new Location(world, x, y, z), args[1], args[2], BotCreateEvent.CreateReason.COMMAND, csender).createAsync(null);
+ new ServerBot.BotCreateState(new Location(world, x, y, z), args[1], args[2], BotCreateEvent.CreateReason.COMMAND, csender).create(null);
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
Expand Down Expand Up @@ -1031,7 +1031,7 @@ index 0000000000000000000000000000000000000000..5bd34353b6ea86cd15ff48b8d6570167
+}
diff --git a/src/main/java/org/leavesmc/leaves/bot/BotUtil.java b/src/main/java/org/leavesmc/leaves/bot/BotUtil.java
new file mode 100644
index 0000000000000000000000000000000000000000..54f6ef77c0bea75d1e0caed572590f5fd64a275b
index 0000000000000000000000000000000000000000..147293d108bfa5fc5d0ffd66cdc7c0569799d404
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/bot/BotUtil.java
@@ -0,0 +1,183 @@
Expand Down Expand Up @@ -1195,7 +1195,7 @@ index 0000000000000000000000000000000000000000..54f6ef77c0bea75d1e0caed572590f5f
+
+ final JsonArray finalActions = fakePlayer.get("actions").getAsJsonArray();
+ final ListTag finalInv = inv;
+ state.createAsync(serverBot -> {
+ state.create(serverBot -> {
+ if (finalInv != null) {
+ serverBot.getInventory().load(finalInv);
+ }
Expand Down Expand Up @@ -1267,7 +1267,7 @@ 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..dd1b8974825781fb34be6faae1da4bb35a1edfe6
index 0000000000000000000000000000000000000000..0cb04bf5c5da387b295897997ae0692eb8baab6e
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/bot/ServerBot.java
@@ -0,0 +1,745 @@
Expand Down Expand Up @@ -1412,7 +1412,7 @@ index 0000000000000000000000000000000000000000..dd1b8974825781fb34be6faae1da4bb3
+
+ MinecraftServer server = MinecraftServer.getServer();
+
+ BotCreateEvent event = new BotCreateEvent(state.name, state.skinName, state.loc, state.createReason, state.creator, state.async);
+ BotCreateEvent event = new BotCreateEvent(state.name, state.skinName, state.loc, state.createReason, state.creator);
+ server.server.getPluginManager().callEvent(event);
+
+ if (event.isCancelled()) {
Expand Down Expand Up @@ -1957,8 +1957,6 @@ index 0000000000000000000000000000000000000000..dd1b8974825781fb34be6faae1da4bb3
+ public BotCreateEvent.CreateReason createReason;
+ public CommandSender creator;
+
+ public boolean async;
+
+ public BotCreateState(Location loc, String realName, String skinName, BotCreateEvent.CreateReason createReason, CommandSender creator) {
+ this.loc = loc;
+ this.skinName = skinName;
Expand All @@ -1977,13 +1975,14 @@ index 0000000000000000000000000000000000000000..dd1b8974825781fb34be6faae1da4bb3
+ this.creator = creator;
+ }
+
+ public ServerBot createSync() {
+ async = false;
+ return createBot(this);
+ }
+
+ public void createAsync(Consumer<ServerBot> consumer) {
+ async = true;
+ public @Nullable Bot create(Consumer<ServerBot> consumer) {
+ if (skin != null) {
+ ServerBot bot = createBot(this);
+ if (bot != null && consumer != null) {
+ consumer.accept(bot);
+ }
+ return bot != null ? bot.getBukkitEntity() : null;
+ }
+ Bukkit.getScheduler().runTaskAsynchronously(CraftScheduler.MINECRAFT, () -> {
+ if (skinName != null) {
+ this.skin = MojangAPI.getSkin(skinName);
Expand All @@ -1996,6 +1995,7 @@ index 0000000000000000000000000000000000000000..dd1b8974825781fb34be6faae1da4bb3
+ }
+ });
+ });
+ return null;
+ }
+
+ public void setName(String name) {
Expand Down Expand Up @@ -3289,16 +3289,15 @@ index 0000000000000000000000000000000000000000..fe1df01906f15e130cf947bbecb5df4b
+}
diff --git a/src/main/java/org/leavesmc/leaves/entity/CraftBotManager.java b/src/main/java/org/leavesmc/leaves/entity/CraftBotManager.java
new file mode 100644
index 0000000000000000000000000000000000000000..5263539709d2b147b952473ba65b915bada821ca
index 0000000000000000000000000000000000000000..badd1d78398fc154254cd465c0a59c64b3fa500c
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/entity/CraftBotManager.java
@@ -0,0 +1,95 @@
@@ -0,0 +1,102 @@
+package org.leavesmc.leaves.entity;
+
+import com.google.common.base.Function;
+import com.google.common.collect.Lists;
+import org.bukkit.Location;
+import org.bukkit.util.Consumer;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.leavesmc.leaves.bot.ServerBot;
Expand All @@ -3311,6 +3310,7 @@ index 0000000000000000000000000000000000000000..5263539709d2b147b952473ba65b915b
+import java.util.Collection;
+import java.util.Collections;
+import java.util.UUID;
+import java.util.function.Consumer;
+
+public class CraftBotManager implements BotManager {
+
Expand All @@ -3333,17 +3333,24 @@ index 0000000000000000000000000000000000000000..5263539709d2b147b952473ba65b915b
+
+ @Override
+ public @Nullable Bot createBot(@NotNull String name, @NotNull String realName, @Nullable String[] skin, @Nullable String skinName, @NotNull Location location) {
+ ServerBot bot = new ServerBot.BotCreateState(location, name, realName, skinName, skin, BotCreateEvent.CreateReason.PLUGIN, null).createSync();
+ if (bot != null) {
+ return bot.getBukkitPlayer();
+ }
+ return null;
+ return this.createBot(name, realName, skin, skinName, location, null);
+ }
+
+ @Override
+ public @Nullable Bot createBot(@NotNull String name, @NotNull String realName, @NotNull String[] skin, @Nullable String skinName, @NotNull Location location, @Nullable Consumer<Bot> consumer) {
+ return new ServerBot.BotCreateState(location, name, realName, skinName, skin, BotCreateEvent.CreateReason.PLUGIN, null).create((serverBot -> {
+ if (consumer != null) {
+ consumer.accept(serverBot.getBukkitPlayer());
+ }
+ }));
+ }
+
+ @Override
+ public void createBot(@NotNull String name, @Nullable String skinName, @NotNull Location location, Consumer<Bot> consumer) {
+ new ServerBot.BotCreateState(location, name, skinName, BotCreateEvent.CreateReason.PLUGIN, null).createAsync((serverBot -> {
+ consumer.accept(serverBot.getBukkitPlayer());
+ public void createBot(@NotNull String name, @Nullable String skinName, @NotNull Location location, @Nullable Consumer<Bot> consumer) {
+ new ServerBot.BotCreateState(location, name, skinName, BotCreateEvent.CreateReason.PLUGIN, null).create((serverBot -> {
+ if (consumer != null) {
+ consumer.accept(serverBot.getBukkitPlayer());
+ }
+ }));
+ }
+
Expand Down

0 comments on commit a26fd68

Please sign in to comment.