Hey what’s up guys welcome back to another episode in the spigot series in this episode i’m going to teach you how to make event listeners okie dokie so welcome to the first episode where we’re going to start coding so we’re going to create a new minecraft Spigot project here so go to new project then go down to minecraft like we did last episode so if you haven’t seen last episode make sure to watch that okay so minecraft spigot plugin click that and click next see the group id me dot cody simpson And the artifact id you can so this will be the project name don’t forget so we shall call this event listeners you can call it whatever you want so next and you can fill out this information here so we’ll say a tutorial plug-in on events listeners great uh minecraft version 1.17 Great you can select any of them though for this episode all of the versions will be the same it’s the same process for everything okay so authors website you can fill in any of that if you want to so next great and now we want to set where our Project is going to live so we’re going to find the folder and i’m going to select tutorials for mine and this will be in the um event listeners folder okay so that’s where our project will be so click finish click create and now this will create a new spigot minecraft Project template for you so you can begin making a spigot plugin great so let’s open this up java there we go okay cool so what is an event listener that’s the first question you you may be asking so an event listener is simply a method of code within your plugin that is run Whenever an event is triggered so an event is something simply that happens and this can be many things like a player dying a player death event a player sheer event so when you share a sheep um when you join the server player join events breaking a block player brock wait Player block break event block break event something like that there’s literally an event for almost everything that you can do in minecraft and that’s the point so what we want to do is create a way for us to run code when those events happen only when those events happen so this is Very very useful to game development in general not just for minecraft plugins so we only want the code to run when the event happens so that’s why we can listen for that event with an event listener so first thing i’m just going to make a output statement here so s out Tab i’m going to say the plugin has started so that’s how we know the plugin has started great and we don’t even need the undisable method if you’re not going to use it you can just remove it so there we go same thing with on enable You don’t actually need it but we will need it for this episode so there’s two different ways you can create an event listener you can either do it within your main class here or not the main class but the class that has your plugin on enable and on disable methods or you Can have it within a separate class and then register it within your main plug-in class so we’re going to do the simple way first which is to create our method that’s our event listeners inside of the main plug-in class okay so to do that we need to make sure that Our plug-in class here implements listener so make sure that it’s listener from org.bukkit.event so this will make this class ex available to have event listeners within it now to create a new event listener we can use the annotation event handler and it shows up here as an auto suggestion so event handler Org.bucket.event great and now under that you can create the method that is run when the event is triggered so let’s say that we want to have an event listener for whenever the player joins the server okay let’s see if we can achieve that so we’re going to do public void because It doesn’t return anything on player join and then inside of here as the parameter is just going to be the event that you’re listening for so in this case we want to listen for a player join event as you can see here it’s auto suggesting player drone event if you do control q On your keyboard this may be hard to read but it says called when a player joins a server great so another player whenever a player joins the server this event listener should be triggered hopefully okay so let’s finish this so player join events and then we can give it the the Parameter name so events okay so open that up and now the code inside of here inside of these brackets here will be run whenever the player join event is triggered so this will listen for that event as an event listener or event handler however you want to call it Okay so let’s just uh to keep it very simple let’s just output some code so s out a player has joined the server great so if this works we should get that message whenever we join the server with our with our test account now before we do this before we run the Plug-in to compiler and all that stuff we actually need to register this event listener that we have created so don’t ever forget that a lot of people make a common mistake where they don’t register the event that they just created so they have to they have to go back and do that so To do that to register your event listener you can do git server dot get plugin manager dot register events register events and make sure it’s events not event so just one event and as you can see the parameters looking for is a listener and as you know this class happens to be A listener because it implements the listener interface so register events plural not singular and then for the first parameter since it’s asking for a listener and we know that this class here is a listener that means that we can just provide this as the first parameter And then now it’s asking for a plug-in for the second parameter and we also know that this class extends java plug-in and java plug-in implements the plug-in interface so we know that this class is also a plug-in so we can just do this for the second parameter okay so that’s how that works So if you make an event listener or an event handler however you want to call it within your main plug-in class with your on enable and undisabled methods you need to implement listener and also you need to do getserver.getplug this comma this and that will register your events for you And sometimes you’re going to hear me say events event listeners event handlers they’re all i’m going to use those all interchangeably but to be technical an event listener or an event handler or something that listens to an event and an event is just simply something that is that happens okay But it’s easier to just say event so a lot of people just say event so okay so that’s how you create a very very simple event listener we’re going to get more practice with this in a second so let’s go ahead and just test this out before we go further with This so to compile the plugin into a jar file just go up here to the run button click run the play button here and now it’s going to compile your plugin for you great it’s done already so where can we find this go to the side here to target And you can find it right here so event listeners dash 1.0 snapshot jar so to get this you can go to target right click it and do open in explore great now go into the target folder and here you can see your event listeners.jar file awesome great i just made it bigger For you so we can copy this with ctrl key ctrl c or just right click and do copy either one and now we want to go to our test server that we have so that we can run this plugin and test it out all right so here’s our test server so let’s Go to plugins and just go ahead and paste this control v or right click and do paste and now we have the event listeners plugin within our plugins folder so we can go back and run the server and see if it works or not okie dokie so if we look in our um Server log here it says event listeners enabling event listeners and then it says the plugin has started now if you remember if we go to our own enable method here we did tell it to output the plugin has started when the plugin is enabled so perfect that’s exactly what we wanted to do And also hopefully since this was run it also registered our events so let’s go ahead and join the server now to see if it did work okay so our test server started up so let’s open this up here and go to direct click connect and localhost that’s our Test server ip so join server and boom okay cool so now nothing happened in game you’re not going to see anything out of the ordinary or changed but if you look in our console here it says a player has joined the server and that is not normal minecraft it Wouldn’t say that like that it said it would say this it would say cody’s cody simpson99 logged in with identity or entity id blah blah blah but this message here is directly from our plugin because if you remember in our event listener here we told it to say a Player has run the server whenever a player joins the server so whenever a player join event is triggered it will say this so awesome right that’s our first um event and it works great so that’s our first event listener and now let’s see if we can make this a little more advanced So we have this events parameter here and we can always use this event parameter here to grab information from the event set information for the event do a bunch of things so if we do event dot you can see all of these methods here for you to do stuff on the event So since this is a player join event we can obviously it would make sense for us to be able to get the player that joined the server so if we do events we can do event i’ll get player and now it says that’s how you can Obtain a player object of the player who joined the server so we’ll do that in a second but also you can get the join message of um you know whenever you join the server so it would be this right here cody simpson99 joined the game and also Let’s see what else we can do you can get the event name handlers we’re not going to use that ever and but also you have a center method here so we can set the drum message and that’s where events get really powerful you can customize the default functionality of the game so Let’s say that we want to make it so that it doesn’t say this whenever you join the server it says something else so we can do event dot set join message and then if we do control p here it’s asking for a string of the joined message and we’ll say add player welcome To the server you big dummy very nice right very welcoming great so now instead of just outputting something to the console it should message the player um the welcome message that we have set here so let’s go ahead and recompile this and throw this into the server and see if it works So i’m just going to stop the server here i’m going to go back to our target folder and recopy it then go to our minecraft plugins folder our server replace that and then we can restart the server here so start up great okie dokie so let’s try joining now And boom look at that it says welcome to the server you big dummy and that isn’t that awesome that’s pretty awesome if you ask me because you know we’re very very at the beginning of this series here but we have done something really really cool and we’ve we’ve created an event Listener so we’re listening for when an event happens and this happens to be the player joint event and then we’re customizing the default functionality of the game using a plug-in so that whenever player joins the server now it’ll tell them welcome to the server you big dummy so That’s how you can customize the player join message by the way in case you’re curious about that so you may be wondering how can i see more information about this event and how it works so you can do control q always to see the basic documentation of this but The base the best thing that you can do is just google the spigot javadoc so if we open this up here we can see i use bing i know i’m so sorry please forgive me so you just search google it or bring it spigot javadocs and then click the first link here so Hub.spigit.mc blah blah i’ll leave a link for this in the description below and so you open this up here and you can see all of the documentation for the spigot and bucket api by the way bucket is part of spigot so that’s why you see a lot of bucket packages here So you can grab any information about how spigot works their their documentation is not bad so if let’s say that you want to see all the different events that you can use whenever making a plugin so if you just scroll down here you can see event or you can see org.bucket.entity You can see org.bucket.ct.memory you can see org.bucket.configuration.file.org.bucket.com but if we keep going here we can see org.bucket.events and we can see org.bucket.event.block or bucket.event enchantment so these different subtypes of events here these sub packages will be the different types of events that you can have within your plugin So the first event that we saw in our plugin here that we’ve used already is the player join event so this would be in the javadocs under the org.bucket.event.player package here so if we click this we can see a list of all of the different events that we can listen for All of the player events that we can listen for so if a player breaks an item if they interact with something damage something holding an item mending something joining kicking level change login move there’s so many different events that you can listen for when you plug in this One makes this is what makes plugins very powerful on a basic level so you can listen for all these different actions and stuff like that so if we go back though we can see what else we can do let’s see there was a block one so there’s block events So let’s click this and we can see all the different block events awesome so we have block break event block burn event block can build event block cook event all these different things so if you want to add custom block functionality to your minecraft server you can listen for block events and then Do stuff depending on you know whatever you want to do right whatever you’re trying to achieve so let’s say that a block is broken and then you want that block to just you know magically explode or like shoot out some a rocket or something like that you can do that by Listening to the block break event and then spawning a you know a rocket or whatever a firework so yeah you’ll learn how to do all this um by the end of this series for sure um really soon actually anyway so that’s how you that’s a basic Understanding of how you can look at the javadocs just to see how you can find information you’ll be using the javadocs religiously whenever making minecraft plugins they help you a lot so if you’re ever trying to find something you just search it up here And it will come up so let’s say that i want to find information on fireworks so firework and now you have all these different things so you have orga bucket.entity firework bucket firework effect all this stuff here so it can be a little overwhelming if you’ve never used javadocs before but As a java developer you should know and should get familiar with this so this is uh going to be your best friend whenever you’re making minecraft plugins because you can’t really watch a video on how to do every single thing right although i’m trying to make videos on every single thing you cannot Realistically watch a video on every single thing you got to be able to you got to be able to do research on your own so you got to be able to read these javadocs and understand them to be able to make minecraft plugins okay so i’m ranting a little bit but That’s how you can look at all the different events on your minecrafts uh that are available to your minecraft plugins to listen to okay anyway so let’s go back here and uh let’s go to oh actually let me show you one more thing so if you want to find More information on one of the events let’s go to uh the player event so real quick okay so let’s go to player bed leave event so if we click on this we can see all the information for the player leave events we have get bed so this returns the bed Block involved in this event so they all have descriptions the the methods have descriptions so it’s cancelled gets the cancellation state of this event we’re going to learn about cancellation of events next episode um and then should set spawn location get if this event should set this New spawn location for the player set spawn location blah blah blah right and uh the one that we saw a second ago is the player join event so we can actually just search it so player join events and we can find information more information on how it works So there we go so it says called when a player joins the server and we can see get join message gets the join message to send to all online players set join message sets the join message to send to all online players so you get the gist of it right You can use these javadocs to get all the information on how these events work okay so i’m going to give you one more example of how to make an event listener in your plugin so this time let’s go to make a player leave event so event handler and then under that You’re going to have your method for your event listener so public avoid on leave bed and by the way as it just has a general design pattern you want to name your listeners like something like this so on and then describe what they do so when a player joins the server So on player join on leave bed or on bed leave you know something like that that’s just a pattern that people use okay so now we put inside of the parameter for this method the actual events itself that we’re listening for so player bed leave events and enter event we have Two different options here so we’re gonna do player bed leaf events e or event a lot of people just say e just as a shorthand it’s easier to access but i’m gonna just call it event for now okay so we’re gonna open this up and let’s see what information we can Grab from this so event dot and we can get the bed we saw this a second ago in the javadocs so we can get the bev we can uh set the spawn location we can do a bunch of stuff but but let’s go ahead and just get the player Associated with this event so we can do events.getplayer if we do control q here it tells you what it does so it says returns the player involved in this event and why because it’s a player based event it’s a player bed leave event so since the get player method returns a Player object we can store that here in a player object reference so player import player player is equal to event doggy player there we go so that so now that we have a player object we can do stuff so we can grab information set information All kinds of stuff so player dot and we can see all these different things here we can send the player a message we can close their inventory get their display name break a block get their xp their health we can chat we can do so much things we’re going to be using These very often obviously because it’s a plug-in right and players are instrumental to plugins so we can do so much and we can set so much look at all these setter methods here we can set so much about these players so really really powerful and really cool so um We’re going to keep it simple this episode though because it’s our one of our first coding episodes so we’re going to do player dot and we’re going to send the player message so this is how you send a player message so player send message and there’s different you know there’s different overloaded Versions of this method here but we’re just going to use the basic one here that accepts a string so if we do control q it says sends the sender a message or aka the player so parameter is the message message to be displayed so player does send message and we’re going to say You left a bed bed not bad oops and then we’re going to talk we’re going to say dork so this is a very mean plug-in we’re going to call them a door and we’re going to tell them they’re a dummy very degrading but anyway so now we have a new event listener here So this is going to be an event listener for when a player leaves a bed so you don’t need to do anything else because we already registered the events of this class here that’s what this does here so there we go we don’t have to touch anything so Let’s go ahead and recompile this and test it out okie dokie so the server started up here so let’s go ahead and join hopefully you cannot hear my spaghetti cooking in the background that’d be unfortunate i’m about to get some grub boy okay so we still see our welcome to the server You big dummy message so that’s really cool but now it’s going to test out our new event listener so that’ll be whenever we leave a bed so we’re going to need a new bed i just killed myself uh so we’ll do slash game mode creative looks like we can’t do that so We need to up ourselves so up cody simpson 99 greats there we go so now we can get a bet for ourselves we like red beds here in this family all right cool so we’re gonna put the bed down join it or get in it you can only sleep at night or during Thunderstorms oops let’s do time set night there we go good night join the bed nothing happens nothing out of the ordinary here now if you leave it it says you left a bed dork awesome right isn’t that really cool so now we’re uh doing even more cool Things we’re listening for when a player leaves a bed and let’s see if we can make this even more crazy right so let’s say that when the player leaves the bed we want them to die so we can easily do that since we have access access here to this player object we can Do stuff like changing the player’s health so player dot set health zero zero point zero if it’s a double but it doesn’t really matter of course so that’ll set the player’s health to zero obviously murdering them and then we can say something like um die there we go so now the player’s gonna Die great let’s rerun this and try again all right so almost burnt the apartment down but i’m back let’s go and join and see if this works so now if i get out of this bed it should kill me and the message should be a little different Let’s try it out boom there we go cody sims 99 died yeah you left a bed to die awesome isn’t that cool so there we go that’s how we make very very basic event listeners so we can listen for events that happen within our minecraft server again events are just Simply things that happen and then event listeners are just listening for those events so that we can run code or methods um whenever the event is listen are triggered in the next episode i’m going to show you how to do event listeners outside of your main Plug-in class and then also i’m going to show you how to cancel events so we’re going to do something a little more advanced next episode so hopefully you’re excited for that and good job if you got this done so far um if you made it through this episode I’m really proud of you let’s keep going keep chugging along um let’s become java developers i mean uh what am i saying plugin developers okay i’m super excited for you to become a plugin developer all right so i’m gonna leave the code for this episode in the description below so you Can check it out later come back to it if you need to see anything like if you forget something on how events work how to make an event listener you can just come back to the uh the code it’ll be all written up with comments and everything like that so you can You don’t have to re-watch the video if you don’t want to although your views are greatly appreciated of course so yeah make sure to check out that so bookmark it for future use and yeah also i’ll be leaving a link to our discord server in the description below So you can check it out and you can join our server get some new friends you can uh get some help on stuff that you need if you’re stuck on like how to make advantage centers or for some reason your plugin doesn’t work you can join the The discord server and join one of our help channels you can ask for help there okay or if you just if you’re lonely you need some friends if you don’t have any friends you can join get some friends there it’s a really great program programming community With a lot of people like you who like making plugins and like minecraft and all that stuff okay so join that one final thing is that if you want to support this channel you can click the join button below this video and you can join this channel as a Member for as low as 99 cents a month and you can cancel at any time um you get some cool perks like early access to all of my new videos including this one here a cool rank on my discord server so you can show everyone how swaggy you are And also you get to see yourself on the screen like you see right now so if that sounds cool to you feel free to join if not that’s okay too i appreciate you watching and that’s it so if you like this video leave a like if you see more subscribe and peace you Video Information
This video, titled ‘Spigot Plugin Development – 2 – Event Listeners, Javadocs, and Players’, was uploaded by Kody Simpson on 2021-07-03 21:18:16. It has garnered 55517 views and 1335 likes. The duration of the video is 00:23:25 or 1405 seconds.
In this episode, I show you how to listen for Minecraft events with event listeners, how to read Javadocs on a basic level, and how to work with the Player object. #Spigot #SpigotDevelopment
Code: https://github.com/Spigot-Plugin-Development-Tutorial/Event-Listeners-Javadocs-and-Players
⭐ Kite is a free AI-powered coding assistant that will help you code faster and smarter. The Kite plugin integrates with all the top editors and IDEs to give you smart completions and documentation while you’re typing. I’ve tried it and it’s awesome! https://www.kite.com/get-kite/?utm_medium=referral&utm_source=youtube&utm_campaign=kodysimpson&utm_content=description-only
Discord: https://rebrand.ly/discordlink Become a Member: https://www.youtube.com/channel/UC_LtbK9pzAEI-4yVprLOcyA/join Instagram: https://www.instagram.com/kody_a_simpson/ Twitter: https://twitter.com/kodysimp
0:00:00 – Intro 0:00:17 – Creating new project 0:01:41 – Event Listeners 0:03:05 – Event Listener in main class 0:08:50 – Event methods 0:11:11 – Reading the Spigot Javadocs 0:16:03 – PlayerBedLeaveEvent 0:21:30 – Outro
More Videos coming soon. Leave a comment for any future video suggestions.