Accessing a name in a different method

52 views Asked by At

I am trying to access a name that is stored within a method of my class Here is my code:

    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;
    }

}

}

I am trying to get the name and store it under String tn, then call upon it in the onInventoryClick method. **This code implements bukkit API

1

There are 1 answers

1
Cath On BEST ANSWER

You are trying to directly get the player using the name, but that can only be done with an online player.

"null" is returned in case the name either isn't found or when the player is offline. I'd recommend you to add a check, and print the result to the console / chat. If you want to work with an actual offline player, use this.

(Also, you are trying to get your player object before even checking if args[0] is actually given)