[API] Selection API, no server-side plugins required!

Discussion in 'Resources' started by SoThatsIt, Feb 16, 2013.

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

    SoThatsIt

    [​IMG]

    [​IMG]
    yes, the world edit api comes with many more amazing features not included in this package but if you choose to use the world edit api every server owner using your plugin will have to have world-edit installed in order for your plugin to work. With the selection api all you have to do is add the selection api folder into your plugin and register the selector in your main class. How to do this will be explained below.


    [​IMG]
    Of course this not requiring the server owner to install the api himself comes with its specific down-falls. If 2 plugins are using the selection api and use the same item to select with the player could recieve 2 messages saying that they have selected a point. Although not game-breaking this could get extremely irritating to the user. One way around this is by making a variable in your config like Selector_Item: <item id> that way if there is a conflict the server owner could easily fix it.

    [​IMG]
    Integrating the selection api in your plugin is very simple below are the steps to implementing it in your plugin.

    1. Creating the Selector
    creating the selector is very easy all you need is one line of code:
    Code:
    Selector selector = new Selector(JavaPlugin yourPlugin, int selectionItemID);
    where you see selectionItemID in the constructor of Selector all you need to put is the item id of the item you want to use to select. for example if you wanted to use a stick to select with you would put in the item id 280. For yourPlugin you simply need to put in the class that has your onEnable() in it, or if your creating the selector in your your main class you would use the keyword this. Below is an example of creating a selector in your onEnable();

    Code:
    Selector selector = new Selector(this, 280);
    this would create a selector with a the selection item set as a stick.

    2. Formatting your class
    Now you may be wondering where do we put this code inside our class? Below is an example of a class set up with a selector already.
    Code:
    import me.sothatsit.selection.api.Selector;
     
    public class YourClass extends JavaPlugin{
        Selector selector;
     
        public void onEnable(){
              selector = new Selector(this, 280);
        }
    }
    We have put the code above into the onEnable of our class so when the server starts it will register the selector. We have also put the variable selector outside of the onEnable() so we can refer back to it later.

    3. Getting a players selection
    getting a players selection from the selector is very easy, although we do have to check some things to make sure that the player has selected 2 points. below is the code to obtain a selection from the selector.
    Code:
    Selection selection = selector.getSelection(id);
    where you see id in the function getSelection() you simply have to put the players name. You have now obtained the selection from the selector, but before using it you may want to check a few things.
    selections do not automatically have both points selected and methods like inSelection() require both points to be selected. If they are not both selected the method will simply return false. This can be a problem if you are trying to make an arena or house plot. To check if both points have been selected you can use the setLocs() method. below is an example use of this.

    Code:
    Selection selection = selector.getSelection(id);
    if(selection.setLocs()){
        //insert code if the player has selected both points here
    }else{
        //insert code if the player hasn't selected both points here
    }
    You now know how to create the selector and get the selections. below this is another tutorial on how to create a selection listener.


    How to create a selection Listener.
    Creating a selection listener if very easy. All you need to do is listen to the PlayerSelectPointEvent in your listener. Below is an example of this.
    Code:
    class yourclass implements Listener{
      @EventHandler
      public void onPlayerSelectPoint(PlayerSelectPointEvent e){
          //insert code here
      }
    }
    Now you can run code every time a player selects a point. Notice that you do not have to do this to send a message to the player when they select a point this is done automatically and you can set the messages by changing the Selector.SUCCESS_POINT_ONE or Selector.SUCCESS_POINT_TWO to your custom message. You can also use this to set the error messages.

    [​IMG]
    Below is a list of functions contained within the selection api.
    Selector:
    Code:
      getPlayerSelection(String id)
          - gets the selection from the id
          - if you are trying to get a players selection use getSelection(playerName)
    Selection:
    Code:
      getWorld()
          - returns the world the selection is in
      getPoint1()
          - returns point 1 in the selection
      getPoint2()
          - returns point 2 in the selection
      areBothPointsSet()
          - returns if both the points in the selection have been set
      setPoint1(Location loc)
          - sets point 1 in the selection
      setPoint2(Location loc)
          - sets point 2 in the selection
      colliding(Selection s)
          - checks if any part of the two selections are colliding
      inSelection(Location loc)
          - checks if the location is inside the selection
      inSelection(Selection s)
          - checks if the whole selection is inside the selection
    PlayerSelectPointEvent:
    Code:
      getPoint()
          - returns either 1 or 2 depending on which point has been modified
      getSelection()
          - returns the selection for the event
      getPlayer()
          - returns the player who set the selection
      isCancelled()
          - returns whether or not the event has been cancelled
      setCancelled(boolean cancelled)
          - sets whether or not the event will be cancelled
    <font color="#ff0000">All the information above is set as of the latest selection api file. If the above is not working it probably means that you haven't got the latest file. If you have the latest file and something above is not working please post below and i will fix it ASAP.</font>

    [​IMG]
    <font color="#000000">Installing the Selection Api is easy, below are the necessary steps to do so. You do not have to install anything on the server for this to work.</font>
    1. download the latest Selection Api from below
    2. add the folder to your class path, on eclipse you can simply drag the folder onto your src

    Thats it! not that hard is it?

    [​IMG]
    home protection plugin:
    you could use this to let the players select their house and protect it. You could charge them depending on the size of the property.

    Creating Arenas for plugins:
    You could use this to create arenas in plugins. For example you could get them to set the selection where the arena is and then use a command to create it


    Plugins using the Selection Api So Far
    Pushball - <font color="#ff6600">play a game of soccer on your server</font>

    Get your plugin here! just start using the selection api and then pm me or post below the name of your plugin.
    [​IMG]
    <Edit by Moderator: Redacted mediafire url>
    (<Edit by Moderator: Redacted mediafire url>
    <Edit by Moderator: Redacted mediafire url>
    (<Edit by Moderator: Redacted mediafire url>

    <font color="#000000">The folder is just an un-packed jar file. The source can be found below.</font>

    [​IMG]
    The source for this project is available on Github.

    [​IMG]
    just copy the following the code into your signature or anywhere else to support the selection api!

    For Bukkit Forums Sigs:
    Code:
    [URL='http://forums.bukkit.org/threads/api-selection-api-no-server-side-plugins-required.129643/'][IMG]http://imageshack.us/a/img203/497/selectionapilogoplainsm.png[/IMG][/URL]
    For BukkitDev sigs:
    Code:
    [[http://forums.bukkit.org/threads/api-selection-api-no-server-side-plugins-required.129643/|{{http://imageshack.us/a/img203/497/selectionapilogoplainsm.png|Selection Api}}]]
    the code above will put the following in your signature
    [​IMG]


    Please post any errors or suggestions below and i will look at them asap.
     
    Last edited by a moderator: Nov 7, 2016
    Hoolean, MrMag518, devilquak and 5 others like this.
  2. Offline

    -_Husky_-

  3. Offline

    SoThatsIt

    thanks :D i might in the future work on a fix for the double message sending
     
  4. Offline

    Deleted user

    Very nice.

    Can I see an example of this being used to protect a location.
     
  5. Offline

    SoThatsIt

    i currently do not have any examples of this being used but if someone has used please tell me what your plugin is and i will add a section with plugins using this api. I am currently implementing this into my plugin.

    Added a link so that you can download the un-packaged version of the jar. This can be useful if you want to add it directly into your project instead of as a library.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  6. Offline

    SoThatsIt

    V1.4 is out. It is nothing major just a lot of little bug fixes.
     
  7. Offline

    SoThatsIt

    hey guys i was bored, so i made this! tell me what you think.
    [​IMG]
    and also this.
    [​IMG]
     
  8. Offline

    -_Husky_-

    SoThatsIt looks nice, text looks a tad weird, other than that, not bad!
     
  9. Offline

    SoThatsIt

    yeah its pixelated, like minecraft. anyway thanks just added a banner as well
     
  10. Offline

    Deleted user

    SoThatsIt

    This API doesn't even work correctly.

    I've setup my project according to the thread and during my testings it seems that PlayerSelectPointEvent is never fired.

    Code:
    private void setupSelector() {
    SSSelectorHandler.getInstance().selector = new Selector(this, 280);
    SSSelectorHandler.getInstance().selector.registerSelectionListener(new SSSelectorHandler());
    }
    

    Code:
    public class SSSelectorHandler implements SelectionListener {
     
    public static SSSelectorHandler instance = new SSSelectorHandler();
     
    public static SSSelectorHandler getInstance() {
    return instance;
    }
     
    public Selector selector;
     
    @Override
    public void onPlayerSelectPoint(PlayerSelectPointEvent event) {
    Player player = event.getPlayer();
    Selection selection = selector.getSelection("280");
     
    if (selection.setLocs()) {
    // insert code if the player has selected both points here
    } else {
    // insert code if the player hasn't selected both points here
    }
     
    String loc = " (X: " + player.getLocation().getBlockX() + ", Y: " + player.getLocation().getBlockY() + ", Z: " + player.getLocation().getBlockZ() + ")";
    player.sendMessage(SSSettings.getInstance().title + ChatColor.GRAY + "Set point." + loc);
    }
    }
    
    When using a stick nothing happens.
     
  11. Offline

    SoThatsIt

    your
    Your not using the selector.getSelection() correctly. You need to use the players name. For example
    Code:
    Selection selection = selector.get(player.getName());
    so unless your players name is 280, which i don't think it is because thats the id of a stick, that should work however you can just get the selection from the event like so.
    Code:
    event.getSelection();
    so i do not know why you are using the selector to get the selection Unless you need to get a selection that is not being edited by the event. Also have you checked if it was firing a nullpointer exception from using selection.setLocs() on a null object.

    also you set the selector id when you created the selector.
    Code:
    Selector selecor = new Selector(this, 280);
    280 is the item code of a stick.
     
  12. Offline

    Debels

    I was thinking of using World Edit for my "selection needs", but it was to much.

    This looks perfect, Can I link this on my plugins Credits (dev.bukkit.org server mod page)?

    SoThatsIt:

    I can't seem to install it correctly, I putted the jar file in the scr file on eclipse, then added it as library and it didn't work. What am I doing wrong?

    edit: Ok, I just included the source file and it works.
    edit 2: Ok, It seems the PlayerSelectPointEvent is never fired, I made this simple test and it doesn't work....

    Code:
    package test;
     
    import me.sothatsit.selection.api.Selector;
    import me.sothatsit.selection.api.events.PlayerSelectPointEvent;
    import me.sothatsit.selection.api.events.SelectionListener;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class tp_main extends JavaPlugin implements Listener, SelectionListener{
        Selector selector;
        @Override
        public void onEnable(){
            getServer().getPluginManager().registerEvents(this, this);
            selector = new Selector(this, 280);
            selector.registerSelectionListener(new tp_main());
        }
        @Override
        public void onPlayerSelectPoint(PlayerSelectPointEvent evt) {
            getLogger().info("test");
        }
    }
    
    edit 3: You should add a basic example just showing how to send a message when the users clicks a block, since its really confusing...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  13. Offline

    keto23

    The problem is that, the selector class itself, is a listener, however it is never getting registered so the interact event inside it is never called, and the PlayerSelectPointEvent is never fired. I have made a pull request (https://github.com/Sothatsit/SelectionApi/pull/1) on the github source, which fixes this issue, aswell as many other bugs and updated it to use Bukkit's built in event system.
     
  14. Offline

    Deleted user

    I thought it was strange the argument called from a String instead of an integer. However even with this changed the API is still broken.

    Code:
    @Override
    public void onPlayerSelectPoint(PlayerSelectPointEvent event) {
    SSMain.getInstance().log.info("Debug");
     
    Player player = event.getPlayer();
    Selection selection = selector.getSelection(player.getName());
     
    if (selection.setLocs()) {
    // insert code if the player has selected both points here
    } else {
    // insert code if the player hasn't selected both points here
    }
     
    String loc = " (X: " + player.getLocation().getBlockX() + ", Y: " + player.getLocation().getBlockY() + ", Z: " + player.getLocation().getBlockZ() + ")";
    player.sendMessage(SSSettings.getInstance().title + ChatColor.GRAY + "Set point." + loc);
    }
    
    The reason why I say the even is never fired is because the debug message is never sent upon a player selecting a point.

    My class is setup correctly:

    Code:
    public class SSSelectorHandler implements SelectionListener {
    
    and the selector is initialized correctly

    Code:
    private void setupSelector() {
    SSSelectorHandler.getInstance().selector = new Selector(this, 280);
    SSSelectorHandler.getInstance().selector.registerSelectionListener(new SSSelectorHandler());
    SSMain.getInstance().log.info("[SimpleSpleef] Selector setup successfully.");
    }
    
    What could I possibly be doing wrong?
     
  15. Offline

    SoThatsIt

    Sorry guys for the late reply. I now realised i have made many mistakes while making this. Special thanks to keto23 for updating this project on github.

    Debels
    It should automatically send a message when you select a block that you can set using a method in the selector and you can definitely credit me and use this of course. I will also put some example code above as well.

    I thank everyone for their support of this, i am not going to give an excuse for not fixing this sooner because i could have but instead have been doing other things.

    ~sothatsit

    I have now uploaded the updated version 1.4! Special thanks to keto23 for this update, without him/her this update would definitely not be out this soon.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  16. Offline

    Debels

    [quote uid=72296 name="SoThatsIt" post=1585946]I have now uploaded the updated version 1.4! Special thanks to keto23 for this update, without him/her this update would definitely not be out this soon.[/quote]
    You made a mistake in the link to 1.4 jar file, you putted: www.http//www.mediafire.com/download.php?dv7ep9tq3pvihub when it is <Edit by Moderator: Redacted mediafire url>
     
    Last edited by a moderator: Mar 7, 2017
  17. Offline

    SoThatsIt

    Last edited by a moderator: Mar 7, 2017
  18. Offline

    Debels

    Just found yet another bug:

    - When you click on a block it executes the command 3 times or at least sends the message 3 times
    - areBothPointsSet() isn't returning true even with the above bug....

    Suggestions:

    - Prevent blocks from being destroyed when you click on them.
     
  19. Offline

    SoThatsIt

    thanks for pointing this out. i will be releasing yet another version soon fixing and adding a few things.
     
  20. Offline

    Debels

    How soon would it be?, If it isn't very soon I can add them my self and upload it to the plugins github.
     
  21. Offline

    SoThatsIt

    im working on it now and will be able to upload what i have done to github. You could then download it from there but i won't be able to completely finish it i dont think.

    I am also adding a few things because with the new version the selections arent saveable so im adding a saveable class that you can use to save.

    i've now updated it to github, ive got to go now but that update should fix everything.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  22. Offline

    Debels

    Ok, awesome.

    You fixed it faster than I expected

    Ok, from sending the message 3 times now we went to 4 times...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  23. Offline

    SoThatsIt

    damn, i'll try figure out why thats happening
     
  24. Offline

    Debels

    Ok, fixed that bug, I'm going to upload to github.

    edit: Nvm, idk how to do that xD just going to put a link to the fixed Selector.java

    edit 2: Remember to end your switch cases with "break" unless there are 1 line long. That was what the multiple messages problem was about.

    edit 3: you should probably update a bit the thread since some methods there aren't in 1.4
     
  25. Offline

    Debels

    When is a new update comming?
     
  26. Offline

    SoThatsIt

    Version 1.5 Is out now, sorry for it being so late, i have had a lot on my plate lately.

    + added colliding(Selection s) method in class selection
    + added inSelection(Selection s) method in class selection
    + added inSelection(Location loc) method in class selection
    * fixed double message sending
    * fixed areBothPointsSet
     
  27. Offline

    Debels

    A rather late update, I already made my own version of Selection API...
     
Thread Status:
Not open for further replies.

Share This Page