getting cuboid loc

Discussion in 'Plugin Development' started by TerroDoor, Mar 18, 2020.

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

    TerroDoor

    hey ive created this cuboid which is working well, how can I get the cuboid location to check if players are inside/outside. I don't want to save the location I just want to be able to 'get' it. How can I do this?\

    Code:
     public void setCube(Location loc) {
     ConfigurationSection cs = pl.getConfig().getConfigurationSection("spawn-loc");
     World w = loc.getWorld();
     int size = 3; 
     int height = 10;
     
     int minx = loc.getBlockX() - size;
     int maxx = loc.getBlockX() + size;
     
     int miny = loc.getBlockY() - 1;
     int maxy = loc.getBlockY() + height;
     
     int minz = loc.getBlockZ() - size;
     int maxz = loc.getBlockZ() + size;
     
     for (int x = minx; x <= maxx; x++) {
     for (int y = miny; y <= maxy; y++) {
     for (int z = minz; z <= maxz; z++) {
     
     Location area = new Location(w, x, y, z);
     
     if (y == loc.getBlockY() - 1) {
     
     area.getBlock().setType(Material.BLACK_STAINED_GLASS);
     
     } else {
     
     area.getBlock().setType(Material.BLUE_STAINED_GLASS);
     
     }
    
     
  2. Online

    CraftCreeper6

    @TerroDoor
    Just get their current position and check if it's within the cube.

    To check if it's within the cube you can crosscheck the min and max x, y and z with the players current coordinates. If they are within the margin then they are inside the cube.
     
  3. Offline

    TerroDoor

    So if I created a Boolean check inside the same class, how do I get the location of the cube?

    @CraftCreeper6

    update: previously I was saving the loc to a config in order to grab my cubes center point, however it seems unnecessary. Can I save the loc into a list and get it that way?
    Sent from my iPhone using Tapatalk
     
    Last edited: Mar 18, 2020
  4. Online

    CraftCreeper6

    @TerroDoor
    Yes, a Boolean method should be fine.
    If you have a set point that doesn't need to be saved then just store it in a variable, however, if you plan on making it so other people are able to set a spawn, you'll need a config to save their choice.
     
  5. Offline

    TerroDoor

    @CraftCreeper6

    Thanks for putting me in the right direction mate, appreciate it.

    well, I have it so OP's can /setspawn to set the cubes location anywhere in the map, so I don't know the XYZ until the cuboid is created, how can I save the location and get it for my Boolean check?

    here's my '/setspawn' cmd class;

    Code:
     public Main pl;
     public Setspawn(Main ins) {
     pl = ins;
     }
     public boolean onCommand(CommandSender s, Command cmd, String commandLabel, String[] args) {
     Player p = (Player)s;
     if (cmd.getName().equalsIgnoreCase("setspawn")) {
     if (args.length > 0) {
     p.sendMessage("unknown command");
     return true;
     }
     pl.cube.setCube(p.getLocation());
     p.sendMessage("spawn cube set");
     return true;
     }
    
    
     
  6. Offline

    Tango_

    How I would do it

    Create a method that returns all locations in a cube. (the for x, for y, for z)
    Then you can use that method to create a cube: for(Location loc : Locationlist) loc.getBlock.setType(Bedrock)
    Then you save the one location used to create the cube, in a list or config or what ever.
    You use that location to and pass it into the all locations method, and check all locations if the X,Y,Z is a match (boolean method maybe)
     
  7. Online

    CraftCreeper6

    @TerroDoor
    After some more Google, I came across a Vector method.

    You can check if a player is within a certain area using this.

    It checks if a player is within AABB cuboid, first argument is min location, second argument is max location. Give it a try.

    NOTE: Ensure it is the TRUE min and max! Using Math.min and Math.max to get these values.
     
    TerroDoor and Tango_ like this.
  8. Offline

    TerroDoor

    Thanks but I’m stuck with the getting part, I can only access my location that holds the cube inside of my method that initially creates the cube


    Sent from my iPhone using Tapatalk

    @Tango_ @CraftCreeper6
    This isn't working guys

    heres the class

    Code:
    public class Cube {
     public Main pl;
     public Cube(Main ins) {
     pl = ins;
     }
    
     public boolean inCube(Player p) {
     
     ConfigurationSection cs = pl.getConfig().getConfigurationSection("spawn-loc");
     
     int px = p.getLocation().getBlockX();
     int py = p.getLocation().getBlockY();
     int pz = p.getLocation().getBlockZ();
     
     int minx = cs.getInt("minx");
     int maxx = cs.getInt("maxx");
     int miny = cs.getInt("miny");
     int maxy = cs.getInt("maxy");
     int minz = cs.getInt("minz");
     int maxz = cs.getInt("maxz");
     
     if  (minx <= px && maxx >= px && miny <= py && maxy >= py && minz <= pz && maxz >= pz) {
     
     return true;
    
     }
     
     return false;
     }
    
     public void setCube(Location loc) {
     ConfigurationSection cs = pl.getConfig().getConfigurationSection("spawn-loc");
     World w = loc.getWorld();
     int size = 3; 
     int height = 10;
     int minx = loc.getBlockX() - size;
     int maxx = loc.getBlockX() + size;
     int miny = loc.getBlockY() - 1;
     int maxy = loc.getBlockY() + height;
     int minz = loc.getBlockZ() - size;
     int maxz = loc.getBlockZ() + size;
     for (int x = minx; x <= maxx; x++) {
     for (int y = miny; y <= maxy; y++) {
     for (int z = minz; z <= maxz; z++) {
    
     Location area = new Location(w, x, y, z);
     
    if (y == loc.getBlockY() - 1) {
     area.getBlock().setType(Material.BLACK_STAINED_GLASS);
     } else {
     area.getBlock().setType(Material.BLUE_STAINED_GLASS);
     }
     
    if (cs == null) {
     cs = pl.getConfig().createSection("spawn-loc");
     }
    
     cs.set("world", w.getName());
     pl.getConfig().set("minx", minx);
     pl.getConfig().set("maxx", maxx);
     pl.getConfig().set("miny", miny);
     pl.getConfig().set("maxy", maxy);
     pl.getConfig().set("minz", minz);
     pl.getConfig().set("maxz", maxz);
     pl.saveConfig();
     }
     }
     }
     return;
     }
    }
    
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 18, 2020
  9. Online

    CraftCreeper6

    @TerroDoor
    You never used Math.min and Math.max like I suggested in my previous post. It's absolutely necessary for the if statement to pass.
     
  10. Offline

    TerroDoor

    So without this it won’t work?


    Sent from my iPhone using Tapatalk
     
  11. Online

    timtower Moderator Moderator

  12. Offline

    TerroDoor

    @CraftCreeper6 @timtower @Tango_

    I'm back after a bit of researching guys and I found a way of temp saving my block location...
    I create a new Block Object of my 'area' location to store into a List of Blocks.
    I can now access my list of blocks in a Boolean method to return true when the player steps in or out.

    Here's what I've come up with, might not be efficient but it's doing the job:

    Code:
    public ArrayList<Block> blocks = new ArrayList<Block>();
    
    Storing the cube:
    Code:
    for (int x = minx; x <= maxx; x++) {
    for (int y = miny; y <= maxy; y++) {
    for (int z = minz; z <= maxz; z++) {
    
    Location area = new Location(w, x, y, z);
    Block b = area.getBlock();
    
    if (y == loc.getBlockY() - 1) {
    area.getBlock().setType(Material.BLACK_STAINED_GLASS);
    }
    
    blocks.add(b);
    }
    
    My Boolean method(used in playermoveevent but I think this uses too much memory lmk):

    Code:
    public boolean inCube(Player p) {
    
    Location ploc = p.getLocation();
    Block b = ploc.getBlock();
    
    if (blocks.contains(b)) {
    return true;
    } else {
    return false;
    }
    }
    
    EDIT:

    I forgot to mention. I can't seem to clear my list incase the OP has created more than one spawn. I want to erase any existing data from the list before I add the cuboid to the list, a null check doesn't seem to work so im stuck, any ideas @timtower
     
  13. Online

    timtower Moderator Moderator

    @TerroDoor Yeah, a contains function that does not loop over all the blocks.
    One that is based on math, together with a cuboid storage instead of a single center spot.
     
  14. Offline

    TerroDoor

    @timtower I don't quite understand, could you please help me gain more understanding of why it's not reading .contains

    Should I be creating another loop of the ploc inside of my Boolean method? instead of just checking the ploc Block
     
  15. Online

    timtower Moderator Moderator

    @TerroDoor You need to restrucure your system.
    Start with an area class. One that has 2 corner points. And a contains function.
    No need to fill the functions yet.
     
  16. Offline

    TerroDoor

    But I don’t have the two corner points until the cube is created?


    Sent from my iPhone using Tapatalk
     
  17. Online

    timtower Moderator Moderator

    Then do that when you save the point.
    You take the position as center, then you turn it into 2 points, minimum and maximum corners.
     
  18. Offline

    TerroDoor

    so once the blocks are added to my list, is it not in cuboid form? or do I have to get the min/max from my centerloc or from my list?
    Sorry im having trouble understanding, could I be shown where in my code im going wrong or what I should be adding/changing instead?

    EDIT:
    @timtower
    something like this?

    Code:
     public boolean inCube(Location loc) {
     World w = loc.getWorld();
     
     int minx = loc.getBlockX() - 3;
     int miny = loc.getBlockY() - 1;
     int minz = loc.getBlockZ() - 3;
     int maxx = loc.getBlockX() + 3;
     int maxy = loc.getBlockY() + 10;
     int maxz = loc.getBlockZ() + 3;
     
     Location loc1 = new Location(w, minx, miny, minz);
     Location loc2 = new Location(w, maxx, maxy, maxz);
    
    
     
    Last edited: Mar 22, 2020
  19. Online

    timtower Moderator Moderator

  20. Offline

    TerroDoor

    @timtower
    empty class? I have the createcube and Boolean check in the one class

    the class:
    Code:
    public class Cube {
    
    public Main pl;
    public Cube(Main ins) {
    pl = ins;
    }
    
    public ArrayList<Block> blocks = new ArrayList<Block>();
    
    public boolean inCube(Location loc) {
    World w = loc.getWorld();
    
    int x = loc.getBlockX();
    int y = loc.getBlockY();
    int z = loc.getBlockZ();
    
    int minx = loc.getBlockX() - 3;
    int miny = loc.getBlockY() - 1;
    int minz = loc.getBlockZ() - 3;
    int maxx = loc.getBlockX() + 3;
    int maxy = loc.getBlockY() + 10;
    int maxz = loc.getBlockZ() + 3;
    
    Location loc1 = new Location(w, minx, miny, minz);
    Location loc2 = new Location(w, maxx, maxy, maxz);
    
    return false;
    }
    
    public void setCube(Location loc) {
    
    ConfigurationSection cs = pl.getConfig().getConfigurationSection("spawn-loc");
    
    World w = loc.getWorld();
    int size = 3;
    int height = 10;
    
    int minx = loc.getBlockX() - size;
    int maxx = loc.getBlockX() + size;
    int miny = loc.getBlockY() - 1;
    int maxy = loc.getBlockY() + height;
    int minz = loc.getBlockZ() - size;
    int maxz = loc.getBlockZ() + size;
    
    for (int x = minx; x <= maxx; x++) {
    for (int y = miny; y <= maxy; y++) {
    for (int z = minz; z <= maxz; z++) {
    
    Location area = new Location(w, x, y, z);
    Block b = area.getBlock();
    
    if (y == loc.getBlockY() - 1) {
    area.getBlock().setType(Material.BLACK_STAINED_GLASS);
    
    }
    
    blocks.add(b);
    }
    }
    }
    return;
    }
    }
    listener class:

    Code:
     public Main pl;
     public Protection(Main ins) {
     pl = ins;
     }
     @EventHandler
     public void onProtect(PlayerMoveEvent e) {
     Player p = (Player)e.getPlayer();
     
     if (pl.cube.inCube(p.getLocation())) {
     
     p.sendMessage("welcome");
     
     return;
     }
     
     return;
     }
    }
    
     
  21. Online

    timtower Moderator Moderator

    @TerroDoor I am talking about this class:
     
  22. Offline

    TerroDoor

    @timtower
    Am I following right?

    Code:
    public class Area {
     public Main pl;
     
     public Area(Main ins) {
     
     pl = ins;
     }
     
     public void myFunction(Location corner1, Location corner2) {
     
     return;
     }
     
  23. Online

    timtower Moderator Moderator

    @TerroDoor No, this is an area. So one of the area's to check.
    Not a utility class.
    It gets 2 locations in the constructor.
    Contains function that has a single location.
     
  24. Offline

    TerroDoor

    @timtower

    I feel like you pointed me in the right direction Tim!
    How's this looking

    Code:
    World world;
    
    double maxx;
    double maxy;
    double maxz;
    double minx;
    double miny;
    double minz;
    int size;
    int height;
    public void setCube(Location loc) {
    ConfigurationSection cs = pl.getConfig().getConfigurationSection("spawn-loc");
    
    world = loc.getWorld();
    size = 5;
    height = 5;
    maxx = Math.max(loc.getX(), loc.getX() + size);
    maxy = Math.max(loc.getY(), loc.getY() + size);
    maxz = Math.max(loc.getZ(), loc.getZ() + size);
    minx = Math.min(loc.getX(), loc.getX() - size);
    miny = Math.min(loc.getY(), loc.getY() - 1);
    minz = Math.min(loc.getZ(), loc.getZ() - size);
    for (double x = minx; x <= maxx; x++) {
    for (double y = miny; y <= maxy; y++) {
    for (double z = minz; z <= maxz; z++) {
    Location area = new Location(world, x, y, z);
    if (y == miny) {
    area.getBlock().setType(Material.BLACK_STAINED_GLASS);
    }
    }
    }
    }
    return;
    }
    EDIT:

    I was going to use a constructor for this but i'm still trying to understand those.
     
Thread Status:
Not open for further replies.

Share This Page