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

Allow for formatted duration in wait action #183

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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 @@ -22,16 +22,16 @@ public void execute(@NotNull ActionExecutionContext context, String value) {
return;
}

int time;
long time;
try {
time = Integer.parseInt(value);
time = Long.parseLong(value);
} catch (NumberFormatException e) {
FancyNpcsPlugin.get().getFancyLogger().warn("Invalid time value for wait action: " + value);
return;
}

try {
Thread.sleep(time * 1000L);
Thread.sleep(time);
} catch (InterruptedException e) {
FancyNpcsPlugin.get().getFancyLogger().warn("Thread was interrupted while waiting");
}
Expand Down
87 changes: 87 additions & 0 deletions src/main/java/de/oliver/fancynpcs/commands/npc/ActionCMD.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import de.oliver.fancynpcs.api.Npc;
import de.oliver.fancynpcs.api.actions.ActionTrigger;
import de.oliver.fancynpcs.api.actions.NpcAction;
import de.oliver.fancynpcs.api.actions.types.WaitAction;
import org.bukkit.command.CommandSender;
import org.incendo.cloud.annotation.specifier.Greedy;
import org.incendo.cloud.annotations.Argument;
Expand All @@ -14,6 +15,7 @@
import org.incendo.cloud.context.CommandContext;
import org.incendo.cloud.context.CommandInput;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -51,6 +53,23 @@ public void onActionAdd(
.send(sender);
}

@Command("npc action <npc> <trigger> add wait <duration>")
@Permission("fancynpcs.command.npc.action.add")
public void onActionAddWait(
final @NotNull CommandSender sender,
final @NotNull Npc npc,
final @NotNull ActionTrigger trigger,
final @NotNull Duration duration
) {
List<NpcAction.NpcActionData> currentActions = npc.getData().getActions().getOrDefault(trigger, new ArrayList<>());

npc.getData().addAction(trigger, currentActions.size() + 1, new WaitAction(), String.valueOf(duration.toMillis()));
translator
.translate("npc_action_add_success")
.replaceStripped("total", String.valueOf(npc.getData().getActions(trigger).size()))
.send(sender);
}

@Command("npc action <npc> <trigger> add_before <index> <actionType> [value]")
@Permission("fancynpcs.command.npc.action.addBefore")
public void onActionAddBefore(
Expand Down Expand Up @@ -79,6 +98,27 @@ public void onActionAddBefore(
.send(sender);
}

@Command("npc action <npc> <trigger> add_before <index> wait <duration>")
@Permission("fancynpcs.command.npc.action.addBefore")
public void onActionAddBeforeWait(
final @NotNull CommandSender sender,
final @NotNull Npc npc,
final @NotNull ActionTrigger trigger,
final @Argument(suggestions = "ActionCMD/number_range") int index,
final @NotNull Duration duration
) {

List<NpcAction.NpcActionData> currentActions = npc.getData().getActions(trigger);
currentActions.add(Math.clamp(index - 1, 0, currentActions.size()), new NpcAction.NpcActionData(index, new WaitAction(), String.valueOf(duration.toMillis())));

npc.getData().setActions(trigger, reorderActions(currentActions));
translator
.translate("npc_action_add_before_success")
.replaceStripped("number", String.valueOf(index))
.replaceStripped("total", String.valueOf(npc.getData().getActions(trigger).size()))
.send(sender);
}

@Command("npc action <npc> <trigger> add_after <index> <actionType> [value]")
@Permission("fancynpcs.command.npc.action.addAfter")
public void onActionAddAfter(
Expand Down Expand Up @@ -107,6 +147,26 @@ public void onActionAddAfter(
.send(sender);
}

@Command("npc action <npc> <trigger> add_after <index> wait <duration>")
@Permission("fancynpcs.command.npc.action.addAfter")
public void onActionAddAfterWait(
final @NotNull CommandSender sender,
final @NotNull Npc npc,
final @NotNull ActionTrigger trigger,
final @Argument(suggestions = "ActionCMD/number_range") int index,
final @NotNull Duration duration
) {
List<NpcAction.NpcActionData> currentActions = npc.getData().getActions(trigger);
currentActions.add(Math.clamp(index, 0, currentActions.size() + 1), new NpcAction.NpcActionData(index, new WaitAction(), String.valueOf(duration.toMillis())));

npc.getData().setActions(trigger, reorderActions(currentActions));
translator
.translate("npc_action_add_after_success")
.replaceStripped("number", String.valueOf(index))
.replaceStripped("total", String.valueOf(npc.getData().getActions(trigger).size()))
.send(sender);
}

@Command("npc action <npc> <trigger> set <number> <actionType> [value]")
@Permission("fancynpcs.command.npc.action.set")
public void onActionSet(
Expand Down Expand Up @@ -142,6 +202,33 @@ public void onActionSet(
.send(sender);
}

@Command("npc action <npc> <trigger> set <number> wait <duration>")
@Permission("fancynpcs.command.npc.action.set")
public void onActionSetWait(
final @NotNull CommandSender sender,
final @NotNull Npc npc,
final @NotNull ActionTrigger trigger,
final @NotNull @Argument(suggestions = "ActionCMD/number_range") Integer number,
final @NotNull Duration duration
) {
List<NpcAction.NpcActionData> currentActions = npc.getData().getActions(trigger);
if (number < 1 || number > currentActions.size()) {
translator
.translate("npc_action_set_failure")
.replaceStripped("number", String.valueOf(number))
.send(sender);
return;
}

currentActions.set(number - 1, new NpcAction.NpcActionData(number, new WaitAction(), String.valueOf(duration.toMillis())));
npc.getData().setActions(trigger, currentActions);
translator
.translate("npc_action_set_success")
.replaceStripped("number", String.valueOf(number))
.replaceStripped("total", String.valueOf(npc.getData().getActions(trigger).size()))
.send(sender);
}

@Command("npc action <npc> <trigger> remove <number>")
@Permission("fancynpcs.command.npc.action.remove")
public void onActionRemove(
Expand Down