CapturePoint plugin help

Discussion in 'Spigot Plugin Development' started by EndureBlackouT, Aug 24, 2016.

  1. So, I am create a capture point plugin and am looking for a way to check if a player is in the region, which is stored in the file regions.yml. here is the code I have right now for the PlayerListener.Java
    Code (Text):
    package me.endureblackout.capturepoint;

    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;

    public class PlayerListener implements Listener {
        private CapturePointMain capturepointmain;
       
        public PlayerListener(CapturePointMain capturepointmain) {
            this.capturepointmain = capturepointmain;
        }
       
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
            File regionsFile = new File(Bukkit.getServer().getPluginManager().getPlugin("CapturePoint").getDataFolder(), "regions.yml");
            Player p = e.getPlayer();
           
            if(regionsFile.exists()) {
                YamlConfiguration y = new YamlConfiguration();
                try {
                    y.load(regionsFile);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
               
    /*            List<String> regions = new ArrayList<>();
                for(String key : y.getConfigurationSection("Regions").getKeys(false)) {
                    regions.add(key);
                } */
               
                for(int i = 0; i < y.getConfigurationSection("Regions").getKeys(false).size(); i++) {
                    if(p.getLocation().getBlockX() >= y.getInt("Regions." + i + ".Selection.Min.x")
                            && p.getLocation().getBlockX() <= y.getInt("Regions." + i + ".Selection.Max.x")
                            && p.getLocation().getBlockY() >= y.getInt("Regions." + i + ".Selection.Min.y")
                            && p.getLocation().getBlockY() <= y.getInt("Regions." + i + ".Selection.Max.y")
                            && p.getLocation().getBlockZ() >= y.getInt("Regions." + i + ".Selection.Max.z")
                            && p.getLocation().getBlockZ() <= y.getInt("Regions." + i + ".Selection.Min.z")) {
                        //Do what ever here
                        p.sendMessage(ChatColor.GREEN + "Capturing the region!");
                    }
                }
            }
        }
    }
     
    Here is the regions.yml
    Code (Text):
    Regions:
      '69':
        Selection:
          Min:
            ==: org.bukkit.Location
            world: prisonworld
            x: 240.0
            y: 67.0
            z: 288.0
            pitch: 0.0
            yaw: 0.0
          Max:
            ==: org.bukkit.Location
            world: prisonworld
            x: 245.0
            y: 72.0
            z: 293.0
            pitch: 0.0
            yaw: 0.0
     
     
  2. They all show world guard regions not custom stored regions...
     
  3. Check if player locatin is within those two points ;l
     
  4. But I am going to have multiple points which means I need to loop through all of them... which isn't working out here is what I have.
    Code (Text):
    package me.endureblackout.capturepoint;

    import java.io.File;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;

    public class PlayerListener implements Listener {
        private CapturePointMain capturepointmain;
       
        public PlayerListener(CapturePointMain capturepointmain) {
            this.capturepointmain = capturepointmain;
        }
       
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
            File regionsFile = new File(Bukkit.getServer().getPluginManager().getPlugin("CapturePoint").getDataFolder(), "regions.yml");
            Player p = e.getPlayer();
           
            if(regionsFile.exists()) {
                YamlConfiguration y = new YamlConfiguration();
                try {
                    y.load(regionsFile);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
               

               
                List<String> regions = new ArrayList<>();
                for(String key : y.getConfigurationSection("Regions").getKeys(false)) {
                    regions.add(key);
                }
               
            for(int i = 0; i < regions.size(); i++) {
                if(p.getLocation().getBlockX() >= y.getInt("Regions." + i + ".Selection.Min.x")
                    && p.getLocation().getBlockX() <= y.getInt("Regions." + i + ".Selection.Max.x")
                    && p.getLocation().getBlockY() >= y.getInt("Regions." + i + ".Selection.Min.y")
                    && p.getLocation().getBlockY() <= y.getInt("Regions." + i + ".Selection.Max.y")
                    && p.getLocation().getBlockZ() >= y.getInt("Regions." + i + ".Selection.Max.z")
                    && p.getLocation().getBlockZ() <= y.getInt("Regions." + i + ".Selection.Min.z")) {
                        //Do what ever here
                        p.sendMessage(ChatColor.GREEN + "Capturing the region!");
                }
            }
            }
        }
    }
     
     
  5. Is there an error or do you need help creating something? I am confused :p
     
  6. Look at most recent post :)
     
  7. Question, is 'Capture Point' your plugin?
     
  8. Yes it is... I started creating a while ago and could never get this


    Sent from my iPhone using Tapatalk
     
  9. In that case, this is an extremely redundant line of code:

    Code (Text):
            File regionsFile = new File(Bukkit.getServer().getPluginManager().getPlugin("CapturePoint").getDataFolder(), "regions.yml");
     
    Use rather the built-in Bukkit method to get the file's input stream:

    Code (Text):
    pluginInstance.getResource("regions.yml");
    And then do your stuff.
     
  10. Are you trying to check if they are in a 2d region or 3d, and what do you mean by multiple?
     
  11. @Amanthemoneyman
    I use worldedit to set the region then use the command /create <region name> then it puts it into the file regions.yml then I put it into a list and try to loop through it to see if the player is inside the region (which is a cube). so there will be able to be multiple different regions.