saving locations to list and retrieving

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

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

    TerroDoor

    Helloo, I'm no professional so im back here for some help. I've created a cube inside a method that executes when the player types /setspawn.

    I've put the cubes location into a list, and attempted to save to a config file but it's putting every block's cord's in which Im not sure is correct.

    im also trying to access the list of locations to create another method for disabling PVP in this cuboid area but it's not working..

    How can I correctly store the cubes location and access it through other methods?

    Here's my code;

    Code:
    
     public ArrayList<Location> blocks = new ArrayList<Location>();
     
     public void generateCube(Location loc) {
     World w = loc.getWorld();
     int radius = 3;
     for (int x = loc.getBlockX() - radius; x <= loc.getBlockX() + radius; x++) {
     for (int y = loc.getBlockY() - 1; y <= loc.getBlockY() + 11; y++) {
     for (int z = loc.getBlockZ() - radius; z <= loc.getBlockZ() + radius; z++) {
     Location area = new Location(w, x, y, z);
     
     blocks.add(area);
     
     pl.getConfig().set("loc", blocks);
     pl.saveConfig();
     
     if (y == loc.getBlockY() - 1) {
     area.getBlock().setType(Material.BEDROCK);
     if (x <= loc.getBlockX() - radius || 
     x <= loc.getBlockX() + radius || 
     z <= loc.getBlockZ() - radius || 
     z <= loc.getBlockZ() + radius) {
     area.getBlock().setType(Material.GLOWSTONE);
     }
     } else {
     area.getBlock().setType(Material.AIR);
     }
     }
     }
     }
     return;
     }
    }
    
    
    Here's the config list:

    Code:
    
    loc:
    - ==: org.bukkit.Location
      world: world
      x: 40.0
      y: 133.0
      z: 81.0
      pitch: 0.0
      yaw: 0.0
    - ==: org.bukkit.Location
      world: world
      x: 40.0
      y: 133.0
      z: 82.0
      pitch: 0.0
      yaw: 0.0
    - ==: org.bukkit.Location
      world: world
      x: 40.0
      y: 133.0
      z: 83.0
      pitch: 0.0
      yaw: 0.0
    - ==: org.bukkit.Location
      world: world
      x: 40.0
      y: 133.0
      z: 84.0
      pitch: 0.0
      yaw: 0.0
    - ==: org.bukkit.Location
      world: world
      x: 40.0
      y: 133.0
      z: 85.0
      pitch: 0.0
      yaw: 0.0
    - ==: org.bukkit.Location
      world: world
      x: 40.0
      y: 133.0
      z: 86.0
      pitch: 0.0
      yaw: 0.0
    - ==: org.bukkit.Location
      world: world
      x: 40.0
      y: 133.0
      z: 87.0
      pitch: 0.0
      yaw: 0.0
    - ==: org.bukkit.Location
      world: world
      x: 40.0
      y: 134.0
      z: 81.0
      pitch: 0.0
      yaw: 0.0
    - ==: org.bukkit.Location
      world: world
      x: 40.0
      y: 134.0
      z: 82.0
      pitch: 0.0
      yaw: 0.0
    - ==: org.bukkit.Location
      world: world
      x: 40.0
      y: 134.0
      z: 83.0
      pitch: 0.0
      yaw: 0.0
    
    ^ I shortened the list there's tons more locations saved
     
  2. Offline

    CraftCreeper6

    @TerroDoor
    You can't directly store a Location in bukkit, you have to serialize it yourself.
     
  3. Offline

    TerroDoor

    Thanks for the heads up, I’ll look into serialisation. While I’ve got you here are you able to give me a quick walkthrough? I want to make the whole location accessible


    Sent from my iPhone using Tapatalk
     
  4. Offline

    CraftCreeper6

    @TerroDoor
    You should create a method called serializeLocation (or something like that) and let it return a String, and take in a Location as a parameter.

    The string it returns should be the serialized version of the Location, for example,

    Code:
    world,X,Y,Z
    Then, instead of saving the Locations to the config, create a new List of String's that hold the serialized locations, and save those instead.
     
    TerroDoor likes this.
  5. Offline

    TerroDoor

    @CraftCreeper6

    That’s making more sense to me, thanks for your reply I appreciate your help.

    If I created a method to check if the player is in this location,
    how do I get the location from my createCube method?


    Sent from my iPhone using Tapatalk
     
  6. Offline

    CraftCreeper6

  7. Offline

    TerroDoor

    its attached to my first post
     
  8. Offline

    CraftCreeper6

    @TerroDoor
    That's generateCube. But I'm assuming now that's what you meant.

    As for getting the location again, you just have to deserialize it.

    Take in each string 1 by 1 and just do the opposite of what you did to serialize it, say you had this as your serialized string:
    Code:
    world,X,Y,Z
    Then to deserialize, you create a new Location, initialize it with the world,x,y,z constructor, and pass each value in.
     
  9. Offline

    TerroDoor

    @CraftCreeper6

    thanks for the tips, I think im going in the right direction based off how it's looking now in the config, although, its print only one x,y,z coord, is it only saving the center location? if so, will it affect my cube in-game?

    Here's how it's looking:

    Code:
    
    public void setCube(Location loc) {
    ConfigurationSection cs = pl.getConfig().getConfigurationSection("loc");
    World w = loc.getWorld();
    int radius = 3;
    for (int x = loc.getBlockX() - radius; x <= loc.getBlockX() + radius; x++) {
    for (int y = loc.getBlockY() - 1; y <= loc.getBlockY() + 11; y++) {
    for (int z = loc.getBlockZ() - radius; z <= loc.getBlockZ() + radius; z++) {
    Location area = new Location(w, x, y, z);
    if (y == loc.getBlockY() - 1) {
    area.getBlock().setType(Material.BEDROCK);
    } else {
    area.getBlock().setType(Material.AIR);
    }
    if (cs == null) {
    cs = pl.getConfig().createSection("loc");
    }
    cs.set("world", w.getName());
    pl.getConfig().set("x", x);
    pl.getConfig().set("y", y);
    pl.getConfig().set("z", z);
    pl.saveConfig();
    }
    }
    }
    
    
    The new Config.yml:

    Code:
    loc:
      world: world
    x: 87
    y: 103
    z: 137
    
    
    @timtower Hey Tim, im back. Any suggestions pal?
     
    Last edited: Mar 17, 2020
  10. Offline

    timtower Administrator Administrator Moderator

  11. Offline

    TerroDoor

    Thanks, how do I use an index?

    Also, I think im just saving the center location of my cuboid which is fine, but Im stuck with accessing my cube region that I created in my method, how would I go about getting the location to use it for checking if players enter/leave the area
     
  12. Offline

    timtower Administrator Administrator Moderator

    @TerroDoor Now you just set x, you need to set <number>.x
    The number is just to get unique values.
     
  13. Offline

    TerroDoor

    @timtower

    okay ive tried understanding all this but maybe this will make sense,
    so far the platform/cube gets built with the above method and saves the cords to a file
    but now, checking if the player enters has confused me..

    heres the code:

    Code:
    
     public ArrayList<Location> blocks = new ArrayList<Location>();
     public boolean inCube(Player p) {
     
     ConfigurationSection cs = pl.getConfig().getConfigurationSection("loc");
     
     World w = pl.getServer().getWorld("world");
     double x = cs.getDouble("x");
     double y = cs.getDouble("y");
     double z = cs.getDouble("z");
     Location spawn = new Location(w, x, y, z);
     
     if (p.getLocation().equals(spawn)) {
     
     p.sendMessage("welcome");
     
     return true;
     } else {
     return false;
     }
     }
    
    Sorry if it doesn't make sense its the a.m's here in Australia :)
     
  14. Offline

    timtower Administrator Administrator Moderator

    @TerroDoor Why would you loop over the config if you have a list of blocks?
     
  15. Offline

    TerroDoor

    @timtower how do I correctly check if players location .equals or .contains the block list?

    both the methods I stated wont work
     
  16. Offline

    timtower Administrator Administrator Moderator

    @TerroDoor No, because you need to be ON the spot for that.
    You need to check if between minX and maxX.
    Those values you can get by adding / subtracting the area size from the center.
     
  17. Offline

    TerroDoor

    @timtower
    im following what your saying, so, the area that's saved in my code is the center?

    would I change this line

    Code:
    
     Location spawn = new Location(w, x, y, z);
    
    
    and add a radius?
     
  18. Offline

    timtower Administrator Administrator Moderator

  19. Offline

    TerroDoor

    @timtower
    size*
    how do I achieve checking if the player is in the loc? am I correct
     
  20. Offline

    timtower Administrator Administrator Moderator

    @TerroDoor You are looking for an area, not a single location.
    Area has a min and max.
    Location is a single point.
     
  21. Offline

    TerroDoor

    @timtower

    Code:
    
     public boolean inCube(Player p) {
     ConfigurationSection cs = pl.getConfig().getConfigurationSection("loc");
     World w = p.getWorld();
     int cx = cs.getInt("x");
     int cy = cs.getInt("y");
     int cz = cs.getInt("z");
     int size = 3;
     for (int x = cx - size; x <= cx + size; x++) {
     for (int y = cy - size; y <= cy + 11; y++) {
     for (int z = cz - size; z <= cz + size; z++) {
     Location area = new Location(w, x, y, z);
     if (p.getLocation() == area) {
     
     p.sendMessage("WELCOME");
     
     return true;
     
     } 
     }
     }
     }
     return false;
     }
    
    Along lines of this maybe?
     
  22. Offline

    timtower Administrator Administrator Moderator

    @TerroDoor Why not make a maxX and minX and check if the player is in between?
     
  23. Offline

    TerroDoor

    Using the configuration ints?


    Sent from my iPhone using Tapatalk
     
  24. Offline

    timtower Administrator Administrator Moderator

    @TerroDoor No, by adding the size of the box to the center.
     
  25. Offline

    TerroDoor

    How can I get the center i only have it saved in the config


    Sent from my iPhone using Tapatalk
     
  26. Offline

    timtower Administrator Administrator Moderator

    You are using it now as well...
    Or are cx, cy and cz magic?
     
    TerroDoor likes this.
  27. Offline

    TerroDoor

    @timtower

    Here, how is it looking so far :)

    Code:
    
    public boolean inCube(Player p) {
    ConfigurationSection cs = pl.getConfig().getConfigurationSection("loc");
    World w = p.getWorld();
    int cx = cs.getInt("x");
    int cy = cs.getInt("y");
    int cz = cs.getInt("z");
    int size = 3;
    
    int minx = cx - size;
    int miny = cy - 1;
    int minz = cz - size;
    
    int maxx = cx + size;
    int maxy = cy + 11;
    int maxz = cz + 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 (p.getLocation().equals(area)) {
    
    p.sendMessage("WELCOME");
    
    return true;
    }
    
    also, im confused as to why im storing my area into a list, I haven't used the list yet.
    What can I do with the list?

    here:

    Code:
    public ArrayList<Location> blocks = new ArrayList<Location>();
    
    @timtower I think it has something to do with this part:

    Code:
    if (p.getLocation().equals(area)) {
    
     
    Last edited: Mar 17, 2020
  28. Offline

    wand555

    @TerroDoor Configuration Files are mostly just for saving data when the server is shut down. I feel like you're prefering configs because you can access it from anywhere, but thats not the point. If you struggle to use a list because you don't know where to use it, then start looking into "constructors" where you can pass variables through classes (instances). If you want to do a lot of things with the "location of the cube" you might also want to look into Bukkit's BoundingBox
     
  29. Offline

    TerroDoor

    My problem with the list I’ve created is I do not know how to use it when I check if the player is inside that list(I tried using p.getLoc().contains(myLocList)
    But no luck ,

    Thanks for the help I’ll look into constructors


    Sent from my iPhone using Tapatalk
     
  30. Offline

    wand555

    It's the other way around.
    listLocs.contains(playerLoc)
     
Thread Status:
Not open for further replies.

Share This Page