-
Notifications
You must be signed in to change notification settings - Fork 3
/
WrappedLPSender.kt
105 lines (85 loc) · 3.51 KB
/
WrappedLPSender.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Copyright (c) 2018-2020 Karlatemp. All rights reserved.
* @author Karlatemp <[email protected]> <https://github.com/Karlatemp>
*
* LuckPerms-Mirai/LuckPerms-Mirai.main/WrappedLPSender.kt
*
* Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 license that can be found via the following link.
*
* https://github.com/Karlatemp/LuckPerms-Mirai/blob/master/LICENSE
*/
package io.github.karlatemp.luckperms.mirai.commands
import io.github.karlatemp.luckperms.mirai.LPMiraiPlugin
import io.github.karlatemp.luckperms.mirai.gui.guiSender
import me.lucko.luckperms.common.command.access.CommandPermission
import me.lucko.luckperms.common.plugin.LuckPermsPlugin
import me.lucko.luckperms.common.sender.Sender
import net.kyori.adventure.text.Component
import net.luckperms.api.util.Tristate
import net.mamoe.mirai.console.command.CommandSender
import net.mamoe.mirai.console.command.ConsoleCommandSender
import java.util.*
import java.util.concurrent.atomic.AtomicBoolean
open class WrappedLPSender(
val delegate: Sender,
val real: CommandSender
) : Sender {
object WrappedConsole : WrappedLPSender(
ConsoleSender,
ConsoleCommandSender
) {
override fun flush() {}
override fun isConsole(): Boolean = true
override fun sendMessage(message: Component?) {
ConsoleSender.sendMessage(message ?: return)
}
override fun isHoverEventSupported(): Boolean = guiSender.isSupported
}
companion object {
fun wrap(sender: CommandSender): WrappedLPSender = if (sender is ConsoleCommandSender) {
WrappedConsole
} else WrappedLPSender(LPMiraiPlugin.senderFactory0.wrap(sender), sender)
}
override fun getPlugin(): LuckPermsPlugin = delegate.plugin
override fun getName(): String = delegate.name
override fun getNameWithLocation(): String = delegate.nameWithLocation
override fun getUniqueId(): UUID = delegate.uniqueId
private val cachedMessages = ArrayList<Component>(16)
private val cache = AtomicBoolean(true)
private val writingLock = AtomicBoolean()
override fun sendMessage(message: Component?) {
message ?: return
@Suppress("ControlFlowWithEmptyBody")
while (writingLock.compareAndSet(false, true)) {
}
if (cache.get()) {
cachedMessages.add(message)
writingLock.set(false)
} else {
writingLock.set(false)
delegate.sendMessage(message)
}
}
override fun getPermissionValue(permission: String?): Tristate = delegate.getPermissionValue(permission)
override fun hasPermission(permission: String?) = delegate.hasPermission(permission)
override fun hasPermission(permission: CommandPermission?) = delegate.hasPermission(permission)
override fun performCommand(commandLine: String?) = delegate.performCommand(commandLine)
override fun isConsole() = delegate.isConsole
override fun isValid() = delegate.isValid
override fun flush() {
@Suppress("ControlFlowWithEmptyBody")
while (writingLock.compareAndSet(false, true)) {
}
if (cache.get()) {
cache.set(false)
val msg = cachedMessages
writingLock.set(false)
if (msg.isNotEmpty()) {
delegate.sendMessage(Component.join({ Component.text('\n') }, msg))
}
} else {
writingLock.set(false)
}
}
override fun isHoverEventSupported(): Boolean = delegate.isHoverEventSupported
}