Skip to content

Commit c926231

Browse files
authored
Merge pull request #105 from Shynixn/development
Merge changes to master --release
2 parents a33cfb8 + 3258233 commit c926231

File tree

59 files changed

+2988
-490
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+2988
-490
lines changed

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ the existing APIs with suspendable commands, events and schedules.
1313

1414
**Supported Game Servers:**
1515

16-
* Spigot
17-
* Paper
1816
* CraftBukkit
19-
* SpongeVanilla v7.x.x
20-
* SpongeForge v7.x.x
2117
* Fabric
18+
* Folia
2219
* Minestom
20+
* Paper
21+
* Spigot
22+
* SpongeVanilla v7.x.x
23+
* SpongeForge v7.x.x
2324

2425
**Supported Proxies:**
2526

@@ -67,10 +68,11 @@ private suspend fun bob() {
6768
## Resources
6869

6970
* [MCCoroutine JavaDocs for the Bukkit-API](https://shynixn.github.io/MCCoroutine/apidocs/mccoroutine-root/com.github.shynixn.mccoroutine.bukkit/index.html)
70-
* [MCCoroutine JavaDocs for the Sponge-v7.x.x-API](https://shynixn.github.io/MCCoroutine/apidocs/mccoroutine-root/com.github.shynixn.mccoroutine.sponge/index.html)
71+
* [MCCoroutine JavaDocs for the BungeeCord-API](https://shynixn.github.io/MCCoroutine/apidocs/mccoroutine-root/com.github.shynixn.mccoroutine.bungeecord/index.html)
7172
* [MCCoroutine JavaDocs for the Fabric-API](https://shynixn.github.io/MCCoroutine/apidocs/mccoroutine-root/com.github.shynixn.mccoroutine.fabric/index.html)
73+
* [MCCoroutine JavaDocs for the Folia-API](https://shynixn.github.io/MCCoroutine/apidocs/mccoroutine-root/com.github.shynixn.mccoroutine.folia/index.html)
7274
* [MCCoroutine JavaDocs for the Minestom-API](https://shynixn.github.io/MCCoroutine/apidocs/mccoroutine-root/com.github.shynixn.mccoroutine.minestom/index.html)
73-
* [MCCoroutine JavaDocs for the BungeeCord-API](https://shynixn.github.io/MCCoroutine/apidocs/mccoroutine-root/com.github.shynixn.mccoroutine.bungeecord/index.html)
75+
* [MCCoroutine JavaDocs for the Sponge-v7.x.x-API](https://shynixn.github.io/MCCoroutine/apidocs/mccoroutine-root/com.github.shynixn.mccoroutine.sponge/index.html)
7476
* [MCCoroutine JavaDocs for the Velocity-API](https://shynixn.github.io/MCCoroutine/apidocs/mccoroutine-root/com.github.shynixn.mccoroutine.velocity/index.html)
7577
* [Article on custom frameworks](https://github.com/Shynixn/MCCoroutine/blob/master/ARTICLE.md)
7678

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ tasks.register("printVersion") {
4343

4444
subprojects {
4545
group 'com.github.shynixn.mccoroutine'
46-
version '2.12.1'
46+
version '2.13.0'
4747

4848
sourceCompatibility = 1.8
4949

docs/wiki/docs/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,20 @@ the existing APIs with suspendable commands, events and schedules.
1717

1818
**Supported Game Servers:**
1919

20-
* Spigot
21-
* Paper
2220
* CraftBukkit
23-
* SpongeVanilla
24-
* SpongeForge
2521
* Fabric
22+
* Folia
2623
* Minestom
24+
* Paper
25+
* Spigot
26+
* SpongeVanilla
27+
* SpongeForge
2728

2829
**Supported Proxies:**
2930

3031
* BungeeCord
31-
* Waterfall
3232
* Velocity
33+
* Waterfall
3334

3435
## Features
3536

docs/wiki/docs/caching.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ In minecraft plugins, players can perform many actions in a short time period. I
66
every action in the database, creating a new database call for every single action may cause performance problems. Therefore, caches are often
77
implemented, which is a lot easier when using coroutines.
88

9+
!!! note "Important"
10+
The following code examples are for Bukkit, but work in a similar way in other mccoroutine implementations.
911

10-
## Implementing a Cache (Bukkit)
12+
## Implementing a Cache
1113

1214
When taking a look at the ``Database`` implementation below, we can observe quite a lot of redundant database
1315
accesses when a player rejoins a server in a very short timeframe.

docs/wiki/docs/commandexecutor.md

Lines changed: 143 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,86 @@ plugins.
6161
}
6262
````
6363

64+
=== "Fabric"
65+
66+
Create a traditional command executor but extend from ``SuspendingCommand`` instead of ``SuspendingCommand``.
67+
68+
````kotlin
69+
import com.github.shynixn.mccoroutine.fabric.SuspendingCommand
70+
import com.mojang.brigadier.context.CommandContext
71+
import net.minecraft.entity.player.PlayerEntity
72+
import net.minecraft.server.command.ServerCommandSource
73+
74+
class PlayerDataCommandExecutor : SuspendingCommand<ServerCommandSource> {
75+
override suspend fun run(context: CommandContext<ServerCommandSource>): Int {
76+
if (context.source.entity is PlayerEntity) {
77+
val sender = context.source.entity as PlayerEntity
78+
println("[PlayerDataCommandExecutor] Is starting on Thread:${Thread.currentThread().name}/${Thread.currentThread().id}")
79+
}
80+
81+
return 1
82+
}
83+
}
84+
````
85+
86+
=== "Folia"
87+
88+
Folia schedules threads on the region of the entity who executed this command. For the console (globalregion) and command blocks (region) this rule
89+
applies as well. Other than that, usage is almost identical to Bukkit.
90+
91+
````kotlin
92+
import com.github.shynixn.mccoroutine.folia.SuspendingCommandExecutor
93+
import org.bukkit.command.Command
94+
import org.bukkit.command.CommandSender
95+
import org.bukkit.entity.Player
96+
97+
class PlayerDataCommandExecutor(private val database: Database) : SuspendingCommandExecutor {
98+
override suspend fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean {
99+
if (sender !is Player) {
100+
return false
101+
}
102+
103+
if (args.size == 2 && args[0].equals("rename", true)) {
104+
val name = args[1]
105+
val playerData = database.getDataFromPlayer(sender)
106+
playerData.name = name
107+
database.saveData(sender, playerData)
108+
return true
109+
}
110+
111+
return false
112+
}
113+
}
114+
````
115+
116+
=== "Minestom"
117+
118+
Create a traditional command and user ``server.launch`` or ``extension.launch`` in the addSyntax handler.
119+
120+
````kotlin
121+
import com.github.shynixn.mccoroutine.minestom.launch
122+
import net.minestom.server.MinecraftServer
123+
import net.minestom.server.command.builder.Command
124+
import net.minestom.server.command.builder.arguments.ArgumentType
125+
import net.minestom.server.entity.Player
126+
127+
class PlayerDataCommandExecutor(private val server: MinecraftServer, private val database: Database) : Command("mycommand") {
128+
init {
129+
val nameArgument = ArgumentType.String("name")
130+
addSyntax({ sender, context ->
131+
server.launch {
132+
if (sender is Player) {
133+
val name : String = context.get(nameArgument)
134+
val playerData = database.getDataFromPlayer(sender)
135+
playerData.name = name
136+
database.saveData(sender, playerData)
137+
}
138+
}
139+
})
140+
}
141+
}
142+
````
143+
64144
=== "Sponge"
65145

66146
Create a traditional command executor but extend from ``SuspendingCommandExecutor`` instead of ``CommandExecutor``. Please
@@ -127,56 +207,6 @@ plugins.
127207

128208
A ``BrigadierCommand`` can be executed asynchronously using the ``executesSuspend`` extension function. More details below.
129209

130-
=== "Minestom"
131-
132-
Create a traditional command and user ``server.launch`` or ``extension.launch`` in the addSyntax handler.
133-
134-
````kotlin
135-
import com.github.shynixn.mccoroutine.minestom.launch
136-
import net.minestom.server.MinecraftServer
137-
import net.minestom.server.command.builder.Command
138-
import net.minestom.server.command.builder.arguments.ArgumentType
139-
import net.minestom.server.entity.Player
140-
141-
class PlayerDataCommandExecutor(private val server: MinecraftServer, private val database: Database) : Command("mycommand") {
142-
init {
143-
val nameArgument = ArgumentType.String("name")
144-
addSyntax({ sender, context ->
145-
server.launch {
146-
if (sender is Player) {
147-
val name : String = context.get(nameArgument)
148-
val playerData = database.getDataFromPlayer(sender)
149-
playerData.name = name
150-
database.saveData(sender, playerData)
151-
}
152-
}
153-
})
154-
}
155-
}
156-
````
157-
158-
=== "Fabric"
159-
160-
Create a traditional command executor but extend from ``SuspendingCommand`` instead of ``SuspendingCommand``.
161-
162-
````kotlin
163-
import com.github.shynixn.mccoroutine.fabric.SuspendingCommand
164-
import com.mojang.brigadier.context.CommandContext
165-
import net.minecraft.entity.player.PlayerEntity
166-
import net.minecraft.server.command.ServerCommandSource
167-
168-
class PlayerDataCommandExecutor : SuspendingCommand<ServerCommandSource> {
169-
override suspend fun run(context: CommandContext<ServerCommandSource>): Int {
170-
if (context.source.entity is PlayerEntity) {
171-
val sender = context.source.entity as PlayerEntity
172-
println("[PlayerDataCommandExecutor] Is starting on Thread:${Thread.currentThread().name}/${Thread.currentThread().id}")
173-
}
174-
175-
return 1
176-
}
177-
}
178-
````
179-
180210
## Register the CommandExecutor
181211

182212
=== "Bukkit"
@@ -235,6 +265,69 @@ plugins.
235265
}
236266
````
237267

268+
=== "Fabric"
269+
270+
````kotlin
271+
class MCCoroutineSampleServerMod : DedicatedServerModInitializer {
272+
override fun onInitializeServer() {
273+
ServerLifecycleEvents.SERVER_STARTING.register(ServerLifecycleEvents.ServerStarting { server ->
274+
// Connect Native Minecraft Scheduler and MCCoroutine.
275+
mcCoroutineConfiguration.minecraftExecutor = Executor { r ->
276+
server.submitAndJoin(r)
277+
}
278+
launch {
279+
onServerStarting(server)
280+
}
281+
})
282+
283+
ServerLifecycleEvents.SERVER_STOPPING.register { server ->
284+
mcCoroutineConfiguration.disposePluginSession()
285+
}
286+
}
287+
288+
/**
289+
* MCCoroutine is ready after the server has started.
290+
*/
291+
private suspend fun onServerStarting(server : MinecraftServer) {
292+
// Register command
293+
val command = PlayerDataCommandExecutor()
294+
server.commandManager.dispatcher.register(CommandManager.literal("mccor").executesSuspend(this, command))
295+
}
296+
}
297+
````
298+
299+
=== "Folia"
300+
301+
Instead of using ``setExecutor``, use the provided extension method ``setSuspendingExecutor`` to register a command executor.
302+
303+
!!! note "Important"
304+
Do not forget to declare the ``playerdata`` command in your plugin.yml.
305+
306+
````kotlin
307+
import com.github.shynixn.mccoroutine.folia.SuspendingJavaPlugin
308+
import com.github.shynixn.mccoroutine.folia.registerSuspendingEvents
309+
import com.github.shynixn.mccoroutine.folia.setSuspendingExecutor
310+
311+
class MCCoroutineSamplePlugin : SuspendingJavaPlugin() {
312+
private val database = Database()
313+
314+
override suspend fun onEnableAsync() {
315+
// Minecraft Main Thread
316+
database.createDbIfNotExist()
317+
server.pluginManager.registerSuspendingEvents(PlayerDataListener(database), this)
318+
getCommand("playerdata")!!.setSuspendingExecutor(PlayerDataCommandExecutor(database))
319+
}
320+
321+
override suspend fun onDisableAsync() {
322+
// Minecraft Main Thread
323+
}
324+
}
325+
````
326+
327+
=== "Minestom"
328+
329+
Register the command in the same way as a traditional command.
330+
238331
=== "Sponge"
239332

240333
Instead of using ``executor``, use the provided extension method ``suspendingExecutor`` to register a command executor.
@@ -335,41 +428,6 @@ plugins.
335428
}
336429
````
337430

338-
=== "Minestom"
339-
340-
Register the command in the same way as a traditional command.
341-
342-
=== "Fabric"
343-
344-
````kotlin
345-
class MCCoroutineSampleServerMod : DedicatedServerModInitializer {
346-
override fun onInitializeServer() {
347-
ServerLifecycleEvents.SERVER_STARTING.register(ServerLifecycleEvents.ServerStarting { server ->
348-
// Connect Native Minecraft Scheduler and MCCoroutine.
349-
mcCoroutineConfiguration.minecraftExecutor = Executor { r ->
350-
server.submitAndJoin(r)
351-
}
352-
launch {
353-
onServerStarting(server)
354-
}
355-
})
356-
357-
ServerLifecycleEvents.SERVER_STOPPING.register { server ->
358-
mcCoroutineConfiguration.disposePluginSession()
359-
}
360-
}
361-
362-
/**
363-
* MCCoroutine is ready after the server has started.
364-
*/
365-
private suspend fun onServerStarting(server : MinecraftServer) {
366-
// Register command
367-
val command = PlayerDataCommandExecutor()
368-
server.commandManager.dispatcher.register(CommandManager.literal("mccor").executesSuspend(this, command))
369-
}
370-
}
371-
````
372-
373431
## Test the CommandExecutor
374432

375433
Join your server and use the playerData command to observe ``getDataFromPlayer`` and ``saveData`` messages getting

0 commit comments

Comments
 (0)