From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: violetc <58360096+s-yh-china@users.noreply.github.com> 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 4705c1d91e39fcc3c608b1f1a38a30d063ccf06e..30cebbf2e194c56450328a7dc04c92c81edce284 100644 --- a/src/main/java/org/bukkit/Bukkit.java +++ b/src/main/java/org/bukkit/Bukkit.java @@ -2906,6 +2906,17 @@ public final class Bukkit { } // Paper end - Folia region threading API + // Leaves start - Bot API + /** + * Returns a bot manager. + * + * @return Bot Manager + */ + public static @NotNull org.leavesmc.leaves.entity.BotManager getBotManager() { + return server.getBotManager(); + } + // Leaves end - Bot API + @NotNull public static Server.Spigot spigot() { return server.spigot(); diff --git a/src/main/java/org/bukkit/Server.java b/src/main/java/org/bukkit/Server.java index 594deedd08c3b3255fe6838471d945759f09a182..6fa638198f75458177af795f00250ce970591315 100644 --- a/src/main/java/org/bukkit/Server.java +++ b/src/main/java/org/bukkit/Server.java @@ -62,6 +62,7 @@ import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.leavesmc.leaves.entity.BotManager; /** * Represents a server implementation. @@ -2551,4 +2552,13 @@ public interface Server extends PluginMessageRecipient, net.kyori.adventure.audi */ boolean isOwnedByCurrentRegion(@NotNull Entity entity); // Paper end - Folia region threading API + + // Leaves start - Bot API + /** + * Returns a bot manager. + * + * @return Bot Manager + */ + @NotNull BotManager getBotManager(); + // Leaves end - Bot API } diff --git a/src/main/java/org/leavesmc/leaves/entity/Bot.java b/src/main/java/org/leavesmc/leaves/entity/Bot.java new file mode 100644 index 0000000000000000000000000000000000000000..922ca5b27bc0dd443d635646f37f879559cc0252 --- /dev/null +++ b/src/main/java/org/leavesmc/leaves/entity/Bot.java @@ -0,0 +1,51 @@ +package org.leavesmc.leaves.entity; + +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.leavesmc.leaves.entity.botaction.LeavesBotAction; + +import java.util.UUID; + +/** + * Represents a fakeplayer + */ +public interface Bot extends Player { + + /** + * Gets the fakeplayer skin + * + * @return fakeplayer skin name + */ + @Nullable + public String getSkinName(); + + /** + * Gets the fakeplayer name without prefix and suffix + * + * @return fakeplayer real name + */ + @NotNull + public String getRealName(); + + @Nullable + public UUID getCreatePlayerUUID(); + + /** + * Sets the fakeplayer action with args. + * + * @param action action name + * @param player player who create this action + * @param args passed action arguments + */ + public boolean setBotAction(@NotNull String action, @NotNull Player player, @NotNull String[] args); + + /** + * Sets the fakeplayer action with args. + * + * @param action leaves bot action + * @param player player who create this action + * @param args passed action arguments + */ + public boolean setBotAction(@NotNull LeavesBotAction action, @NotNull Player player, @NotNull String[] args); +} 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..ee6848b8990c516aa5c5490546dd16ae5c909740 --- /dev/null +++ b/src/main/java/org/leavesmc/leaves/entity/BotManager.java @@ -0,0 +1,124 @@ +package org.leavesmc.leaves.entity; + +import org.bukkit.Location; +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 + */ +public interface BotManager { + + /** + * Gets a fakeplayer object by the given uuid. + * + * @param uuid the uuid to look up + * @return a fakeplayer if one was found, null otherwise + */ + @Nullable + public Bot getBot(@NotNull UUID uuid); + + /** + * Gets a fakeplayer object by the given name. + * + * @param name the name to look up + * @return a fakeplayer if one was found, null otherwise + */ + @Nullable + public Bot getBot(@NotNull String name); + + /** + * Creates a fakeplayer with given param. + *
+ * 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 + * @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. + *
+ * 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> getTabComplete();
+
+ /**
+ * Return a ticks to wait between {@link CustomBotAction#doTick(Bot)}
+ *
+ * @return the ticks to wait between runs
+ */
+ public int getTickDelay();
+
+ /**
+ * Return a number of times {@link CustomBotAction#doTick(Bot)} can return true
+ *
+ * @return the number of times an action can be executed
+ */
+ public int getNumber();
+}
diff --git a/src/main/java/org/leavesmc/leaves/entity/botaction/LeavesBotAction.java b/src/main/java/org/leavesmc/leaves/entity/botaction/LeavesBotAction.java
new file mode 100644
index 0000000000000000000000000000000000000000..b239acd298b299e338ae56aa6507570942ce44e2
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/entity/botaction/LeavesBotAction.java
@@ -0,0 +1,32 @@
+package org.leavesmc.leaves.entity.botaction;
+
+/**
+ * A Leaves bot action enum
+ */
+public enum LeavesBotAction {
+ ATTACK("attack"),
+ ATTACK_SELF("attack_self"),
+ BREAK("break"),
+ DROP("drop"),
+ FISH("fish"),
+ JUMP("jump"),
+ LAY("lay"),
+ LOOK("look"),
+ ROTATE("rotate"),
+ SNEAK("sneak"),
+ STOP("stop"),
+ SWIM("swim"),
+ USE("use"),
+ USE_ON("use_on"),
+ USE_TO("use_to");
+
+ private final String name;
+
+ private LeavesBotAction(String name) {
+ this.name = name;
+ }
+
+ public String getName() {
+ return name;
+ }
+}
diff --git a/src/main/java/org/leavesmc/leaves/event/bot/BotActionEvent.java b/src/main/java/org/leavesmc/leaves/event/bot/BotActionEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..91ea5540387b7d7e1be5b6368a2f02b3b784614a
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/event/bot/BotActionEvent.java
@@ -0,0 +1,49 @@
+package org.leavesmc.leaves.event.bot;
+
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.HandlerList;
+import org.jetbrains.annotations.NotNull;
+import org.leavesmc.leaves.entity.Bot;
+
+public class BotActionEvent extends BotEvent implements Cancellable {
+ private static final HandlerList handlers = new HandlerList();
+
+ private final String actionName;
+ private final String[] actionArgs;
+ private boolean cancel = false;
+
+ public BotActionEvent(@NotNull Bot who, String actionName, String[] actionArgs) {
+ super(who);
+ this.actionArgs = actionArgs;
+ this.actionName = actionName;
+ }
+
+ @NotNull
+ public String[] getActionArgs() {
+ return actionArgs;
+ }
+
+ @NotNull
+ public String getActionName() {
+ return actionName;
+ }
+
+ @Override
+ public @NotNull HandlerList getHandlers() {
+ return handlers;
+ }
+
+ @Override
+ public boolean isCancelled() {
+ return cancel;
+ }
+
+ @Override
+ public void setCancelled(boolean cancel) {
+ this.cancel = cancel;
+ }
+
+ public static HandlerList getHandlerList() {
+ return handlers;
+ }
+}
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..5e55759fd3d7891e8e1d5d6a306dc8144d366469
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/event/bot/BotConfigModifyEvent.java
@@ -0,0 +1,49 @@
+package org.leavesmc.leaves.event.bot;
+
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.HandlerList;
+import org.jetbrains.annotations.NotNull;
+import org.leavesmc.leaves.entity.Bot;
+
+public class BotConfigModifyEvent extends BotEvent implements Cancellable {
+ private static final HandlerList handlers = new HandlerList();
+
+ private final String configName;
+ private final String configValue;
+ private boolean cancel;
+
+ public BotConfigModifyEvent(@NotNull Bot who, String configName, String configValue) {
+ super(who);
+ this.configName = configName;
+ this.configValue = configValue;
+ }
+
+ @NotNull
+ public String getConfigName() {
+ return configName;
+ }
+
+ @NotNull
+ public String getConfigValue() {
+ return configValue;
+ }
+
+ @Override
+ public @NotNull HandlerList getHandlers() {
+ return handlers;
+ }
+
+ @Override
+ public boolean isCancelled() {
+ return cancel;
+ }
+
+ @Override
+ public void setCancelled(boolean cancel) {
+ this.cancel = cancel;
+ }
+
+ public static HandlerList getHandlerList() {
+ return handlers;
+ }
+}
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..be510d565c5942efea3423190b06c01873a7abd2
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/event/bot/BotCreateEvent.java
@@ -0,0 +1,118 @@
+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 final CreateReason reason;
+ private final Optional