Skip to content

Commit

Permalink
New Fakeplayer start
Browse files Browse the repository at this point in the history
  • Loading branch information
s-yh-china committed Sep 1, 2024
1 parent 9338453 commit bb2b457
Show file tree
Hide file tree
Showing 19 changed files with 1,326 additions and 837 deletions.
204 changes: 108 additions & 96 deletions patches/api/0003-Add-fakeplayer-api.patch
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ 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..ee6848b8990c516aa5c5490546dd16ae5c909740
index 0000000000000000000000000000000000000000..457395381d1d78eb53f307d4465ea520c1aa57fd
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/entity/BotManager.java
@@ -0,0 +1,124 @@
@@ -0,0 +1,58 @@
+package org.leavesmc.leaves.entity;
+
+import org.bukkit.Location;
Expand Down Expand Up @@ -159,72 +159,6 @@ index 0000000000000000000000000000000000000000..ee6848b8990c516aa5c5490546dd16ae
+ public Bot getBot(@NotNull String name);
+
+ /**
+ * 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
+ * @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
+ * @param location a location will create fakeplayer
+ * @param consumer a consumer after create fakeplayer success
+ */
+ public void createBot(@NotNull String name, @Nullable String skinName, @NotNull Location location, @Nullable Consumer<Bot> consumer);
+
+ /**
+ * Removes a fakeplayer object by the given name.
+ *
+ * @param name the name to look up
+ */
+ public void removeBot(@NotNull String name);
+
+ /**
+ * Removes a fakeplayer object by the given uuid.
+ *
+ * @param uuid the uuid to look up
+ */
+ public void removeBot(@NotNull UUID uuid);
+
+ /**
+ * Removes all fakeplayers.
+ */
+ public void removeAllBots();
+
+ /**
+ * Save fakeplayers data if resident-fakeplayer is true, or remove all fakeplayer.
+ */
+ public void saveOrRemoveAllBots();
+
+ /**
+ * Gets a view of all currently logged in fakeplayers. This view is a reused object, making some operations like Collection.size() zero-allocation.
+ *
+ * @return a view of fakeplayers.
Expand Down Expand Up @@ -405,7 +339,7 @@ index 0000000000000000000000000000000000000000..91ea5540387b7d7e1be5b6368a2f02b3
+}
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
index 0000000000000000000000000000000000000000..1ebdd3657e615a825918d455691cc1d6df78cfba
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/event/bot/BotConfigModifyEvent.java
@@ -0,0 +1,49 @@
Expand All @@ -420,10 +354,10 @@ index 0000000000000000000000000000000000000000..5e55759fd3d7891e8e1d5d6a306dc814
+ private static final HandlerList handlers = new HandlerList();
+
+ private final String configName;
+ private final String configValue;
+ private final String[] configValue;
+ private boolean cancel;
+
+ public BotConfigModifyEvent(@NotNull Bot who, String configName, String configValue) {
+ public BotConfigModifyEvent(@NotNull Bot who, String configName, String[] configValue) {
+ super(who);
+ this.configName = configName;
+ this.configValue = configValue;
Expand All @@ -435,7 +369,7 @@ index 0000000000000000000000000000000000000000..5e55759fd3d7891e8e1d5d6a306dc814
+ }
+
+ @NotNull
+ public String getConfigValue() {
+ public String[] getConfigValue() {
+ return configValue;
+ }
+
Expand All @@ -460,10 +394,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..be510d565c5942efea3423190b06c01873a7abd2
index 0000000000000000000000000000000000000000..ec0ed853a6d6dec263784da284ee521c81aebbc8
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/event/bot/BotCreateEvent.java
@@ -0,0 +1,118 @@
@@ -0,0 +1,119 @@
+package org.leavesmc.leaves.event.bot;
+
+import org.bukkit.Location;
Expand All @@ -483,14 +417,15 @@ index 0000000000000000000000000000000000000000..be510d565c5942efea3423190b06c018
+ public enum CreateReason {
+ COMMAND,
+ PLUGIN,
+ INTERNAL
+ INTERNAL,
+ UNKNOWN,
+ }
+ private static final HandlerList handlers = new HandlerList();
+
+ private final String bot;
+ private final String skin;
+ private final CreateReason reason;
+ private final Optional<CommandSender> creator;
+ private final CommandSender creator;
+ private Location createLocation;
+ private boolean cancel = false;
+
Expand All @@ -499,7 +434,7 @@ index 0000000000000000000000000000000000000000..be510d565c5942efea3423190b06c018
+ this.skin = skin;
+ this.createLocation = createLocation;
+ this.reason = reason;
+ this.creator = Optional.ofNullable(creator);
+ this.creator = creator;
+ }
+
+ /**
Expand Down Expand Up @@ -552,12 +487,12 @@ index 0000000000000000000000000000000000000000..be510d565c5942efea3423190b06c018
+
+ /**
+ * Gets the creator of the bot
+ * if the create reason is not COMMAND, the creator might be Optional.empty()
+ * if the create reason is not COMMAND, the creator might be null
+ *
+ * @return An optional of creator
+ */
+ @NotNull
+ public Optional<CommandSender> getCreator() {
+ @Nullable
+ public CommandSender getCreator() {
+ return creator;
+ }
+
Expand All @@ -582,6 +517,81 @@ index 0000000000000000000000000000000000000000..be510d565c5942efea3423190b06c018
+ return handlers;
+ }
+}
diff --git a/src/main/java/org/leavesmc/leaves/event/bot/BotDeathEvent.java b/src/main/java/org/leavesmc/leaves/event/bot/BotDeathEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..3366b50cf1835129a027b5342e4d8cf070cecf4a
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/event/bot/BotDeathEvent.java
@@ -0,0 +1,69 @@
+package org.leavesmc.leaves.event.bot;
+
+import net.kyori.adventure.text.Component;
+import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
+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;
+
+public class BotDeathEvent extends BotEvent implements Cancellable {
+
+ private static final HandlerList handlers = new HandlerList();
+
+ private boolean cancel = false;
+ private boolean sendDeathMessage;
+ private Component deathMessage;
+
+ public BotDeathEvent(@NotNull Bot who, @Nullable Component deathMessage, boolean sendDeathMessage) {
+ super(who);
+ this.deathMessage = deathMessage;
+ this.sendDeathMessage = sendDeathMessage;
+ }
+
+ @Override
+ public @NotNull HandlerList getHandlers() {
+ return handlers;
+ }
+
+ @NotNull
+ public static HandlerList getHandlerList() {
+ return handlers;
+ }
+
+ public Component deathMessage() {
+ return deathMessage;
+ }
+
+ public void deathMessage(Component deathMessage) {
+ this.deathMessage = deathMessage;
+ }
+
+ @Nullable
+ public String getDeathMessage() {
+ return this.deathMessage == null ? null : LegacyComponentSerializer.legacySection().serialize(this.deathMessage);
+ }
+
+ public void setDeathMessage(@Nullable String deathMessage) {
+ this.deathMessage = deathMessage != null ? LegacyComponentSerializer.legacySection().deserialize(deathMessage) : null;
+ }
+
+ public boolean isSendDeathMessage() {
+ return sendDeathMessage;
+ }
+
+ public void setSendDeathMessage(boolean sendDeathMessage) {
+ this.sendDeathMessage = sendDeathMessage;
+ }
+
+ @Override
+ public boolean isCancelled() {
+ return cancel;
+ }
+
+ @Override
+ public void setCancelled(boolean cancel) {
+ this.cancel = cancel;
+ }
+}
diff --git a/src/main/java/org/leavesmc/leaves/event/bot/BotEvent.java b/src/main/java/org/leavesmc/leaves/event/bot/BotEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..ad358081f1e1da4075243d7ca0a01c1f7b00631b
Expand Down Expand Up @@ -745,10 +755,10 @@ index 0000000000000000000000000000000000000000..07f6d81c4cd897230bbd6712dac09b89
+}
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..7af990a5ee020dfdbec2efc6aecf6393a4223730
index 0000000000000000000000000000000000000000..e950643e4b4da99b067d7392034a651361094853
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/event/bot/BotRemoveEvent.java
@@ -0,0 +1,105 @@
@@ -0,0 +1,107 @@
+package org.leavesmc.leaves.event.bot;
+
+import net.kyori.adventure.text.Component;
Expand Down Expand Up @@ -776,23 +786,17 @@ index 0000000000000000000000000000000000000000..7af990a5ee020dfdbec2efc6aecf6393
+ private static final HandlerList handlers = new HandlerList();
+
+ private final RemoveReason reason;
+ private final Optional<CommandSender> remover;
+ private final CommandSender remover;
+ private Component removeMessage;
+ private boolean saved;
+ 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) {
+ this(who, reason, remover, null);
+ }
+
+ public BotRemoveEvent(@NotNull final Bot who, @NotNull RemoveReason reason, @Nullable CommandSender remover, @Nullable Component removeMessage) {
+ public BotRemoveEvent(@NotNull final Bot who, @NotNull RemoveReason reason, @Nullable CommandSender remover, @Nullable Component removeMessage, boolean saved) {
+ super(who);
+ this.reason = reason;
+ this.remover = Optional.ofNullable(remover);
+ this.remover = remover;
+ this.removeMessage = removeMessage;
+ this.saved = saved;
+ }
+
+ /**
Expand All @@ -807,12 +811,12 @@ index 0000000000000000000000000000000000000000..7af990a5ee020dfdbec2efc6aecf6393
+
+ /**
+ * Gets the remover of the bot
+ * if the remove reason is not COMMAND, the creator might be Optional.empty()
+ * if the remove reason is not COMMAND, the creator might be null
+ *
+ * @return An optional of remover
+ */
+ @NotNull
+ public Optional<CommandSender> getRemover() {
+ @Nullable
+ public CommandSender getRemover() {
+ return remover;
+ }
+
Expand Down Expand Up @@ -843,6 +847,14 @@ index 0000000000000000000000000000000000000000..7af990a5ee020dfdbec2efc6aecf6393
+ this.cancel = cancel;
+ }
+
+ public boolean isSaved() {
+ return saved;
+ }
+
+ public void setSaved(boolean saved) {
+ this.saved = saved;
+ }
+
+ @Override
+ @NotNull
+ public HandlerList getHandlers() {
Expand Down
Loading

0 comments on commit bb2b457

Please sign in to comment.