Hey everyone this is alex from wornoffkeys.com and in this video i’m going to show you how you can create your own commands within your spaghetti plugins now before we start i want to quickly mention that i’ll be creating my own server live here on youtube pretty soon so if you’re Interested in that check out blockblast.net for more information so in the previous video we had this very simple thing where we’re registering our own event listener and within here we have our main plugin and i’m looking to create a static instance of this plugin because from time to time We do need an instance of a java plugin and so we need an instance of this class so i can go ahead and say private static myfirstplugin and we’ll call this instance and then under on enable i can say instance equals this and then we can create a static getter to get this So public static my first plugin get instance and we can simply just return instance so we’ll use this later on in the video and next we want to go into our plug-in yml and by default this is how you’re going to register all of your commands So within here we can add in commands and then here we can list all of our individual commands so in this video i’ll be making a heal and a feed command to heal our players health and to fill all of their hunger so let’s say heal And if i use control space we see five different options we have a description aliases usage permissions and permission messages so under description i can say heals your player i then have aliases so if i wanted h to be a shortcut for this we can do that I can then say usage is for slash heel and for now we’re not going to have any permissions but you can go ahead and add them if you want and this is our command right here so now we need a way to actually register this within our main Folder i’m going to go ahead and make a new package and i’ll call this commands and this is where all of our commands can live and i’m going to make a new java class and i’m going to call this heel so this class is going to implement command executer so we can say Implements command executor and it automatically is going to give us an error if we hover over this we can click on implement method and then click on ok and it’s going to generate this method for us now returning true or returning false does different things returning true essentially means the command worked okay And returning false is going to send the usage string to the user so returning false should be used whenever the user did not use the command correctly i like to always return true that way there’s no error message whenever the command is reused correctly so the first Thing what to do is take a look at this command center right here which could be the console but also could be a player so because this is a heal command we want to assume that this is going to be a player but we can’t always be sure of That so i could say if not command sender is an instance of player we can go ahead and import that we can return true which will avoid all the error messages when it comes to the usage and then we can send our own custom message now before we actually send this message I find sending messages in the default bucket api slightly annoying for example if i say command sender dot send message i cannot color it by default so this message here would literally say and c here if i wanted to color it i have to use chat color dot translate alternate color Codes pass on the and symbol and then pass in my string here and then also pass in the color codes of course i find this really annoying just to send a colored message so what i’m going to do is i’m going to go into my main file I’m going to make a new java class and i’m going to call this msg short for message and within here i’m going to create two static methods so we can say public static void send two parameters here first is the command sender which i will simply call sender The second one would be string which i’ll call message now the second method is similar so public static void send command sender we’ll call the sender string of message and then another string of prefix within here i can say dot send message chat color dot translate alternate color Codes we can add in an and and then add in prefix plus message now this prefix is useful because you can now pass in any default prefix that you want now within send we want to basically just say send sender message and then a default color code let’s say and a So now under our command here we can very easily just say message.send command center and then pass in some text here and this will automatically color and a or anything else we want so we no longer need this giant line here now in this case i’m going to use and c And we’re going to say only players can use this command so down after this we’re sure that our command sender is an instance of player so i can say player player equals will cast player from command center now that we have access to our player we Can actually heal them so we can say player dot set health and each individual half heart is a single value so we can say 20.0 double and this is going to set them to full health so now that this command is in my plug-in yml and we actually have our Command listener here it’s time to register this in our main plugin so i can say get command we can pass and heal and we can set an executor attached to this command and this requires a command executor which is why this is going to implement command executor right here So going back setting our executor we can pass in a new instance of our heal class and i can go ahead and build the artifacts and if you watched and follow the previous video this should build directly into your plugins folder now that everything has compiled correctly i’m going to go ahead and Start the server and now we can go into minecraft and i’m going to connect to my local server i’m going to go ahead and fly into the air here then i can go into survival so we’re going to take some damage and if i do forward slash heal Our health now goes to full health so this is because of our command here and also our commands will automatically be imported into our help command so if we go to the fourth page right here we see forward slash heal heals your player so this is the text that we added in so Whatever you added as your description will show up in the built-in help menu so let’s go back and now i’m going to show you how you can create a feed command but more importantly i’m going to show you how to create your own basic command handler that you can modify to Your own needs and will make your life much easier so i’m going to go ahead and stop my server for now and i’m going to make a new file within our main package here and i’m going to call this command base so this class is going to extend buckets command And it’s also going to implement command executor and from within here we’re going to want to override the constructor a number of different times to make it easier to actually register these for any use case so we can say public command base this is only going to take in the Command as a string i’ll come back and actually enter the body of this soon we also want another overloaded constructor this will take in the command as well and also a boolean called player only so this command handler will automatically detect if the sender is a player and will send them an error Message if they are the console that way you don’t have to manually check that for each command you create we are then going to have another one here with the command and this time a number of required arguments so we can say public command base string command int min arguments int max arguments Now we only have two more we can say public command base string command hint required arguments and then boolean player only and the final one is going to be command base string command and min arguments int max arguments and then boolean player only so this is going to be the constructor That handles all the logic and then all the previous constructors are going to call each other to ensure that everything is going to work no matter how little or how much information we need to pass in so in this first case we’re going to say this command and 0. And this is going to call this constructor right here afterwards we’re going to say this command 0 player only then we’re going to say this command required arguments required arguments which we’ll then call this constructor here we’re going to say this command minimum arguments maximum arguments and then false for player only And then here we’re going to say this command required arguments required arguments player only so now no matter how many arguments we have as a minimum or maximum or if it’s player only we always have a constructor we can call to easily register our command now all this logic can be handled within This and the first thing i want to do is call super and pass in our command we also want to create a couple private properties on this class so i can say private list this will be a list of strings we’ll call this delayed players and we’ll set This equal to null by default we can also have private int delay which will equal zero and this is the delay in seconds in case you have a cooldown on your commands which will be built in to easily add with this command handler we can also say private final int min Arguments and because this is final we don’t have to assign a value right here but we’re going to do that soon in the main constructor and similarly we’re going to create max arguments so private final int max arguments then also private final boolean player only So now we can assign values to all these within this constructor right here so this dot minimum arguments equals min arguments list.max arguments equals max arguments and finally this top player only equals player only so now we need to gain access to something called a command map using reflection And this is a fairly advanced concept if you’re new to java so go ahead and just follow along the exact details of what we’re about to write aren’t super important for the functionality anyways so i can say public command map get command map we can now say if bucket dot get plug-in Manager is an instance of a simple plug-in manager so now that we know how the plug-in manager is a simple plugin manager we can say field field equals simple plugin manager dot class dot get declared field and we’ll pass in command map with a lowercase c and an uppercase m Now from here we can say field dot set accessible true and then we can return something that is cast to a command map and that is going to be field dot get bucket dot get plug-in manager now these things can throw exceptions so i’m actually going to highlight all of This and wrap this in a try catch where we’re going to try and catch a no such field exception as well as a legal access exception on either of these we’re simply just going to print this and then here we’re going to return null because ideally we should have returned something here And if it didn’t that means either an exception occurred or we’re not having an instance of a simple plug-in manager so we’re simply just going to return null now after we’ve set these initial values here we want to gain access to this command map so we can actually register our command So i can say command map command map equals git command map and if this is not equal to null so if command map is not equal to null we can then say command map dot register we can pass in our command string and then we can pass in this So this is actually going to register our command and scrolling down we can now handle our delay logic just in case there’s cooldowns within your command so i can say public command base enable delay and this is going to take an integer called delay i can say this.delay equals delay and Then this dot delayed players equals a new arraylist would they want to return this because this could be used as a factory function to easily chain together multiple functions now we want the ability to remove delay from a player so i can say public void Remove delay i can pass in a player here i can then say this dot delay players not remove player dot get name we can now have a public void send usage command we can then pass in a command sender as an argument and this is going to send The correct usage for a command to our user so i’m going to say message.send sender and then get usage which is something that is part of our command by default but we are going to override that soon we’re now going to create our own execute method and this is going to be Ran whenever the command is ran so i can say public boolean execute we can pass in a command sender as sender we can pass in string as alias and then string array for arguments by default we just want to return true and here we’re going to do a number of checks so just Making sure that the number of arguments are correct as well as making sure that it is player only and also making sure that they are not delayed just in case there is a cooldown on some of your commands so the first step is to make sure that The number of arguments is correct so i can say if arguments.length is less than minimum arguments or arguments.length is less than max arguments and max arguments that’s not equal to negative one so this logic here allows us to pass in negative one to ensure that there is no Maximum number of arguments in case you’re looking for the user to enter as many words as they want so if either of these are the case we can call send usage and pass on our sender and then we can return true again if we return false it will show the Defaults send usage message so we’re looking to return true in every case here that way that’s not sent so we can handle that logic ourselves now next we want to see if the sender is an actual player so i can say if player only and not sender instance of player So within here i can return true to make sure we don’t continue with our code execution and we can also tell the sender that they can only be a player to run the command so we’re going to use the new message class that i created we can say message.send sender And then and c only players can use this command so this way you can pass in true for your player only and you don’t have to worry about the console using it in case you require the player object so scrolling back down the next thing we need is to ensure that The user has permission to use the commands so i can say string permission equals get permission then i can say if permission is not equal to null and not sender dot has permission we can then pass in permission if this is the case we can return true And then we can send them a message saying message dot send sender and see you do not have permission to use this command and of course you can add in your own strings for any of these error messages in this case you might want to add in what permission they’re missing but i Prefer to do this right here so that’s one of the upsides of following along with this is that you get a good starting point and you can customize it further based off of your own needs so the last thing we need to check is if the user is delayed So i could say if delayed players is not a go to which is null by default as we see up here but we assign this to a new array right down here under enable delay where we’re assigning a delay and we’re also assigning this to a new array list So this is only not null if we’ve already enabled a delay so if delayed players is not equal to null and sender is an instance of player so this will ensure we’re only going to delay players because obviously we don’t want to delay the console so now we’re Going to cast sender to a player so i can say player player equals cast of player cinder so we have access to the player and we can say if delayed players that contains player dot get name now within here we know that the player has used the command too frequently so We can simply just return true but then above this we want to tell them why they can’t use the command so i can say message dot send to player and c please wait before using this command again and obviously you can make this a more advanced message this is just a simple Example that i prefer to use now after this if statement we know that they have not been delayed but now we want to delay this player so we can say dot delayedplayers.add name and then we want to schedule a task for the number of seconds to delay so i Could say bucket dot get scheduler does schedule sync delayed task we now have to pass in our plugin which is why we created the get instance method earlier so i can say myfirstplugin.getinstance and then we can pass in our own lambda function here and within here is a logic that should Be ran after our scheduled task is delayed so i can simply say delayedplayers.org player.getname and then we have to specify how long we want this task to be delayed for so after a function i can say 20l because this expects a long and there’s 20 ticks within a second So i’m going to say 20 l times delay which is the number of seconds that we provided for the specific command so essentially if they are delayed we’re going to tell them to slow down but if they’re not we’re going to continue on and add them to the delayed players list And then after the delay we’re going to remove them from that player list now outside of here we can then actually run the command so we can say if not on command we can pass in our sender and our arguments we can then say send usage And pass on our sender and we’re getting an error here because we’re not supposed to use just on command with these two arguments but we have more overloading to do within this class there’s only a couple more steps the next is to create the proper on command handler i’m going To say public boolean on command we can pass any command center known as sender a command known as command a string called alias and then a string array for arguments now below this we’re looking to create our own abstract method for oncommand because in the earlier example i showed You at the start of the video we’re able to basically create a new instance of our command base and pass in our own function to handle that so to do that we have to have an abstract method and to do that we have to make our Entire class abstract so we go to the top we can say public abstract class so now we can create that method public abstract boolean we’ll call this oncommand and we only want two parameters here we want a command center called sender and then a string array known as arguments Now we also want one more abstract method so i can say public abstract string get usage and this will return the usage for that specific command so now within the default on command with the proper built in arguments which will be ran by default we want to call this method right here So i can say this dot on command pass on our sender and our arguments and now our entire thing is done here keep in mind that we are not going to register this within plug-in yml this is one of the benefits of getting our own command map here is that we don’t have To do anything in our plug-in yml i’ll keep this heel here just as an example but we’re going to create a new command for feed so i’m going to make a new java class called feed and then this is going to simply have a constructor and within here i can say new command Base i’ll pass in the arguments here in the constructor in a few seconds but within here i can say at override public boolean on command command sender has a sender and then a string array for arguments for now i’m just going to return true and now we need to use the get usage Which also is an abstract method so we have to overwrite it as well so at override public string get usage and here we can just return a simple string of forward slash feed so this string will be sent to users who do not use the command correctly So now what do we add within here we have a number of options the start is going to be the actual name of the command in this case feed we can also pass in player only which is true now if this had arguments we can set a Minimum number of arguments as well as a maximum number of arguments let’s say one if we wanted no max such as a forward slash say command we can add in negative one because as we remember if we go back we are checking to see if max arguments is not negative 1. I also just noticed this error right here we’re supposed to return this because this expects a boolean so make sure you do that going back here as you can see we have a number of different useful things but in this case we don’t need any arguments so We’re just going to say feed and that player required is true so now on our on command we know for sure that our sender is a player and not the console so we can easily cast that player player equals a cast of player from center And now we have access to our player we can say player got set food level 20. so again we are not registering this in our plug-in yml the command base will do all that for us and we also could apply a cooldown to this so here i can click on this curly Brace and it shows the ending one here before the semicolon i can do a dot and we see all the methods we can use here in this case we can say enable delay we can pass n2 for two seconds and this will automatically handle the delay for all players behind the scenes So now we’re almost ready as you can tell we’re making the new command within this constructor so therefore we have to actually call this for this command to be ran in our main plugin we can now say new feed and this will automatically register the command because it’s going to make a new Instance of this class therefore run this code and therefore register the command so we can go ahead and build and this will automatically be built into the plugins folder for my test server if you followed one of the previous videos so going back into my test server Console i can go ahead and start off the server and then we can try out the plugin so i’ve logged into my server and i’ve done some running around so we’ve now lost some hunger and if i do forward slash we now see the feed command is right there So if i run this we now see that we are full hunger and so our command has automatically ran even though we did not add it into our plug-in yml so this is all the code we need in order to register our own command let’s go ahead and try out the delay system So if i do forward slash feed and i do four touch feed again it’s going to say please wait before using this command again now when it doesn’t say anything that means it’s actually working because we did not send a confirmation message now one last thing we might want to add Is a confirmation message just so the user know it works so we can say message.send player you have been fed and we now have a working feed and heal command Video Information
This video, titled ‘How to create commands – Minecraft Plugin Development Ep. 3’, was uploaded by Worn Off Keys on 2021-09-10 14:59:47. It has garnered 16664 views and 300 likes. The duration of the video is 00:24:03 or 1443 seconds.
Let’s create our own commands in our Spigot plugins. 🧠 Free Java course ➡ https://wornoffkeys.com/java-for-free
🔥 Updated video on how to deploy your plugin: https://youtu.be/XslTgP6Fgz4
🙋♂️ Need help? Ask in our Discord community: https://wornoffkeys.com/discord
📺 Watch more Discord.JS videos here: https://wornoffkeys.com/playlist/spigot
💡 Have an idea for a video or course? Request it here: https://wornoffkeys.com/content-request
——————————
🕒 Timestamps: 00:00 Creating a standard heal command 06:36 Creating your own command handler 20:26 Creating a feed command 24:02 Outro
#minecraft #spigot #wornoffkeys
FTC Legal Disclaimer – Some links found in my video descriptions might be affiliate links, meaning I will make commission on sales you make through my link. This is at no extra cost to you and it helps support the channel so I can make more free YouTube videos.