How do you get targetPlayers in events?

Discussion in 'Plugin Development' started by tacos1223, May 1, 2014.

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

    tacos1223

    In something like:
    Code:
    public void onPlayerMove(PlayerMoveEvent event){
    How would you grab a target player? I tried something like this:
    Code:
        public void onTargetPlayerMove(PlayerMoveEvent event, String[] args){
            if(targetFreeze == true){
                Player player = event.getPlayer;
                Player targetPlayer = player.getServer().getPlayer(args[0]);
                targetPlayer.teleport(targetPlayer);
            }
    Just realized I forgot to add @EventHandler, still doesn't work though. Just wanted to point that out in case any of you guys notice it.

    Anyone?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  2. Offline

    Garris0n

    ...what target player? What on earth are you talking about?
     
  3. Offline

    tacos1223


    I tried adding hashmaps, that doesn't work either. I want it so when a someone types /freeze (targetplayer) the targetplayer will get frozen.
    Code:
              if(commandLabel.equalsIgnoreCase("freeze")){ 
    if(args.length == 1){
                if(!player.hasPermission("freeze.player")){
                        player.sendMessage(ChatColor.RED + "Error:" + ChatColor.DARK_RED + " No Permissions!");
                        } else {
            Player targetPlayer = player.getServer().getPlayer(args[0]);
                    if (!hashmap.containsKey(targetPlayer)){
                        targetPlayer.sendMessage(ChatColor.GREEN + "You are now frozen!");
                        hashmap.put(targetPlayer, null);
                            } else {
                                targetPlayer.sendMessage(ChatColor.RED + "You are no longer frozen!");
                                hashmap.remove(targetPlayer);
                    }
                }
              }
    }
    Code:
        @EventHandler
        public void onTargetPlayerMove(PlayerMoveEvent event, String[] args){
            Player player = event.getPlayer();
            Player targetPlayer = player.getServer().getPlayer(args[0]);
            if(hashmap.containsKey(targetPlayer)){
                targetPlayer.teleport(targetPlayer);
            }
        }

    When I type /freeze (tacos1223) it will send me the messages "You are now frozen" and "You are no longer frozen", but it doesn't disable/enable freeze mode.
     
  4. Offline

    Garris0n

    Where did you get the idea that events have arguments? And, again, what on earth is the "target player"?
     
  5. Offline

    tacos1223

    It is a variable, if there is more than 1 argument, it will look for the target player which I have defined. And since you said events can't have any arguments, what do you suggest I do?
     
  6. Offline

    Garris0n

    Where is the variable coming from? What is it? You can't just declare "this player exists, I made it up" and expect everything to work. What you are saying makes absolutely no sense.
     
  7. Offline

    augustt198

    That method won't even be called.
     
  8. Offline

    tacos1223


    Seriously dude? Look up -.-
    Code:
    Player targetPlayer = player.getServer().getPlayer(args[0]);
    I am trying to get the 2nd arguments in /freeze (((TARGETPLAYER))) and making it so they are frozen.

    What should I do?
     
  9. Offline

    Garris0n

    Yes, if there are command arguments (from a command) then you can get a player from the server with the name from the command. Last time I checked a PlayerMoveEvent was not a command. Perhaps you should've looked that up.
     
    tacos1223 likes this.
  10. Offline

    augustt198

    Store the frozen players and cancel the PlayerMoveEvent if you see that the player is contained in the frozen players.
     
    tacos1223 likes this.
  11. Offline

    tacos1223

    That was my question, and I am glad you could of answered it. Nothing to be so harsh on a learning coder for.

    My new question, is, since you can't get a argument for a PlayerMoveEvent, like you said above, how will I freeze the targetPlayer in /freeze (targetPlayer)?

    Ok I will try that. Thank you for some progress.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  12. Offline

    Garris0n

    Your question was "how to get a target player from an event". To put that in perspective, it would be like me walking up to you and asking how to microwave a tree using a bulldozer. When you respond, confused as to why somebody would attempt to use a bulldozer to microwave a tree, I would explain that I simply attached a microwave's number pad to the bulldozer to make it work. In great confusion, you ask why I would assume attaching a number pad to a bulldozer turned it into a microwave, and I would respond that I also attempted to fix the issue by throwing a bunch of sticks into a microwave and waiting for the bulldozer to finish the job.
     
  13. Offline

    tacos1223

    YAY! It worked. What I was doing wrong thanks to Garris0n and augustt198 was I was accidently trying to find the targetPlayer data in the onPlayerMove event when I already gathered the information above, and only needed to use player to find the targetPlayer data. I know it sounds confusing but I am super glad it worked! :D

    /ignore Garris0n
    Every comment you have said towards me so far was put into a negative aspect. Anyways, we are getting off topic and I have already solved my problem. Case closed.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
Thread Status:
Not open for further replies.

Share This Page