Skip to content

Commit

Permalink
[2.0.4] Update & Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsFlicker committed Jul 31, 2023
1 parent c9e0eb9 commit 5ce5469
Show file tree
Hide file tree
Showing 32 changed files with 291 additions and 111 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group=me.arasple.mc.trchat
version=2.0.3
version=2.0.4
taboolib_version=6.0.11-31
kotlin.incremental=true
kotlin.incremental.java=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ interface ComponentManager {
*/
fun filterComponent(component: ComponentText, maxLength: Int = -1): ComponentText

fun validateComponent(component: ComponentText, maxLength: Int = -1): ComponentText

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class NMS {
abstract fun craftChatMessageFromComponent(component: ComponentText): Any

/**
* IChatBaseComponent -> RawMessage
* IChatBaseComponent -> raw string
*/
abstract fun rawMessageFromCraftChatMessage(component: Any): String

Expand Down Expand Up @@ -55,11 +55,7 @@ abstract class NMS {
// 特殊药水
"CustomPotionEffects",
// 隐藏标签
"HideFlags",
// 方块标签
"BlockEntityTag",
// Bukkit 自定义标签
"PublicBukkitValues"
"HideFlags"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package me.arasple.mc.trchat.api.nms

import me.arasple.mc.trchat.util.reportOnce
import net.minecraft.network.protocol.game.ClientboundCustomChatCompletionsPacket
import net.minecraft.network.protocol.game.ClientboundSystemChatPacket
import net.minecraft.server.v1_12_R1.ChatMessageType
import net.minecraft.server.v1_12_R1.IChatBaseComponent
import net.minecraft.server.v1_12_R1.PacketPlayOutChat
Expand All @@ -16,6 +15,7 @@ import taboolib.module.nms.sendPacket
import taboolib.platform.util.isNotAir
import java.util.*

private typealias CraftPlayer19 = org.bukkit.craftbukkit.v1_19_R1.entity.CraftPlayer
private typealias CraftItemStack19 = org.bukkit.craftbukkit.v1_19_R2.inventory.CraftItemStack
private typealias CraftChatMessage19 = org.bukkit.craftbukkit.v1_19_R2.util.CraftChatMessage
private typealias NMSNBTTagCompound = net.minecraft.nbt.NBTTagCompound
Expand Down Expand Up @@ -54,16 +54,9 @@ class NMSImpl : NMS() {
}

override fun sendMessage(receiver: Player, component: ComponentText, sender: UUID?) {
if (majorLegacy >= 11901) {
receiver.sendPacket(ClientboundSystemChatPacket::class.java.invokeConstructor(
craftChatMessageFromComponent(component),
false
))
} else if (majorLegacy == 11900) {
receiver.sendPacket(ClientboundSystemChatPacket::class.java.invokeConstructor(
craftChatMessageFromComponent(component),
0
))
if (majorLegacy >= 11900) {
val player = (receiver as CraftPlayer19).handle
player.sendSystemMessage(craftChatMessageFromComponent(component) as NMSIChatBaseComponent)
} else if (majorLegacy >= 11600) {
receiver.sendPacket(PacketPlayOutChat::class.java.invokeConstructor(
craftChatMessageFromComponent(component),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ object BukkitComponentManager : ComponentManager {
PlatformFactory.registerAPI<ComponentManager>(this)
}

override fun filterComponent(component: ComponentText, maxLength: Int): ComponentText {
return validateComponent(DefaultComponent(listOf(filterComponent(component.toSpigotObject()))), maxLength)
}

override fun sendComponent(receiver: Any, component: ComponentText, sender: Any?) {
val commandSender = when (receiver) {
is ProxyCommandSender -> receiver.cast()
Expand All @@ -58,8 +54,10 @@ object BukkitComponentManager : ComponentManager {
}
val newComponent = if (isFilterEnabled && commandSender is Player && commandSender.data.isFilterEnabled) {
filterComponent(component, Settings.componentMaxLength)
} else {
} else if (commandSender is Player) {
validateComponent(component, Settings.componentMaxLength)
} else {
component
}

if (commandSender is Player) {
Expand All @@ -69,10 +67,14 @@ object BukkitComponentManager : ComponentManager {
}
}

private fun validateComponent(component: ComponentText, maxLength: Int): ComponentText {
override fun filterComponent(component: ComponentText, maxLength: Int): ComponentText {
return validateComponent(DefaultComponent(listOf(filterComponent(component.toSpigotObject()))), maxLength)
}

override fun validateComponent(component: ComponentText, maxLength: Int): ComponentText {
if (maxLength <= 0) return component
return if (component.toRawMessage().length > maxLength) {
Components.text("This chat component is too big to show (> $maxLength).")
Components.text("This chat component is too big to show ( > $maxLength ).")
} else {
component
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package me.arasple.mc.trchat.module.conf.file

import me.arasple.mc.trchat.TrChat
import org.bukkit.command.CommandSender
import taboolib.common.LifeCycle
import taboolib.common.platform.Awake
import taboolib.common.platform.Platform
import taboolib.common.platform.PlatformSide
import taboolib.common.platform.function.adaptCommandSender
import taboolib.common.platform.ProxyCommandSender
import taboolib.common.platform.function.console
import taboolib.module.configuration.Config
import taboolib.module.configuration.Configuration
Expand All @@ -27,15 +26,16 @@ object Filters {
conf.onReload { reload() }
}

fun reload(notify: CommandSender? = null) {
fun reload(notify: ProxyCommandSender = console()) {
TrChat.api().getFilterManager().loadFilter(
conf.getStringList("Local"),
conf.getStringList("Ignored-Punctuations"),
conf.getString("Replacement", "*")!![0],
conf.getBoolean("Cloud-Thesaurus.Enabled"),
conf.getStringList("Cloud-Thesaurus.Urls"),
conf.getStringList("Cloud-Thesaurus.Ignored"),
notify = notify?.let { adaptCommandSender(it) } ?: console()
updateCloud = true,
notify = notify
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import taboolib.common.LifeCycle
import taboolib.common.platform.Awake
import taboolib.common.platform.Platform
import taboolib.common.platform.PlatformSide
import taboolib.common.platform.function.console
import taboolib.common.util.ResettableLazy
import taboolib.common5.Baffle
import taboolib.module.configuration.Config
Expand All @@ -29,7 +30,7 @@ object Functions {
@Awake(LifeCycle.LOAD)
fun init() {
conf.onReload {
Loader.loadFunctions()
Loader.loadFunctions(console())
ResettableLazy.reset("functions")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import taboolib.module.configuration.Config
import taboolib.module.configuration.ConfigNode
import taboolib.module.configuration.ConfigNodeTransfer
import taboolib.module.configuration.Configuration
import taboolib.module.kether.Kether

/**
* @author ItsFlicker
Expand Down Expand Up @@ -40,6 +41,9 @@ object Settings {

@Awake(LifeCycle.ENABLE)
fun init() {
conf.onReload {
Kether.isAllowToleranceParser = conf.getBoolean("Options.Kether-Allow-Tolerance-Parser", true)
}
if (conf.getBoolean("Options.Check-Update", true)) {
submitAsync(delay = 20, period = 15 * 60 * 20) {
Updater.grabInfo()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ class ChatSession(
var channel: String?
) {

val receivedMessages = mutableListOf<ChatMessage>()
var lastPublicMessage = ""
var lastPrivateMessage = ""
var lastPrivateTo = ""
val receivedMessages = mutableListOf<ChatMessage>()
var cancelChat = false

fun getColor(default: CustomColor?): CustomColor {
val forces = MessageColors.getForceColors(player)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ open class Channel(
.mapNotNull { suffix -> suffix.value.firstOrNull { it.condition.pass(player) }?.content?.toTextComponent(player) }
.forEach { suffix -> component.append(suffix) }
} ?: return null
if (session.cancelChat) {
session.cancelChat = false
return null
}
// TODO: 跨服事件传递
// Proxy
if (settings.proxy) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ class PrivateChannel(
.forEach { suffix -> receive.append(suffix) }
} ?: return null

if (session.cancelChat) {
session.cancelChat = false
return null
}
// Channel event
if (!events.send(player, session.lastPrivateTo, msg)) {
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package me.arasple.mc.trchat.module.display.function.standard

import com.google.common.cache.Cache
import com.google.common.cache.CacheBuilder
import me.arasple.mc.trchat.api.impl.BukkitProxyManager
import me.arasple.mc.trchat.module.adventure.toNative
import me.arasple.mc.trchat.module.conf.file.Functions
import me.arasple.mc.trchat.module.display.function.Function
Expand All @@ -12,13 +13,17 @@ import me.arasple.mc.trchat.module.internal.script.Reaction
import me.arasple.mc.trchat.util.*
import org.bukkit.Material
import org.bukkit.entity.Player
import org.bukkit.inventory.Inventory
import org.bukkit.inventory.ItemStack
import taboolib.common.io.digest
import taboolib.common.platform.Platform
import taboolib.common.platform.PlatformSide
import taboolib.common.util.asList
import taboolib.common.util.replaceWithOrder
import taboolib.common.util.resettableLazy
import taboolib.common5.util.encodeBase64
import taboolib.common5.util.parseMillis
import taboolib.library.xseries.XMaterial
import taboolib.module.chat.ComponentText
import taboolib.module.chat.Components
import taboolib.module.chat.impl.DefaultComponent
Expand All @@ -27,8 +32,12 @@ import taboolib.module.configuration.ConfigNodeTransfer
import taboolib.module.nms.MinecraftVersion
import taboolib.module.nms.getI18nName
import taboolib.module.nms.getInternalName
import taboolib.module.ui.buildMenu
import taboolib.module.ui.type.Hopper
import taboolib.platform.util.asLangText
import taboolib.platform.util.buildItem
import taboolib.platform.util.sendLang
import taboolib.platform.util.serializeToByteArray

/**
* @author ItsFlicker
Expand Down Expand Up @@ -62,7 +71,10 @@ object ItemShow : Function("ITEM") {
@ConfigNode("General.Item-Show.Keys", "function.yml")
var keys = emptyList<String>()

private val cache: Cache<ItemStack, ComponentText> = CacheBuilder.newBuilder()
private val cacheComponent: Cache<ItemStack, ComponentText> = CacheBuilder.newBuilder()
.maximumSize(50)
.build()
val cacheHopper: Cache<String, Inventory> = CacheBuilder.newBuilder()
.maximumSize(50)
.build()

Expand Down Expand Up @@ -94,15 +106,25 @@ object ItemShow : Function("ITEM") {
newItem
}
}
return cache.get(item) {
val sha1 = computeAndCache(sender, item).let {
BukkitProxyManager.sendMessage(sender, arrayOf(
"ItemShow",
MinecraftVersion.minecraftVersion,
sender.name,
it.first,
it.second)
)
it.first
}
return cacheComponent.get(item) {
sender
.getComponentFromLang("Function-Item-Show-Format-New", item.amount) { type, i, part, proxySender ->
.getComponentFromLang("Function-Item-Show-Format-With-Hopper", item.amount, sha1) { type, i, part, proxySender ->
val component = if (part.isVariable && part.text == "item") {
item.getNameComponent(sender)
} else {
Components.text(part.text.translate(proxySender).replaceWithOrder(item.amount))
Components.text(part.text.translate(proxySender).replaceWithOrder(item.amount, sha1))
}
component.applyStyle(type, part, i, proxySender, item.amount).hoverItemFixed(item)
component.applyStyle(type, part, i, proxySender, item.amount, sha1).hoverItemFixed(item)
}
}
}
Expand All @@ -124,6 +146,22 @@ object ItemShow : Function("ITEM") {
return true
}

fun computeAndCache(sender: Player, item: ItemStack): Pair<String, String> {
val sha1 = item.serializeToByteArray().encodeBase64().digest("sha-1")
if (cacheHopper.getIfPresent(sha1) != null) {
return sha1 to cacheHopper.getIfPresent(sha1)!!.serializeToByteArray().encodeBase64()
}
val menu = buildMenu<Hopper>(sender.asLangText("Function-Item-Show-Title", sender.name)) {
rows(1)
map("xxixx")
set('x', XMaterial.BLACK_STAINED_GLASS_PANE) { name = "§r" }
set('i', item)
onClick(lock = true)
}
cacheHopper.put(sha1, menu)
return sha1 to menu.serializeToByteArray().encodeBase64()
}

@Suppress("Deprecation")
private fun ItemStack.getNameComponent(player: Player): ComponentText {
return if (originName || itemMeta?.hasDisplayName() != true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package me.arasple.mc.trchat.module.internal

import me.arasple.mc.trchat.TrChat
import me.arasple.mc.trchat.api.impl.BukkitProxyManager
import me.arasple.mc.trchat.module.conf.Loader
import me.arasple.mc.trchat.module.conf.file.Filters
import me.arasple.mc.trchat.module.conf.file.Functions
import me.arasple.mc.trchat.module.conf.file.Settings
Expand All @@ -15,7 +14,6 @@ import org.bukkit.Bukkit
import taboolib.common.platform.*
import taboolib.common.platform.function.console
import taboolib.common.platform.function.pluginVersion
import taboolib.module.kether.Kether
import taboolib.module.lang.sendLang
import taboolib.module.nms.MinecraftVersion.majorLegacy

Expand Down Expand Up @@ -63,9 +61,7 @@ object TrChatBukkit : Plugin() {
Settings.conf.reload()
Functions.conf.reload()
Filters.conf.reload()
Kether.isAllowToleranceParser = Settings.conf.getBoolean("Options.Kether-Allow-Tolerance-Parser", true)
TrChat.api().getChannelManager().loadChannels(notify)
Loader.loadFunctions(notify)
}

}
Loading

0 comments on commit 5ce5469

Please sign in to comment.