how do i make a area

Discussion in 'Plugin Development' started by ahuby09, Dec 28, 2013.

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

    ahuby09

    ive made made a temple run plugin and it has different block effects the problem is it goes throughout the world so I was wondering how to make a area so I can do a check weather there in this certain area and if they are let the effect take place if not do nothing anyone know how I would do this thanks in advanced
     
  2. Offline

    stonar96

    What do you mean with block effects? Please describe your problem better, I don't know exactly what you want to do.

    You can use the API of WorldGuard, then install WorldGuard on the server, and create the regions.

    Or you try to create your own regions with coordinates (2 corners of a cuboid ...). You can create a config file with

    <region name>:
    - xMin
    - yMin
    - zMin
    - xMax
    - yMax
    - zMax
    <region name>:
    - xMin
    - yMin
    - zMin
    - xMax
    - yMax
    - zMax
    ...

    (similar to WorldGuard regions)

    Create a Region Class with the following content:
    Code:java
    1. package ...;
    2.  
    3. import org.bukkit.Location;
    4.  
    5. public class Region {
    6. String name;
    7. double xMin;
    8. double xMax;
    9. double yMin;
    10. double yMax;
    11. double zMin;
    12. double zMax;
    13.  
    14. public Region(String name, double xMin, double xMax, double yMin, double yMax, double zMin, double zMax) {
    15. this.name = name;
    16. this.xMin = xMin;
    17. this.xMax = xMax;
    18. this.yMin = yMin;
    19. this.yMax = yMax;
    20. this.zMin = zMin;
    21. this.zMax = zMax;
    22. }
    23.  
    24. public double getxMin() {
    25. return this.xMin;
    26. }
    27.  
    28. public double getxMax() {
    29. return this.xMax;
    30. }
    31.  
    32. public double getyMin() {
    33. return this.yMin;
    34. }
    35.  
    36. public double getyMax() {
    37. return this.yMax;
    38. }
    39.  
    40. public double getzMin() {
    41. return this.zMin;
    42. }
    43.  
    44. public double getzMax() {
    45. return this.zMax;
    46. }
    47.  
    48. public boolean containsLocation(Location location) {
    49. double x = location.getX();
    50. double y = location.getY();
    51. double z = location.getZ();
    52.  
    53. if (x >= this.xMin && x < this.xMax && y >= this.yMin && y < this.yMax && z >= this.zMin && z < this.zMax) {
    54. return true;
    55. }
    56.  
    57. return false;
    58. }
    59. }
    60.  


    Then create a second class where all your regions are stored like this:
    Code:java
    1. package survivalgamesmainserver;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.Location;
    7.  
    8. public class Regions {
    9. private List<Region> regions = new ArrayList<Region>();
    10.  
    11. public Regions() {
    12. //read the config and create for each region a Region object and add the Region object to the List<Region> regions
    13. //check that each region name is unique
    14. }
    15.  
    16. public Region getRegionByName(String name) {
    17. for (Region region : regions) {
    18. if (region.getName().equalsIgnoreCase(name)) {
    19. return region;
    20. }
    21. }
    22.  
    23. return null;
    24. }
    25.  
    26. public List<Region> getRegionsByLocation(Location location) {
    27. List<Region> regions = new ArrayList<Region>();
    28.  
    29. for (Region region : this.regions) {
    30. if (region.containsLocation(location)) {
    31. regions.add(region);
    32. }
    33. }
    34.  
    35. return regions;
    36. }
    37. }

    (remove the comment with code!)

    Then add this to your main class:
    Code:java
    1. public Regions regionAPI;
    2.  
    3. public void onEnable() {
    4. regionAPI = new Regions();
    5. }


    Now you have created your regions. You can use the following 3 methodes:

    Code:java
    1. List<Region> regionsByLocation = regionAPI.getRegionsByLocation(new Location(getServer().getWorld("world"), 10, 100, 50));
    2.  
    3. Region regionByName = regionAPI.getRegionByName("Region1");
    4.  
    5. Boolean containsLocation = regionByName.containsLocation(new Location(getServer().getWorld("world"), 10, 100, 50));


    Here is an example how to use this code in an eventhandler:
    Code:java
    1. @EventHandler
    2. public void BlockBreak(BlockBreakEvent event) {
    3. List<Region> regions = plugin.regionAPI.getRegionsByLocation(event.getBlock().getLocation());
    4.  
    5. if (regions.contains(plugin.regionAPI.getRegionByName("protect")) || regions.contains(plugin.regionAPI.getRegionByName("spawn"))) {
    6. event.setCancelled(true);
    7. }
    8. }


    The whole code is untested, but it should work, if you have further questions, don't hesitate to ask me.
     
  3. Offline

    ahuby09

    i basically would like to make a area selection like with wand then would like to check if certain blocks are in that area if they are let the effect take place (iv'e already made the effects) and if there not just do nothing
     
Thread Status:
Not open for further replies.

Share This Page