9
0
mirror of https://github.com/LeavesMC/Leaves.git synced 2026-01-06 15:51:33 +00:00

feat: refactor fakeplayer api(#593)

This commit is contained in:
MC_XiaoHei
2025-07-08 10:55:59 +08:00
parent 4f839fbd68
commit 90a5a1d450
97 changed files with 1415 additions and 1051 deletions

View File

@@ -19,7 +19,7 @@ index 3bde4ad79ade5aae18e9073307f637717e8dd9e3..9971ed1347f0f37800911c6cd9d0f8ae
+ *
+ * @return Bot Manager
+ */
+ public static @NotNull org.leavesmc.leaves.entity.BotManager getBotManager() {
+ public static @NotNull org.leavesmc.leaves.entity.bot.BotManager getBotManager() {
+ return server.getBotManager();
+ }
+ // Leaves end - Bot API
@@ -39,6 +39,6 @@ index 9bab00ab10c78908090c8a1a12d4c84e9324b08b..3e7aad4ddf573f7c868b7824c4f0f34f
+ *
+ * @return Bot Manager
+ */
+ @NotNull org.leavesmc.leaves.entity.BotManager getBotManager();
+ @NotNull org.leavesmc.leaves.entity.bot.BotManager getBotManager();
+ // Leaves end - Bot API
}

View File

@@ -14,7 +14,7 @@ index 9971ed1347f0f37800911c6cd9d0f8ae1a4f100c..803611b793daed2d51ef6ab34d01fc8b
// Leaves end - Bot API
+
+ // Leaves start - Photographer API
+ public static @NotNull org.leavesmc.leaves.entity.PhotographerManager getPhotographerManager() {
+ public static @NotNull org.leavesmc.leaves.entity.photographer.PhotographerManager getPhotographerManager() {
+ return server.getPhotographerManager();
+ }
+ // Leaves end - Photographer API
@@ -25,10 +25,10 @@ index 3e7aad4ddf573f7c868b7824c4f0f34fa08cb1fe..ce128dd8120b75884cb208d7ba7d316e
+++ b/src/main/java/org/bukkit/Server.java
@@ -2732,4 +2732,8 @@ public interface Server extends PluginMessageRecipient, net.kyori.adventure.audi
*/
@NotNull org.leavesmc.leaves.entity.BotManager getBotManager();
@NotNull org.leavesmc.leaves.entity.bot.BotManager getBotManager();
// Leaves end - Bot API
+
+ // Leaves start - Photographer API
+ @NotNull org.leavesmc.leaves.entity.PhotographerManager getPhotographerManager();
+ @NotNull org.leavesmc.leaves.entity.photographer.PhotographerManager getPhotographerManager();
+ // Leaves end - Photographer API
}

View File

@@ -25,7 +25,7 @@ index ce128dd8120b75884cb208d7ba7d316ee110333b..e63fb4e0c55929f2721e16f69e0c0a4b
+++ b/src/main/java/org/bukkit/Server.java
@@ -2736,4 +2736,8 @@ public interface Server extends PluginMessageRecipient, net.kyori.adventure.audi
// Leaves start - Photographer API
@NotNull org.leavesmc.leaves.entity.PhotographerManager getPhotographerManager();
@NotNull org.leavesmc.leaves.entity.photographer.PhotographerManager getPhotographerManager();
// Leaves end - Photographer API
+
+ // Leaves start - Bytebuf API

View File

@@ -1,9 +1,9 @@
package org.leavesmc.leaves.entity;
package org.leavesmc.leaves.entity.bot;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.leavesmc.leaves.entity.botaction.LeavesBotAction;
import org.leavesmc.leaves.entity.bot.action.BotAction;
import java.util.UUID;
@@ -39,7 +39,7 @@ public interface Bot extends Player {
* @param action bot action
*/
@org.jetbrains.annotations.ApiStatus.Experimental
void addAction(@NotNull LeavesBotAction action);
void addAction(@NotNull BotAction action);
/**
* Get the copy action in giving index
@@ -48,7 +48,7 @@ public interface Bot extends Player {
* @return Action of that index
*/
@org.jetbrains.annotations.ApiStatus.Experimental
LeavesBotAction getAction(int index);
BotAction getAction(int index);
/**
* Get action size

View File

@@ -1,4 +1,4 @@
package org.leavesmc.leaves.entity;
package org.leavesmc.leaves.entity.bot;
import org.bukkit.Bukkit;
import org.bukkit.Location;

View File

@@ -1,9 +1,10 @@
package org.leavesmc.leaves.entity;
package org.leavesmc.leaves.entity.bot;
import org.bukkit.Location;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.leavesmc.leaves.entity.botaction.CustomBotAction;
import org.leavesmc.leaves.entity.bot.action.BotAction;
import org.leavesmc.leaves.entity.bot.action.CustomBotAction;
import java.util.Collection;
import java.util.UUID;
@@ -55,5 +56,13 @@ public interface BotManager {
@org.jetbrains.annotations.ApiStatus.Experimental
boolean unregisterCustomBotAction(String name);
/**
* Create a bot action by class.
*
* @param type action class
* @return a bot action instance if one was found, null otherwise
*/
<T extends BotAction<T>> T newAction(@NotNull Class<T> type);
BotCreator botCreator(@NotNull String realName, @NotNull Location location);
}

View File

@@ -0,0 +1,4 @@
package org.leavesmc.leaves.entity.bot.action;
public interface AttackAction extends TimerBotAction<AttackAction> {
}

View File

@@ -0,0 +1,27 @@
package org.leavesmc.leaves.entity.bot.action;
import java.util.UUID;
import java.util.function.Consumer;
public interface BotAction<T> {
String getName();
UUID getUUID();
void setCancelled(boolean cancel);
boolean isCancelled();
void setOnFail(Consumer<T> onFail);
Consumer<T> getOnFail();
void setOnSuccess(Consumer<T> onSuccess);
Consumer<T> getOnSuccess();
void setOnStop(Consumer<T> onStop);
Consumer<T> getOnStop();
}

View File

@@ -0,0 +1,4 @@
package org.leavesmc.leaves.entity.bot.action;
public interface BreakBlockAction extends TimerBotAction<BreakBlockAction>{
}

View File

@@ -1,17 +1,16 @@
package org.leavesmc.leaves.entity.botaction;
package org.leavesmc.leaves.entity.bot.action;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.leavesmc.leaves.entity.Bot;
import org.leavesmc.leaves.entity.bot.Bot;
import java.util.List;
/**
* Represents a class which contains methods for a custom bot action
*/
@org.jetbrains.annotations.ApiStatus.Experimental
public interface CustomBotAction {
public interface CustomBotAction extends BotAction<CustomBotAction> {
/**
* Executes the action, returning its success.
@@ -36,25 +35,4 @@ public interface CustomBotAction {
* @return A List of a List of possible completions for the argument.
*/
@NotNull List<List<String>> getTabComplete();
/**
* Return the interval between {@link CustomBotAction#doTick(Bot)}
*
* @return the tick interval
*/
int getInitialTickInterval();
/**
* Return the tick delay to the first {@link CustomBotAction#doTick(Bot)}
*
* @return the tick delay
*/
int getInitialTickDelay();
/**
* Return a number of times {@link CustomBotAction#doTick(Bot)} can return true
*
* @return the number of times an action can be executed
*/
int getInitialNumber();
}

View File

@@ -0,0 +1,28 @@
package org.leavesmc.leaves.entity.bot.action;
public interface CustomStateBotAction extends StateBotAction<CustomStateBotAction> {
/**
* Executes the action, returning its success.
*
* @param bot bot of the action
* @return true if once action finish, otherwise false
*/
boolean doTick(org.leavesmc.leaves.entity.bot.Bot bot);
/**
* Created a new action instance.
*
* @param player player who create this action
* @param args passed action arguments
* @return a new action instance with given args
*/
@org.jetbrains.annotations.Nullable CustomStateBotAction getNew(@org.jetbrains.annotations.Nullable org.bukkit.entity.Player player, String[] args);
/**
* Requests a list of possible completions for a action argument.
*
* @return A List of a List of possible completions for the argument.
*/
@org.jetbrains.annotations.NotNull java.util.List<java.util.List<String>> getTabComplete();
}

View File

@@ -0,0 +1,38 @@
package org.leavesmc.leaves.entity.bot.action;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.leavesmc.leaves.entity.bot.Bot;
import java.util.List;
/**
* Represents a class which contains methods for a custom timer bot action
*/
public interface CustomTimerBotAction extends TimerBotAction<CustomTimerBotAction> {
/**
* Executes the action, returning its success.
*
* @param bot bot of the action
* @return true if once action finish, otherwise false
*/
boolean doTick(Bot bot);
/**
* Created a new action instance.
*
* @param player player who create this action
* @param args passed action arguments
* @return a new action instance with given args
*/
@Nullable CustomTimerBotAction getNew(@Nullable Player player, String[] args);
/**
* Requests a list of possible completions for a action argument.
*
* @return A List of a List of possible completions for the argument.
*/
@NotNull List<List<String>> getTabComplete();
}

View File

@@ -0,0 +1,4 @@
package org.leavesmc.leaves.entity.bot.action;
public interface DropAction extends TimerBotAction<DropAction> {
}

View File

@@ -0,0 +1,4 @@
package org.leavesmc.leaves.entity.bot.action;
public interface FishAction extends TimerBotAction<FishAction> {
}

View File

@@ -0,0 +1,4 @@
package org.leavesmc.leaves.entity.bot.action;
public interface JumpAction extends TimerBotAction<JumpAction> {
}

View File

@@ -0,0 +1,39 @@
package org.leavesmc.leaves.entity.bot.action;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
public interface LookAction extends BotAction<LookAction> {
/**
* Sets the position to look at.
*
* @param pos the position to look at
* @return this action instance for method chaining
*/
LookAction setPos(Vector pos);
/**
* Gets the position to look at.
*
* @return the position to look at
*/
Vector getPos();
/**
* Sets the player to look to.
* When set to a player, the bot will look at the player's current position,
* which will override the position set by {@link #setPos(Vector)}.
*
* @param player the player to set
* @return this action instance for method chaining
*/
LookAction setTarget(Player player);
/**
* Gets the player to look to.
*
* @return the player
*/
Player getTarget();
}

View File

@@ -0,0 +1,31 @@
package org.leavesmc.leaves.entity.bot.action;
public interface MoveAction extends StateBotAction<MoveAction> {
/**
* Gets the direction of the move action.
*
* @return the direction of the move action
*/
MoveDirection getDirection();
/**
* Sets the direction of the move action.
*
* @param direction the direction to set
*/
void setDirection(MoveDirection direction);
enum MoveDirection {
FORWARD("forward"),
BACKWARD("backward"),
LEFT("left"),
RIGHT("right");
public final String name;
MoveDirection(String name) {
this.name = name;
}
}
}

View File

@@ -0,0 +1,33 @@
package org.leavesmc.leaves.entity.bot.action;
public interface RotationAction extends BotAction<RotationAction> {
/**
* Sets the yaw of the rotation.
*
* @param yaw the yaw to set
* @return this action instance
*/
RotationAction setYaw(float yaw);
/**
* Sets the pitch of the rotation.
*
* @param pitch the pitch to set
* @return this action instance
*/
RotationAction setPitch(float pitch);
/**
* Gets the yaw of the rotation.
*
* @return the yaw
*/
float getYaw();
/**
* Gets the pitch of the rotation.
*
* @return the pitch
*/
float getPitch();
}

View File

@@ -0,0 +1,18 @@
package org.leavesmc.leaves.entity.bot.action;
public interface ShootAction extends TimerBotAction<ShootAction> {
/**
* Gets the drawing tick for the shoot action.
*
* @return the drawing tick
*/
int getDrawingTick();
/**
* Sets the drawing tick for the shoot action.
*
* @param drawingTick the drawing tick to set
*/
ShootAction setDrawingTick(int drawingTick);
}

View File

@@ -0,0 +1,4 @@
package org.leavesmc.leaves.entity.bot.action;
public interface SneakAction extends BotAction<SneakAction> {
}

View File

@@ -0,0 +1,4 @@
package org.leavesmc.leaves.entity.bot.action;
public interface StateBotAction<E> extends BotAction<E> {
}

View File

@@ -0,0 +1,4 @@
package org.leavesmc.leaves.entity.bot.action;
public interface SwimAction extends StateBotAction<SwimAction> {
}

View File

@@ -0,0 +1,20 @@
package org.leavesmc.leaves.entity.bot.action;
public interface TimerBotAction<E> extends BotAction<E> {
void setStartDelayTick(int delayTick);
int getStartDelayTick();
void setDoIntervalTick(int intervalTick);
int getDoIntervalTick();
void setDoNumber(int doNumber);
int getDoNumber();
int getTickToNext();
int getDoNumberRemaining();
}

View File

@@ -0,0 +1,4 @@
package org.leavesmc.leaves.entity.bot.action;
public interface UseItemAction extends TimerBotAction<UseItemAction> {
}

View File

@@ -0,0 +1,4 @@
package org.leavesmc.leaves.entity.bot.action;
public interface UseItemAutoAction extends TimerBotAction<UseItemAutoAction> {
}

View File

@@ -0,0 +1,4 @@
package org.leavesmc.leaves.entity.bot.action;
public interface UseItemAutoOffhandAction extends TimerBotAction<UseItemAutoOffhandAction> {
}

View File

@@ -0,0 +1,4 @@
package org.leavesmc.leaves.entity.bot.action;
public interface UseItemOffHandAction extends TimerBotAction<UseItemOffHandAction> {
}

View File

@@ -0,0 +1,4 @@
package org.leavesmc.leaves.entity.bot.action;
public interface UseItemOnAction extends TimerBotAction<UseItemOnAction> {
}

View File

@@ -0,0 +1,4 @@
package org.leavesmc.leaves.entity.bot.action;
public interface UseItemOnOffhandAction extends TimerBotAction<UseItemOnOffhandAction> {
}

View File

@@ -0,0 +1,4 @@
package org.leavesmc.leaves.entity.bot.action;
public interface UseItemToAction extends TimerBotAction<UseItemToAction> {
}

View File

@@ -0,0 +1,4 @@
package org.leavesmc.leaves.entity.bot.action;
public interface UseItemToOffhandAction extends TimerBotAction<UseItemToOffhandAction> {
}

View File

@@ -1,38 +0,0 @@
package org.leavesmc.leaves.entity.botaction;
/**
* A Leaves bot action enum
*/
@org.jetbrains.annotations.ApiStatus.Experimental
public enum BotActionType {
ATTACK("attack"),
BREAK("break"),
DROP("drop"),
FISH("fish"),
JUMP("jump"),
LOOK("look"),
ROTATE("rotate"),
ROTATION("rotation"),
SNEAK("sneak"),
SWIM("swim"),
MOVE("move"),
USE("use"),
USE_ON("use_on"),
USE_TO("use_to"),
USE_AUTO("use_auto"),
USE_OFFHAND("use_offhand"),
USE_ON_OFFHAND("use_on_offhand"),
USE_TO_OFFHAND("use_to_offhand"),
USE_AUTO_OFFHAND("use_auto_offhand");
private final String name;
BotActionType(String name) {
this.name = name;
}
public String getName() {
return name;
}
}

View File

@@ -1,112 +0,0 @@
package org.leavesmc.leaves.entity.botaction;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.UUID;
import java.util.function.Consumer;
@org.jetbrains.annotations.ApiStatus.Experimental
public class LeavesBotAction {
private final String actionName;
private final UUID uuid;
private final int initialTickDelay;
private final int initialTickInterval;
private final int initialNumber;
private final @Nullable Consumer<LeavesBotAction> onSuccess;
private final @Nullable Consumer<LeavesBotAction> onFail;
private Player actionPlayer;
private int tickToNext;
private int numberRemaining;
private boolean cancel;
public LeavesBotAction(@NotNull BotActionType type, int initialTickInterval, int initialNumber) {
this(type.getName(), UUID.randomUUID(), 0, initialTickInterval, initialNumber, null, null);
}
public LeavesBotAction(@NotNull BotActionType type, int initialTickInterval, int initialNumber, @Nullable Consumer<LeavesBotAction> onSuccess, @Nullable Consumer<LeavesBotAction> onFail) {
this(type.getName(), UUID.randomUUID(), 0, initialTickInterval, initialNumber, onSuccess, onFail);
}
public LeavesBotAction(@NotNull BotActionType type, int initialTickDelay, int initialTickInterval, int initialNumber) {
this(type.getName(), UUID.randomUUID(), initialTickDelay, initialTickInterval, initialNumber, null, null);
}
public LeavesBotAction(@NotNull BotActionType type, int initialTickDelay, int initialTickInterval, int initialNumber, @Nullable Consumer<LeavesBotAction> onSuccess, @Nullable Consumer<LeavesBotAction> onFail) {
this(type.getName(), UUID.randomUUID(), initialTickDelay, initialTickInterval, initialNumber, onSuccess, onFail);
}
protected LeavesBotAction(String name, UUID actionUUID, int initialTickDelay, int initialTickInterval, int initialNumber, @Nullable Consumer<LeavesBotAction> onSuccess, @Nullable Consumer<LeavesBotAction> onFail) {
this.actionName = name;
this.uuid = actionUUID;
this.initialTickDelay = initialTickDelay;
this.initialTickInterval = initialTickInterval;
this.initialNumber = initialNumber;
this.onSuccess = onSuccess;
this.onFail = onFail;
}
public String getActionName() {
return actionName;
}
public UUID getUuid() {
return uuid;
}
public int getInitialTickDelay() {
return initialTickDelay;
}
public int getInitialTickInterval() {
return initialTickInterval;
}
public int getInitialNumber() {
return initialNumber;
}
@Nullable
public Player getActionPlayer() {
return actionPlayer;
}
public void setActionPlayer(@Nullable Player actionPlayer) {
this.actionPlayer = actionPlayer;
}
public boolean isCancel() {
return cancel;
}
public void setCancel(boolean cancel) {
this.cancel = cancel;
}
public int getNumberRemaining() {
return numberRemaining;
}
public void setNumberRemaining(int numberRemaining) {
this.numberRemaining = numberRemaining;
}
public int getTickToNext() {
return tickToNext;
}
public void setTickToNext(int tickToNext) {
this.tickToNext = tickToNext;
}
public @Nullable Consumer<LeavesBotAction> getOnSuccess() {
return onSuccess;
}
public @Nullable Consumer<LeavesBotAction> getOnFail() {
return onFail;
}
}

View File

@@ -1,14 +0,0 @@
package org.leavesmc.leaves.entity.botaction;
public enum MoveDirection {
FORWARD("forward"),
BACKWARD("backward"),
LEFT("left"),
RIGHT("right");
public final String name;
MoveDirection(String name) {
this.name = name;
}
}

View File

@@ -1,4 +1,4 @@
package org.leavesmc.leaves.entity;
package org.leavesmc.leaves.entity.photographer;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

View File

@@ -1,4 +1,4 @@
package org.leavesmc.leaves.entity;
package org.leavesmc.leaves.entity.photographer;
import org.bukkit.Location;
import org.jetbrains.annotations.NotNull;

View File

@@ -1,7 +1,7 @@
package org.leavesmc.leaves.event.bot;
import org.jetbrains.annotations.NotNull;
import org.leavesmc.leaves.entity.Bot;
import org.leavesmc.leaves.entity.bot.Bot;
import java.util.UUID;

View File

@@ -3,7 +3,7 @@ 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;
import org.leavesmc.leaves.entity.bot.Bot;
import java.util.UUID;

View File

@@ -5,7 +5,7 @@ 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 org.leavesmc.leaves.entity.bot.Bot;
import java.util.UUID;

View File

@@ -5,7 +5,7 @@ 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 org.leavesmc.leaves.entity.bot.Bot;
import java.util.UUID;

View File

@@ -4,7 +4,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
import org.leavesmc.leaves.entity.Bot;
import org.leavesmc.leaves.entity.bot.Bot;
public class BotConfigModifyEvent extends BotEvent implements Cancellable {

View File

@@ -6,7 +6,7 @@ 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 org.leavesmc.leaves.entity.bot.Bot;
public class BotDeathEvent extends BotEvent implements Cancellable {

View File

@@ -2,7 +2,7 @@ package org.leavesmc.leaves.event.bot;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;
import org.leavesmc.leaves.entity.Bot;
import org.leavesmc.leaves.entity.bot.Bot;
/**
* Represents a fakeplayer related event

View File

@@ -5,7 +5,7 @@ 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 org.leavesmc.leaves.entity.bot.Bot;
public class BotInventoryOpenEvent extends BotEvent implements Cancellable {

View File

@@ -5,7 +5,7 @@ import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.leavesmc.leaves.entity.Bot;
import org.leavesmc.leaves.entity.bot.Bot;
/**
* Called when a fakeplayer joins a server

View File

@@ -7,7 +7,7 @@ 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 org.leavesmc.leaves.entity.bot.Bot;
/**
* Call when a fakeplayer removed

View File

@@ -3,7 +3,7 @@ package org.leavesmc.leaves.event.bot;
import org.bukkit.Location;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
import org.leavesmc.leaves.entity.Bot;
import org.leavesmc.leaves.entity.bot.Bot;
public class BotSpawnLocationEvent extends BotEvent {