Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite bot action #440

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ public abstract class AbstractBotAction<E extends AbstractBotAction<E>> {
private final String name;
private final CommandArgument argument;
private final Supplier<E> creator;

private boolean cancel;
private int tickDelay;
private int number;
private UUID uuid;

private int needWaitTick;
private int initialTickDelay;
private int initialTickInterval;
private int initialNumber;

private int tickToNext;
private int canDoNumber;
private boolean cancel;

public AbstractBotAction(String name, CommandArgument argument, Supplier<E> creator) {
this.name = name;
Expand All @@ -35,129 +36,150 @@ public AbstractBotAction(String name, CommandArgument argument, Supplier<E> crea
this.creator = creator;

this.cancel = false;
this.tickDelay = 20;
this.number = -1;
this.initialTickInterval = 20;
this.initialNumber = -1;
}

public void init() {
this.needWaitTick = 0;
this.canDoNumber = this.getNumber();
this.tickToNext = initialTickDelay;
this.canDoNumber = this.getInitialNumber();
this.setCancelled(false);
}

public String getName() {
return this.name;
}

public UUID getUUID() {
return uuid;
}

public int getTickDelay() {
return this.tickDelay;
}

@SuppressWarnings("unchecked")
public E setTickDelay(int tickDelay) {
this.tickDelay = Math.max(0, tickDelay);
return (E) this;
}

public int getNumber() {
return this.number;
}

@SuppressWarnings("unchecked")
public E setNumber(int number) {
this.number = Math.max(-1, number);
return (E) this;
}

public int getCanDoNumber() {
return this.canDoNumber;
}

public boolean isCancelled() {
return cancel;
}

public void setCancelled(boolean cancel) {
this.cancel = cancel;
}

public void stop(@NotNull ServerBot bot, BotActionStopEvent.Reason reason) {
new BotActionStopEvent(bot.getBukkitEntity(), this.name, this.uuid, reason, null).callEvent();
this.setCancelled(true);
}

public CommandArgument getArgument() {
return this.argument;
}

@SuppressWarnings("unchecked")
public E setTabComplete(int index, List<String> list) {
this.argument.setTabComplete(index, list);
return (E) this;
}

public void tryTick(ServerBot bot) {
if (this.canDoNumber == 0) {
this.stop(bot, BotActionStopEvent.Reason.DONE);
return;
}

if (this.needWaitTick <= 0) {
if (this.tickToNext <= 0) {
BotActionExecuteEvent event = new BotActionExecuteEvent(bot.getBukkitEntity(), name, uuid);

event.callEvent();
if (event.getResult() == BotActionExecuteEvent.Result.SOFT_CANCEL) {
this.needWaitTick = this.getTickDelay();
this.tickToNext = this.getInitialTickInterval() - 1;
return;
} else if (event.getResult() == BotActionExecuteEvent.Result.HARD_CANCEL) {
if (this.canDoNumber > 0) {
this.canDoNumber--;
}
this.needWaitTick = this.getTickDelay();
this.tickToNext = this.getInitialTickInterval() - 1;
return;
}

if (this.doTick(bot)) {
if (this.canDoNumber > 0) {
this.canDoNumber--;
}
this.needWaitTick = this.getTickDelay();
this.tickToNext = this.getInitialTickInterval() - 1;
}
} else {
this.needWaitTick--;
this.tickToNext--;
}
}

@NotNull
public E create() {
return this.creator.get();
}

@NotNull
public CompoundTag save(@NotNull CompoundTag nbt) {
if (!this.cancel) {
nbt.putString("actionName", this.name);
nbt.putUUID("actionUUID", this.uuid);

nbt.putInt("initialTickDelay", this.initialTickDelay);
nbt.putInt("initialTickInterval", this.initialTickInterval);
nbt.putInt("initialNumber", this.initialNumber);

nbt.putInt("tickToNext", this.tickToNext);
nbt.putInt("canDoNumber", this.canDoNumber);
nbt.putInt("needWaitTick", this.needWaitTick);
nbt.putInt("tickDelay", this.tickDelay);
}
return nbt;
}

public void load(@NotNull CompoundTag nbt) {
this.tickDelay = nbt.getInt("tickDelay");
this.needWaitTick = nbt.getInt("needWaitTick");
this.canDoNumber = nbt.getInt("canDoNumber");
this.uuid = nbt.getUUID("actionUUID");

this.initialTickDelay = nbt.getInt("initialTickDelay");
this.initialTickInterval = nbt.getInt("initialTickInterval");
this.initialNumber = nbt.getInt("initialNumber");

this.tickToNext = nbt.getInt("tickToNext");
this.canDoNumber = nbt.getInt("canDoNumber");
}

public void stop(@NotNull ServerBot bot, BotActionStopEvent.Reason reason) {
new BotActionStopEvent(bot.getBukkitEntity(), this.name, this.uuid, reason, null).callEvent();
this.setCancelled(true);
}

public abstract void loadCommand(@Nullable ServerPlayer player, @NotNull CommandArgumentResult result);

public abstract boolean doTick(@NotNull ServerBot bot);

@SuppressWarnings("unchecked")
public E setTabComplete(int index, List<String> list) {
this.argument.setTabComplete(index, list);
return (E) this;
}

public String getName() {
return this.name;
}

public UUID getUUID() {
return uuid;
}

@SuppressWarnings("unchecked")
public E setInitialTickDelay(int initialTickDelay) {
this.initialTickDelay = initialTickDelay;
return (E) this;
}

public int getInitialTickDelay() {
return this.initialTickDelay;
}

public int getInitialTickInterval() {
return this.initialTickInterval;
}

@SuppressWarnings("unchecked")
public E setInitialTickInterval(int initialTickInterval) {
this.initialTickInterval = Math.max(1, initialTickInterval);
return (E) this;
}

public int getInitialNumber() {
return this.initialNumber;
}

@SuppressWarnings("unchecked")
public E setInitialNumber(int initialNumber) {
this.initialNumber = Math.max(-1, initialNumber);
return (E) this;
}

public int getTickToNext() {
return this.tickToNext;
}

public int getCanDoNumber() {
return this.canDoNumber;
}

public boolean isCancelled() {
return cancel;
}

public void setCancelled(boolean cancel) {
this.cancel = cancel;
}

public CommandArgument getArgument() {
return this.argument;
}

@NotNull
public E create() {
return this.creator.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
import org.leavesmc.leaves.command.CommandArgumentResult;
import org.leavesmc.leaves.command.CommandArgumentType;

import java.util.List;
import java.util.Collections;
import java.util.function.Supplier;

public abstract class AbstractTimerAction<E extends AbstractTimerAction<E>> extends AbstractBotAction<E> {

public AbstractTimerAction(String name, Supplier<E> creator) {
super(name, CommandArgument.of(CommandArgumentType.INTEGER, CommandArgumentType.INTEGER), creator);
this.setTabComplete(0, List.of("[TickDelay]")).setTabComplete(1, List.of("[DoNumber]"));
this.setTabComplete(0, Collections.singletonList("[TickDelay]")).setTabComplete(1, Collections.singletonList("[TickInterval]")).setTabComplete(2, Collections.singletonList("[DoNumber]"));
}

@Override
public void loadCommand(@Nullable ServerPlayer player, @NotNull CommandArgumentResult result) {
this.setTickDelay(result.readInt(20)).setNumber(result.readInt(-1));
this.setInitialTickDelay(result.readInt(0)).setInitialTickInterval(result.readInt(20)).setInitialNumber(result.readInt(-1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class CraftBotAction extends LeavesBotAction {
private final AbstractBotAction<?> handle;

public CraftBotAction(@NotNull AbstractBotAction<?> action) {
super(BotActionType.valueOf(action.getName()), action.getTickDelay(), action.getCanDoNumber());
super(BotActionType.valueOf(action.getName()), action.getInitialTickInterval(), action.getCanDoNumber());
this.handle = action;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ public CraftCustomBotAction createCraft(@Nullable Player player, String[] args)
}

@Override
public int getNumber() {
public int getInitialNumber() {
return realAction.getNumber();
}

@Override
public int getTickDelay() {
public int getInitialTickInterval() {
return realAction.getTickDelay();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public DropAction() {

@Override
public void loadCommand(@Nullable ServerPlayer player, @NotNull CommandArgumentResult result) {
this.setTickDelay(result.readInt(100)).setNumber(result.readInt(1));
this.setInitialTickDelay(result.readInt(100)).setInitialTickInterval(result.readInt(100)).setInitialNumber(result.readInt(1));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,38 @@ public FishAction() {
super("fish", FishAction::new);
}

private int delay = 0;
private int nowDelay = 0;
private static final int CATCH_ENTITY_DELAY = 20;

private int initialFishInterval = 0;
private int tickToNextFish = 0;

@Override
public FishAction setTickDelay(int tickDelay) {
super.setTickDelay(0);
this.delay = tickDelay;
public FishAction setInitialTickInterval(int initialTickInterval) {
super.setInitialTickInterval(1);
this.initialFishInterval = initialTickInterval;
return this;
}

@Override
@NotNull
public CompoundTag save(@NotNull CompoundTag nbt) {
super.save(nbt);
nbt.putInt("fishDelay", this.delay);
nbt.putInt("fishNowDelay", this.nowDelay);
nbt.putInt("initialFishInterval", this.initialFishInterval);
nbt.putInt("tickToNextFish", this.tickToNextFish);
return nbt;
}

@Override
public void load(@NotNull CompoundTag nbt) {
super.load(nbt);
this.delay = nbt.getInt("fishDelay");
this.nowDelay = nbt.getInt("fishNowDelay");
this.initialFishInterval = nbt.getInt("initialFishInterval");
this.tickToNextFish = nbt.getInt("tickToNextFish");
}

@Override
public boolean doTick(@NotNull ServerBot bot) {
if (this.nowDelay > 0) {
this.nowDelay--;
if (this.tickToNextFish > 0) {
this.tickToNextFish--;
return false;
}

Expand All @@ -56,12 +58,12 @@ public boolean doTick(@NotNull ServerBot bot) {
if (fishingHook != null) {
if (fishingHook.currentState == FishingHook.FishHookState.HOOKED_IN_ENTITY) {
mainHand.use(bot.level(), bot, InteractionHand.MAIN_HAND);
this.nowDelay = 20;
this.tickToNextFish = CATCH_ENTITY_DELAY;
return false;
}
if (fishingHook.nibble > 0) {
mainHand.use(bot.level(), bot, InteractionHand.MAIN_HAND);
this.nowDelay = this.delay;
this.tickToNextFish = this.initialFishInterval - 1;
return true;
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public LookAction() {
public void loadCommand(@Nullable ServerPlayer player, @NotNull CommandArgumentResult result) throws IllegalArgumentException {
Vector pos = result.readVector();
if (pos != null) {
this.setPos(pos).setTickDelay(0).setNumber(1);
this.setPos(pos).setInitialTickDelay(0).setInitialTickInterval(1).setInitialNumber(1);
} else {
throw new IllegalArgumentException("pos?");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public RotateAction() {

@Override
public void loadCommand(@Nullable ServerPlayer player, @NotNull CommandArgumentResult result) {
this.setPlayer(player).setTickDelay(0).setNumber(1);
this.setPlayer(player).setInitialTickDelay(0).setInitialTickInterval(1).setInitialNumber(1);
}

public RotateAction setPlayer(ServerPlayer player) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void loadCommand(@Nullable ServerPlayer player, @NotNull CommandArgumentR
return;
}

this.setYaw(result.readFloat(player.getYRot())).setPitch(result.readFloat(player.getXRot())).setTickDelay(0).setNumber(1);
this.setYaw(result.readFloat(player.getYRot())).setPitch(result.readFloat(player.getXRot())).setInitialTickDelay(0).setInitialTickInterval(1).setInitialNumber(1);
}

public RotationAction setYaw(float yaw) {
Expand Down
Loading