From 6a9e4e904fc6b40514719f464ab4da2c863c497c Mon Sep 17 00:00:00 2001 From: Lumine1909 <133463833+Lumine1909@users.noreply.github.com> Date: Mon, 1 Jul 2024 19:18:35 +0800 Subject: [PATCH] change to v1.21 --- patches/api/0003-Add-fakeplayer-api.patch | 176 ++++++++++++++++--- patches/server/0010-Fakeplayer-support.patch | 92 ++++++---- 2 files changed, 204 insertions(+), 64 deletions(-) diff --git a/patches/api/0003-Add-fakeplayer-api.patch b/patches/api/0003-Add-fakeplayer-api.patch index 381bb2f1..c60f35f6 100644 --- a/patches/api/0003-Add-fakeplayer-api.patch +++ b/patches/api/0003-Add-fakeplayer-api.patch @@ -439,36 +439,47 @@ 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..c093f68e5f1749c792255220f39bdbdffb78f0f9 +index 0000000000000000000000000000000000000000..2ce7e91bf86df8630ba928409f0fc2ef5d359376 --- /dev/null +++ b/src/main/java/org/leavesmc/leaves/event/bot/BotCreateEvent.java -@@ -0,0 +1,106 @@ +@@ -0,0 +1,119 @@ +package org.leavesmc.leaves.event.bot; + +import org.bukkit.Location; ++import org.bukkit.command.CommandSender; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + ++import java.util.Optional; ++ +/** + * Call when a fakeplayer creates a server + */ +public class BotCreateEvent extends Event implements Cancellable { ++ public enum CreateReason { ++ COMMAND, ++ PLUGIN, ++ INTERNAL ++ } + private static final HandlerList handlers = new HandlerList(); + + private final String bot; + private final String skin; -+ private String joinMessage; ++ private final CreateReason reason; ++ private final Optional creator; + private Location createLocation; + private boolean cancel = false; + -+ public BotCreateEvent(@NotNull final String who, @NotNull final String skin, @NotNull final Location createLocation, @Nullable final String joinMessage) { ++ public BotCreateEvent(@NotNull final String who, @NotNull final String skin, @NotNull final Location createLocation, @NotNull CreateReason reason, @Nullable CommandSender creator, boolean async) { ++ super(async); + this.bot = who; + this.skin = skin; -+ this.joinMessage = joinMessage; + this.createLocation = createLocation; ++ this.reason = reason; ++ this.creator = Optional.ofNullable(creator); + } + + /** @@ -481,25 +492,6 @@ index 0000000000000000000000000000000000000000..c093f68e5f1749c792255220f39bdbdf + } + + /** -+ * Gets the join message to send to all online players -+ * -+ * @return string join message. Can be null -+ */ -+ @Nullable -+ public String getJoinMessage() { -+ return joinMessage; -+ } -+ -+ /** -+ * Sets the join message to send to all online players -+ * -+ * @param joinMessage join message. If null, no message will be sent -+ */ -+ public void setJoinMessage(@Nullable String joinMessage) { -+ this.joinMessage = joinMessage; -+ } -+ -+ /** + * Gets the location to create the fakeplayer + * + * @return Location to create the fakeplayer @@ -528,6 +520,27 @@ index 0000000000000000000000000000000000000000..c093f68e5f1749c792255220f39bdbdf + return skin; + } + ++ /** ++ * Gets the create reason of the bot ++ * ++ * @return create reason ++ */ ++ @NotNull ++ public CreateReason getReason() { ++ return reason; ++ } ++ ++ /** ++ * Gets the creator of the bot ++ * if the create reason is not COMMAND, the creator might be Optional.empty() ++ * ++ * @return An optional of creator ++ */ ++ @NotNull ++ public Optional getCreator() { ++ return creator; ++ } ++ + @Override + public boolean isCancelled() { + return cancel; @@ -640,14 +653,15 @@ index 0000000000000000000000000000000000000000..a369b468d4793b36dd0944a1368a70e0 +} diff --git a/src/main/java/org/leavesmc/leaves/event/bot/BotJoinEvent.java b/src/main/java/org/leavesmc/leaves/event/bot/BotJoinEvent.java new file mode 100644 -index 0000000000000000000000000000000000000000..7500652b01a4ed3c8d59ca003a644a9e024f6512 +index 0000000000000000000000000000000000000000..e2e0b9fe697ab3b89373264d20d013cb9f65dd40 --- /dev/null +++ b/src/main/java/org/leavesmc/leaves/event/bot/BotJoinEvent.java -@@ -0,0 +1,27 @@ +@@ -0,0 +1,51 @@ +package org.leavesmc.leaves.event.bot; + +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; ++import org.jetbrains.annotations.Nullable; +import org.leavesmc.leaves.entity.Bot; + +/** @@ -656,8 +670,116 @@ index 0000000000000000000000000000000000000000..7500652b01a4ed3c8d59ca003a644a9e +public class BotJoinEvent extends BotEvent { + private static final HandlerList handlers = new HandlerList(); + -+ public BotJoinEvent(@NotNull Bot who) { ++ private String joinMessage; ++ ++ public BotJoinEvent(@NotNull Bot who, @Nullable final String joinMessage) { ++ super(who); ++ this.joinMessage = joinMessage; ++ } ++ ++ @Override ++ @NotNull ++ public HandlerList getHandlers() { ++ return handlers; ++ } ++ ++ @NotNull ++ public static HandlerList getHandlerList() { ++ return handlers; ++ } ++ ++ ++ /** ++ * Gets the join message to send to all online players ++ * ++ * @return string join message. Can be null ++ */ ++ @Nullable ++ public String getJoinMessage() { ++ return joinMessage; ++ } ++ ++ /** ++ * Sets the join message to send to all online players ++ * ++ * @param joinMessage join message. If null, no message will be sent ++ */ ++ public void setJoinMessage(@Nullable String joinMessage) { ++ this.joinMessage = joinMessage; ++ } ++} +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..6aee942d7db322196504d386a009e22e2aa16230 +--- /dev/null ++++ b/src/main/java/org/leavesmc/leaves/event/bot/BotRemoveEvent.java +@@ -0,0 +1,79 @@ ++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.Optional; ++ ++/** ++ * Call when a fakeplayer creates a server ++ */ ++public class BotRemoveEvent extends BotEvent implements Cancellable { ++ public enum RemoveReason { ++ COMMAND, ++ PLUGIN, ++ DEATH, ++ INTERNAL ++ } ++ private static final HandlerList handlers = new HandlerList(); ++ ++ private final RemoveReason reason; ++ private final Optional remover; ++ private boolean cancel = false; ++ ++ public BotRemoveEvent(@NotNull final Bot who, @NotNull RemoveReason reason) { ++ this(who, reason, null); ++ } ++ ++ public BotRemoveEvent(@NotNull final Bot who, @NotNull RemoveReason reason, @Nullable CommandSender remover) { + super(who); ++ this.reason = reason; ++ this.remover = Optional.ofNullable(remover); ++ } ++ ++ /** ++ * Gets the remove reason of the bot ++ * ++ * @return remove reason ++ */ ++ @NotNull ++ public RemoveReason getReason() { ++ return reason; ++ } ++ ++ /** ++ * Gets the remover of the bot ++ * if the remove reason is not COMMAND, the creator might be Optional.empty() ++ * ++ * @return An optional of remover ++ */ ++ @NotNull ++ public Optional getRemover() { ++ return remover; ++ } ++ ++ @Override ++ public boolean isCancelled() { ++ return cancel; ++ } ++ ++ @Override ++ public void setCancelled(boolean cancel) { ++ this.cancel = cancel; + } + + @Override diff --git a/patches/server/0010-Fakeplayer-support.patch b/patches/server/0010-Fakeplayer-support.patch index e07fbbd2..244426e9 100644 --- a/patches/server/0010-Fakeplayer-support.patch +++ b/patches/server/0010-Fakeplayer-support.patch @@ -34,7 +34,7 @@ index 35772110e9318df46a2729dbc0b5879b290011b7..f26989a44cdda9baabf337d573436c6c Set> set = (Set) playerAdvancements.criterionData.get(this); // Paper - fix AdvancementDataPlayer leak if (set != null && !set.isEmpty()) { diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java -index 307c5a0b4b7c68fe2c7d1cf77ff23c766335882d..803d0b817e5d0acb1e4c6601d4784cc5cbd5e612 100644 +index 2d60beb706f99e0c570caf37ccc777e69c672ef2..2b29dacb541468d4f6410402c2e4eb1a57b80e2a 100644 --- a/src/main/java/net/minecraft/server/MinecraftServer.java +++ b/src/main/java/net/minecraft/server/MinecraftServer.java @@ -682,6 +682,8 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop bot.createPlayer = player.getUniqueId()); -+ } else if (sender instanceof ConsoleCommandSender) { ++ new ServerBot.BotCreateState(player.getLocation(), args[1], args.length < 3 ? args[1] : args[2], org.leavesmc.leaves.event.bot.BotCreateEvent.CreateReason.COMMAND, player).createAsync(bot -> bot.createPlayer = player.getUniqueId()); ++ } else if (sender instanceof ConsoleCommandSender csender) { + if (args.length < 6) { + sender.sendMessage(ChatColor.RED + "Use /bot create to create a fakeplayer"); + return; @@ -536,7 +536,7 @@ index 0000000000000000000000000000000000000000..4b31cd0407d46c3405506470f70568a1 + double z = Double.parseDouble(args[6]); + + if (world != null) { -+ new ServerBot.BotCreateState(new Location(world, x, y, z), args[1], args[2]).createAsync(null); ++ new ServerBot.BotCreateState(new Location(world, x, y, z), args[1], args[2], org.leavesmc.leaves.event.bot.BotCreateEvent.CreateReason.COMMAND, csender).createAsync(null); + } + } catch (Exception e) { + e.printStackTrace(); @@ -582,7 +582,7 @@ index 0000000000000000000000000000000000000000..4b31cd0407d46c3405506470f70568a1 + return; + } + -+ bot.die(bot.damageSources().fellOutOfWorld()); ++ bot.onRemove(org.leavesmc.leaves.event.bot.BotRemoveEvent.RemoveReason.COMMAND, sender); + } + + private void onAction(CommandSender sender, String @NotNull [] args) { @@ -1029,7 +1029,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..e63bdf65183fb2c55df865b0adc2e207ac50071a +index 0000000000000000000000000000000000000000..54f6ef77c0bea75d1e0caed572590f5fd64a275b --- /dev/null +++ b/src/main/java/org/leavesmc/leaves/bot/BotUtil.java @@ -0,0 +1,183 @@ @@ -1177,7 +1177,7 @@ index 0000000000000000000000000000000000000000..e63bdf65183fb2c55df865b0adc2e207 + String skin = fakePlayer.get("skin").getAsString(); + + Location location = new Location(Bukkit.getWorld(dimension), pos_x, pos_y, pos_z, yaw, pitch); -+ ServerBot.BotCreateState state = new ServerBot.BotCreateState(location, username, skin); ++ ServerBot.BotCreateState state = new ServerBot.BotCreateState(location, username, skin, org.leavesmc.leaves.event.bot.BotCreateEvent.CreateReason.INTERNAL, null); + + ListTag inv = null; + File file = MinecraftServer.getServer().getWorldPath(LevelResource.ROOT).resolve("fakeplayer/" + getBotUUID(state) + ".dat").toFile(); @@ -1265,10 +1265,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..549fd54c35c8283200562d678fa4bba593878c62 +index 0000000000000000000000000000000000000000..5dd6778347c121f0b0817e2395b335941348d151 --- /dev/null +++ b/src/main/java/org/leavesmc/leaves/bot/ServerBot.java -@@ -0,0 +1,725 @@ +@@ -0,0 +1,743 @@ +package org.leavesmc.leaves.bot; + +import com.google.common.collect.Lists; @@ -1408,7 +1408,7 @@ index 0000000000000000000000000000000000000000..549fd54c35c8283200562d678fa4bba5 + + MinecraftServer server = MinecraftServer.getServer(); + -+ BotCreateEvent event = new BotCreateEvent(state.name, state.skinName, state.loc, ChatColor.YELLOW + state.name + " joined the game"); ++ BotCreateEvent event = new BotCreateEvent(state.name, state.skinName, state.loc, state.createReason, state.creator, state.async); + server.server.getPluginManager().callEvent(event); + + if (event.isCancelled()) { @@ -1438,10 +1438,6 @@ index 0000000000000000000000000000000000000000..549fd54c35c8283200562d678fa4bba5 + bot.isRealPlayer = true; + bot.createState = state; + -+ if (event.getJoinMessage() != null) { -+ Bukkit.broadcastMessage(event.getJoinMessage()); -+ } -+ + bot.teleportTo(location.getX(), location.getY(), location.getZ()); + bot.setRot(location.getYaw(), location.getPitch()); + bot.getBukkitEntity().setRotation(location.getYaw(), location.getPitch()); @@ -1451,9 +1447,13 @@ index 0000000000000000000000000000000000000000..549fd54c35c8283200562d678fa4bba5 + server.getPlayerList().addNewBot(bot); + bots.add(bot); + -+ BotJoinEvent event1 = new BotJoinEvent(bot.getBukkitPlayer()); ++ BotJoinEvent event1 = new BotJoinEvent(bot.getBukkitPlayer(), ChatColor.YELLOW + state.name + " joined the game"); + server.server.getPluginManager().callEvent(event1); + ++ if (event1.getJoinMessage() != null) { ++ Bukkit.broadcastMessage(event1.getJoinMessage()); ++ } ++ + return bot; + } + @@ -1462,7 +1462,7 @@ index 0000000000000000000000000000000000000000..549fd54c35c8283200562d678fa4bba5 + return false; + } + -+ if (Bukkit.getPlayer(name) != null || ServerBot.getBot(name) != null) { ++ if (Bukkit.getPlayerExact(name) != null || ServerBot.getBot(name) != null) { + return false; + } + @@ -1524,18 +1524,25 @@ index 0000000000000000000000000000000000000000..549fd54c35c8283200562d678fa4bba5 + @Override + public void die(@NotNull DamageSource damageSource) { + super.die(damageSource); -+ this.dieCheck(); ++ if (removeOnDeath) { ++ onRemove(org.leavesmc.leaves.event.bot.BotRemoveEvent.RemoveReason.DEATH); ++ } + } + -+ private void dieCheck() { -+ if (removeOnDeath) { -+ bots.remove(this); -+ server.getPlayerList().removeBot(this); -+ remove(RemovalReason.KILLED); -+ this.setDead(); -+ this.removeTab(); -+ Bukkit.broadcastMessage(ChatColor.YELLOW + this.getName().getString() + " left the game"); // TODO i18n ++ public void onRemove(org.leavesmc.leaves.event.bot.BotRemoveEvent.RemoveReason reason) { ++ onRemove(reason, null); ++ } ++ ++ public void onRemove(org.leavesmc.leaves.event.bot.BotRemoveEvent.RemoveReason reason, @Nullable org.bukkit.command.CommandSender remover) { ++ if (!new org.leavesmc.leaves.event.bot.BotRemoveEvent(this.getBukkitPlayer(), reason, remover).callEvent()) { ++ return; + } ++ bots.remove(this); ++ server.getPlayerList().removeBot(this); ++ remove(RemovalReason.DISCARDED); ++ this.setDead(); ++ this.removeTab(); ++ Bukkit.broadcastMessage(ChatColor.YELLOW + this.getName().getString() + " left the game"); // TODO i18n + } + + private void removeTab() { @@ -1883,7 +1890,7 @@ index 0000000000000000000000000000000000000000..549fd54c35c8283200562d678fa4bba5 + e.printStackTrace(); + } + } else { -+ removeAllBot(); ++ removeAllBot(org.leavesmc.leaves.event.bot.BotRemoveEvent.RemoveReason.INTERNAL); + } + } + @@ -1906,11 +1913,11 @@ index 0000000000000000000000000000000000000000..549fd54c35c8283200562d678fa4bba5 + } + } + -+ public static boolean removeAllBot() { ++ public static boolean removeAllBot(org.leavesmc.leaves.event.bot.BotRemoveEvent.RemoveReason reason) { + Iterator iterator = bots.iterator(); + while (iterator.hasNext()) { + ServerBot bot = iterator.next(); -+ bot.die(bot.damageSources().fellOutOfWorld()); ++ bot.onRemove(reason); + } + return true; + } @@ -1943,25 +1950,36 @@ index 0000000000000000000000000000000000000000..549fd54c35c8283200562d678fa4bba5 + private String realName; + private String name; + -+ public BotCreateState(Location loc, String realName, String skinName) { ++ public org.leavesmc.leaves.event.bot.BotCreateEvent.CreateReason createReason; ++ public org.bukkit.command.CommandSender creator; ++ ++ public boolean async; ++ ++ public BotCreateState(Location loc, String realName, String skinName, org.leavesmc.leaves.event.bot.BotCreateEvent.CreateReason createReason, org.bukkit.command.CommandSender creator) { + this.loc = loc; + this.skinName = skinName; + this.setRealName(realName); ++ this.createReason = createReason; ++ this.creator = creator; + } + -+ public BotCreateState(Location loc, String name, String realName, String skinName, String[] skin) { ++ public BotCreateState(Location loc, String name, String realName, String skinName, String[] skin, org.leavesmc.leaves.event.bot.BotCreateEvent.CreateReason createReason, org.bukkit.command.CommandSender creator) { + this.loc = loc; + this.skinName = skinName; + this.skin = skin; + this.realName = realName; + this.name = name; ++ this.createReason = createReason; ++ this.creator = creator; + } + + public ServerBot createSync() { ++ async = false; + return createBot(this); + } + + public void createAsync(Consumer consumer) { ++ async = true; + Bukkit.getScheduler().runTaskAsynchronously(CraftScheduler.MINECRAFT, () -> { + if (skinName != null) { + this.skin = MojangAPI.getSkin(skinName); @@ -3267,7 +3285,7 @@ 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..3995912855a612f09567027138f855ec40e26acf +index 0000000000000000000000000000000000000000..5397be4032d1a6339843aeb6b9a34182545bde02 --- /dev/null +++ b/src/main/java/org/leavesmc/leaves/entity/CraftBotManager.java @@ -0,0 +1,93 @@ @@ -3309,7 +3327,7 @@ index 0000000000000000000000000000000000000000..3995912855a612f09567027138f855ec + + @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).createSync(); ++ ServerBot bot = new ServerBot.BotCreateState(location, name, realName, skinName, skin, org.leavesmc.leaves.event.bot.BotCreateEvent.CreateReason.PLUGIN, null).createSync(); + if (bot != null) { + return bot.getBukkitPlayer(); + } @@ -3318,7 +3336,7 @@ index 0000000000000000000000000000000000000000..3995912855a612f09567027138f855ec + + @Override + public void createBot(@NotNull String name, @Nullable String skinName, @NotNull Location location, Consumer consumer) { -+ new ServerBot.BotCreateState(location, name, skinName).createAsync((serverBot -> { ++ new ServerBot.BotCreateState(location, name, skinName, org.leavesmc.leaves.event.bot.BotCreateEvent.CreateReason.PLUGIN, null).createAsync((serverBot -> { + consumer.accept(serverBot.getBukkitPlayer()); + })); + } @@ -3327,7 +3345,7 @@ index 0000000000000000000000000000000000000000..3995912855a612f09567027138f855ec + public void removeBot(@NotNull String name) { + ServerBot bot = ServerBot.getBot(name); + if (bot != null) { -+ bot.die(bot.damageSources().fellOutOfWorld()); ++ bot.onRemove(org.leavesmc.leaves.event.bot.BotRemoveEvent.RemoveReason.PLUGIN); + } + } + @@ -3335,13 +3353,13 @@ index 0000000000000000000000000000000000000000..3995912855a612f09567027138f855ec + public void removeBot(@NotNull UUID uuid) { + ServerBot bot = ServerBot.getBot(uuid); + if (bot != null) { -+ bot.die(bot.damageSources().fellOutOfWorld()); ++ bot.onRemove(org.leavesmc.leaves.event.bot.BotRemoveEvent.RemoveReason.PLUGIN); + } + } + + @Override + public void removeAllBots() { -+ ServerBot.removeAllBot(); ++ ServerBot.removeAllBot(org.leavesmc.leaves.event.bot.BotRemoveEvent.RemoveReason.PLUGIN); + } + + @Override