Solved Best way to create structures

Discussion in 'Spigot Plugin Development' started by Jaume, Apr 25, 2017.

  1. Choco


    How you want to format it is up to you. You can take some inspiration from the .schem file format, which you can see here, or just write your own format based on what data you want to save. I believe if you actually run the //schematic command (or whatever it's called) and create a .schem file, you can open it with Notepad++ and see how they lay out the information. It may be in JSON. I'm not quite sure

    EDIT: They're in NBT format, which is similar to a loose-JSON format with various other features.
  2. Do I really need a format other than a txt (maybe with its extension changed to .struc from structure or something) with a list of materials in it?
  3. Choco


    I think you're over complicating what a file format is. All it is, is just a way of data being structured or laid out (often times only machine-readable) in a file that the computer can understand how to parse when required. File extensions don't really mean a whole lot, to be honest. It's just a way of categorizing your file, in a sense. The .schem files mean absolutely nothing in the real world, and is just a format used by various programs. They create parsers to read those files and adapt it to their own program.

    Take for example the OBJ file format (file extension ".obj"). The OBJ file format is used by many 3D modelling programs, most prominently by Blender. It's become one of the most widely accepted formats because it cleanly lists out various bits of information under very specific categories. Those files can get pretty long depending on what information you want to store in it (vertices, texture coordinates, vertex normals, etc.), and can even reach 50,000 lines or more because it lists every bit of information on a new line.

    This is what I mean when I say, "define your own format". A format is really just a specific pattern of writing information to a file in order to be able to read it and parse it by the program when required. If you would like, your format could be as simple as listing every single block, its data and its x, y and z coordinates on a new line for each block. The file extension can be anything you wish, as long as you remember it.
    Code (Java):
    File file = new File("some/path/to/your/File.format");
    That file has the extension "format", but if I really wanted to, I could make it have the extension, ".thisisareallylongfileextension". They really do not mean anything.

    I hope that cleared things up a little bit
  4. Thanks, it clearly did help! I'll then keep it simple. I'll first write a line stating x, y and z dimensions (I only need to read and write cuboid selections) and then a list of material names.
    Does the getData(); method from block return null if there's no data (e.g. an iron block which can't hold any data)? Because I could then only write the MaterialData if it exists and, if it doesn't, just write the material name. Or does MaterialData already contain the Material name?
  5. Choco


    MaterialData will contain its Material type and its byte data, but if a block does not have any data (such as an iron block, for example), its data value is just 0. It's your choice whether you want to write that to file or completely ignore it if the data is 0. It's your file format :D
  6. Nice to hear :D

    Then to create a block and set it's type and data from a string... is it easy enough?
  7. Choco


    Should be, yea.

    Material#getMaterial(String) - Get a Material based on a String
    Byte#parseByte(String) - Get a byte based on a String
    Block#setType(Material) - Set a Block's type
    Block#setData(byte) - Set a Block's data

    That should be all you need, I believe. The coordinates you store will be relative to wherever the structure is going to be created, obviously.
  8. I just got it to work, but I'm not using BukkitRunnables to paste the structure (using a normal while loop). Is it really bad if I'm not going to paste structures over 10x10x10 (1000 blocks = really small amount of time)? Or is it OK?

    EDIT: If someone wants to check the code just ask for it :). Thanks!
  9. 1k block pastes shouldn't be too bad if you use settypeanddata (depreciated) it is much faster than setType(Material) and setting the data.

    As long as you don't have 30-40 of these loops running one after the other you should be okay. Scaling this to a larger project you could easily move the paste routine to its own runnable or do it async and it won't lag the main game thread.

    Sent from my iPhone using Tapatalk
  10. All solved, thanks!
    • Like Like x 1