Solved onPortalCreate

Discussion in 'Plugin Development' started by Vaughan, Nov 8, 2012.

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

    Vaughan

    Hi Guys,

    You'll probably see a lot of me now as I'm getting into Java and creating plugins; I find it really fun. I've already made a thread (having solved it myself there was still good posts explaining better ways which I like).

    Ok, my problem:

    I want a plugin that on the creation of a portal, sends a world message (e.g. /say blahblah) to everyone.

    So far, I've got..

    Code:
    package me.amphios.amphiportal;
     
     
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.world.PortalCreateEvent;
     
    public class AmphiPortalListener implements Listener {
     
        @EventHandler(priority = EventPriority.HIGH)
        public void onPortalCreate(PortalCreateEvent event){
         
     
         
        }
    }
    I dont want to be given the EXACT code, otherwise I wont learn anything. I'd love it if someone could show me how to do it with explanation to what's happening. I think it would go something like:

    player = getPlayer
    name = getName

    if(player.createPortal) {
    console.sendMessage(name +" created a portal");
    }else {
    dont do anything
    }

    I know that probably doesn't make any sence. If anyone could help that'd be awesome!
     
  2. Offline

    fireblast709

    well good that you enjoy it :3. Now, lets start shall we:
    you have the event, thats good. Now, you want that player right? Then you would go here:
    http://jd.bukkit.org/apidocs/org/bukkit/event/player/PlayerPortalEvent.html
    This is basically a full description of what you can do with that event. Might be a bit confusing at start though. On that page, you are going to start to seach for something that would get a Player object. (obvious hint here).

    Now, you found your Player object? good if you did. You assign it to a variable of the same type, so something like Player player, as you have a Player object. We will refer to player from now on.

    Next is the name. Again, you start to search on the javadocs: http://jd.bukkit.org/apidocs/org/bukkit/entity/Player.html
    But now you are looking for a method that gets the name of a Player object. This would be a String (a letter, word or complete phrase). You assign it as String name = ... (your found method here).

    Now you want to know if he made a portal. Well bukkit makes that quite ease: player did. (Otherwise there would be no reason to call the event right?

    So all that rests is the broadcasting of the message. You get that one for free:
    Bukkit.broadcastMessage("I will be broadcasted if a dev uses this in his code :D!");
    Now apply all of the above, and gl
     
    Vaughan likes this.
  3. Offline

    Vaughan

    You said here:
    I've put:
    Code:
        @EventHandler(priority = EventPriority.HIGH)
        public void onPortalCreate(PortalCreateEvent event){
            Player player = event.getPlayer();
           
        }
    }
    Yet it wont allow that .. can I not use the event getPlayer in the PortalCreateEvent?
     
  4. Offline

    one4me

    PlayerPortalEvent gets called when a player uses a portal, not when a player creates one. And there isn't a way of getting who created the portal in PortalCreateEvent.
     
  5. Offline

    Vaughan

    Just having it say "A portal was created" would do, it doesn't specifically need the name.
     
  6. Offline

    fireblast709

    Mmh... *facepalms* misread the event name xD. There is no actual way of getting the player who created this. You could try to make an ArrayList with playernames who created fire (or did a right click with a flint n steel), and remove them like 0.5 seconds later. The portal event will check that list, to see if there is a player nearby (you can get a list of blocks, just pick one of the bottom ones and check the distance). The closest player will probably lit it

    one4me ^ yes there is a (maybe not 100% correct, but it gets close) way of doing this
    Vaughan just broadcast a message when this even occurs and the reason is Fire (and not OBC) http://jd.bukkit.org/apidocs/org/bukkit/event/world/PortalCreateEvent.CreateReason.html
     
    Vaughan likes this.
  7. Offline

    one4me

    Then you would just broadcast the message.
    Code:
    @EventHandler(priority = EventPriority.HIGH)
    public void onPortalCreate(PortalCreateEvent event){
      Bukkit.broadcastMessage("A portal was created");
    }
    
    That will probably lead to a lot of spam.

    fireblast709
    When I said "there isn't a way" I meant that the event does not include a method which allows you to get the player.
     
    Vaughan likes this.
  8. Offline

    fireblast709

    one4me true misread that one word
     
    Vaughan likes this.
  9. Offline

    Vaughan

    Code:
    package me.amphios.amphiportal;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.world.PortalCreateEvent;
     
    public class AmphiPortalListener implements Listener {
       
        @EventHandler(priority = EventPriority.HIGH)
        public void onPortalCreate(PortalCreateEvent event){
           
        Bukkit.broadcastMessage(ChatColor.DARK_RED + "A portal has been created.");
     
        }
    }
    
    Thank you very much guy's, this is what I've got (I actually didn't see one4me post before I tried this). Both very helpful and I understood.
     
  10. Offline

    one4me

    Vaughan
    I know this is solved, but here's a way to get the player that created the portal. It's similar to what fireblast709 described.
    Code:
    public Block[] block = new Block[1];
    public String[] player = new String[1];
    @EventHandler
    public void onPlayerInteractEvent(PlayerInteractEvent e) {
      if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
        if(e.getPlayer().getItemInHand().getTypeId() == 259) {
          Block b = e.getClickedBlock();
          if(b.getTypeId() == 49) {
            block[0] = b;
            player[0] = e.getPlayer().getName();
            Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
              public void run() {
                block[0] = null;
                player[0] = null;
              }
            },1);
          }
        }
      }
    }
    @EventHandler
    public void onPortalCreateEvent(PortalCreateEvent e) {
      if(e.getBlocks().contains(block[0])) {
        Bukkit.broadcastMessage(ChatColor.DARK_RED + player[0] + " created a portal");
      }
    }
    
     
    Vaughan likes this.
  11. Offline

    Vaughan

    Wow, thank you! I know it's probably a lot of effort, but could you explain what's happening briefly if you can?

    Also, this throw's an error but still works: http://puu.sh/1nWfL
     
  12. Offline

    fireblast709

    In a nutshell. (I do not know the exact size of that nutshell)

    On line 25, you use your listener class instead of your main class.
    Add this to the listener:
    Code:java
    1. public MainClass plugin;
    2. public AmphiPortalListener(MainClass p)
    3. {
    4. plugin = p;
    5. }

    and replace
    Code:java
    1. Bukkit.getScheduler().scheduleSyncDelayedTask(this,...

    with
    Code:java
    1. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin,...
     
    Vaughan likes this.
  13. Offline

    one4me

    Code:
    /*A Block array and String array are created with a size of 1*/
    public Block[] block = new Block[1];
    public String[] player = new String[1];
    
    /*Lister to PlayerInteractEvent*/
    @EventHandler
    public void onPlayerInteractEvent(PlayerInteractEvent e) {
    
      /*Check if player right clicked a block*/
      if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
    
        /*Check if player is holding flint and steel*/
        if(e.getPlayer().getItemInHand().getTypeId() == 259) {
          Block b = e.getClickedBlock();
    
          /*Check if block is obsidian*/
          if(b.getTypeId() == 49) {
    
            /*Set the first element of the array you created earlier*/
            block[0] = b;
            player[0] = e.getPlayer().getName();
    
            /*Create a delayed task that sets the arrays to null, so that there are no false positive when a portal is created*/
            Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
              public void run() {
                block[0] = null;
                player[0] = null;
              }
            },1);
          }
        }
      }
    }
    
    /*Listen to PortalCreateEvent*/
    @EventHandler
    public void onPortalCreateEvent(PortalCreateEvent e) {
    
      /*Check if the blocks involved in this event contain the block you added to the array in the previous event*/
      if(e.getBlocks().contains(block[0])) {
    
        /*Broadcasts the message with the players name*/
        Bukkit.broadcastMessage(ChatColor.DARK_RED + player[0] + " created a portal");
      }
    }
    
    That error is from the delayed task, change this to Bukkit.getPluginManager().getPlugin("PluginName")

    Edit -
    Or to fix the error do it the more practical way like how fireblast703 posted above.
     
    Vaughan likes this.
  14. Offline

    Vaughan

    Thank you very much the both of you! :)
     
  15. Offline

    fireblast709

    one4me *coughs* "fireblast703 >->" :p
     
  16. Offline

    Vaughan

    I'm acutally still getting the error.. I couldn't quite understand what you mean in the first part of your fix so I used the other one. fireblast709

    Code:
    package me.amphios.amphiportal;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.block.Block;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.world.PortalCreateEvent;
     
    public class AmphiPortalListener implements Listener {
     
     
     
        /*A Block array and String array are created with a size of 1*/
        public Block[] block = new Block[1];
        public String[] player = new String[1];
     
        /*Lister to PlayerInteractEvent*/
        @EventHandler
        public void onPlayerInteractEvent(PlayerInteractEvent e) {
     
          /*Check if player right clicked a block*/
          if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
     
            /*Check if player is holding flint and steel*/
            if(e.getPlayer().getItemInHand().getTypeId() == 259) {
              Block b = e.getClickedBlock();
     
              /*Check if block is obsidian*/
              if(b.getTypeId() == 49) {
     
                /*Set the first element of the array you created earlier*/
                block[0] = b;
                player[0] = e.getPlayer().getName();
     
                /*Create a delayed task that sets the arrays to null, so that there are no false positive when a portal is created*/
                Bukkit.getScheduler().scheduleSyncDelayedTask(Bukkit.getPluginManager().getPlugin("PluginName"), new Runnable(){
                  public void run() {
                    block[0] = null;
                    player[0] = null;
                  }
                },1);
              }
            }
          }
        }
     
        /*Listen to PortalCreateEvent*/
        @EventHandler
        public void onPortalCreateEvent(PortalCreateEvent e) {
     
          /*Check if the blocks involved in this event contain the block you added to the array in the previous event*/
          if(e.getBlocks().contains(block[0])) {
     
            /*Broadcasts the message with the players name*/
            Bukkit.broadcastMessage(ChatColor.DARK_RED + player[0] + " created a portal");
          }
        }
    }
    I've got this.
     
  17. Offline

    fireblast709

    Code:java
    1. package me.amphios.amphiportal;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.block.Block;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.block.Action;
    9. import org.bukkit.event.player.PlayerInteractEvent;
    10. import org.bukkit.event.world.PortalCreateEvent;
    11.  
    12. public class AmphiPortalListener implements Listener {
    13.  
    14. public MainClass plugin;
    15.  
    16. public AmphiPortalListener(MainClass p)
    17. {
    18. plugin = p;
    19. }
    20.  
    21. /*A Block array and String array are created with a size of 1*/
    22. public Block[] block = new Block[1];
    23. public String[] player = new String[1];
    24.  
    25. /*Lister to PlayerInteractEvent*/
    26. @EventHandler
    27. public void onPlayerInteractEvent(PlayerInteractEvent e) {
    28.  
    29. /*Check if player right clicked a block*/
    30. if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
    31.  
    32. /*Check if player is holding flint and steel*/
    33. if(e.getPlayer().getItemInHand().getTypeId() == 259) {
    34. Block b = e.getClickedBlock();
    35.  
    36. /*Check if block is obsidian*/
    37. if(b.getTypeId() == 49) {
    38.  
    39. /*Set the first element of the array you created earlier*/
    40. block[0] = b;
    41. player[0] = e.getPlayer().getName();
    42.  
    43. /*Create a delayed task that sets the arrays to null, so that there are no false positive when a portal is created*/
    44. Bukkit.getScheduler().scheduleSyncDelayedTask(Bukkit.getPluginManager().getPlugin("PluginName"), new Runnable(){
    45. public void run() {
    46. block[0] = null;
    47. player[0] = null;
    48. }
    49. },1);
    50. }
    51. }
    52. }
    53. }
    54.  
    55. /*Listen to PortalCreateEvent*/
    56. @EventHandler
    57. public void onPortalCreateEvent(PortalCreateEvent e) {
    58.  
    59. /*Check if the blocks involved in this event contain the block you added to the array in the previous event*/
    60. if(e.getBlocks().contains(block[0])) {
    61.  
    62. /*Broadcasts the message with the players name*/
    63. Bukkit.broadcastMessage(ChatColor.DARK_RED + player[0] + " created a portal");
    64. }
    65. }
    66. }

    Replace 'MainClass' with your main class ('public class MainClass extends JavaPlugin' <- what MainClass is there, is what you should use). Also, in the main class
    Code:java
    1. registerEvents(new AmphiPortalListener());

    should change to
    Code:java
    1. registerEvents(new AmphiPortalListener(this));

    obviously, everything before the method remains the same (so only the method parameters change a bit)
     
  18. Offline

    Vaughan


    Hmm.. Not sure it's working .. am I missing something completely obvious. Sorry, I'm not great at this yet! Haha.

    Code:
    public class AmphiPortalListener implements Listener {
     
        public class MainClass extends JavaPlugin plugin;
     
        public AmphiPortalListener(MainClass p)
        {
            plugin = p;
        }
    It's throwing up errors.
     
  19. Offline

    fireblast709

    lolz. What is your main class called? The class with onEnable() and onDisable()
     
  20. Offline

    Vaughan

    Wow yeah I'm sutpid, yeah my main is AmphiPortal! I don't know why I got so confused..

    It's also throwing an error towards "plugin"?

    Code:
    public class AmphiPortalListener implements Listener {
       
        public class AmphiPortal extends JavaPlugin [COLOR=#ff0000]plugin[/COLOR];
       
        [COLOR=#ff0000]public AmphiPortalListener(AmphiPortal p)[/COLOR]
        {
            [COLOR=#ff0000]plugin[/COLOR] = p;
        }
    In red is what's showing as an error, should "Public AmphiPortalListener(AmphiPortal p)" have a "void" in it?
     
  21. Offline

    fireblast709

    almost correct:
    Code:java
    1. public class AmphiPortalListener implements Listener
    2. {
    3. public AmphiPortal plugin;
    4.  
    5. public AmphiPortalListener(AmphiPortal p)
    6. {
    7. plugin = p;
    8. }
     
  22. Offline

    Vaughan

    Ahhhh, that makes way more sense! Thank's. :)

    Code:
    public void onEnable(){
    getServer().getPluginManager().registerEvents(new AmphiPortalListener(this));
    }
    The "RegisterEvents" seems to be erroring? It's saying to add a "null" to the end of "(this)) null"; .. not sure what a null does to it.
     
  23. Offline

    Vaughan

    one4me I dont suppose you could solve this last error?
     
  24. Offline

    one4me

    Maybe try this?
    Code:
    getServer().getPluginManager().registerEvents(new AmphiPortalListener(this), this);
    
     
  25. Offline

    Vaughan

    one4me Hmm.. that seem's to have solved the error; weirdly enough though it's still erroring in game.

    Edit: Forgot to mention, the plugin works. But when you create a portal it still throw's the error.
    http://puu.sh/1ohis
     
  26. Offline

    one4me

    Can you post your Main class and your Listener class?
     
  27. Offline

    Vaughan

    Main:
    Code:
    package me.amphios.amphiportal;
     
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class AmphiPortal extends JavaPlugin {
       
        public void onEnable(){
            getServer().getPluginManager().registerEvents(new AmphiPortalListener(this), this);
        }
       
        public void onDisable(){
           
        }
    }
    Listener Class:
    Code:
    package me.amphios.amphiportal;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.block.Block;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.world.PortalCreateEvent;
     
    public class AmphiPortalListener implements Listener
    {
        public AmphiPortal plugin;
     
        public AmphiPortalListener(AmphiPortal p)
        {
            plugin  = p;
        }
       
        /*A Block array and String array are created with a size of 1*/
        public Block[] block = new Block[1];
        public String[] player = new String[1];
     
        /*Lister to PlayerInteractEvent*/
        @EventHandler
        public void onPlayerInteractEvent(PlayerInteractEvent e) {
     
          /*Check if player right clicked a block*/
          if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
     
            /*Check if player is holding flint and steel*/
            if(e.getPlayer().getItemInHand().getTypeId() == 259) {
              Block b = e.getClickedBlock();
     
              /*Check if block is obsidian*/
              if(b.getTypeId() == 49) {
     
                /*Set the first element of the array you created earlier*/
                block[0] = b;
                player[0] = e.getPlayer().getName();
     
                /*Create a delayed task that sets the arrays to null, so that there are no false positive when a portal is created*/
                Bukkit.getScheduler().scheduleSyncDelayedTask(Bukkit.getPluginManager().getPlugin("PluginName"), new Runnable(){
                  public void run() {
                    block[0] = null;
                    player[0] = null;
                  }
                },1);
              }
            }
          }
        }
     
        /*Listen to PortalCreateEvent*/
        @EventHandler
        public void onPortalCreateEvent(PortalCreateEvent e) {
     
          /*Check if the blocks involved in this event contain the block you added to the array in the previous event*/
          if(e.getBlocks().contains(block[0])) {
     
            /*Broadcasts the message with the players name*/
            Bukkit.broadcastMessage(ChatColor.DARK_RED + player[0] + " created a portal");
          }
        }
    }
    
     
  28. Offline

    one4me

    Vaughan
    You were suppose to put the actual name of the plugin in this line not leave "PluginName"
    Code:
    Bukkit.getScheduler().scheduleSyncDelayedTask(Bukkit.getPluginManager().getPlugin("PluginName"), new Runnable(){
    
    But now that you did all of that other stuff just go ahead and change that line to this:
    Code:
    Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
    
     
  29. Offline

    Vaughan

    Oooooh! Haha wow, that was stupid of me. Still doesn't appear to be working. Am I being stupid again?

    http://puu.sh/1ohR8
     
  30. Offline

    one4me

    Can you post your Listener again? I'm not getting that error.
     
Thread Status:
Not open for further replies.

Share This Page