Solved import schematics without using worldEdit API

Discussion in 'Spigot Plugin Development' started by sir_dynamo, Mar 9, 2020.

  1. Hello,
    I created an ore populator for my speed uhc plugin I'm coding, and I would like to create a nether populator. Here are a few in-depth explanations:
    I found a piece of code which apparently was a schematic interpretor, and for some reasons I never got it to work. So I was wondering if there was a way to import schematics (since my nether biomes are exported as schematics) without using the world edit API. If not, is there a way to integrate world Edit's source code in my plugin?
    I hope I was clear enough

    here 's the coed of the schematic interpretor:

    https://pastebin.com/DziFWuU4
     
  2. So, do you want to import a ".schematic"-file or a ".nbt"-file?
    I have written a piece of code a while ago that enables the latter one (somewhat minimalistic version thou) if you are interested. If you want to import a ".schematic", the best thing you can do is probably just use the WorldEdit API
    Edit: or write your own parser. I have written one waaay back, if you want to I can see if I can find it
     
  3. I said a schematic but it can also be a .nbt file (I can export my nether biome to either one of those it doesn't really matter as long as I can get it imported back into my world, during world generation). I am indeed interested by the code you wrote, it would be a great help.
     
  4. Well if you're looking at a whole biome, you're looking at a .schematic file. ".nbt" is limited to a max size of 32x32x32.
    Lemme see if I can find it...
     
  5. could be, however I exported it using litematica as a nbt file as well and it seems like it worked even though it is indeed bigger than 32x32x32
    (haven't tried to import the actuall file back into the world though)
     
  6. DavidDevelops

    Supporter

    You'd more or less be redoing what the WorldEdit API does, You create a phraser that reds the NBT tags and data and use it to build the schematic again by placing the blocks, I have done this myself before and honestly there is no reason too, I did it due to I was needing to alter and make changes along with slowly place blocks, So I needed my own builder but writing a phrase is really unneeded, I could also attempt to find my old code for it though it'll be messy if I do find it, (Pretty sure I have a jar i can decompile on dropbox with it)
     
  7. So the reason why I wanna do it this way is because the server that is gonna use my plugin will not have the world edit plugin, and so if I'm using the wordEdit API I'm guessing (now I might be wrong) that the server won't be able to run my jar file (missing librairies etc???)
     
  8. DavidDevelops

    Supporter

    Well yes it wouldn't be able too, But you can also just import the worldedit API alone into your plugin (A bigger file and slower load time and would be needless if the server rand worldedit)
    The point being, Almost every server runs worldedit as its a VERY needed plugin and almost any plugin depends on it now adays, Doing with this without it is much more of a pain and probably not as optimized (Unless you really spend A LOT of time on it) The best bet would be going to look at the worldedit API and use that to help you rewrite what you need, I have a bare bone example of how to load a schematic file but its not perfect, Nor do I know if this was my working version so it might have some issues to deal with (Aka, Its not drag & drop coding)

    Code (Text):
        public static SchematicReader loadSchematic(File file) throws IOException
        {
            FileInputStream stream = new FileInputStream(file);
            NBTInputStream nbtStream = new NBTInputStream(new GZIPInputStream(stream));
            CompoundTag schematicTag = (CompoundTag) nbtStream.readTag();
            if (!schematicTag.getName().equals("Schematic")) {
                throw new IllegalArgumentException("Tag \"Schematic\" does not exist or is not first");
            }
            Map<String, Tag> schematic = schematicTag.getValue();
            if (!schematic.containsKey("Blocks")) {
                throw new IllegalArgumentException("Schematic file is missing a \"Blocks\" tag");
            }
            short width = getChildTag(schematic, "Width", ShortTag.class).getValue();
            short length = getChildTag(schematic, "Length", ShortTag.class).getValue();
            short height = getChildTag(schematic, "Height", ShortTag.class).getValue();
            String materials = getChildTag(schematic, "Materials", StringTag.class).getValue();
            if (!materials.equals("Alpha")) {
                throw new IllegalArgumentException("Schematic file is not an Alpha schematic");
            }
            byte[] blocks = getChildTag(schematic, "Blocks", ByteArrayTag.class).getValue();
            byte[] blockData = getChildTag(schematic, "Data", ByteArrayTag.class).getValue();
            return new SchematicReader(blocks, blockData, width, length, height);
        }
       
        private static <T extends Tag> T getChildTag(Map<String, Tag> items, String key, Class<T> expected) throws IllegalArgumentException
        {
            if (!items.containsKey(key)) {
                throw new IllegalArgumentException("Schematic file is missing a \"" + key + "\" tag");
            }
            Tag tag = items.get(key);
            if (!expected.isInstance(tag)) {
                throw new IllegalArgumentException(key + " tag is not of tag type " + expected.getName());
            }
            return expected.cast(tag);
        }
    Past that it's all just loading the schematic file by looping through values and such.
    -= Warning -=
    You'll need to importn Jnbt to your buildpath to use this if I'm right as thats the version with it, I don't have the code with my fully working self version but if you want to use NOTHING else you can use my code as a base board and just recode the jnbt methods used.

    If I helped at all please leave a rating on my posts and if the issues is resolved mark as solved!
     
  9. Okay thanks a lot could you give me any indications on how to import the API into my project (is it just a regular importation : Java build path/add librairy/ worldedit api or is there any tricks I should be aware of?)
     
  10. DavidDevelops

    Supporter

    Sorry, You didn't word this well not quite sure I understand? Are you trying to important JNBT? If so yes just regular importation.
    If you're trying to build the world edit API into your project I'd suggest looking into Maven to do that.
     
  11. well you told me I could import the world edit API into my plugin so it would work even if the server doesn't have it (I wasn't aware that was possible so I think that's what I'm gonna do). So my question was how do I import the world edit api in my plugin
     
  12. DavidDevelops

    Supporter

    Yes, As I said though, I don't do this much and don't feel like I have the best knowledge to tell you! I'd suggest looking at Maven and how to build projects into yours.
     
  13. alright thanks for your time
     
  14. DavidDevelops

    Supporter

    Not a problem, My pleasure, Please mark this post as Solved if the issue is resolved and you got what the post was intended for.
    (This way future people with a problem like this can find it and know it the solution!)
    I'd also appreciate if you'd rate my posts with positive feedback though it's unneeded.

    To mark a post as solved (If you don't know)
    Go up to the top of the page and hover over the same icon to change the thread name, Selecting that you'll be able to mark it as solved and lock the thread if you choose (Recommended)
     
  15. drives_a_ford

    Moderator

    This is most certainly untrue. Most plugins have nothing to do with WorldEdit.

    You mean that he needs to use the Maven shade plugin.


    As for the OP:
    It sounds like all you want to do is copy a schematic to a world/hub/spawn. This sounds like a one time deal.
    If that is the case, you can simply load up WorldEdit once, paste the schematic and then remove the plugin (i.e never use it again).
     
  16. actually I'd like to use the schematic in a populator as I said in a previous post. The plugin would be used each time a new world is loaded aka each time the server restarts. Now my issu would be how do I import the World edit API in my plugin (I've added the jar file as an external jarfile in the java build path but I don't think that's enough since the server that will use the plugin doesn't have we )
     
  17. drives_a_ford

    Moderator

    Again, use the Maven Shade plugin.