使用其他方法访问名称

时间:2016-12-24 05:07:45

标签: object getter bukkit accessor

我正在尝试访问存储在我班级方法中的名称 这是我的代码:

    public class PunishCommand implements Listener, CommandExecutor {

    private String tn;
    private Player target;


@SuppressWarnings("deprecation")
public boolean onCommand(CommandSender sender, Command cmd, String label,
        String[] args) {
    Player p = (Player) sender;
    target = Bukkit.getServer().getPlayer(args[0]);
    tn = target.getName();
    if(!sender.hasPermission("punishgui.open")) {

        sender.sendMessage(ChatColor.RED + "You do not have enough permissions to do that.");
        return true;
    }

    if(args.length == 0) {

        sender.sendMessage(ChatColor.RED + "Usage: /punish <player>");
        return true;
    }

    if(args.length == 1) {
        if (target == null) {
            sender.sendMessage(ChatColor.RED + "Your target is invalid!");
            return true;
        }
        else
        {
            openGUI(p);
            return true;
        }
    }

    if (args.length > 1) {

        sender.sendMessage(ChatColor.RED + "Usage: /punish <player>");
        return true;
    }

    return false;
}
public String getNameTarget()
{
    return this.tn;
}

public void openGUI(Player player)
{
    //owner (null=everyone), slots(rowsx9), name
    Inventory inv = Bukkit.createInventory(null, 9, ChatColor.RED + "Punish Player");

    ItemStack clearInv = new ItemStack (Material.BARRIER);
    ItemMeta clearInvMeta = clearInv.getItemMeta();
    ItemStack changeGM = new ItemStack (Material.LAVA_BUCKET);
    ItemMeta changeGMMeta = changeGM.getItemMeta();
    ItemStack invis = new ItemStack (Material.REDSTONE);
    ItemMeta invisMeta = invis.getItemMeta();
    ItemStack fix = new ItemStack (Material.SPIDER_EYE);
    ItemMeta fixMeta = fix.getItemMeta();
    ItemStack stop = new ItemStack (Material.FERMENTED_SPIDER_EYE);
    ItemMeta stopMeta = stop.getItemMeta();
    ItemStack tMute = new ItemStack (Material.POISONOUS_POTATO);
    ItemMeta tMuteMeta = tMute.getItemMeta();


    clearInvMeta.setDisplayName(ChatColor.RED + "Warn");
    clearInv.setItemMeta(clearInvMeta);
    changeGMMeta.setDisplayName(ChatColor.RED + "Mute");
    changeGM.setItemMeta(changeGMMeta);
    invisMeta.setDisplayName(ChatColor.RED + "Kick");
    invis.setItemMeta(invisMeta);
    fixMeta.setDisplayName(ChatColor.RED + "Temp Ban");
    fix.setItemMeta(fixMeta);
    stopMeta.setDisplayName(ChatColor.RED + "Ban");
    stop.setItemMeta(stopMeta);
    tMuteMeta.setDisplayName(ChatColor.RED + "Temp Mute");
    tMute.setItemMeta(tMuteMeta);


    inv.setItem(5, tMute);
    inv.setItem(4, invis);
    inv.setItem(3, fix);
    inv.setItem(2, clearInv);
    inv.setItem(1, stop);
    inv.setItem(0, changeGM);

    player.openInventory(inv);
}

public void openGUI1(Player player)
{
    Inventory tb = Bukkit.createInventory(null, 9, ChatColor.RED + "Temp Ban Options");

    ItemStack ten = new ItemStack (Material.OBSIDIAN);
    ItemMeta tenMeta = ten.getItemMeta();
    ItemStack thirty = new ItemStack (Material.BEDROCK);
    ItemMeta thirtyMeta = thirty.getItemMeta();

    tenMeta.setDisplayName(ChatColor.RED + "10 Minutes");
    ten.setItemMeta(tenMeta);
    thirtyMeta.setDisplayName(ChatColor.RED + "30 Minutes");
    thirty.setItemMeta(thirtyMeta);

    tb.setItem(0, ten);
    tb.setItem(1, thirty);

    player.openInventory(tb);
}

public void openGUI2(Player player)
{
    Inventory tb2 = Bukkit.createInventory(null, 9, ChatColor.RED + "Temp Mute Options");

    ItemStack ten2 = new ItemStack (Material.WATER_BUCKET);
    ItemMeta ten2Meta = ten2.getItemMeta();
    ItemStack thirty2 = new ItemStack (Material.GRASS);
    ItemMeta thirty2Meta = thirty2.getItemMeta();

    ten2Meta.setDisplayName(ChatColor.RED + "10 Minutes");
    ten2.setItemMeta(ten2Meta);
    thirty2Meta.setDisplayName(ChatColor.RED + "30 Minutes");
    thirty2.setItemMeta(thirty2Meta);

    tb2.setItem(0, ten2);
    tb2.setItem(1, thirty2);

    player.openInventory(tb2);
}

@EventHandler
public void onInventoryClick(InventoryClickEvent event)
{

    Player player = (Player) event.getWhoClicked(); 
    event.setCancelled(true);
    if(!ChatColor.stripColor(event.getInventory().getName()).equalsIgnoreCase("Punish Player"))
    {
        return;
    }
    if(event.getCurrentItem()==null || event.getCurrentItem().getType()==Material.AIR || !event.getCurrentItem().hasItemMeta())
    {
        player.closeInventory();
        return;
    }

    switch(event.getCurrentItem().getType())
    {
    case BARRIER:
        player.sendMessage(ChatColor.DARK_RED + "This is a warning: " + ChatColor.RED + "Stop what you are doing or further punishments will follow.");
        break;
    case LAVA_BUCKET:
        Bukkit.dispatchCommand(Bukkit.getConsoleSender() , "kill " + getNameTarget());
        break;
    case SPIDER_EYE:
        openGUI1(player);
        break;
    case FERMENTED_SPIDER_EYE:
        Bukkit.dispatchCommand(Bukkit.getConsoleSender() , "ban " + getNameTarget());
        break;
    case REDSTONE:
        Bukkit.dispatchCommand(Bukkit.getConsoleSender() , "kick " + getNameTarget());
        break;
    case POISONOUS_POTATO:
        openGUI2(player);
        break;
    case OBSIDIAN:
        Bukkit.dispatchCommand(Bukkit.getConsoleSender() , "tempban " + getNameTarget() + " 10 minutes");
        break;
    case BEDROCK:
        Bukkit.dispatchCommand(Bukkit.getConsoleSender() , "tempban " + getNameTarget() + " 30 minutes");
        break;
    default:
        player.closeInventory();
        break;
    }

}

}

我正在尝试获取名称并将其存储在String tn下,然后在onInventoryClick方法中调用它。 **此代码实现了bukkit API

1 个答案:

答案 0 :(得分:0)

您正在尝试使用该名称直接获取播放器,但这只能通过在线播放器完成。

&#34;空&#34;如果找不到名称或玩家离线时返回。我建议你添加一张支票,然后将结果打印到控制台/聊天室。如果您想使用实际的离线播放器,请使用this

(另外,你甚至在检查是否实际给出了args [0]之前试图获取你的玩家对象)