Skip to content

Commit

Permalink
Merge pull request #105 from Shynixn/development
Browse files Browse the repository at this point in the history
Merge changes to master --release
  • Loading branch information
Shynixn authored Aug 20, 2023
2 parents a33cfb8 + 3258233 commit c926231
Show file tree
Hide file tree
Showing 59 changed files with 2,988 additions and 490 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ the existing APIs with suspendable commands, events and schedules.

**Supported Game Servers:**

* Spigot
* Paper
* CraftBukkit
* SpongeVanilla v7.x.x
* SpongeForge v7.x.x
* Fabric
* Folia
* Minestom
* Paper
* Spigot
* SpongeVanilla v7.x.x
* SpongeForge v7.x.x

**Supported Proxies:**

Expand Down Expand Up @@ -67,10 +68,11 @@ private suspend fun bob() {
## Resources

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

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ tasks.register("printVersion") {

subprojects {
group 'com.github.shynixn.mccoroutine'
version '2.12.1'
version '2.13.0'

sourceCompatibility = 1.8

Expand Down
11 changes: 6 additions & 5 deletions docs/wiki/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@ the existing APIs with suspendable commands, events and schedules.

**Supported Game Servers:**

* Spigot
* Paper
* CraftBukkit
* SpongeVanilla
* SpongeForge
* Fabric
* Folia
* Minestom
* Paper
* Spigot
* SpongeVanilla
* SpongeForge

**Supported Proxies:**

* BungeeCord
* Waterfall
* Velocity
* Waterfall

## Features

Expand Down
4 changes: 3 additions & 1 deletion docs/wiki/docs/caching.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ In minecraft plugins, players can perform many actions in a short time period. I
every action in the database, creating a new database call for every single action may cause performance problems. Therefore, caches are often
implemented, which is a lot easier when using coroutines.

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

## Implementing a Cache (Bukkit)
## Implementing a Cache

When taking a look at the ``Database`` implementation below, we can observe quite a lot of redundant database
accesses when a player rejoins a server in a very short timeframe.
Expand Down
228 changes: 143 additions & 85 deletions docs/wiki/docs/commandexecutor.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,86 @@ plugins.
}
````

=== "Fabric"

Create a traditional command executor but extend from ``SuspendingCommand`` instead of ``SuspendingCommand``.

````kotlin
import com.github.shynixn.mccoroutine.fabric.SuspendingCommand
import com.mojang.brigadier.context.CommandContext
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.server.command.ServerCommandSource

class PlayerDataCommandExecutor : SuspendingCommand<ServerCommandSource> {
override suspend fun run(context: CommandContext<ServerCommandSource>): Int {
if (context.source.entity is PlayerEntity) {
val sender = context.source.entity as PlayerEntity
println("[PlayerDataCommandExecutor] Is starting on Thread:${Thread.currentThread().name}/${Thread.currentThread().id}")
}

return 1
}
}
````

=== "Folia"

Folia schedules threads on the region of the entity who executed this command. For the console (globalregion) and command blocks (region) this rule
applies as well. Other than that, usage is almost identical to Bukkit.

````kotlin
import com.github.shynixn.mccoroutine.folia.SuspendingCommandExecutor
import org.bukkit.command.Command
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player

class PlayerDataCommandExecutor(private val database: Database) : SuspendingCommandExecutor {
override suspend fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean {
if (sender !is Player) {
return false
}

if (args.size == 2 && args[0].equals("rename", true)) {
val name = args[1]
val playerData = database.getDataFromPlayer(sender)
playerData.name = name
database.saveData(sender, playerData)
return true
}

return false
}
}
````

=== "Minestom"

Create a traditional command and user ``server.launch`` or ``extension.launch`` in the addSyntax handler.

````kotlin
import com.github.shynixn.mccoroutine.minestom.launch
import net.minestom.server.MinecraftServer
import net.minestom.server.command.builder.Command
import net.minestom.server.command.builder.arguments.ArgumentType
import net.minestom.server.entity.Player

class PlayerDataCommandExecutor(private val server: MinecraftServer, private val database: Database) : Command("mycommand") {
init {
val nameArgument = ArgumentType.String("name")
addSyntax({ sender, context ->
server.launch {
if (sender is Player) {
val name : String = context.get(nameArgument)
val playerData = database.getDataFromPlayer(sender)
playerData.name = name
database.saveData(sender, playerData)
}
}
})
}
}
````

=== "Sponge"

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

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

=== "Minestom"

Create a traditional command and user ``server.launch`` or ``extension.launch`` in the addSyntax handler.

````kotlin
import com.github.shynixn.mccoroutine.minestom.launch
import net.minestom.server.MinecraftServer
import net.minestom.server.command.builder.Command
import net.minestom.server.command.builder.arguments.ArgumentType
import net.minestom.server.entity.Player

class PlayerDataCommandExecutor(private val server: MinecraftServer, private val database: Database) : Command("mycommand") {
init {
val nameArgument = ArgumentType.String("name")
addSyntax({ sender, context ->
server.launch {
if (sender is Player) {
val name : String = context.get(nameArgument)
val playerData = database.getDataFromPlayer(sender)
playerData.name = name
database.saveData(sender, playerData)
}
}
})
}
}
````

=== "Fabric"

Create a traditional command executor but extend from ``SuspendingCommand`` instead of ``SuspendingCommand``.

````kotlin
import com.github.shynixn.mccoroutine.fabric.SuspendingCommand
import com.mojang.brigadier.context.CommandContext
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.server.command.ServerCommandSource

class PlayerDataCommandExecutor : SuspendingCommand<ServerCommandSource> {
override suspend fun run(context: CommandContext<ServerCommandSource>): Int {
if (context.source.entity is PlayerEntity) {
val sender = context.source.entity as PlayerEntity
println("[PlayerDataCommandExecutor] Is starting on Thread:${Thread.currentThread().name}/${Thread.currentThread().id}")
}

return 1
}
}
````

## Register the CommandExecutor

=== "Bukkit"
Expand Down Expand Up @@ -235,6 +265,69 @@ plugins.
}
````

=== "Fabric"

````kotlin
class MCCoroutineSampleServerMod : DedicatedServerModInitializer {
override fun onInitializeServer() {
ServerLifecycleEvents.SERVER_STARTING.register(ServerLifecycleEvents.ServerStarting { server ->
// Connect Native Minecraft Scheduler and MCCoroutine.
mcCoroutineConfiguration.minecraftExecutor = Executor { r ->
server.submitAndJoin(r)
}
launch {
onServerStarting(server)
}
})

ServerLifecycleEvents.SERVER_STOPPING.register { server ->
mcCoroutineConfiguration.disposePluginSession()
}
}

/**
* MCCoroutine is ready after the server has started.
*/
private suspend fun onServerStarting(server : MinecraftServer) {
// Register command
val command = PlayerDataCommandExecutor()
server.commandManager.dispatcher.register(CommandManager.literal("mccor").executesSuspend(this, command))
}
}
````

=== "Folia"

Instead of using ``setExecutor``, use the provided extension method ``setSuspendingExecutor`` to register a command executor.

!!! note "Important"
Do not forget to declare the ``playerdata`` command in your plugin.yml.

````kotlin
import com.github.shynixn.mccoroutine.folia.SuspendingJavaPlugin
import com.github.shynixn.mccoroutine.folia.registerSuspendingEvents
import com.github.shynixn.mccoroutine.folia.setSuspendingExecutor

class MCCoroutineSamplePlugin : SuspendingJavaPlugin() {
private val database = Database()

override suspend fun onEnableAsync() {
// Minecraft Main Thread
database.createDbIfNotExist()
server.pluginManager.registerSuspendingEvents(PlayerDataListener(database), this)
getCommand("playerdata")!!.setSuspendingExecutor(PlayerDataCommandExecutor(database))
}

override suspend fun onDisableAsync() {
// Minecraft Main Thread
}
}
````

=== "Minestom"

Register the command in the same way as a traditional command.

=== "Sponge"

Instead of using ``executor``, use the provided extension method ``suspendingExecutor`` to register a command executor.
Expand Down Expand Up @@ -335,41 +428,6 @@ plugins.
}
````

=== "Minestom"

Register the command in the same way as a traditional command.

=== "Fabric"

````kotlin
class MCCoroutineSampleServerMod : DedicatedServerModInitializer {
override fun onInitializeServer() {
ServerLifecycleEvents.SERVER_STARTING.register(ServerLifecycleEvents.ServerStarting { server ->
// Connect Native Minecraft Scheduler and MCCoroutine.
mcCoroutineConfiguration.minecraftExecutor = Executor { r ->
server.submitAndJoin(r)
}
launch {
onServerStarting(server)
}
})

ServerLifecycleEvents.SERVER_STOPPING.register { server ->
mcCoroutineConfiguration.disposePluginSession()
}
}

/**
* MCCoroutine is ready after the server has started.
*/
private suspend fun onServerStarting(server : MinecraftServer) {
// Register command
val command = PlayerDataCommandExecutor()
server.commandManager.dispatcher.register(CommandManager.literal("mccor").executesSuspend(this, command))
}
}
````

## Test the CommandExecutor

Join your server and use the playerData command to observe ``getDataFromPlayer`` and ``saveData`` messages getting
Expand Down
Loading

0 comments on commit c926231

Please sign in to comment.