I need help with an issue i keep running into.

Discussion in 'Plugin Development' started by Rayhon27, Aug 23, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    Rayhon27

    I am trying to call for the targetPlayer from my onCommand to my onClickEvent but i can't get it to work. If anyone knows how i can go about fixing this please feel free to comment the fix below because i am in dire need of this.

    http://pastebin.com/KGEnnWM8

    This is my code!
     
  2. Offline

    mythbusterma

    @Rayhon27

    How would you get a target of the click event? What do you mean?
     
  3. Offline

    Blockhead7360

    Are you just trying to get who clicked on the item? If so, you don't need to do all that targetPlayer stuff with getPlayer and the array list. Just do Player targetPlayer = (Player) e.getWhoClicked(); in the click event.

    If you are trying to get a player from an argument, use a HashMap instead.

    Map<String,String> players = new HashMap<String,String>();

    That would be at the top of the code. In onCommand, you would put:

    players.put(sender.getName(), args[0]);

    Then you would open the inventory. In the click event, you can get the target player by doing:

    Player target = Bukkit.getServer().getPlayer(players.get(e.getWhoClicked().getName());

    Sorry for the non code blocks because I am on my phone
     
  4. Offline

    HeartandSoul

    Code:
        ArrayList<Player> playername = new ArrayList<Player>();
      
        Logger myPluginLogger = Bukkit.getLogger();
    Bukkit logs your plugin for you xD

    Code:
    public class main extends JavaPlugin {
    Don't name your main class "Main!" It's bad practice.

    Code:
    [LIST=1]
    [*]    @EventHandler
    [*]    public void onClick(InventoryClickEvent e){
    [*]       
    [*]            if(e.getInventory().getTitle().equals(ChatColor.DARK_RED + "" + ChatColor.BOLD + "           Punishment" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "Selector")){
    [*]                if(e.getCurrentItem().getItemMeta().getDisplayName().equals(ChatColor.BLUE + "Freeze")){
    [*]                    Player targetPlayer = Bukkit.getServer().getPlayer(targetPlayer);
    [*]            }
    [*]        }
    [*]       
    [*]    }
    [*]    public boolean onCommand(CommandSender Sender, Command cmd, String commandLabel, String[] args){
    [*]
    
    [*]        if(Sender instanceof Player){
    [*]            Player player = (Player) Sender;
    [*]           
    [*]            if(commandLabel.equalsIgnoreCase("punish")){
    [*]                if(args.length == 0){
    [*]                    return false;  
    [*]                }
    [*]                    if(args.length > 1){
    [*]                        return false;
    [*]                    }
    [*]                        if(args.length == 1){
    [*]                            Player targetPlayer = Bukkit.getPlayer(args[0]);
    [*]                            playername.add(targetPlayer);
    [*]                            player.openInventory(inv);
    [*]                        }
    [*]            }
    [*]           
    [*]        }
    [*]     return false;    
    [*]    }
    [/LIST]
    
    Put the command and the events in different classes. Then, separate the different parts of onCommand in methods (returning the player). Have the ClickEvent class extend your onCommand class, and call the method in ClickEvent.
     
  5. Offline

    timtower Administrator Administrator Moderator

    And why is this needed?

    @Rayhon27 Please don't store Player instances, use the UUID's instead. Memory leaks etc.
     
  6. Offline

    Rayhon27

    No none of these help me at all. So the whole purpose of the plugin is for an Admin on the server to type /punish <playername> and then it will open a gui up with different punishments. But i need to know how to get the player they type in the /punish <playername> so i can put it in the onClickEvent of one of the punishment items in the gut so that they can punish they player they typed in the /punish <playername> command.
     
  7. Offline

    InstanceofDeath

    Code:
    // gets the player on the server with the args[0] as the name
    Player target = Bukkit.getServer().getPlayer(args[0]);
    // checks if the player exists or is online (just basics) Later you will replace this row with some other stuff
    // That you can ban players without their being online atm
    if(target != null) {
       // code
    } else {
    // Message to the player, when there cant be found the targetPlayer
        player.sendMessage(ChatColor.Red + "Player is not online or doesnt even exist");
    
    And btw. This code get explained in rlly every bukkit.api tutorial
     
    Zombie_Striker likes this.
Thread Status:
Not open for further replies.

Share This Page