Skip to content

Commit da8aa6b

Browse files
committed
Avoid scheduling new coroutines when not necessary
1 parent e702774 commit da8aa6b

File tree

13 files changed

+61
-36
lines changed

13 files changed

+61
-36
lines changed

src/main/kotlin/network/warzone/mars/Mars.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import org.bukkit.plugin.java.JavaPlugin
3333
import tc.oc.pgm.api.PGM
3434
import tc.oc.pgm.tablist.MatchTabManager
3535
import java.util.*
36+
import java.util.concurrent.CompletableFuture
3637

3738
class Mars : JavaPlugin() {
3839
companion object {
@@ -44,6 +45,17 @@ class Mars : JavaPlugin() {
4445
}
4546
}
4647

48+
fun asyncAsFuture(block: suspend() -> Unit) : CompletableFuture<Void?> {
49+
val future = CompletableFuture<Void?>()
50+
Bukkit.getScheduler().runTaskAsynchronously(get()) {
51+
runBlocking {
52+
block.invoke()
53+
future.complete(null)
54+
}
55+
}
56+
return future
57+
}
58+
4759
fun sync(block: () -> Unit) = Bukkit.getScheduler().runTask(get(), block) // Run next tick
4860

4961
fun get() = instance

src/main/kotlin/network/warzone/mars/broadcast/BroadcastFeature.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ object BroadcastFeature : Feature<Broadcast>() {
2525
return broadcasts.find { it.name.equals(target, ignoreCase = true) }
2626
}
2727

28+
override fun fetchCached(target: String): Broadcast? {
29+
return broadcasts.find { it.name.equals(target, ignoreCase = true) }
30+
}
31+
2832
override suspend fun init() {
2933
index = 0
3034
broadcasts = BroadcastService.getBroadcasts()

src/main/kotlin/network/warzone/mars/commands/providers/BroadcastProvider.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import app.ashcon.intake.argument.ArgumentParseException
44
import app.ashcon.intake.argument.CommandArgs
55
import app.ashcon.intake.argument.Namespace
66
import app.ashcon.intake.bukkit.parametric.provider.BukkitProvider
7-
import kotlinx.coroutines.runBlocking
87
import network.warzone.mars.broadcast.Broadcast
98
import network.warzone.mars.broadcast.BroadcastFeature
109
import network.warzone.mars.broadcast.exceptions.BroadcastMissingException
@@ -15,13 +14,13 @@ class BroadcastProvider : BukkitProvider<Broadcast> {
1514
return "broadcast"
1615
}
1716

18-
override fun get(sender: CommandSender?, args: CommandArgs, annotations: MutableList<out Annotation>?): Broadcast = runBlocking {
17+
override fun get(sender: CommandSender?, args: CommandArgs, annotations: MutableList<out Annotation>?): Broadcast {
1918
val broadcastName = args.next()
2019

21-
val broadcast: Broadcast? = BroadcastFeature.fetch(broadcastName)
20+
val broadcast: Broadcast? = BroadcastFeature.fetchCached(broadcastName)
2221
broadcast ?: throw ArgumentParseException(BroadcastMissingException(broadcastName).asTextComponent().content())
2322

24-
return@runBlocking broadcast
23+
return broadcast
2524
}
2625

2726
override fun getSuggestions(

src/main/kotlin/network/warzone/mars/commands/providers/PlayerProfileProvider.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import app.ashcon.intake.argument.ArgumentParseException
44
import app.ashcon.intake.argument.CommandArgs
55
import app.ashcon.intake.argument.Namespace
66
import app.ashcon.intake.bukkit.parametric.provider.BukkitProvider
7-
import kotlinx.coroutines.runBlocking
87
import network.warzone.mars.player.feature.PlayerFeature
98
import network.warzone.mars.player.feature.exceptions.PlayerNotOnlineException
109
import network.warzone.mars.player.models.PlayerProfile
@@ -20,13 +19,13 @@ class PlayerProfileProvider : BukkitProvider<PlayerProfile> {
2019
sender: CommandSender?,
2120
args: CommandArgs,
2221
annotations: MutableList<out Annotation>?
23-
): PlayerProfile = runBlocking {
22+
): PlayerProfile {
2423
val name = args.next()
2524

2625
val profile = PlayerFeature.getCached(name) // Only online players
2726
profile ?: throw ArgumentParseException(PlayerNotOnlineException(name).asTextComponent().content())
2827

29-
return@runBlocking profile
28+
return profile
3029
}
3130

3231
override fun getSuggestions(

src/main/kotlin/network/warzone/mars/feature/Feature.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ abstract class Feature<T : Resource> {
88
*/
99
abstract suspend fun fetch(target: String): T?
1010

11+
/**
12+
* Query cache exclusively
13+
*/
14+
open fun fetchCached(target: String): T? { return null }
15+
1116
/**
1217
* Get resource from cache first, and API if not in cache.
1318
* Defaults to API fetch if feature has no cache.

src/main/kotlin/network/warzone/mars/match/tracker/PlayerTracker.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package network.warzone.mars.match.tracker
22

3-
import kotlinx.coroutines.runBlocking
43
import net.kyori.adventure.text.Component
54
import net.kyori.adventure.text.Component.text
65
import net.kyori.adventure.text.format.NamedTextColor
@@ -100,12 +99,12 @@ class PlayerTracker : Listener {
10099

101100
// todo: set XP bar progress & level (and ensure compatibility with vanilla exp)
102101
@EventHandler
103-
fun onPlayerXPGain(event: PlayerXPGainEvent) = runBlocking {
102+
fun onPlayerXPGain(event: PlayerXPGainEvent) {
104103
val (id, gain, reason, notify, multiplier) = event.data
105104

106-
val context = PlayerManager.getPlayer(id) ?: return@runBlocking
105+
val context = PlayerManager.getPlayer(id) ?: return
107106
val player = context.player
108-
val profile = context.getPlayerProfile()
107+
val profile = context.getPlayerProfileCached() ?: return
109108

110109
val currentLevel = profile.stats.level
111110

@@ -126,7 +125,7 @@ class PlayerTracker : Listener {
126125
}
127126

128127
@EventHandler
129-
fun onPlayerLevelUp(event: PlayerLevelUpEvent) = runBlocking {
128+
fun onPlayerLevelUp(event: PlayerLevelUpEvent) {
130129
val (player, level) = event.data
131130

132131
player.playSound(player.location, Sound.LEVEL_UP, 1000f, 1f)

src/main/kotlin/network/warzone/mars/player/PlayerContext.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,8 @@ class PlayerContext(val uuid: UUID, val player: Player, val activeSession: Sessi
3434
// The player is online so we know they exist.
3535
return PlayerFeature.getKnown(uuid)
3636
}
37+
38+
fun getPlayerProfileCached(): PlayerProfile? {
39+
return PlayerFeature.getCached(uuid)
40+
}
3741
}

src/main/kotlin/network/warzone/mars/player/achievements/gui/AchievementMenu.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class AchievementMenu(player: Player) : Listener {
154154
}
155155

156156
// Navigate the player up the menu hierarchy.
157-
private fun createBackArrow(action: suspend InventoryClickEvent.(Player) -> Unit): GUI.Slot.() -> Unit {
157+
private fun createBackArrow(action: InventoryClickEvent.(Player) -> Unit): GUI.Slot.() -> Unit {
158158
return {
159159
item = item(Material.ARROW) {
160160
name = "${ChatColor.GRAY}Back"
@@ -164,7 +164,7 @@ class AchievementMenu(player: Player) : Listener {
164164
}
165165

166166
// Navigate the player to the next page of a paginated GUI.
167-
private fun createNextPageArrow(currentPage: Int, action: suspend InventoryClickEvent.(Player) -> Unit): GUI.Slot.() -> Unit {
167+
private fun createNextPageArrow(currentPage: Int, action: InventoryClickEvent.(Player) -> Unit): GUI.Slot.() -> Unit {
168168
return {
169169
item = item(Material.ARROW) {
170170
name = "${ChatColor.GRAY}Next Page"
@@ -175,7 +175,7 @@ class AchievementMenu(player: Player) : Listener {
175175
}
176176

177177
// Navigate the player to the previous page of a paginated GUI.
178-
private fun createPreviousPageArrow(currentPage: Int, action: suspend InventoryClickEvent.(Player) -> Unit): GUI.Slot.() -> Unit {
178+
private fun createPreviousPageArrow(currentPage: Int, action: InventoryClickEvent.(Player) -> Unit): GUI.Slot.() -> Unit {
179179
return {
180180
item = item(Material.ARROW) {
181181
name = "${ChatColor.GRAY}Previous Page"

src/main/kotlin/network/warzone/mars/player/feature/PlayerFeature.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ object PlayerFeature : NamedCachedFeature<PlayerProfile>(), Listener {
166166
}
167167

168168
@EventHandler
169-
fun onPlayerLogin(event: PlayerLoginEvent) = runBlocking {
169+
fun onPlayerLogin(event: PlayerLoginEvent) {
170170
val player = event.player
171171

172172
val (_, profile, activePuns, activeSession) = queuedJoins[player.uniqueId]
@@ -222,7 +222,7 @@ object PlayerFeature : NamedCachedFeature<PlayerProfile>(), Listener {
222222
}
223223

224224
@EventHandler
225-
fun onPlayerLogout(event: PlayerQuitEvent) = runBlocking {
225+
fun onPlayerLogout(event: PlayerQuitEvent) {
226226
Mars.async {
227227
val player = event.player
228228
val uuid = player.uniqueId

src/main/kotlin/network/warzone/mars/player/level/LevelDisplayListener.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package network.warzone.mars.player.level
22

3-
import kotlinx.coroutines.runBlocking
43
import network.warzone.mars.Mars
54
import network.warzone.mars.match.tracker.PlayerXPGainEvent
65
import network.warzone.mars.player.PlayerManager
@@ -67,9 +66,9 @@ class LevelDisplayListener : Listener {
6766
}
6867

6968
// Mars stat system level
70-
private fun showStatsLevel(player: Player) = runBlocking {
69+
private fun showStatsLevel(player: Player) {
7170
val context = PlayerManager.getPlayer(player.uniqueId)
72-
val profile = context?.getPlayerProfile()
71+
val profile = context?.getPlayerProfileCached()
7372
val stats = profile?.stats
7473
player.level = stats?.level ?: 1
7574
player.exp = (stats?.xp?.rem(5000F))?.div(5000F) ?: 0F

src/main/kotlin/network/warzone/mars/report/ReportFeature.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package network.warzone.mars.report
22

3-
import kotlinx.coroutines.runBlocking
43
import network.warzone.mars.Mars
54
import network.warzone.mars.api.socket.models.SimplePlayer
65
import network.warzone.mars.feature.Feature

src/main/kotlin/network/warzone/mars/tag/commands/TagsCommand.kt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,20 @@ class TagsCommand {
5454
// todo: send request on GUI close so player can't spam API reqs by clicking
5555
onclick = {
5656
if (isActive) {
57-
PlayerFeature.setActiveTag(context.uuid.toString(), null)
58-
profile.activeTagId = null
57+
Mars.asyncAsFuture {
58+
PlayerFeature.setActiveTag(context.uuid.toString(), null)
59+
}.thenRun {
60+
profile.activeTagId = null
61+
}
5962
} else if (profile.tagIds.contains(tag._id)) {
60-
PlayerFeature.setActiveTag(
61-
context.uuid.toString(),
62-
tag
63-
)
64-
profile.activeTagId = tag._id
63+
Mars.asyncAsFuture {
64+
PlayerFeature.setActiveTag(
65+
context.uuid.toString(),
66+
tag
67+
)
68+
}.thenRun {
69+
profile.activeTagId = tag._id
70+
}
6571
}
6672

6773
refresh()

src/main/kotlin/network/warzone/mars/utils/menu/GUI.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package network.warzone.mars.utils.menu
22

3-
import kotlinx.coroutines.runBlocking
43
import network.warzone.mars.Mars
54
import org.bukkit.Bukkit
65
import org.bukkit.Sound
@@ -49,13 +48,13 @@ class GUI(
4948
}
5049

5150
@EventHandler(priority = EventPriority.LOWEST)
52-
fun onClick(e: InventoryClickEvent) = runBlocking {
53-
if (e.inventory != inventory) return@runBlocking
51+
fun onClick(e: InventoryClickEvent) {
52+
if (e.inventory != inventory) return
5453
e.isCancelled = true
5554

56-
if (e.clickedInventory != inventory) return@runBlocking
57-
val player = e.whoClicked as? Player ?: return@runBlocking
58-
val slot = slots.getOrNull(e.slot) ?: return@runBlocking
55+
if (e.clickedInventory != inventory) return
56+
val player = e.whoClicked as? Player ?: return
57+
val slot = slots.getOrNull(e.slot) ?: return
5958

6059
// Play a sound if the slot has a click handler
6160
if (slot.onclick != null) {
@@ -73,8 +72,8 @@ class GUI(
7372

7473
inner class Slot {
7574
var item: Item? = null
76-
var onclick: (suspend InventoryClickEvent.(Player) -> Unit)? = null
77-
var sound: (suspend InventoryClickEvent.(Player) -> Unit)? = { it.playSound(it.location, Sound.ORB_PICKUP, .05f, 1f) }
75+
var onclick: (InventoryClickEvent.(Player) -> Unit)? = null
76+
var sound: (InventoryClickEvent.(Player) -> Unit)? = { it.playSound(it.location, Sound.ORB_PICKUP, .05f, 1f) }
7877
}
7978

8079
fun slot(

0 commit comments

Comments
 (0)