Hello everyone this is nunu from pre-emptive earth net and today on this video of our minecraft plug-in development series we will be looking at how to bring stuff to the console and how to register events if you have any questions feel free to leave a comment And as usual the source code will be linked in the description so stick around and don’t forget to subscribe So the first thing we want to do is to go to our intelligent idea and open our project and we are going to open our main class in here and for this video and for the next videos I will be increasing the version number to the Number of the video just for an easier way and easy way to manage the files but basically when the bucket or spank it starts our server it will look for some lifecycle methods if this method exists it will run at specific stages and as an example we are going to create an own Enable lifecycle method so lovely void on enable and basically when our server starts it will run this method here and any code that we get in here so do you remember that on our last video we had the message pop into the console saying tutorial is valid this message was from Spangle itself and not our test message here we can add our custom message and imagine that on more complex plug-in you are doing a database connection you are doing a web request you are loading a config file and we when you need something to happen when the plug-in is Starting you can do it Iran on a naval method so on this case we are going to do get server this is a function from bucket and spagett that gets you access to most stuff on your server so we’re going to do get console sender so we can Send a message to the console and we’re going to do send message we could just write like this is a test message from our login on in Mayville and these will appear on our console but if you want to have this message looking better we can be like Is a shot-caller dot red now green science it’s an enable message and this message will appear green in our council but please note that you can use this on all sending messages to players – we’ll see you later how to do that but you can send colored message to the console and It will probably show up in a Windows PowerShell or on Linux patch but most most web hosts include including three empty server dotnet will not display this color on the console so we can also have a public void public for it on disable and this is another life cycle Methods that will run when the plug-in is disabling so here you will probably save your config files save your database save some server states that you’d want to keep when the server starts again because let’s say we have a bunch of variables here you had the server running and setting stuff let Variables when the server stops that variables are clean you don’t get that variables unless you save them we will at a later date see how to handle config files on to read and write the config files but on this case I’m just going to do again get Server dot get console Center dot send message and this time I’m going to do shed color dot red and I’m going to say this is our plugin being disabled so basically when we run this plug-in on our server when the server starts spying it will look on our main class the one That we set on here the main class to look for unable method if he finds one it will execute in this case it is going to send this message to our console in green same thing for when the server stops it will look for an on disable and send this message to our Console these are the main two life cycle methods so let’s compile our plug-in and make a test so it has built so I’m going to go here and go to the folder where our plugin is I’m going to copy paste it in our plugins folder and I’m going to start our server just first reset not reset how do I do this in Windows yeah now I’m going to start our server and we should see at some point our custom messages in on enable and on disabled so how you can see here that we Have message showing that our plug-in was enabled is the message from yesterday it just says 2.0 here and after that it says the test this is a test message from our plug-in on a novel in green so the it’s here bucket as executed this code here and when we stop Our server we should see a DS our plugin being disabled in red so I’m going here I’m going to type stop these are plugging being disabled so our code world and we have code being executed on enable and on disabled of our server so the next thing we are Going to talk about art events where our events events are basically things that happen in your server that will trigger some code to run on your play let’s say a player places a block that will children event a player braids a block that will trigger an event a player moves That’s another event you can resistor those events and when something happens you can get that notification on your code and execute some of your code and in response to that event so the best way to know what events there are used to go to the spy gate documentation I will be including this link on the description but if you google spying at event list or a spy gets Javadoc you will probably find this so here we have all classes and basically all the API for plugins but for now we are going to look for events events so we have a Bunch of events here so hora got bucket dot with rented block events relating to block so basically you look in the description and what it’s probably going to be like if you are looking for something that happens when something happens to a block you probably want to Look in here and not here but for this example we will look at a plot break events and that’s an event that is triggered when a block is destroyed by a player so we are going to enter the block events then we are going to look for block break events it’s the first One as you can see here it has a little description on when this happens and we are going to go to the block break event here it has a bunch of explanation of an events and if you can see here it see it said it implements the interface cancel Cancel of all these means that this event can be canceled when an event is cancelled the action that made this event happen you will no longer happens so let’s say I cancel the block break event then the block that a player tried to destroy it will reappear like when You have protected re in your server and someone breaks the block and the block comes back and shows a message like you can’t break this they’re basically cancelling the block break event some events like the most common ones will include some more information like this and all of them Will have this constructor here the constructor will be a way that you call the event and the information it is going to give you so when you register a listener and we’ll talk about vision in a bit but when you register a block break event listener It will pass you an instance of a block and an instance of a player so the block that was broken and the player that broke that block so now we are going to go to our IntelliJ IDEA and you have stuff a little bit organized I’m going To create a new package and I’m going to create this list nurse this will be a package that will contain one class for each listener we have so we have the code organized a listener is basically a code our code that will listen to an event so I’m going here I’m going to Create a new class and I’m going to call this block listener so this class will listen for event related to events related to blocks in order for this to actually receive any information about events we have to implement the listener Clause implements the listener class and We have to import our class from org bucket event so science we now want to register our block break event we can see how the constructor looks like in here so we know what we are going to receive with our event so we are going Here and we are going to do public void break block this is just a name of the function it is not it’s not the name of the event but now we are going to set the name of the event we’re going to register lock break event and now I Forgot here basically this will is our function it is named break block it will receive a break block event that will be saved to the variable named E so here we can do something like e dot get player this is the player that is here on a constructor the player that Has broken the block so we can like to get player dot and we can see what we can do to the player you can do a bunch of stuff to the player so when you have an object from buckets in this case player object you have a bunch of Features that you can do to it and you can pass this player object to other functions but we will look into that later but let’s say we want to send a message to the player so send message and here we can do again shot caller dot And we will use aqua and say you just broke a block and for these two app or for this to be actually picked up by spagett we need to make two things first one will be to set this as an event handler we just add add the event Handler decorator so we go on the line before our declaration and we do at event handler these will tell spagett that this here is an event handler and we’ll handle the block break event you can have multiple event handlers here you could do like this not On the same name here but if you did like another it will work it will trigger this and this and you can have event priorities to sell like these will run before these but we don’t we were not going to try that now so we have our Event handler here on our block listener class that implements a new listener class and now we need to register this block listener class into spagett so it knows that this class will have event handlers inside that will intern and the events so remember that I said when you Initialize something you will do it on the own enable so we are now going to initialize our block listener and on our non enable will be registering it so spying it knows that that class will end 11:00 so in order to register an event we are going to do again like get server Dot and now science this is a plugin get plugin manager plugin manager then we want to register events and how do we register events we have to pass an instance of our listener class so an instance of block listener so we are going to makes a new block listener log Listener and make sure you are importing the right one this will be different for you but it has to be inside somewhere your code and not on a library so we are going to import this comma and now you have to pass an instance of the plugin Signs the onion able is the peas on the plug-in itself and by plugging I mean the class that extends Java plugin we can just say this and Mister events and not raiser event reader events so this should be an egg for us to ever plug in Send these message to a player when it breaks a block so let’s compile our plug-in upload it to our server and give it a try so the plugin has built so i’m going here on the target folder that is a folder where the plugin was compiled I’m going to copy the plug-in go over to our plugin folders you’ll eat these paste to plug in here and i’m going to start our server again so our server has started so let’s make some tests so whenever we break a block we should get a message saying that we broke a Block and so we are here let’s just break some blocks you just broke up look you just broke a block so our plugin is picking up the event and doing what we told him to do so if you go here now you can see that the message is here so this Event is working now let’s say that instead of sending a message we are going to change these to red and let’s say you want to when I break a block instead of the block just disappearing and breaking normally we want to cancel the event so the block is not destroyed And then we want to change that block to a sponge so let’s say I break a block and it instantly turns into a sponge so we can do it like a dot set canceled – these will cancel event so if the block is not actually break and now we can do Like e dot get block this is the block that was on the constructor on our constructor here tgd the block that was broken that is no longer broken break as we cancel the event so we are going to get a block and we are going to set type to material dot And we are going to go for sponge so Wendy’s event happens it will send a message in red saying that we broke a lock and it is now as punch then it will cancel the event so the block is no longer broken and is still there and Then it will get the block and set it to a sponge so let’s build our plugin and give it a try so plugin is built so I’m going back and do the same thing I’m going to copy this plug-in drop it to the plugin folder replace the file I’m going to start our Server again and wait for it to start and then we’re going to join and when we break a block it should send us a message and then turn the block into a sponge so our server and started let’s join our server again and give it a try So when you when we break a block it should come back and turn into a sponge and we should get a message in our chat you just broke a block and is now a sponge it’s working if you break a sponge it turns into a sponge again so Yeah let’s try something if I go underwater and break a block underwater will it pull in the water yep it works and now imagine that we want to make it so if we break any block that is not a sponge it will turn into a sponge but if It’s a sponge the block will break and will give us some eaten so we’re going back into our idea and make that logic work so close these close these so we’re going to get our event I’m going to make a condition an if condition if our block Dot get material get now get type this will return a material and to know the what it will return you can’t get your mouse clicking it and ctrl Q and it will give you this pop-up window so it will tell that will return you the material so Also when you are writing it will get type you can see here it will return a material so if our material outlet is from is equal to material dot sponge it’s a sponge else it is not a sponge not a sponge so when a player breaks a block if it is a Sponge the code inside these brackets will be executed else that mean if it is not a sponge this code here will help us be executed so we want these code that we had before to be executed when it is not a sponge so it is not a sponge so we Are going to study the spent as a sponge and when it is a sponge we are going to send a message to the player that says you just broke a sponge and take this take this and we are going to change the color to green and now we are going to Add an item to the player inventory how do you do you do that so we are going to get a player from the event and then we are going to get inventory get this will be the player inventory will be an object of player inventory so now you Have the player Inventure what we want to do now we want to add an item items in Minecraft are items text items tags are like a bundle of the material material is the item itself that you see on the creative menu it has a quantity so one item two items three items Can have some metadata that will be duration of the item like us or sorts if it’s damaged or not at big eggs if it’s damaged or not some info like enchantments the name of an item that you can change out the interview will not be changing that right now but it’s Possible it is part of the item stack class we will look into it on a later video so now we want to add material dot and what are we going to give the player we can give him a Golden Apple if the player breaks breaks sponge but We need to do another remember that I said you need to pass an item stack I was best material so that’s why it is another line here in red so we need to remove this and now we we need to pass a new item stack and here we’ll pass the Material so material material dot Golden Apple Golden Apple so when it is a sponge we are going to send a message we are going to get a player then we’re going to get a player inventory and add an item stack will generate a new item stack based on material dot Abel and in This case we will not we will not cancel event so the sponge will be destroyed if it is not a sponge then we are going to give a message cancel event so the block is still that is still there and then we are going to change that block into a Sponge so let’s build or plugin and give it a try so our plugin has built so we are going to get the target folder and again we are going to copy these and paste this into our plugin folder we are going to start our server So our server has started and now we are going to test if our code works so refresh our server connect to our server and remember when we break a block that is not a sponge it should work the same way it was working before to give us a Message and turns went into into a sponge so you just broke a block it is now a sponge but if we break these sponges it should give us an a golden apple and should not reappear as a sponge or as anything should just break the block you just broke a block take This and it broke the block and babies and called an apple so our code is working as intended to golden apples and now if we go here and we write these golden apples and then we break these and the water is gone then we break this And we get another golden apple so this is basically how you can register events send messages to the console and to the player it may seem trivial but if you if you if you look back to it in this video you just learn how to register events how to Find what events there are how to add items to a player inventory out to log stuff to the console and how to send messages to a player so basically it’s this for this video and on the next video we will be talking about something that you are probably wondering how do You create commands how do you register I recommend so you can do like my commands and something happened from your plugin trigger some code do something this will be what we are going to do on the next video so stay tuned and don’t forget to subscribe so you Know when there is a new video on the series thank you Video Information
This video, titled ‘FreeMcServer.net – Creating Minecraft Plugins – Events – [2]’, was uploaded by FreeMcServer on 2019-11-08 16:00:12. It has garnered 2567 views and 61 likes. The duration of the video is 00:26:45 or 1605 seconds.
In this video you will learn what are events and how to use them.
Create your free Minecraft server: https://freemcserver.net
Spigot JavaDocs: https://hub.spigotmc.org/javadocs/bukkit/overview-summary.html
Source code as-of this video: https://github.com/nfacha/TutorialPlugin/tree/video-2
Forum: https://forum.freemcserver.net/index.php?forums/creating-minecraft-plugins.30/