Mirror von
https://github.com/ViaVersion/ViaVersion.git
synchronisiert 2024-11-04 23:30:24 +01:00
Implement Task Ids
Dieser Commit ist enthalten in:
Ursprung
23f1589c28
Commit
771c00b146
@ -12,10 +12,12 @@ import us.myles.ViaVersion.api.ViaAPI;
|
||||
import us.myles.ViaVersion.api.ViaVersion;
|
||||
import us.myles.ViaVersion.api.command.ViaCommandSender;
|
||||
import us.myles.ViaVersion.api.configuration.ConfigurationProvider;
|
||||
import us.myles.ViaVersion.api.platform.TaskId;
|
||||
import us.myles.ViaVersion.api.platform.ViaPlatform;
|
||||
import us.myles.ViaVersion.bukkit.classgenerator.ClassGenerator;
|
||||
import us.myles.ViaVersion.bukkit.commands.BukkitCommandHandler;
|
||||
import us.myles.ViaVersion.bukkit.commands.BukkitCommandSender;
|
||||
import us.myles.ViaVersion.bukkit.platform.BukkitTaskId;
|
||||
import us.myles.ViaVersion.bukkit.platform.BukkitViaAPI;
|
||||
import us.myles.ViaVersion.bukkit.platform.BukkitViaInjector;
|
||||
import us.myles.ViaVersion.bukkit.platform.BukkitViaLoader;
|
||||
@ -151,33 +153,37 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int runAsync(Runnable runnable) {
|
||||
public TaskId runAsync(Runnable runnable) {
|
||||
if (isPluginEnabled()) {
|
||||
return getServer().getScheduler().runTaskAsynchronously(this, runnable).getTaskId();
|
||||
return new BukkitTaskId(getServer().getScheduler().runTaskAsynchronously(this, runnable).getTaskId());
|
||||
} else {
|
||||
asyncQueuedTasks.add(runnable);
|
||||
return -1;
|
||||
return new BukkitTaskId(null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int runSync(Runnable runnable) {
|
||||
public TaskId runSync(Runnable runnable) {
|
||||
if (isPluginEnabled()) {
|
||||
return getServer().getScheduler().runTask(this, runnable).getTaskId();
|
||||
return new BukkitTaskId(getServer().getScheduler().runTask(this, runnable).getTaskId());
|
||||
} else {
|
||||
queuedTasks.add(runnable);
|
||||
return -1;
|
||||
return new BukkitTaskId(null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int runRepeatingSync(Runnable runnable, Long ticks) {
|
||||
return getServer().getScheduler().runTaskTimer(this, runnable, ticks, 0).getTaskId(); // TODO or the other way around?
|
||||
public TaskId runRepeatingSync(Runnable runnable, Long ticks) {
|
||||
return new BukkitTaskId(getServer().getScheduler().runTaskTimer(this, runnable, ticks, 0).getTaskId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancelTask(int taskId) {
|
||||
getServer().getScheduler().cancelTask(taskId);
|
||||
public void cancelTask(TaskId taskId) {
|
||||
if (taskId == null) return;
|
||||
if (taskId.getObject() == null) return;
|
||||
if (taskId instanceof BukkitTaskId) {
|
||||
getServer().getScheduler().cancelTask((Integer) taskId.getObject());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -0,0 +1,11 @@
|
||||
package us.myles.ViaVersion.bukkit.platform;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import us.myles.ViaVersion.api.platform.TaskId;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public class BukkitTaskId implements TaskId {
|
||||
private Integer object;
|
||||
}
|
@ -10,14 +10,12 @@ import us.myles.ViaVersion.api.ViaAPI;
|
||||
import us.myles.ViaVersion.api.ViaVersionConfig;
|
||||
import us.myles.ViaVersion.api.command.ViaCommandSender;
|
||||
import us.myles.ViaVersion.api.configuration.ConfigurationProvider;
|
||||
import us.myles.ViaVersion.api.platform.TaskId;
|
||||
import us.myles.ViaVersion.api.platform.ViaPlatform;
|
||||
import us.myles.ViaVersion.bungee.platform.BungeeViaAPI;
|
||||
import us.myles.ViaVersion.bungee.platform.BungeeViaInjector;
|
||||
import us.myles.ViaVersion.bungee.platform.BungeeViaLoader;
|
||||
import us.myles.ViaVersion.bungee.platform.*;
|
||||
import us.myles.ViaVersion.bungee.commands.BungeeCommand;
|
||||
import us.myles.ViaVersion.bungee.commands.BungeeCommandHandler;
|
||||
import us.myles.ViaVersion.bungee.commands.BungeeCommandSender;
|
||||
import us.myles.ViaVersion.bungee.platform.BungeeConfigAPI;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@ -60,23 +58,27 @@ public class Bungee extends Plugin implements ViaPlatform {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int runAsync(Runnable runnable) {
|
||||
return getProxy().getScheduler().runAsync(this, runnable).getId();
|
||||
public TaskId runAsync(Runnable runnable) {
|
||||
return new BungeeTaskId(getProxy().getScheduler().runAsync(this, runnable).getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int runSync(Runnable runnable) {
|
||||
return getProxy().getScheduler().runAsync(this, runnable).getId(); // TODO don't run sync @ Bungee?
|
||||
public TaskId runSync(Runnable runnable) {
|
||||
return new BungeeTaskId(getProxy().getScheduler().runAsync(this, runnable).getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int runRepeatingSync(Runnable runnable, Long ticks) {
|
||||
return getProxy().getScheduler().schedule(this, runnable, 0, ticks * 50, TimeUnit.MILLISECONDS).getId();
|
||||
public TaskId runRepeatingSync(Runnable runnable, Long ticks) {
|
||||
return new BungeeTaskId(getProxy().getScheduler().schedule(this, runnable, 0, ticks * 50, TimeUnit.MILLISECONDS).getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancelTask(int taskId) {
|
||||
getProxy().getScheduler().cancel(taskId);
|
||||
public void cancelTask(TaskId taskId) {
|
||||
if (taskId == null) return;
|
||||
if (taskId.getObject() == null) return;
|
||||
if (taskId instanceof BungeeTaskId) {
|
||||
getProxy().getScheduler().cancel((Integer) taskId.getObject());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -125,7 +127,7 @@ public class Bungee extends Plugin implements ViaPlatform {
|
||||
|
||||
@Override
|
||||
public void onReload() {
|
||||
// TODO handle
|
||||
// Injector prints a message <3
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -0,0 +1,11 @@
|
||||
package us.myles.ViaVersion.bungee.platform;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import us.myles.ViaVersion.api.platform.TaskId;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public class BungeeTaskId implements TaskId {
|
||||
private Integer object;
|
||||
}
|
11
common/src/main/java/us/myles/ViaVersion/api/platform/TaskId.java
Normale Datei
11
common/src/main/java/us/myles/ViaVersion/api/platform/TaskId.java
Normale Datei
@ -0,0 +1,11 @@
|
||||
package us.myles.ViaVersion.api.platform;
|
||||
|
||||
public interface TaskId {
|
||||
/**
|
||||
* Returns the actual object represented by this TaskId
|
||||
* Null if task cannot be cancelled.
|
||||
*
|
||||
* @return Platform based Object (don't assume)
|
||||
*/
|
||||
Object getObject();
|
||||
}
|
@ -42,7 +42,7 @@ public interface ViaPlatform<T> {
|
||||
* @param runnable The task to run
|
||||
* @return The Task ID
|
||||
*/
|
||||
int runAsync(Runnable runnable);
|
||||
TaskId runAsync(Runnable runnable);
|
||||
|
||||
/**
|
||||
* Run a task Sync
|
||||
@ -50,7 +50,7 @@ public interface ViaPlatform<T> {
|
||||
* @param runnable The task to run
|
||||
* @return The Task ID
|
||||
*/
|
||||
int runSync(Runnable runnable);
|
||||
TaskId runSync(Runnable runnable);
|
||||
|
||||
/**
|
||||
* Run a task at a repeating interval.
|
||||
@ -60,14 +60,14 @@ public interface ViaPlatform<T> {
|
||||
* @param ticks The interval to run it at
|
||||
* @return The Task ID
|
||||
*/
|
||||
int runRepeatingSync(Runnable runnable, Long ticks);
|
||||
TaskId runRepeatingSync(Runnable runnable, Long ticks);
|
||||
|
||||
/**
|
||||
* Cancel a task
|
||||
*
|
||||
* @param taskId The task ID to cancel
|
||||
*/
|
||||
void cancelTask(int taskId);
|
||||
void cancelTask(TaskId taskId);
|
||||
|
||||
/**
|
||||
* Get the online players
|
||||
|
@ -16,15 +16,13 @@ import us.myles.ViaVersion.api.ViaAPI;
|
||||
import us.myles.ViaVersion.api.ViaVersionConfig;
|
||||
import us.myles.ViaVersion.api.command.ViaCommandSender;
|
||||
import us.myles.ViaVersion.api.configuration.ConfigurationProvider;
|
||||
import us.myles.ViaVersion.api.platform.TaskId;
|
||||
import us.myles.ViaVersion.api.platform.ViaPlatform;
|
||||
import us.myles.ViaVersion.dump.PluginInfo;
|
||||
import us.myles.ViaVersion.sponge.VersionInfo;
|
||||
import us.myles.ViaVersion.sponge.commands.SpongeCommandHandler;
|
||||
import us.myles.ViaVersion.sponge.commands.SpongeCommandSender;
|
||||
import us.myles.ViaVersion.sponge.platform.SpongeConfigAPI;
|
||||
import us.myles.ViaVersion.sponge.platform.SpongeViaAPI;
|
||||
import us.myles.ViaVersion.sponge.platform.SpongeViaInjector;
|
||||
import us.myles.ViaVersion.sponge.platform.SpongeViaLoader;
|
||||
import us.myles.ViaVersion.sponge.platform.*;
|
||||
import us.myles.ViaVersion.sponge.util.LoggerWrapper;
|
||||
import us.myles.ViaVersion.util.GsonUtil;
|
||||
|
||||
@ -98,28 +96,30 @@ public class SpongePlugin implements ViaPlatform {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int runAsync(Runnable runnable) {
|
||||
public TaskId runAsync(Runnable runnable) {
|
||||
asyncExecutor.execute(runnable);
|
||||
return -1;
|
||||
return new SpongeTaskId(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int runSync(Runnable runnable) {
|
||||
public TaskId runSync(Runnable runnable) {
|
||||
syncExecutor.execute(runnable);
|
||||
return -1;
|
||||
return new SpongeTaskId(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int runRepeatingSync(Runnable runnable, Long ticks) {
|
||||
public TaskId runRepeatingSync(Runnable runnable, Long ticks) {
|
||||
Long time = ticks * 50L;
|
||||
syncExecutor.scheduleAtFixedRate(runnable, time, time, TimeUnit.MILLISECONDS);
|
||||
// use id?
|
||||
return -1;
|
||||
return new SpongeTaskId(syncExecutor.scheduleAtFixedRate(runnable, time, time, TimeUnit.MILLISECONDS).getTask());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancelTask(int taskId) {
|
||||
// oh.
|
||||
public void cancelTask(TaskId taskId) {
|
||||
if (taskId == null) return;
|
||||
if (taskId.getObject() == null) return;
|
||||
if (taskId instanceof SpongeTaskId) {
|
||||
((SpongeTaskId) taskId).getObject().cancel();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -0,0 +1,12 @@
|
||||
package us.myles.ViaVersion.sponge.platform;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import org.spongepowered.api.scheduler.Task;
|
||||
import us.myles.ViaVersion.api.platform.TaskId;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public class SpongeTaskId implements TaskId {
|
||||
private Task object;
|
||||
}
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren