Well hello everybody and welcome to another Minecraft plugin development tutorial Series today we are going to be looking at world edit and how to hook into it as a library and how to manipulate schematics this is going to be really useful if you have for example a minigame plugin and you need to Automatically reset its map after each round so we have a sample villager right here I’m going to demonstrate what I mean so I’m going to show you how to programmatically set two positions and please note that while this is similar to world edit it is not world edit it is My own code there we go now we s save the both both of them and then we can just save it as for example villager 2 and as you can see this file now appears inside my data folder so it is fully customized and then pretty much anywhere In the world I can just paste it oops not this one that was for a previous test villager 2 there we go and now you can even see that we have a beautiful villager standing here now before I crack into this if you want to learn more about Minecraft plugin development And you are interested in perhaps making your own mini games or custom entities and you want to build your own unique servers from the ground up we have a special training for you called project orian I’ll leave the link in the description there’s a couple spots left Open so if you can check it out you can join today you can start the training for up to 2 months later and you’ll be covered with our 30-day money back guarantee not from the day you enroll but from the actual date that you start The training which can be up to 2 months later so the first step is we have to get world edit API inside our plugin as developers luckily world edit provides us uh with a maven repository and I’m pretty sure you also are able to configure this for Gradle but I just Assume that most of you guys are following this tutorial series reviews Maven Maven means uh this little p.x ml file this one so what I did I simply Googled world edit Maven and literally the first link is going to give you both the repository so I can just copy this Go to the pom file and then inside where is the repository yeah we have a huge list here I can just paste it at the bottom there we go and then I can just copy the dependency for bucket you don’t need to copy the core just copy the one For bucket and I would suggest you place it on top of before paper AP so that it takes priority now for the purpose of this video I’m going to be working with the latest 7.3.0 snapshot version cuz it has more features and there’s been some changes I think pretty much by the time You’re watching this video this one is out as a stable build if you have that done just click on load Maven changes wait a little bit and you should be good to go okay guys so the first thing I did I made a new class I just called it World edit hook and inside you will need two methods one is to save the schematic remember the safe command and then the other one is to paste the schematic into a specific location now you’re going to be careful with these Imports some people miss them so make sure to import Location from or. buuck it. location package the first thing inside the save method is going to be the region method and basically what this will do we have to first create a threedimensional region from the main the first location and the secondary position the secondary location which is going to work as a Cubicle and this this one again you have to be very careful when it comes to Imports because there’s this there is the same class in multiple packages so make sure to import the region from com. SQ 89q world edit package and then this one takes in new cuboid region and Unfortunately world edit works with its own adaptation for classes which typically have other names in a bucket so instead of working with location you work with something called black Vector it looks maybe not so similar but it holds similar data it holds X Y and Z However there need to be a way to convert that into uh to convert bucket into world edit stuff and for that they provide something called bucket adapter that is the reason why I suggest that you import world edit Dash bucket otherwise you would not have access to This so we can just call as block Vector inside the bucket adapter method and we can just open up the region uh from the main location the first one which I call the primary and the second location which I called the secondary right here next up we have to make something called The edit session each operation in world edit is guarded by an edit session reason for this I don’t know but perhaps they wanted to make it more customizable because each edit session has like a start it has finish it has couple of parameters and you know it’s just really Uh fancy if you are some super Advanced developer they want to customize the whole thing this one takes a little bit to create so I’ve made a little bit of a helper method called create edit session this one is going to take a bucket world and here it’s quite important that you Import this explicitly otherwise you will run into issues because this world is actually from world edit package so maybe you need to import at least one explicitly and perhaps I’ll just import both so that you can see this a little bit better and perhaps you can get rid Of this one completely yeah you can just copy this based it inside new edit session just like this one and now it should work perhaps uh maybe we’ll have to return to this later so you have to be careful with the package names basically how to make a new edit session Just go to world edit open up the main instance and then call new edit session and then this one takes in a world so again we have to convert bucket world into World edits World using bucket adapter here there’s a couple of side effects which you can find I’ll post a Link to it in the video description especially for 7.3.0 uh this is I believe new feature and I just loaded it with a default setting uh because I simply had no reason to to make this video 3 hours long and explain all the intricacies if you just need to copy and paste stuff Now going back to the save method here is where it where it starts to get a little bit complicated so try to bear with me as good as you can and I’ll explain everything line by line so the first thing we have to do is create a Clipboard now there’s a bunch of types of of clipboards the easiest one is simply a block array so you can think about this as a clipboard that will simply copy a list of blocks right and luckily you can make a new block array clipboard and used the region that we Made uh to put in it and then you cannot say clipboard do do the work the way that the operation will actually take place is a little bit more complicated because we have to create the operation itself which is called forward extend copy doesn’t make lot bunch of sense to Me but I’m pretty sure that devs might have had a good reason to call it this way and this one is a little more complicated it takes in the edit session takes in the region takes in the clipboard and finally takes in the minimum point which I believe is the Primary location that is where I was standing in the game that I TP when I typed position one now you’d be thinking we’re done we’re not done we actually have to call another piece of block another code of block another block of code sorry and this one is called Operations and then we have to complete the copy inside the complete method and this one will complain because it apparently throws unchecked exceptions so we have to surround it with a world edit exception and catch it here great so now we have an object inside our Memory finally it’s time to save it into a file and the way we’re going to do so is by using a closer so a closer comes from not Google package but actually world edit package and this one deals with basically open it’s it’s basically a helper for this whole operation again I think it’s over complicated but the way this that’s the way it works so you know please just bear with me that’s why I’m making this video because it can be confusing if you don’t follow anything and inside a closer well first of all sorry first of all we have to make sure To close the closer thankfully we can just use Java 8 feature called autoclose so if you put this inside the tri block at the very end uh you don’t have have to call closer. close this will be done by Java automatically and then of course make sure to catch any exceptions Because when working with file streams uh these also throw unchecked exceptions okay so the first thing we have to make a new file output stream and that the way we can do so is simply use the closure to register a new file output stream for the schematic file this will Return the one that we can actually use and then we can create a new clipboard writer by again again calling closure register and then it will it will actually ask for the format so the format is basically the do scam file format right if I attempt to open this With say notepad it’s going to give me a lot of garbage but this is actually the format of these blocks right and I just decided to go with the latest one sponge version 3 if you’re coding for an older Minecraft version you have to check but You know I think your best bet is just to test it yourself and to see whatever works uh for me on 1.20 plus sponge V3 works just fine and then when you do so you have to get the writer for this schematic and the rider is whatever we Have to register to get the clipboard Rider unless you have IQ 200 you would not just figure it out and then finally we have to call the Rider ride and then finally um the clipboard which we created up above here thankfully thank God I’ve survived the safe operation and Now it will work just just fine moving on to the paste operations so first things first we have to make the edit session thankfully we already have a method for it so that one is easy and then we have to start reading the file on our disk for example villager or test Or villager to file so for that we have to actually open this as a clipboard format and you can use Clipboard formats find by file and you can just open the file directly and world edit is smart here thankfully at least somewhere and it’s going to figure out uh the format So it should automatically find that it uses sponge W3 now remember here we used to have a clipboard writer well here we going to have something called clipboard reader that’s right so we have to call the format get reader new file input stream and don’t worry it’s complaining I I’ll fix that in a moment and then the actual clipboard which is the actual schematic object we can actually get if you call reader. read quite easy surprisingly now what’s less easy than that is actually working with these operations so the operations that complete you might already be familiar With however how do we actually create an a a paste operation well there isn’t such as forward extend paste unfortunately but there is something called new clipboard holder for the schematic file create paste for the edit session and then chew and then this takes in the location so where I’m Standing from my own position it’s going to paste all these blocks in a certain direction and then it’s going to build it convert the holder into an operation which we can run right here maybe you can even paste this inside but for the sake of Simplicity I just like to have These two explicitly all right there’s a lot of complaints so we have to just make it very cheap and I’ll just for the sake of demo of this video I’ll just surround it with try and catch and I throw and I catch every exception right here all right guys magic happened I did Code the command behind the scenes the reason being I already have a video on how to make commands so please refer back to that video it’s one of the first ones first five ones in this Channel and I just want to save your time because I think that the Comm command logic you’re Already familiar with however I still want to show this to you because it has a couple of unique features to it first of all we have to basically store uh the first location and the second location for every player using the command suver so I typically don’t really recommend Storing this inside the same command but this is just the demo plugin right if you do want to make this properly then put it inside for example player cache class so to do move into player cache and then you can actually store more than just this chupo however here all I Did I simply came up with a very simple idea of storing this in a map the unique ID is per player so each player has a unique ID and the chupo basically holds two keys the first one and the second one being the primary and the secondary location the chup actually actually Doesn’t really exist I could not import this reliably from uh Minecraft because it’s not stored inside bucket so what I did I simply implemented the class right here again extremely simple it holds any key as the first and any key as the B and if you just want to make the chupal For location you can obviously just delete the ab and replace it with location that’s also going to work although this one a little bit has a little bit higher coding standard and maybe yeah you can even make the class static as intelligence suggest just make sure not to create these as finals Because we’re going to be calling the set first set second later on so first things first I simply check if we are dealing with a player and then if we are uh putting in some command arguments such as position one position two save or paste then we need to get the actual Plug-in instance and I can just get rid of the finals so it doesn’t confuse you and then I’ll just cast the player to uh from the sender and then I simply read whatever I typed in so whatever I typed after the space after typing the region Command right here and then we need to get the actual selection so for that I simply call get or default for the player unique ID and if this is not stored in the map instead of it returning a n I’ll simply return a new truple with both keys as n this is Important because we can actually set these two keys and then if the parameter equals to position one we can actually set the first key to where I’m standing you can obviously change this to whatever you want to I send the player a nice message with a color and an emoji Obviously you can change it to whatever you want to and then I simply store uh the selection inside the sorry the whole chupal inside the map per the player similarly I do the same thing for the second secondary position and then when it comes to the saving mechanism what we Do is let me just rename this cuz in the back scene when I was making the first take of this video I just called the command schematic so here what we do we simply check if we uh set both the first and the second position if not I’ll Simply say hey please set both positions first uh before calling the save command and likewise we also need to specify the name so the length of the arguments remember here we simply check it if it’s at least one well when it comes to save In it has to be at least two because not only I have to type in save but now I also have to type in the name and then we simply open up the file we’re going to open up the file inside the data folder of the plugin which is where is That it’s this file right inside plugins the data folder is always the name of your plug-in so this is what’s inside inside here I want to make sure to have a folder called schematic and inside of which I’ll simply create the file by the Name now I also made it so if you don’t have a schematic folder then Java is going to to automatically make it for you so if the parent file doesn’t exist the parent file being the schematic folder because the file is actually the actual uh schematic so if the folder Doesn’t exist if this folder doesn’t exist we simply make it as a directory and then here this one is now called world edit hook we can just call the save method from the first position the second position and then the location can actually be simplified to this Because this is just a duplicate there we go and then I simply say hey the schematic has been saved likewise the base operation also requires us to put in a name just like this one and then we simply create uh the file so I can maybe rename the location file right here so Here we’re just going to read the file from the schematic folder check if it exist if not I just say schematic not found and then here I’m going to again go to world edit hook and I simply paste it from wherever I’m standing and I’ll simply paste this file there we go and Then if I type in something else such as I don’t know region and I make a typo here right well it’s going to say that uh this is what I should type in or maybe you can perhaps return false here but then you have to return True for all of these if statement so I just decided to simplify it if you check the commands video I’m also going to teach you how to make the sub commands uh how to make the parameters tab completed so you can go ahead and check that this video doesn’t cover that However what I’m going to do at the very end I’ll boot you in the game and we’ll be testing it together now lastly if you’re making a new command please make sure to register it inside the plugin. yml F folder or file as well as inside The on enable in your main plug-in class so this is going to work with world edit however if you do have also World guard installed please change the command label to something else now before I do test it make sure to actually put the edit session from a field right here Inside the try and catch block because turns out surprise surprise actually I did not expect that that we have to put it here that we without this it will not work okay so we have to close this and you can either call close at the end or You can simply hack this around place it and try and catch and it’s going to work now okay so in the game I I’m going to type region position one and I’m going to show this on this little Arena over here let me just go to the other Corner Type in position two and then let let me just fly away there we go and now we are going to be saving this so save Arena and if I look into my plugins Co canoon we can already see that we have the file properly saved and then here if I type In region based Arena Bast it it should almost crush me there we go and now it appears this way and it’s beautiful it’s really the exact copy as what I pasted so I hope that you’ve learned something new obviously you don’t need to be pasting it where you’re standing you can Just find the minimum point of your mini game you can just paste it in there if you want to automate it and feel free to check out the official world edit documentation even though it might be a little bit too complicated so I hope that this video was helpful check out Project Orient if you want to learn more even deeper stuff even more advanced stuff than this and I will see you guys next time Video Information
This video, titled ‘Ep45. WorldEdit Schematic – Minecraft Plugin Development’, was uploaded by Matej on 2023-12-09 17:00:37. It has garnered 255 views and 12 likes. The duration of the video is 00:19:33 or 1173 seconds.
How to use WorldEdit API in your Minecraft plugin to save, copy and paste schematics and edit millions of blocks at once.
This is AMAZING for world/region restore automatically in your minigame plugins, but the possibilities are endless.
In this video, we’ll cover:
– WorldEdit API — Using WorldEdit API to manipulate clipboard and schematics.
== COURSES == Minecraft Plugin Development: https://mineacademy.org/project-orion?st=yt&sc=tut_ep45 Forge Modding: https://mineacademy.org/forge-mastery?st=yt&sc=tut_ep45
== RESOURCES == Blog post: https://mineacademy.org/tutorial-45/?st=yt&sc=tut_ep45
#coding #code #tutorial #money #developer #java #minecraft #minecraftplugin #bukkit #spigot #spigotmc #paper #paperspigot #programming