Commands in eventhandler

Discussion in 'Plugin Development' started by Asgernohns, Jul 25, 2013.

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

    Asgernohns

    hello all java coders! :)

    how can i get commands inside an eventhandler?
    i going to use it for a doorbell plugin. (PlayerInteractEvent)
    Can anyone help me? :)
     
  2. Offline

    adam753

    What do you mean by "get commands"? If you want to listen to when the player sends a command then you'll need to use onCommand.
     
  3. Offline

    Asgernohns

    adam753 i need the objects and strings in the onPlayerInteract method.
     
  4. Offline

    Rocoty

    Not getting you. Explain what you want to happen
     
  5. Offline

    Asgernohns

    Rocoty

    i need the doorbell location to tp to it. But the location of the sign is stored in the eventhandler.
     
  6. Offline

    adam753

    You will need to store the variables from the event in some field variables in the plugin class if you want to refer to them later from a different place.
     
  7. Offline

    Asgernohns

    adam753 i cant store Sign sign = (Sign) event.getClickedBlock();
    because that it belongs to EventHandler.
    Nothing is called event. outside the EventHandler.
    i need sign to find the location of it.
     
  8. Offline

    Tarestudio

    Asgernohns
    Like adam753 said, you need to store the variable from the event.
    Code:java
    1. public HashMap<String,Location>() store;
    2. ...
    3. @EventHandler
    4. public void onEvent(YourEvent event) {
    5. ...
    6. store.put(idName,sign.getLocation());
    7. ...
    8. }
     
  9. Offline

    Asgernohns

    Tarestudio
    idname?? what id? id of sign block ? but that doesn't make sense!

    i also get an error at put
     
  10. Offline

    Tarestudio

    Asgernohns
    idName is just something to identify the sign again later. There will be more then one doorbell, wont it? So you have to identify the sign in some way. That is idName for. What it excatly is, is up to you. Its only an example, not really useable code.
     
  11. Offline

    Asgernohns

    Tarestudio
    i made this code: here you can tell me whats wrong.
    Code:java
    1. public class PIL implements Listener {
    2.  
    3. public final HashMap<Location, String> signs = new HashMap<Location, String>();
    4. public HashMap<String ,Location> store;
    5. int tpcount = 0;
    6. String idname;
    7.  
    8.  
    9.  
    10.  
    11. @EventHandler
    12. public void onPlayerInteract(PlayerInteractEvent e, SignChangeEvent se) {
    13. Player p = e.getPlayer();
    14. if (e.getClickedBlock().getType() == Material.SIGN || e.getClickedBlock().getType() == Material.WALL_SIGN) {
    15. Sign sign = (Sign) e.getClickedBlock();
    16.  
    17. if (signs.containsKey(sign.getLocation())) {
    18. Player signCreator = Bukkit.getPlayerExact(signs.get(sign.getLocation()));
    19. if (signs.containsKey(sign.getLocation())) {
    20. if (signCreator != null) {
    21. if (signCreator.isOnline()) {
    22. signCreator.sendMessage(ChatColor.AQUA
    23. + p.getName() + ChatColor.GREEN
    24. + " Is At Your House! Use "
    25. + ChatColor.YELLOW + "/doorbell tp "
    26. + ChatColor.GREEN + "To Get Home!");
    27. tpcount++;
    28. if (tpcount > 1) {
    29. tpcount--;
    30. }
    31. store.put(idname, sign.getLocation());
    32. p.sendMessage(ChatColor.AQUA
    33. + signCreator.getName()
    34. + ChatColor.GREEN
    35. + " Has Been Informed!");
    36. } else {
    37. p.sendMessage(ChatColor.GREEN + "Cannot Find "
    38. + ChatColor.AQUA
    39. + signCreator.getName());
    40. }
    41. }
    42. }
    43. }
    44. }
    45. }
    46.  
    47.  
    48.  
    49. public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args){
    50.  
    51.  
    52.  
    53.  
    54. if (cmd.equals("doorbell")) {
    55. String signCreator = Bukkit.idname;
    56. if (args.length == 0) {
    57. signCreator.sendMessage(ChatColor.DARK_RED
    58. + "Usage: /doorbell tp");
    59. } else if (args.length == 1) {
    60. if (args[0].equals("tp")) {
    61. if (tpcount == 1) {
    62. signCreator
    63. .sendMessage(ChatColor.AQUA
    64. + "Teleporting");
    65. Location l = sign.getLocation();
    66. signCreator.teleport(l);
    67. tpcount--;
    68. }else{
    69. signCreator.sendMessage(ChatColor.DARK_RED+"No Requsts!");
    70. }
    71.  
    72. } else {
    73. signCreator.sendMessage(ChatColor.DARK_RED
    74. + "Unknown Argument! Try /doorbell tp");
    75. }
    76. }
    77. }
    78. }
    79. }


    The onCommand() was put in the EventHandler but it didn't work, so i want to import singCreator from the eventhandler. :)
    can you help me with that? I know i'm a noob to this but i think that i had learn alot!.
     
  12. Offline

    Tarestudio

    Asgernohns
    First, onCommand have to stay at the plugin.
    Second, instead of 'store.put(idname, sign.getLocation());' (line 31) try 'store.put(signCreator.getName(), sign.getLocation());
    Third, onCommand if there is an entry for the player using the command, and if teleport him to the location. After that remove this entry.

    I dont know what you do with this tpcount, what should this do?
     
  13. Offline

    Asgernohns

    Tarestudio
    tpcount is for teleporting tickets if you can call it that. the
    if tpcount > 1
    tpcount--
    is so that players don't abuses the cmd as a /home cmd. ;)

    Do you wanna explain "Third" more close? doesn't quite understand it :(.
     
  14. Offline

    Tarestudio

    Asgernohns
    Well, if you store the 'ringing at the doorbell' in "store" with signCreator (the owner of the house) and location of the used 'doorbell', check "store" onCommand if it has an entry for 'the owner of the house' aka 'signCreator' aka 'the one using the command'. if there is an entry, port him, else dont.
    your tpcount will only work with one doorbell.
     
  15. Offline

    xigsag

    You could do this. Get who clicked the doorbell and save it to an ArrayList. Tell the owner of the doorbell that he has <insert time limit here> to answer this god damn door.

    Let's say the ArrayList was called 'doorbell'.


    Code:java
    1. List<Player> doorbell = new ArrayList<Player>();
    2. Player whoclicked = e.getWhoClicked();
    3.  
    4. Bukkit.getScheduler().scheduledSyncDelayedTask(mainclass, new Runnable(){
    5. public void run(){
    6. doorbell.remove(whoclicked);
    7. }
    8. }, 20*timelimit);
    9. //I'm not sure what 20 is, (ticks?), but its 1 second.

    That's what I would do. I don't know if that's an efficient way or not, but that's how I do it.
     
Thread Status:
Not open for further replies.

Share This Page