Solved Generate field problem

Discussion in 'Plugin Development' started by Josh014, Dec 29, 2014.

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

    Josh014

    Hello,

    I'm trying to generate a field between two coordinates, my code works but there is a problem I seem not to figure it out... I figured it out that the problem is when I try getting the world.

    My code:
    Code:
        public static void generateField(World world, String event) {
               
            if (event.equalsIgnoreCase("spleef")) {
                       
                int x1 = (int) plugin.getConfig().get("Locations.Spleef.corner1.x");
                int y1 = (int) plugin.getConfig().get("Locations.Spleef.corner1.y");
                int z1 = (int) plugin.getConfig().get("Locations.Spleef.corner1.z");
    
                int x2 = (int) plugin.getConfig().get("Locations.Spleef.corner2.x");
                int y2 = (int) plugin.getConfig().get("Locations.Spleef.corner2.y");
                int z2 = (int) plugin.getConfig().get("Locations.Spleef.corner2.z");
                Bukkit.broadcastMessage(" " + x1 + " " + x2 + " " + y1 + " " + y2 + " " + z1 + " " + z2);
               
                for (int xPoint = x1; xPoint <= x2; xPoint++) {
                    for (int yPoint = y1; yPoint <= y2; yPoint++) {
                        for (int zPoint = z1; zPoint <= z2; zPoint++) {
                            Block currentBlock = world.getBlockAt(xPoint, yPoint, zPoint);
                            currentBlock.setType(Material.SNOW_BLOCK);
                        }
                    }
                }
            }
    }
    What I use to get this method is:

    Code:
    Event.generateField(player.getLocation().getWorld(), send);
    The 'send' is the 'spleef'. Everything worked first but I made a mistake somewhere but I can't find it.
    If someone can find the problem it would be amazing.

    Thanks in advance,
     
  2. Offline

    1Rogue

    So what's the actual issue here?
     
  3. Offline

    Josh014

    @1Rogue Well, it doesn't work somehow.
     
  4. Offline

    1Rogue

    That literally tells me nothing.
     
  5. Offline

    Josh014

    @1Rogue I get the broadcast message, but the blocks can't be placed. There is no error at all.
     
  6. Offline

    1Rogue

    There we are!

    When you say "the blocks can't be placed", do you mean that none of them are changing in accordance to your loop?
     
  7. Offline

    Josh014

    @1Rogue
    Haha sorry, just woke up. Yes, none of them are changing.
     
  8. Offline

    1Rogue

    Try printing to the console the progress of your block changes. See if it stops halfway through. If not, see if you're able to just change a few blocks.
     
  9. Offline

    Josh014

    @1Rogue Alright, give me a few.

    @RoGue Well.. It doesn't even start the first for() loop. I've put a logger.info("") message after the first for loop and it doesn't seem to print it out in the console..

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 31, 2016
  10. Offline

    1Rogue

    What are all their values (as printed out in-game, not in your config)?

    Also you don't need an additional int for those loops:

    Code:java
    1. for (; x1 <= x2; x1++) { //use x1, etc...
     
  11. Offline

    Funergy

  12. Offline

    1Rogue

    For what exactly?
     
  13. Offline

    Josh014

    @Funergy
    Yeah, for what?

    @1Rogue the values after the for loops or before?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 31, 2016
  14. Offline

    1Rogue

    Before, in your broadcast statement.
     
  15. Offline

    Josh014

    @1Rogue
    x1: 177, x2: 166
    y1: 60, y2: 60
    z1: -6, z2:1

    @1Rogue Ah... It works only one specific way... I see now

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 31, 2016
  16. Offline

    1Rogue

    Yup! Just always start from the smaller value (and save the smaller as x1, etc.)
     
  17. Offline

    Josh014

    @1Rogue Is there a quick way to do so? Instead of using a ton of if statements?
     
  18. Offline

    1Rogue

    Well you could always make a ConfigurationSerializable class for it, and then have that class automatically determine the smallest for you.
     
  19. Offline

    Josh014

    @1Rogue is there another way to do so xD?
     
  20. Offline

    1Rogue

    Code:java
    1. public Vector correct(Vector one, Vector two, boolean high) {
    2. if (high) {
    3. return new Vector(
    4. one.getX() > two.getX() ? one.getX() : two.getX(),
    5. one.getY() > two.getY() ? one.getY() : two.getY(),
    6. one.getZ() > two.getZ() ? one.getZ() : two.getZ()
    7. );
    8. } else {
    9. return new Vector(
    10. one.getX() < two.getX() ? one.getX() : two.getX(),
    11. one.getY() < two.getY() ? one.getY() : two.getY(),
    12. one.getZ() < two.getZ() ? one.getZ() : two.getZ()
    13. );
    14. }
    15. }


    Then just save the location vectors to the file and when loading them pass them through accordingly:

    Code:java
    1. Vector one, two = /* your location vectors */;
    2. Vector min = correct(one, two, false);
    3. Vector max = correct(one, two, true);


    And you can work your loop from there, substituting *1 as min, and *2 as max.
     
  21. Offline

    Josh014

    @1Rogue
    I still have the same problem, it only generates a part of it or if I set a corner on a differen location it doesn't do anything. I have probably done something wrong...

    My code to generate the field:
    Code:
    public static void generateField(World world, String event) {
              
            if (event.equalsIgnoreCase("spleef")) {
              
                Vector one = (Vector) plugin.getConfig().get("Locations.Spleef.corner1.Vector");
                Vector two = (Vector) plugin.getConfig().get("Locations.Spleef.corner2.Vector");
                Vector min = correct(one, two, false);
                Vector max = correct(one, two, true);
              
                int x1 = (int) min.getX();
                int y1 = (int) min.getY();
                int z1 = (int) min.getZ();
    
                int x2 = (int) max.getX();
                int y2 = (int) max.getY();
                int z2 = (int) max.getZ();
              
                for (; x1 <= x2; x1++) {
                    for (; y1 <= y2; y1++) {
                        for (; z1 <= z2; z1++) {
                            Block currentBlock = world.getBlockAt(x1, y1, z1);
                            currentBlock.setType(Material.SNOW_BLOCK);
                        }
                    }
                }
            }
       }
    My code to save the locations vector:
    Code:
    plugin.getConfig().set("Locations.Spleef." + corner + ".Vector", location.toVector());
    And then of course your code. But I haven't changed anything about it so it's not necessary to paste it I think.

    @1Rogue
    Alright I see the problem... It only does the third loop and skips the first two.

    Alright nevermind. I fixed it by looking to the codes of other plugin and learning from it.

    This is the loops and you don't need to calculate the minimum with this
    Code:
    for (int xPoint = x1 <= x2 ? x1 : x2; xPoint <= (x1 <= x2 ? x2 : x1); xPoint++) {
                      for (int yPoint = y1 <= y2 ? y1 : y2; yPoint <= (y1 <= y2 ? y2 : y1); yPoint++) {
                        for (int zPoint = z1 <= z2 ? z1 : z2; zPoint <= (z1 <= z2 ? z2 : z1); zPoint++) {
                            Block currentBlock = world.getBlockAt(xPoint, yPoint, zPoint);
                            currentBlock.setType(Material.SNOW_BLOCK);
                        }
                    }
                }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 31, 2016
  22. Offline

    1Rogue

    That loop is a giant mess considering you already have two Vectors. That and you are constantly re-evaluating the differences in every iteration of each loop. You can just do:

    Code:java
    1. Vector plusX = new Vector(1, 0, 0);
    2. Vector plusY = new Vector(0, 1, 0);
    3. Vector plusZ = new Vector(0, 0, 1);
    4. for (; min.getX() <= max.getX(); min.add(plusX)) {
    5. for(; min.getY() <= max.getY(); min.add(plusY)) {
    6. for (; min.getZ() <= max.getZ(); min.add(plusZ)) {
    7. min.toLocation(world).getBlock().setType(Material.SNOW_BLOCK);
    8. }
    9. }
    10. }
     
Thread Status:
Not open for further replies.

Share This Page