Skip to content

Commit

Permalink
Merge pull request #9 from Shynixn/development
Browse files Browse the repository at this point in the history
Merge changes to master. --release
  • Loading branch information
Shynixn authored Oct 12, 2020
2 parents f837fa7 + fab54eb commit 61d8e75
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 50 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ Include the dependency to MCCoroutine
<dependency>
<groupId>com.github.shynixn.mccoroutine</groupId>
<artifactId>mccoroutine-bukkit-api</artifactId>
<version>0.0.3</version>
<version>0.0.4</version>
<scope>provided</scope>
</dependency>
```
**Gradle**

```xml
dependencies {
compileOnly("com.github.shynixn.mccoroutine:mccoroutine-bukkit-api:0.0.3")
compileOnly("com.github.shynixn.mccoroutine:mccoroutine-bukkit-api:0.0.4")
}
```

Expand Down Expand Up @@ -488,7 +488,7 @@ class PlaceHolderApiConnector(private val cache : UserDataCache) {
<dependency>
<groupId>com.github.shynixn.mccoroutine</groupId>
<artifactId>mccoroutine-bukkit-core</artifactId>
<version>0.0.3</version>
<version>0.0.4</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand All @@ -514,7 +514,7 @@ class PlaceHolderApiConnector(private val cache : UserDataCache) {

```xml
dependencies {
implementation("com.github.shynixn.mccoroutine:mccoroutine-bukkit-core:0.0.3")
implementation("com.github.shynixn.mccoroutine:mccoroutine-bukkit-core:0.0.4")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.x.x")
implementation("org.jetbrains.kotlin:kotlin-reflect:1.x.x")
compileOnly("io.netty:netty-all:4.1.52.Final")
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ allprojects {

subprojects {
group 'com.github.shynixn.mccoroutine'
version '0.0.3'
version '0.0.4'

sourceCompatibility = 1.8

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.github.shynixn.mccoroutine.service
import com.github.shynixn.mccoroutine.SuspendingCommandExecutor
import com.github.shynixn.mccoroutine.contract.CommandService
import com.github.shynixn.mccoroutine.contract.CoroutineSession
import com.github.shynixn.mccoroutine.minecraftDispatcher
import kotlinx.coroutines.Dispatchers
import org.bukkit.command.PluginCommand
import org.bukkit.plugin.Plugin
Expand All @@ -20,7 +21,7 @@ internal class CommandServiceImpl(private val plugin: Plugin, private val corout
// If the result is delayed we can automatically assume it is true.
var success = true

coroutineSession.launch(Dispatchers.Unconfined) {
coroutineSession.launch(plugin.minecraftDispatcher) {
success = commandExecutor.onCommand(p0, p1, p2, p3)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ package com.github.shynixn.mccoroutine.service
import com.github.shynixn.mccoroutine.contract.EventService
import com.github.shynixn.mccoroutine.extension.invokeSuspend
import com.github.shynixn.mccoroutine.launch
import com.github.shynixn.mccoroutine.minecraftDispatcher
import kotlinx.coroutines.Dispatchers
import org.bukkit.Warning
import org.bukkit.event.*
import org.bukkit.plugin.*
import org.bukkit.plugin.java.JavaPluginLoader
import org.spigotmc.CustomTimingsHandler
import java.lang.Deprecated
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method
import java.util.*
import java.util.logging.Level
import kotlin.Any
import kotlin.Exception
import kotlin.IllegalArgumentException
import kotlin.Int
import kotlin.String
Expand Down Expand Up @@ -129,12 +130,7 @@ internal class EventServiceImpl(private val plugin: Plugin) :
clazz = clazz.superclass
}

val timings = CustomTimingsHandler(
"Plugin: " + plugin.description.fullName + " Event: " + listener.javaClass.name + "::" + method.name + "(" + eventClass.simpleName + ")",
JavaPluginLoader.pluginParentTimer
)

val executor = createEventExecutor(plugin, eventClass, method, timings)
val executor = createEventExecutor(plugin, eventClass, method)
eventSet!!.add(
RegisteredListener(
listener,
Expand All @@ -154,20 +150,21 @@ internal class EventServiceImpl(private val plugin: Plugin) :
private fun createEventExecutor(
plugin: Plugin,
eventClass: Class<*>,
method: Method,
timings: CustomTimingsHandler
method: Method
): EventExecutor {
return EventExecutor { listener, event ->
try {
if (eventClass.isAssignableFrom(event.javaClass)) {
val isAsync = event.isAsynchronous

if (!isAsync) {
timings.startTiming()
val dispatcher = if (isAsync) {
// Unconfined because async events should be supported too.
Dispatchers.Unconfined
} else {
plugin.minecraftDispatcher
}

// Unconfined because async events should be supported too.
plugin.launch(Dispatchers.Unconfined) {
plugin.launch(dispatcher) {
try {
// Try as suspension function.
method.invokeSuspend(listener, event)
Expand All @@ -176,10 +173,6 @@ internal class EventServiceImpl(private val plugin: Plugin) :
method.invoke(listener, event)
}
}

if (!isAsync) {
timings.stopTiming()
}
}
} catch (var4: InvocationTargetException) {
throw EventException(var4.cause)
Expand Down
8 changes: 5 additions & 3 deletions mccoroutine-bukkit-sample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ dependencies {
implementation(project(":mccoroutine-bukkit-api"))
implementation(project(":mccoroutine-bukkit-core"))

compileOnly("io.netty:netty-all:4.1.52.Final")
compileOnly("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9")
compileOnly("org.jetbrains.kotlin:kotlin-reflect:1.3.72")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9")
implementation("org.jetbrains.kotlin:kotlin-reflect:1.3.72")

compileOnly("org.spigotmc:spigot-api:1.16.3-R0.1-SNAPSHOT")
compileOnly("io.netty:netty-all:4.1.52.Final")
testCompile("org.spigotmc:spigot-api:1.16.3-R0.1-SNAPSHOT")
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ class AdminCommandExecutor(private val userDataCache: UserDataCache) : Suspendin
val playerKills = args[2].toInt()
val otherPlayer = Bukkit.getPlayer(playerName)!!

println("[AdminCommandExecutor] Is starting on Primary Thread: " + Bukkit.isPrimaryThread())
val userData = userDataCache.getUserDataFromPlayer(otherPlayer)
userData.amountOfPlayerKills = playerKills
userDataCache.saveUserData(otherPlayer)
println("[AdminCommandExecutor] Is ending on Primary Thread: " + Bukkit.isPrimaryThread())
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.github.shynixn.mccoroutine.sample.impl
import com.github.shynixn.mccoroutine.asyncDispatcher
import com.github.shynixn.mccoroutine.sample.entity.UserData
import kotlinx.coroutines.*
import org.bukkit.Bukkit
import org.bukkit.entity.Player
import org.bukkit.plugin.Plugin

Expand Down Expand Up @@ -33,9 +34,11 @@ class UserDataCache(private val plugin: Plugin, private val fakeDatabase: FakeDa
return coroutineScope {
if (!cache.containsKey(player)) {
cache[player] = async(plugin.asyncDispatcher) {
println("[Cache] is downloading async: " + !Bukkit.isPrimaryThread())
fakeDatabase.getUserDataFromPlayer(player)
}
}
println("[Cache] is downloading waiting on Primary Thread: " + Bukkit.isPrimaryThread())
cache[player]!!.await()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.github.shynixn.mccoroutine.sample.impl.UserDataCache
import com.github.shynixn.mccoroutine.sample.packet.MyPacketPlayInPositionLooks
import com.github.shynixn.mccoroutine.sample.packet.MySupportedPacketType
import kotlinx.coroutines.withContext
import org.bukkit.Bukkit
import org.bukkit.Material
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
Expand All @@ -20,42 +21,26 @@ class PlayerConnectListener(private val plugin: Plugin, private val userDataCach
*/
@EventHandler
suspend fun onPlayerJoinEvent(playerJoinEvent: PlayerJoinEvent) {
println("[PlayerConnectListener-Join] Is starting on Primary Thread: " + Bukkit.isPrimaryThread())
val userData = userDataCache.getUserDataFromPlayer(playerJoinEvent.player)
println("[PlayerConnectListener] " + playerJoinEvent.player.name + " joined the server. KillCount [${userData.amountOfPlayerKills}].")
println("[PlayerConnectListener-Join] " + playerJoinEvent.player.name + " joined the server. KillCount [${userData.amountOfPlayerKills}].")
println("[PlayerConnectListener-Join] Is ending on Primary Thread: " + Bukkit.isPrimaryThread())
}

/**
* Gets called on player quit event.
*/
@EventHandler
suspend fun onPlayerQuitEvent(playerQuitEvent: PlayerQuitEvent) {
val apple = withContext(plugin.asyncDispatcher) {
Thread.sleep(500)
ItemStack(Material.APPLE)
}

userDataCache.clearCache(playerQuitEvent.player)
println("[PlayerConnectListener] " + playerQuitEvent.player.name + " left the server. Don't forget your " + apple + ".")
}

/**
* Gets called on player packet event.
*/
@EventHandler
suspend fun onPlayerPacketEvent(playerPacketEvent: PlayerPacketEvent) {
val packetType = MySupportedPacketType.findSupportedPacketType(playerPacketEvent.packet.javaClass.simpleName)

if (packetType != MySupportedPacketType.PACKETPLAYINPOSITIONLOOK) {
return
}

val myPacket = MyPacketPlayInPositionLooks(playerPacketEvent.byteData)
println("[PlayerConnectListener-Quit] Is starting on Primary Thread: " + Bukkit.isPrimaryThread())

val apple = withContext(plugin.asyncDispatcher) {
Thread.sleep(500)
ItemStack(Material.APPLE)
}

println("Player: " + playerPacketEvent.player + " received packet: " + myPacket + ". Don't forget your " + apple + ".")
userDataCache.clearCache(playerQuitEvent.player)
println("[PlayerConnectListener-Quit] " + playerQuitEvent.player.name + " left the server. Don't forget your " + apple + ".")
println("[PlayerConnectListener-Quit] Is ending on Primary Thread: " + Bukkit.isPrimaryThread())
}
}
2 changes: 1 addition & 1 deletion mccoroutine-bukkit-sample/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: MCCoroutine-Sample
version: 0.0.3
version: 0.0.4
author: Shynixn
main: com.github.shynixn.mccoroutine.sample.MCCoroutineSamplePlugin
commands:
Expand Down

0 comments on commit 61d8e75

Please sign in to comment.