1
+ package me.arasple.mc.trchat.module.display.function.standard
2
+
3
+ import com.google.common.cache.Cache
4
+ import com.google.common.cache.CacheBuilder
5
+ import me.arasple.mc.trchat.api.impl.BukkitProxyManager
6
+ import me.arasple.mc.trchat.module.conf.file.Functions
7
+ import me.arasple.mc.trchat.module.display.function.Function
8
+ import me.arasple.mc.trchat.module.display.function.StandardFunction
9
+ import me.arasple.mc.trchat.module.internal.script.Reaction
10
+ import me.arasple.mc.trchat.util.CooldownType
11
+ import me.arasple.mc.trchat.util.getCooldownLeft
12
+ import me.arasple.mc.trchat.util.passPermission
13
+ import me.arasple.mc.trchat.util.updateCooldown
14
+ import org.bukkit.entity.Player
15
+ import org.bukkit.inventory.Inventory
16
+ import org.bukkit.inventory.ItemStack
17
+ import taboolib.common.io.digest
18
+ import taboolib.common.platform.Platform
19
+ import taboolib.common.platform.PlatformSide
20
+ import taboolib.common.util.asList
21
+ import taboolib.common.util.resettableLazy
22
+ import taboolib.common5.util.encodeBase64
23
+ import taboolib.common5.util.parseMillis
24
+ import taboolib.library.xseries.XMaterial
25
+ import taboolib.module.chat.ComponentText
26
+ import taboolib.module.configuration.ConfigNode
27
+ import taboolib.module.configuration.ConfigNodeTransfer
28
+ import taboolib.module.nms.MinecraftVersion
29
+ import taboolib.module.ui.buildMenu
30
+ import taboolib.module.ui.type.PageableChest
31
+ import taboolib.platform.util.*
32
+
33
+ /* *
34
+ * @author ItsFlicker
35
+ * @since 2022/3/18 19:14
36
+ */
37
+ @StandardFunction
38
+ @PlatformSide(Platform .BUKKIT )
39
+ object EnderChestShow : Function(" ENDERCHEST" ) {
40
+
41
+ override val alias = " EnderChest-Show"
42
+
43
+ override val reaction by resettableLazy(" functions" ) {
44
+ Functions .conf[" General.EnderChest-Show.Action" ]?.let { Reaction (it.asList()) }
45
+ }
46
+
47
+ @ConfigNode(" General.EnderChest-Show.Enabled" , " function.yml" )
48
+ var enabled = true
49
+
50
+ @ConfigNode(" General.EnderChest-Show.Permission" , " function.yml" )
51
+ var permission = " none"
52
+
53
+ @ConfigNode(" General.EnderChest-Show.Cooldown" , " function.yml" )
54
+ val cooldown = ConfigNodeTransfer <String , Long > { parseMillis() }
55
+
56
+ @ConfigNode(" General.EnderChest-Show.Keys" , " function.yml" )
57
+ var keys = listOf<String >()
58
+
59
+ val cache: Cache <String , Inventory > = CacheBuilder .newBuilder()
60
+ .maximumSize(10 )
61
+ .build()
62
+
63
+ private val AIR_ITEM = buildItem(XMaterial .GRAY_STAINED_GLASS_PANE ) { name = " §f" }
64
+
65
+ override fun createVariable (sender : Player , message : String ): String {
66
+ if (! enabled) {
67
+ return message
68
+ }
69
+ var result = message
70
+ keys.forEach {
71
+ result = result.replaceFirst(it, " {{ENDERCHEST:${sender.name} }}" , ignoreCase = true )
72
+ }
73
+ return result
74
+ }
75
+
76
+ override fun parseVariable (sender : Player , arg : String ): ComponentText ? {
77
+ return computeAndCache(sender).let {
78
+ BukkitProxyManager .sendMessage(sender, arrayOf(
79
+ " ForwardMessage" ,
80
+ " EnderChestShow" ,
81
+ MinecraftVersion .minecraftVersion,
82
+ sender.name,
83
+ it.first,
84
+ it.second)
85
+ )
86
+ sender.getComponentFromLang(" Function-EnderChest-Show-Format" , sender.name, it.first)
87
+ }
88
+ }
89
+
90
+ override fun canUse (sender : Player ): Boolean {
91
+ return sender.passPermission(permission)
92
+ }
93
+
94
+ override fun checkCooldown (sender : Player , message : String ): Boolean {
95
+ if (enabled && keys.any { message.contains(it, ignoreCase = true ) } && ! sender.hasPermission(" trchat.bypass.enderchestcd" )) {
96
+ val enderChestCooldown = sender.getCooldownLeft(CooldownType .ENDERCHEST_SHOW )
97
+ if (enderChestCooldown > 0 ) {
98
+ sender.sendLang(" Cooldowns-EnderChest-Show" , enderChestCooldown / 1000 )
99
+ return false
100
+ } else {
101
+ sender.updateCooldown(CooldownType .ENDERCHEST_SHOW , cooldown.get())
102
+ }
103
+ }
104
+ return true
105
+ }
106
+
107
+ fun computeAndCache (sender : Player ): Pair <String , String > {
108
+ val inventory = sender.enderChest
109
+ val sha1 = inventory.serializeToByteArray(zipped = false ).encodeBase64().digest(" sha-1" )
110
+ if (cache.getIfPresent(sha1) != null ) {
111
+ return sha1 to cache.getIfPresent(sha1)!! .serializeToByteArray().encodeBase64()
112
+ }
113
+ val menu = buildMenu<PageableChest <ItemStack >>(sender.asLangText(" Function-EnderChest-Show-Title" , sender.name)) {
114
+ rows(3 )
115
+ slots((0 .. 26 ).toList())
116
+ elements { (0 .. 26 ).map { inventory.getItem(it).replaceAir() } }
117
+ onGenerate { _, element, _, _ -> element }
118
+ onClick(lock = true )
119
+ }
120
+ cache.put(sha1, menu)
121
+ return sha1 to menu.serializeToByteArray().encodeBase64()
122
+ }
123
+
124
+ private fun ItemStack?.replaceAir () = if (isAir()) AIR_ITEM else this
125
+
126
+ }
0 commit comments