There is a way for paste/save custom build from/to files?

Discussion in 'Spigot Plugin Development' started by HotDoctor, Nov 10, 2019.

  1. well im practically asking if someone has a way for save/load custom builds made by player.

    With this I mean something like schematic system that has WorldEdit but thanks compatibility version I cant, because Im running 1.8.8 but I want a system which can supports all versions, and actually WorldEdit supports 1.13/1.14

    I would appreciate if someone can give me a good answer for this question, and if can give me a code for do the thing what I want do.

    Thanks for all.
     
  2. You would simply iterate through every x, y, z coord between the two corners of the build, and add all of the block data to the list (blockType, blockData, location relative to the player).
    To load the build you would just do the opposite, iterating through the list and setting the blocks relative to the player.
     
  3. maybe a little example please?
     
  4. Code (Java):
    List<String> serializedBlocks = new ArrayList<>();
            for (int x = (int) location1.getX(); x <= location2.getX(); x++) {
                for (int y = (int) location1.getY(); y <= location2.getY(); y++) {
                    for (int z = (int) location1.getZ(); z <= location2.getZ(); z++) {
                        serializedBlocks.add( /* add block data (location, blockType, block) */ );
                    }
                }
            }
    You can also change location1 to 0 and change location2 - location1 to make it relative to your location as you probably don't to load and unload the build at the exact same spot
    You would also obviously make an unserialializor for it
     
  5. Im not a really "pro" at programming
    I have understood the code and I think i know how to do it

    But my problem is how to Paste it?
    I dont really know about Positions (Location, Vectors)
    how to paste all blocks in a diferent location when i have already saved them with another location?
     
  6. Just iterate through each block in the list and set the block at the coords
    Keep in mind, that in your block iterator to save the build, you will need to save the block's world, x, y, z, and blocktype
    Code (Java):
    for (String block : serializedBlocks) {
        String[] parts = block.split("," /* symbol you used to seperate the data */);
        Bukkit.getWorld(parts[worldIndex]).getBlockAt(parts[xIndex], parts[yIndex], parts[zIndex]).setType(parts[blockTypeIndex]);
    }
    EDIT: realized that you probably dont need to serialize the block into a string, but you can i guess
     
    #6 Plqy, Nov 10, 2019
    Last edited: Nov 12, 2019
  7. I still don't know how to make the blocks stick together depending on the player's location, I'm sorry, I just don't understand, I don't know how it works, any other help?
     
  8. I've already explain how to do so in a post above, but I will go more in depth with it (as i realize now you can actually just store it as a block object)

    We are trying to store every block between 2 locations. We can do this by looping through every block between the 2 corners of the build (aka looping through every x, y, z coord in the build), and storing the data in a list. We would loop through the bottom-most x value to the top-most x value, the bottom-most y value to the top-most y value, and the bottom-most z value to the top-most z value to get every block in the region. The slight issue is we don't know which location is the lowest or highest, but we can solve this by doing a small check on which x, y, z value is bigger.

    Code (Java):

            List<Block> blocks = new ArrayList<>();
            int topBlockX, bottomBlockX;
            if (location1.getBlockX() > location2.getBlockX()) {
                topBlockX = location1.getBlockX();
                bottomBlockX = location2.getBlockX();
            } else {
                topBlockX = location2.getBlockX();
                bottomBlockX = location1.getBlockX();
            }
           
            // same thing for topblocky, bottomblocky, topblockz, and bottomblockz...
            // you see how if location1's x is bigger, it should be the higher/top block, while if location2's x is bigger, it    would be the higher/top block (same applies visa versa)
    We can now simply iterate through all of these values and add them to the list.

    Code (Java):

            for (int x = bottomBlockX; x <= topBlockX; x++) {
                for (int y = bottomBlockY; y <= topBlockY; y++) {
                    for (int z = bottomBlockZ; z <= topBlockZ; z++) {
                        Block block = YOURWORLD.getBlockAt(x, y, z);
                        blocks.add(block);
                        // this will store all block data
                    }
                }
            }
    To repaste the data, you would simply reiterate through each block, and paste it relative to the players coordinates.

    Code (Java):

    for (Block block : blocks) {    
          YOURWORLD.getBlockAt(playerLocationX - (difference between playerX and blockX), /*  same for y and z, not sure if this logic is correct lol, you can figure it out */).setType(block.getType()); // you can also set the data of the block if needed
    }
    Hopefully this helped you understand it better :)
     
  9. #9 Reflxction, Nov 12, 2019
    Last edited: Nov 12, 2019
    • Friendly Friendly x 1