Skip to content

Commit

Permalink
add ActionScheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Jun 28, 2024
1 parent d813ac9 commit 783c01a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.extendedclip.deluxemenus.action;

import com.extendedclip.deluxemenus.DeluxeMenus;
import io.github.projectunified.minelib.scheduler.async.AsyncScheduler;
import io.github.projectunified.minelib.scheduler.common.scheduler.Scheduler;
import io.github.projectunified.minelib.scheduler.entity.EntityScheduler;
import io.github.projectunified.minelib.scheduler.global.GlobalScheduler;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;

import java.util.function.Function;

public enum ActionScheduler {
GLOBAL(player -> GlobalScheduler.get(DeluxeMenus.getInstance())),
ASYNC(player -> AsyncScheduler.get(DeluxeMenus.getInstance())),
PLAYER(player -> {
if (player == null) {
throw new IllegalArgumentException("Player cannot be null for player scheduler");
}
return EntityScheduler.get(DeluxeMenus.getInstance(), player);
})
;
private final Function<Player, Scheduler> schedulerFunction;

ActionScheduler(Function<Player, Scheduler> schedulerFunction) {
this.schedulerFunction = schedulerFunction;
}

public Scheduler getScheduler(@Nullable Player player) {
return schedulerFunction.apply(player);
}
}
14 changes: 12 additions & 2 deletions src/main/java/com/extendedclip/deluxemenus/action/ActionType.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public enum ActionType {
META("[meta]", "Handle meta for a player",
"- '[meta] <set/remove/add/subtract/switch> <key> <value type> <value>'"),
CONSOLE("[console]", "Execute a command from the console",
CONSOLE(ActionScheduler.GLOBAL, "[console]", "Execute a command from the console",
"- '[console] <command with no slash>'"),
PLAYER("[player]", "Execute a command for the menu viewer",
"- '[player] <command with no slash>'"),
Expand Down Expand Up @@ -52,16 +52,22 @@ public enum ActionType {
private static final Map<String, ActionType> BY_NAME = Arrays.stream(values())
.collect(Collectors.toMap(e -> e.name().toUpperCase(Locale.ROOT), Function.identity()));

private final ActionScheduler scheduler;
private final String identifier;
private final String description;
private final String usage;

ActionType(@NotNull final String identifier, @NotNull final String description, @NotNull final String usage) {
ActionType(@NotNull final ActionScheduler scheduler, @NotNull final String identifier, @NotNull final String description, @NotNull final String usage) {
this.scheduler = scheduler;
this.identifier = identifier;
this.description = description;
this.usage = usage;
}

ActionType(@NotNull final String identifier, @NotNull final String description, @NotNull final String usage) {
this(ActionScheduler.PLAYER, identifier, description, usage);
}

/**
* Get an {@link ActionType} by its name.
*
Expand Down Expand Up @@ -118,6 +124,10 @@ public static String listAllActionTypes() {
return builder.toString();
}

public @NotNull ActionScheduler getScheduler() {
return scheduler;
}

public @NotNull String getIdentifier() {
return identifier;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.github.projectunified.minelib.scheduler.entity.EntityScheduler;
import io.github.projectunified.minelib.scheduler.global.GlobalScheduler;
import io.github.projectunified.minelib.scheduler.common.scheduler.Scheduler;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.TextReplacementConfig;
Expand Down Expand Up @@ -188,7 +187,7 @@ public boolean onCommand(
true
);

EntityScheduler scheduler = EntityScheduler.get(plugin, holder.getViewer());
Scheduler scheduler = action.getType().getScheduler().getScheduler(holder.getViewer());

if (action.hasDelay()) {
scheduler.runLater(actionTask, action.getDelay(holder));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import io.github.projectunified.minelib.scheduler.entity.EntityScheduler;
import io.github.projectunified.minelib.scheduler.global.GlobalScheduler;

import io.github.projectunified.minelib.scheduler.common.scheduler.Scheduler;
import com.google.common.base.Enums;
import org.bukkit.DyeColor;
import org.bukkit.Location;
Expand Down Expand Up @@ -1505,7 +1504,7 @@ public void onClick(@NotNull final MenuHolder holder) {
holder.parsePlaceholdersAfterArguments()
);

EntityScheduler scheduler = EntityScheduler.get(plugin, holder.getViewer());
Scheduler scheduler = action.getType().getScheduler().getScheduler(holder.getViewer());

if (action.hasDelay()) {
scheduler.runLater(actionTask, action.getDelay(holder));
Expand Down

0 comments on commit 783c01a

Please sign in to comment.