Help replacing blocks in game using a List

Discussion in 'Spigot Plugin Development' started by MajkTwyg, May 28, 2016.

  1. Hello, I am needing to replace blocks inside the world with blocks that are saved in a List. Currently, my plugin creates a List<Block> when starting up for specific zones of the world. Then, after players have built on the zone it replaces all blocks in that zone with blocks from the List.

    Currently, this is how I am attempting to do so. However it is not producing any changes, or errors.

    Code (Text):

    int topBlockX = (point1.getBlockX() < point2.getBlockX() ? point2.getBlockX() : point1.getBlockX());
            int bottomBlockX = (point1.getBlockX() > point2.getBlockX() ? point2.getBlockX() : point1.getBlockX());

            int topBlockZ = (point1.getBlockZ() < point2.getBlockZ() ? point2.getBlockZ() : point1.getBlockZ());
            int bottomBlockZ = (point1.getBlockZ() > point2.getBlockZ() ? point2.getBlockZ() : point1.getBlockZ());

            //Loops through List<Block> saved in the class.
            for (int i = 0; i < blocks.size(); i++) {
               
                Block saved = blocks.get(i);

                //Loops through blocks between the two points of the zone
                for (int x = bottomBlockX; x <= topBlockX; x++) {
                    for (int z = bottomBlockZ; z <= topBlockZ; z++) {
                        for (int y = 0; y <= point1.getWorld().getMaxHeight(); y++) {

                            Block block = point1.getWorld().getBlockAt(x, y, z);
                           
                            if(block.getLocation() == saved.getLocation()){
                               
                                block.setType(saved.getType());
                               
                            }
                        }
                    }
                }
            }
     
     
  2. People really need to learn to debug here.

    Print to console in parts of the code right after you check something.
    For example, after
    Code (Text):
    Block block = point1.getWorld().getBlockAt(x, y, z);
    Type
    Code (Text):
    System.out.println("test1");
    Then after
    Code (Text):
    if(block.getLocation() == saved.getLocation()){
    Type
    Code (Text):
    System.out.println("test2");
    See what is printed when your code runs, and you will know what is causing the issue and then you can solve it.
     
  3. @Xilixir I know how to debug... no need to be smug about it or jump to conclusions.

    The point is, if(block.getLocation() == saved.getLocation()) isn't connecting, and I want help finding out why. If I remove it, it just replaces it with a random block from the list but I need a way to make sure its the exact block from the list.
     
  4. Okay, but wouldn't it make more sense to debug and then let us know what your issue is if you can't figure it out?

    Or if you've already debugged, relaying the information about what's screwing up definitely would help get your issue solved.

    We don't know how the saved block is being stored, so how are we supposed to help?
     
    • Agree Agree x 1
  5. Consider using schematics instead.

    Have you verified that your list of blocks has anything in it, and that what is in it is what you expect? If the chunk unloads, the data may be rendered meaningless (i am not sure of the internals of the Block object).
     
  6. The list does contain all the blocks I've loaded, and when I filter out the Air material type it returns all of what I would expect. However when I loop through and compare all the current zone blocks with the ones saved in the list it can't ever seem to find two that have the same location.

    I'll look at schematics.
     
  7. I'm not sure why you loop through to grab the block at that point. Why not just grab it directly from the location which you already have? Also, you could switch out the for with indexing with the for-each loop, since you're not using the index for anything else.

    If I recall correctly, the Block at a location is never reassigned. I believe you would need to save BlockStates, then update the state of Block.

    EDIT: Yes, the docs validate the above paragraph.