mirror of
https://github.com/smartcmd/MinecraftConsoles.git
synced 2026-05-16 01:03:51 +00:00
Merge branch 'feature/plugin-api' of github.com:sylvessa/MinecraftConsoles into feature/plugin-api
This commit is contained in:
commit
8e66b2c19e
13 changed files with 570 additions and 5 deletions
98
Minecraft.Server.FourKit/docs/scheduler-programming.md
Normal file
98
Minecraft.Server.FourKit/docs/scheduler-programming.md
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
@page scheduler-programming Scheduler Programming
|
||||
|
||||
@section introduction Introduction
|
||||
Usually when we code in FourKit, everything is ran linearly. Despite this you may wish to schedule some code to be executed at a later point of time.
|
||||
|
||||
Do NOT use the built in C# Thread system, it is not safe for FourKit.
|
||||
|
||||
@section getting_started Getting Started
|
||||
|
||||
To get started we either need the FourKitScheduler instance. You can get this from the server instance.
|
||||
```csharp
|
||||
FourKitScheduler scheduler = FourKit.getScheduler();
|
||||
```
|
||||
|
||||
When scheduling a task, you will also need to pass your main plugin class instance. Here is an example of how it can be done:
|
||||
|
||||
```csharp
|
||||
public class ExampleOne
|
||||
{
|
||||
// you can also use a getter
|
||||
public static ExampleOne Instance { get; private set; }
|
||||
|
||||
public void OnEnable()
|
||||
{
|
||||
Instance = this;
|
||||
}
|
||||
}
|
||||
// then
|
||||
public class Other
|
||||
{
|
||||
private readonly ExampleOne plugin = ExampleOne.Instance;
|
||||
}
|
||||
```
|
||||
|
||||
@section scheduling_delayed_task Scheduling a Delayed Task
|
||||
|
||||
The scheduling itself is based on ticks, the Minecraft time unit. 1 tick = 0.05 seconds or 1 second = 20 ticks.
|
||||
|
||||
Let's say you want to schedule a task to run 30 seconds later which broadcasts a message:
|
||||
|
||||
```csharp
|
||||
FourKitScheduler scheduler = FourKit.getScheduler();
|
||||
|
||||
scheduler.runTaskLater(plugin, () => {
|
||||
FourKit.broadcastMessage("Mooooo!");
|
||||
}, 20 * 30 /*<-- the delay */);
|
||||
```
|
||||
|
||||
@section scheduling_repeating_task Scheduling a Repeating Task
|
||||
|
||||
Repeating tasks are tasks that can reschedule themselves.
|
||||
|
||||
Let's say you want to schedule a task to run 10 seconds later then after that it should repeat itself a finite amount of times with an interval of 5 seconds between each consecutive run:
|
||||
|
||||
```csharp
|
||||
FourKitScheduler scheduler = FourKit.getScheduler();
|
||||
|
||||
scheduler.runTaskTimer(plugin, () => {
|
||||
FourKit.broadcastMessage("Mooooo!");
|
||||
}, 20 * 10 /*<-- the initial delay */, 20 * 5 /*<-- the interval */);
|
||||
```
|
||||
|
||||
@section run_task_next_tick Running a Task on the Next Tick
|
||||
|
||||
Sometimes we just want to run some code on the next tick:
|
||||
|
||||
```csharp
|
||||
FourKitScheduler scheduler = FourKit.getScheduler();
|
||||
|
||||
scheduler.runTask(plugin, () => {
|
||||
FourKit.broadcastMessage("Mooooo again!");
|
||||
});
|
||||
```
|
||||
|
||||
@section canceling_tasks Canceling Tasks
|
||||
|
||||
Sometimes we want to just cancel a task!
|
||||
|
||||
```csharp
|
||||
FourKitScheduler scheduler = FourKit.getScheduler();
|
||||
|
||||
// Cancel outside
|
||||
FourKitTask task = scheduler.runTaskLater(plugin, () => {
|
||||
FourKit.broadcastMessage("Mooooo again!");
|
||||
}, 20 * 60);
|
||||
|
||||
// Cancel inside
|
||||
scheduler.runTaskTimer(plugin, () => {
|
||||
if (FourKit.getOnlinePlayers().Count == 0) {
|
||||
task.cancel(); // <--
|
||||
return;
|
||||
}
|
||||
FourKit.broadcastMessage("Mooooo again!");
|
||||
}, 0, 20 * 60);
|
||||
|
||||
// then
|
||||
task.cancel(); // <--
|
||||
```
|
||||
Loading…
Add table
Add a link
Reference in a new issue