显然,我正在制作一个插件,让Shulker box像背包一样工作.您可以右击打开它们在您的主手或甚至副手.我有我需要的所有功能,但我想更改我右击时出现的图形用户界面的标题.

Here's the GUI! [Shulker GUI](https://i.stack.imgur.com/IJMiS.png)

正如你所看到的,它显示了"舒尔克盒子",我想要改变它(如果我们可以使用带有"&"的 colored颜色 代码而不是部分符号,那就太棒了)

有没有什么我可以更改这个标题的?我已经在我的项目中有ProtocolLib个最新版本,这是我第一次开发这样的图形用户界面插件.

以下是我的kotlin maven个代码(我正在开发纸质插件):

package me.hoangxuanlam2007.baloshulker

import org.bukkit.Material
import org.bukkit.block.ShulkerBox
import org.bukkit.command.Command
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.block.Action
import org.bukkit.event.inventory.InventoryCloseEvent
import org.bukkit.event.player.PlayerInteractEvent
import org.bukkit.inventory.EquipmentSlot
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.BlockStateMeta
import org.bukkit.plugin.java.JavaPlugin

class BaloShulker : JavaPlugin(), Listener {

private val openedShulkers = HashMap<Player, ItemStack>()

    override fun onEnable() {
        // Register the event listener
        server.pluginManager.registerEvents(this, this)

        // Log a message to the console when the plugin is enabled
        logger.info("BaloShulker plugin enabled!")
    }

    override fun onDisable() {
        // Log a message to the console when the plugin is disabled
        logger.info("BaloShulker plugin disabled!")
    }

    @EventHandler
    fun onPlayerInteract(event: PlayerInteractEvent) {
        val player = event.player
        val action = event.action
        val hand = event.hand
        val item = if (hand == EquipmentSlot.HAND) player.inventory.itemInMainHand else player.inventory.itemInOffHand

        // Check if the player is holding a shulker box in the hand and right-clicked in the air
        if (isShulkerBox(item) && action == Action.RIGHT_CLICK_AIR) {
            event.isCancelled = true

            // Open the shulker box
            openShulkerBox(player, item)
        }
    }

    private fun isShulkerBox(item: ItemStack): Boolean {
        return item.type == Material.SHULKER_BOX || item.type.name.endsWith("_SHULKER_BOX")
    }

    private fun openShulkerBox(player: Player, shulkerBoxItem: ItemStack) {
        val shulkerBoxMeta = shulkerBoxItem.itemMeta as? BlockStateMeta

        if (shulkerBoxMeta != null && shulkerBoxMeta.blockState is ShulkerBox) {
            val shulkerBox = shulkerBoxMeta.blockState as ShulkerBox
            player.openInventory(shulkerBox.inventory)
            openedShulkers[player] = shulkerBoxItem
        }
    }

    @EventHandler
    fun onInventoryClose(event: InventoryCloseEvent) {
        val player = event.player as? Player

        // Check if the player has a shulker that was previously opened
        if (player != null && openedShulkers.containsKey(player)) {
            val shulkerBoxItem = openedShulkers[player]
            if (shulkerBoxItem != null) {
                saveShulkerContents(shulkerBoxItem, event.inventory.contents.filterNotNull().toTypedArray())
            }

            openedShulkers.remove(player)
        }
    }

    override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean {
        if (command.name.equals("bs", ignoreCase = true) && args.isNotEmpty() && args[0].equals("open", ignoreCase = true)) {
            if (sender is Player) {
                // Check if the player has the permission to open shulker boxes
                if (sender.hasPermission("baloshulker.use")) {
                    val itemInMainHand = sender.inventory.itemInMainHand
                    val itemInOffHand = sender.inventory.itemInOffHand

                    // Check if the player is holding a shulker box in either hand
                    if (isShulkerBox(itemInMainHand)) {
                        openShulkerBox(sender, itemInMainHand)
                    } else if (isShulkerBox(itemInOffHand)) {
                        openShulkerBox(sender, itemInOffHand)
                    } else {
                        sender.sendMessage("You are not holding a shulker box!")
                    }
                } else {
                    sender.sendMessage("You don't have permission to use this command!")
                }
            } else {
                sender.sendMessage("Only players can use this command!")
            }
            return true
        }
        return false
    }

    private fun saveShulkerContents(shulkerItem: ItemStack, contents: Array<ItemStack>) {
        val shulkerBoxMeta = shulkerItem.itemMeta as? BlockStateMeta

        if (shulkerBoxMeta != null && shulkerBoxMeta.blockState is ShulkerBox) {
            val shulkerBox = shulkerBoxMeta.blockState as ShulkerBox
            shulkerBox.inventory.contents = contents
            shulkerBox.update()
            shulkerBoxMeta.blockState = shulkerBox
            shulkerItem.itemMeta = shulkerBoxMeta
        }
    }
}

推荐答案

绑定到区块状态的库存不能通过BukkitAPI直接更改名称.这些库存的标题与在其元数据中设置的显示名称相关联.你要么用你自己的定制名称创建自己的图形用户界面,要么通过它们的元重命名Shulker Box.

val shulkerBoxMeta = shulkerItem.itemMeta as? BlockStateMeta
shulkerBoxMeta.displayName(
    ChatColor.translateAlternateColorCodes('&', "&7My cool gray shulker!")
);

Kotlin相关问答推荐

如果一项工作失败,请继续在Kotlin 等待其他工作/子元素完成

在Webflux应用程序中通过kotlin协程启动fire and forget job

如果启用了Flyway迁移,则不能具有配置属性';datources.default.架构-生成

Kotlin:将泛型添加到列表任何>

T和T有什么区别:任何>

如何在Spring Boot中注册新的集合工厂

为什么我的通用Kotlin函数中的这个转换未经判断?

使用 Kotlin 的 Springboot 中缺少 ResponseEntity 正文属性

如何在 Spring Boot 3 中为内部类提供运行时提示

Kotlin - 协程未按预期执行

使用 LazyListScope 嵌套可组合项

将子元素放在一个列表中

Kotlin 中 lambda 表达式中的默认参数

包括登录Elvis operator?

Kotlin 是如何编译的?

以编程方式重新启动 Spring Boot 应用程序/刷新 Spring 上下文

如何从kotlin中的类实例化对象

未解决的参考 dagger 2 + kotlin + android gradle

什么是 Kotlin 等价于 Class<?>

如何在Kotlin中使用Handler和handleMessage?