CapturePoint Plugin

Discussion in 'Plugin Development' started by EndureBlackout, Aug 22, 2016.

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

    EndureBlackout

    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:
    package me.endureblackout.capturepoint;
    
    import java.io.File;
    
    import org.bukkit.Bukkit;
    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;
    
    import com.sk89q.worldedit.regions.CuboidRegion;
    
    public class PlayerListener implements Listener {
        private CapturePointMain capturepointmain;
       
        public PlayerListener(CapturePointMain capturepointmain) {
            this.capturepointmain = capturepointmain;
        }
       
        private CuboidRegion r;
       
        @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();
                }
               
            }
        }
    }
    
    If any more code is need to better understand what I'm trying to do please let me know.
     
  2. Offline

    Zombie_Striker

    This can be simplified to
    Code:
    ... = newFile(capturepointmain.getDataFolder()+"/regions.yml")
    To check if a player is in a region that is in the config, do the following:
    1. Load all the regions from the config, and put them into an Arraylist
    2. For loop through the arraylist.
    3. If the XMin for the region is less than the player's X, and the XMax for the region is greater than the player.
    4. Repeat for Y and Z
    5. If #3 and #4 are true, then the player is in the region. Here you can do what you want.
     
  3. Offline

    EndureBlackout

    @Zombie_Striker is this what you are talking about?
    Code:
                List<String> regions = new ArrayList<>();
                regions.add((String) y.get("Regions"));
               
                for(int i = 0; i < regions.size(); i++) {
                    if(y.getInt("Regions." + i + ".Min" + ".X") == p.getLocation().getBlockX()) {
                        //Do what ever here
                    }
                }
     
  4. Offline

    Zombie_Striker

    @EndureBlackout
    Not really. If you only store the strings to the config, you will need to convert each String into a region before you can use it. After that, you will need to loop through all the regions and do bounding checks; You need to check to make sure they are within the area, not standing exactly at one corner.
     
  5. Offline

    EndureBlackout

  6. Offline

    Zombie_Striker

    @EndureBlackout
    Depends on how you are getting the Regions. If the regions are actual objects in the array, then you will need to do an object loop. If the regions are stored as individual strings in the config, then you would use your for int loop.
     
  7. Offline

    EndureBlackout

    @Zombie_Striker here is how they are stored.
    Code:
    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
    
     
  8. Offline

    Zombie_Striker

    @EndureBlackout
    In this case, your for int loop is fine. For this, do the following:
    1. Use your for int loop. Loop through all the regions in the config.
    2. For each one, create the min location and the max location.
    3. Use bounding checks. See if the minimum X is less than your X, and that the Max X is greater than your X. Repeat for Y and Z.
    4. If the X,Y, and Z are within the region, do what you want and break out of the for loop (since you won't be needing to loop through the rest.)
     
  9. Offline

    EndureBlackout

    @Zombie_Striker
    I am getting an error adding the region sections to the List. here is the code and the error.
    Code:
    package me.endureblackout.capturepoint;
    
    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    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;
    
    import net.md_5.bungee.api.ChatColor;
    
    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<>();
                regions.add((String) y.get("Regions"));
               
                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!");
                    }
                }
            }
        }
    }
    
    Code:
    [22:04:39 ERROR]: Could not pass event PlayerMoveEvent to CapturePoint v1.0.7
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:270) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.PacketPlayInFlying.a(SourceFile:126) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.PacketPlayInFlying$PacketPlayInPosition.a(SourceFile:57) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_60]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_60]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:714) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:653) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:556) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_60]
    Caused by: java.lang.ClassCastException: org.bukkit.configuration.MemorySection cannot be cast to java.lang.String
            at me.endureblackout.capturepoint.PlayerListener.onPlayerMove(PlayerListener.java:36) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_60]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_60]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_60]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_60]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-fdc1440-53fac9f]
            ... 15 more
     
  10. Offline

    Zombie_Striker

    You are casting a memory section to a string:
    Since we no longer need the array, delete these two lines.
     
  11. Offline

    EndureBlackout

    @Zombie_Striker
    Then how would I do the for loop without using and array? it used the regions array

    Bump @Zombie_Striker

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

    EndureBlackout

  13. @EndureBlackout change the net.md_5.bungeeAPI.ChatColor to org.bukkit, Sorry I do not know World/Guard API. Just an error I found in your code.
     
  14. Offline

    ArsenArsen

Thread Status:
Not open for further replies.

Share This Page