Skip to content

Commit

Permalink
Fixed lots of stuff in TaskUtils.java
Browse files Browse the repository at this point in the history
  • Loading branch information
vansencool committed Jul 24, 2024
1 parent f674b5c commit a044f44
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 32 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>dev.vansen</groupId>
<artifactId>Utility</artifactId>
<version>1.0.9</version>
<version>1.1.0</version>
<packaging>jar</packaging>

<name>Utility</name>
Expand Down
41 changes: 10 additions & 31 deletions src/main/java/dev/vansen/utility/tasks/scheduler/TaskUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import dev.vansen.utility.PluginHolder;
import dev.vansen.utility.Utility;
import dev.vansen.utility.tc.TimeConverter;
import org.bukkit.Bukkit;
import org.bukkit.Statistic;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -41,55 +40,35 @@ public void run(@NotNull Runnable task) {
}

public BukkitTask runLater(@NotNull Runnable task, long delay, @NotNull TimeUnit timeUnit) {
long ticks = convertMillisToTicks(TimeConverter.convertToMillis(timeUnit.name(), delay));
long delayMillis = timeUnit.toMillis(delay);
long ticks = convertMillisToTicks(delayMillis);
if (async) {
return Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, task, ticks);
} else {
return Bukkit.getScheduler().runTaskLater(plugin, task, ticks);
}
}

public BukkitTask runLater(@NotNull Runnable task, @NotNull String timeUnit, long duration) {
long delay = TimeConverter.convertToMillis(timeUnit, duration);
return runLater(task, delay, TimeUnit.MILLISECONDS);
}

public BukkitTask runTimer(@NotNull Runnable task, long delay, long period, @NotNull TimeUnit timeUnit) {
long delayTicks = convertMillisToTicks(TimeConverter.convertToMillis(timeUnit.name(), delay));
long periodTicks = convertMillisToTicks(TimeConverter.convertToMillis(timeUnit.name(), period));
long delayMillis = timeUnit.toMillis(delay);
long periodMillis = timeUnit.toMillis(period);
long delayTicks = convertMillisToTicks(delayMillis);
long periodTicks = convertMillisToTicks(periodMillis);
if (async) {
return Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, task, delayTicks, periodTicks);
} else {
return Bukkit.getScheduler().runTaskTimer(plugin, task, delayTicks, periodTicks);
}
}

public BukkitTask runTimer(@NotNull Runnable task, @NotNull String timeUnit, long delay, long period) {
long delayMillis = TimeConverter.convertToMillis(timeUnit, delay);
long periodMillis = TimeConverter.convertToMillis(timeUnit, period);
return runTimer(task, delayMillis, periodMillis, TimeUnit.MILLISECONDS);
}

public String when(@NotNull String taskId, @NotNull String timeFormat) {
long delayMillis = TimeConverter.convertToMillis(timeFormat, Long.parseLong(taskId));
return formatTime(timeFormat, delayMillis);
}

public void cancelAll() {
Bukkit.getScheduler().cancelTasks(plugin);
}

public void cancelTask(@NotNull BukkitTask task) {
task.cancel();
}

public void repeatForever(@NotNull Runnable task, long period, @NotNull TimeUnit timeUnit) {
runTimer(task, 0, period, timeUnit);
}

public void repeatForUnit(@NotNull Runnable task, @NotNull String timeUnit, long duration, long period) {
long delay = TimeConverter.convertToMillis(timeUnit, duration);
runTimer(task, delay, period, TimeUnit.MILLISECONDS);
public void repeatForUnit(@NotNull Runnable task, long duration, @NotNull TimeUnit durationUnit, long period, @NotNull TimeUnit periodUnit) {
long durationMillis = durationUnit.toMillis(duration);
long periodMillis = periodUnit.toMillis(period);
runTimer(task, durationMillis, periodMillis, TimeUnit.MILLISECONDS);
}

public void runConditional(@NotNull Runnable task, @NotNull TaskCondition condition, @NotNull Predicate<Object> predicate, int playerCount) {
Expand Down

0 comments on commit a044f44

Please sign in to comment.