如何在onPlayerRenameItem事件中测试玩家是否具有特定权限?

huangapple 未分类评论47阅读模式
英文:

How do I test if the player in an onPlayerRenameItem event has a certain permission?

问题

我制作了一个铁砧颜色重命名插件,但我想将该功能限制在具有特定权限的用户中。当前的代码抛出此错误:方法"getPlayer()"在类型"PrepareAnvilEvent"中未定义。

我的监听器:

package com.delight.anvilcolorrename;

import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.PrepareAnvilEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

public class AnvilColorRenameListener implements Listener {
    
    //构造函数
    public AnvilColorRenameListener(Main plugin) {
        
    }

    //事件处理方法
    @EventHandler
    public void onPlayerRenameItem(PrepareAnvilEvent event){
        if (event.getPlayer().hasPermission("AnvilColorRename.use")) {
            if(event.getResult() != null && event.getResult().hasItemMeta() && event.getInventory().getRenameText() != ""){
                ItemStack result = event.getResult();
                ItemMeta resultMeta = result.getItemMeta();
                String nameColored = ChatColor.translateAlternateColorCodes('&', event.getInventory().getRenameText());
                resultMeta.setDisplayName(nameColored);
                result.setItemMeta(resultMeta);
            }
        }
    }
}

请注意,我已经将代码中的 HTML 实体转义符号(""" 和 "&")还原为正常字符。如果您想要使用原始的 HTML 实体符号,请在代码中保留它们。

英文:

I made an anvil Color rename plugin but i want to restrict the feature to users with a specific permission. The current code throws this error: The method getPlayer() is undefined for the type PrepareAnvilEvent
My listener:

package com.delight.anvilcolorrename;

import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.PrepareAnvilEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

public class AnvilColorRenameListener implements Listener {
    
    //Constructor
    public AnvilColorRenameListener(Main plugin) {
        
    }

    //EventHandler
    @EventHandler
    public void onPlayerRenameItem(PrepareAnvilEvent event){
        if (event.getPlayer().hasPermission("AnvilColorRename.use")) {
            if(event.getResult() != null && event.getResult().hasItemMeta() && event.getInventory().getRenameText() != ""){
                ItemStack result = event.getResult();
                ItemMeta resultMeta = result.getItemMeta();
                String nameColored = ChatColor.translateAlternateColorCodes('&', event.getInventory().getRenameText());
                resultMeta.setDisplayName(nameColored);
                result.setItemMeta(resultMeta);
            }
            }// if(!event.hasPermission("AnvilColorRename.use")) {
//                event.sendMessage("You can not use color codes in an anvil!");
//            }
    }
//}

答案1

得分: 0

请参考官方的 Spigot Javadocs:https://hub.spigotmc.org/javadocs/spigot

PrepareAnvilEvent 类没有一个返回玩家的方法。

英文:

Please refer to the official Spigot javadocs: https://hub.spigotmc.org/javadocs/spigot

The PrepareAnvilEvent class does not have a method that returns a player.

答案2

得分: 0

问题在于PrepareAnvilEvent没有带有可访问的Player字段。但是,您可以通过InventoryView获取到Player

event.getInventoryView().getPlayer()
英文:

The problem is that the PrepareAnvilEvent doesn't come with a Player field that you can access. However, you can get the Player via the InventoryView:

event.getInventoryView().getPlayer()

huangapple
  • 本文由 发表于 2020年7月23日 17:22:47
  • 转载请务必保留本文链接:https://java.coder-hub.com/63050987.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定