我正在为kotlin中的Minecraft做一个排名系统。现在我想在制表器和聊天消息
中显示球员名字前面的排名聊天的例子:你好,世界[用户]玩家:Hey Fabboy
有人知道我是怎么做的吗?(排名保存在配置,Minecraft 1.17.1, Java 16)
我不是Kotlin编程的专业人员,所以如果你能给我一个详细的答案,我将非常感激
代码:
package dev.fabboy.bork
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.TextComponent
import org.bukkit.Bukkit
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerJoinEvent
import org.bukkit.plugin.java.JavaPlugin
import org.bukkit.scoreboard.DisplaySlot
const val version = "0.1"
//
lateinit var INSTANCE: Bork
class Bork : JavaPlugin(), Listener {
override fun onEnable() {
INSTANCE = this
println("----------n[Bork] Plugin loaded nVersion: $versionnVersion from: 2021-09-07n${(System.currentTimeMillis())}n----------")
server.pluginManager.registerEvents(this, this)
getCommand("broadcast")?.setExecutor(Broadcast())
getCommand("setrank")?.setExecutor(setrank())
getCommand("rank")?.setExecutor(rank())
}
override fun onDisable() {
Bukkit.getConsoleSender().sendMessage("[Bork] Plugin Disabled")
}
@EventHandler
fun onJoin(joinEvent: PlayerJoinEvent) {
val player = joinEvent.player
var j = this.config.get("Bork.${player.uniqueId}.joins", 0) as Int
var r = this.config.get("Bork.${player.uniqueId}.rank", 1) as Int
j += 1
var ranker = ""
if(r == 1){
ranker = "§3User"
}
if(r == 2){
ranker = "§dVIP"
}
if(r == 3){
ranker = "§6Admin"
}
if(r == 4){
ranker = "§cOwner"
}
player.sendMessage("§e-------------§r§lPrivate§r§e--------------§rn§lWelcome§rn${(player.displayName() as TextComponent).content()}nThis is your: §e§l$j Join§rnYour Rank: $ranker §rn§e----------------------------------§rnRanks:n1. §3User§rn2. §dVIP§rn3. §6Admin§rn4. §cOwner§rn§e-------------§r§lPrivate§r§e--------------")
this.config.set("Bork.${player.uniqueId}.joins", j)
this.config.set("Bork.${player.uniqueId}.rank", r)
this.saveConfig()
}
}
要更改聊天消息,必须使用事件AsyncPlayerChatEvent
。
Java代码:
@EventHandler
public void onChat(AsyncPlayerChatEvent e) {
Player p = (Player) e.getPlayer();
e.setFormat(getRank(p) + " " + p.getDisplayName() + ": " e.getMessage()); // here you define the chat message
}
Kotlin代码(类似这样):
@EventHandler
fun onChat(e: AsyncPlayerChatEvent) {
Player p = (Player) e.getPlayer();
e.setFormat(getRank(p) + " " + p.getDisplayName() + ": " e.getMessage()); // here you define the chat message
}