Hey guys it’s Thomas here we take no vision and welcome back to some minecraft modding for 1.16 in this episode we’re gonna be covering events which is my personal favorite part of forge modding because where you start to get really creative with what you add to The game and all an event is is essentially a action in the game a simple action like hitting an entity or opening up a GUI screen just a simple action that when it occurs runs a little bit of code that you said that’s all an event is and we’ll be covering three in This video but I will show you how to find all the other events and hopefully you can do your own research and add some cool stuff to the game alright so to get started we want to come over to our package explorer and we want to Create a new package and we’re going to name this events of course as you would expect and instead of here we’re gonna make all of our classes that will hold events now you can organize these however you want based on you know whatever you’re making but I’m just Gonna show one class that has all of my events in it but just know that you can throw these in any different classes you want in any different order it doesn’t really matter as long as you have the the event bus subscriber tag which we’ll talk about so I’m gonna make it one Class here and I’m just gonna call it mod client events because these are gonna hold all the events that I want to fire client-side and we want to add to repository alright so inside of this class here first thing we want to do is register it with the event bus so to do That and by the way the event bus is essentially what manages and fires events for you so by registering this class of the event bus what we’re doing is we’re saying make sure to run the code that’s in this class when events are fired so to do that to register this Class with the event bus we can do at Madh dot events bus subscriber Subscriber and we should probably import mod first from minecraft forge event bus sub scriber and then we’re gonna pass in a few things in here so I guess you could just pass in dist doc client but we’re gonna actually make sure that we pass in everything just in case I don’t Want to mess anything up for you so just you mod ID is equal to your mana D so your main class stop mod ID tutorial oh that’s definitely we want tutorial dot my ID then we want to set our bus equal to mod dot can’t spell today month aught event bus Subscriber a dot bus and then you can choose between two here you could choose mod if you wanted to override events that have to do with like other people’s mods and stuff but that’s rarely the case I think for beginners so just click Forge that’s really gonna allow you to Use like minecraft and Forge events so leave this is Forge unless you know what you’re doing and then for value you want to set this equal to either client or server you can do this with dist dot and you can see here we have client and then Dedicated server now if you want your class to have events that are run server-side then you’re definitely gonna want to have a dedicated server set here but if you’re doing client-side then you’re definitely gonna want to put client I’m gonna put client because like I said here this is mod client events so We’re gonna have client events in here but I will show you about server events in a second so just keep that in mind but I would just put this as dock client because it’s very unlikely that you’re making a server-side mod especially if you’re a beginner so instead of here Whenever you want to create a new event or you know utilize an event you want to create a public static it has to be static very important void and you want to name this method just whatever it doesn’t really matter but whatever is like relevant to your event so the first Event we’re gonna make is going to be it’s really silly I planned it out earlier but I want to make it so that when the player jumps with a stick in their hand it sets the block under them to a ruby block just like a silly event but just Something for example and again these are all hypothetical examples so definitely you don’t have to copy these word-for-word this is just to get you comfortable with events so that you can make your own events so what we want to do is just name this something relevant like on jump with stick For example so what you want to do is pass into this an event now an event there’s lots of events one way that you can actually find all the events in the game is you can come to external libraries here you want to find Gradle forge 1.16 map snapshot and if you open This up and you just go to like nets minecraft forge then go to like event here and then let’s just open up like anvil update event what you can do is if you hold the control key you can enter the event class here and inside of here If we do your control H this will open up the hierarchy window and well this was not the best example here let’s go back to anvil update event and do control H there we go so if we right-click on our event class here which is the the superclass of anvil Update event we can right-click base on this class and you can see all of the events in the game to my knowledge I think I mean I might be wrong but I think this is pretty much all of them yeah I think that’s definitely wrong there’s definitely more than this but This is some of them you can also find more in like the client folder and the event folder all that sort of stuff but this is just a way to sort of like look at some of them and you can see there’s like block event GUI screen event sound Event tick event all these things and again they’re in these folders as well let me close out of here they’re in these folders like village you can find like village events you can find brewing events there’s like client events over here all sorts of stuff so definitely Check out this folder if you want to find the different events but I’m just gonna show three for now all right so let’s go back to our class here now we want to pass in our event in here so the event I’m going to use for this one is Going to be the living jump event so living jump event and we’re just going to call this event and make some braces now in order for this to actually get fired with the event bus we need to add the add subscribe tag event subscribe event rather now this tag you Have to add it to every method that you are using with events otherwise these event bus subscriber will not know what to actually look at when they’re firing events so very important always add this tag above every method that you are going to use an event in so now that we Have our tag it’s registered with the event bus and we’ve got our event we’re using the living jump event and we named it event now we can actually do something so this method this whole method here hopefully you know Java and you know what a method is but this Method is going to run every single time this event gets fired so every time an entity a living entity jumps everything in this event will be fired so you can do whatever you want here it’s super creative open-ended so what I’m gonna do to sort of visualize what I had planned Here I’m gonna first get the player and we can do that with living entity now a player is always a living entity so we can do living entity player and we can just set this equal to the player and we can get the player by doing an event dot And you can notice here that there’s a ton of stuff you can do with the events I’m gonna do a vent dot get living entity and that’s gonna get the player so now we have the actual player that jumped in this specific event here and Now what I want to do is make sure that the player is actually holding that stick that I mentioned because we only want the ground under them to change in this event at least for my idea to work if they’re holding a stick so to do that You’re gonna do if player dots and you can get all sorts of stuff for the player I’m going to get the held item in their main hand and then I’m just gonna get the item and so now I have the item that the player is holding their hand And we want to make sure this equals items dot and then I can just select like stick from this list here so we’re checking essentially if the player’s main hand is holding a stick if it is then what we can do is actually set the block under them to a custom block so One thing you can also do by the way and I’m gonna mention this just really quickly if you want to log like text to the console so that you can see our debug your events or see what’s going on behind-the-scenes come over to your main class here and you can turn your logger That we have from previous episodes from private to public it really should have been public to begin with I don’t know why I didn’t do that but set it to public if it’s not already and you can call it in your event by doing your main Class dot logger dots info and this will print some info to the console so whatever you want I’m gonna print just for you no testing purposes player tried to jump with a stick so that’s gonna print to the console every time this runs just in case you want to debug with That so now we can actually do the action so first we need to get the world so we can change a block in the world and we can do that by setting a variable world and let’s just import world here import class we can set world equal to Like player dot ghent get entity world and that will get the actual world and then to set a block in the world we want to do world dots set block state and then we need to pass in two things the location of the block you want to change And then what we want to change it to so the location is going to be our players location so we can pass it in with player dot get location or sorry it’s going to be actually get position but if you don’t have the most up-to-date mappings and I don’t actually think I Have the most up-to-date mappings because of the newest update this is actually going to be an unmapped function which is I know super scary it’s actually going to be func underscore two three three five eight zero underscore CY underscore this is going to be equivalent to get position Or get block position so we’re gonna use this but I’m just gonna add a little tag here or a little comment rather that is just gonna let you know that if you have updated mappings this method here is actually going to be equal to living entity living entity get position So yeah for right now this this method your get position is unmatched so it’s func underscore whatever but if you are watching this in the future from now it will be get positioned so do that instead of this weird method here just so you know but now that we have the Players position we want to get the block under them so that would be to get a block separate like a different location from the players location we can do dot add and you can pass in three numbers here one for XY and Z and we Want X to be the same but we want the Y value the Y block directly below the player so we’re just gonna do negative one for that and then zero for Z so all this is doing is getting essentially the players location but we’re getting the players location minus one y value so The block directly below them hopefully that makes sense but you can use this to essentially make small movement distances from the player and so yeah that is going to get the players location or at least the block directly below the players location now we want to set what we’re gonna change in the World so I want to change it to our custom block so we can your registry handler dots and then I’m gonna get my ruby block dog gets and of course we need to get the default state because we are changing the block state here and There we go so this event is all done see it’s not too scary essentially what’s happening here is whenever a player or rather an entity a living entity jumps this is going to get fired it’s gonna check if the entity has a stick hopefully the player does have a stick If they do then it’s going to change the block directly under them here to our Ruby block not too hard so yes there you go now I’m gonna show you a few other events and then we’re going to test all these out at the end of the video so the Next event I want to show you is a little bit more advanced and we’re gonna make a little space here and then by the way at the end of this video I will also show you how to cancel events which is very important so definitely wait and Watch that part as well so this next event is going to be just an idea that I’m coming up with right now I want to make it so that that poison apple object we made food objects I want to make it so that if you hit a like a sheep for example With a poison apple it actually poisons the entity I think that’d be kind of cool just for example purposes so what we can do is we can create a public static void and then I’m gonna name this like bond damage sheep again it doesn’t matter and the event I’m gonna pass in for this one is going to be the attack and city event and we’re gonna call this event and this is going to fire every time and entity is attacked by you know I think it actually attacks when like entities attack each other but this is mainly gonna be used For when like players attack an entity and again we do have to add that add subscribe event so that it does know that there is an event here being fired and we can add some code here so again hopefully you’re getting the gist of things anytime you want to add an event Just make a new public static method that’s void add the the tag here and then pass in the event alright so what I want to run here I first want to make sure that the player is holding the poison apple when they hit via the entity so if the event get entity living That’s gonna get the player dog get held item main hand dot get item I want to check if this is equal to the registry handler dot poison apple dot gets with some brackets here so we’re making sure that the player is holding the poison apple and if they hit the the actual Entity with a poison apple then first I want to make sure that the the person or the entity that they attacked is still alive because if it’s dead we don’t want the code around obviously there’s going to be a null pointer exceptions so if the event get targets that’s gonna get The target the the entity that the player hit dot is alive so if the the target is still alive then let’s first get the target as like a an object here so living entity we’re gonna call us target is equal to and then we can just cast to living entity the event Dog cat target and I’m doing that because get target gives you an entity and we need a living entity because it is and we’ve made sure that its living here with this check so we can actually cast it to a living entity but if you haven’t done this check here then you Definitely won’t be able to cast and so now that we have the target here we can make sure that the target is actually a sheep because I want this to only happen to sheep just again for example purposes so if target is an instance of sheep entity that’s going to make sure that The target is a sheep of course so if that’s the case I want a few things to happen first I want to add a poison effect so the way we can do that is we can do target dots add potion effects and we can pass in a new effect instance And this is gonna take in an effect dot and we can like pass in for example like effect dot poison effects rather sorry effects dot poison and then we can pass in like the duration this is in ticks so remember it’s seconds times 20 because there’s 20 ticks per second so I want This to run I want the sheep to have poison for like ten seconds so ten and then always multiply it by 20 because it’s in ticks so ten seconds times the amount of time is the tick value essentially and then for amplifier we can make it like poison to if we want With putting one here I’m not gonna set an amplifier because I wanted to just be poison one so yeah there we go that is going to give the sheep poison effects and I also let’s just see what else we can do if we do target dot there’s a ton Of stuff you can do here you can set it on fire that’s pretty cool you can set it on fire for a few seconds you can set oh my gosh all sorts of stuff set sheared you could shear the sheep whatever you really want but in my case Well let’s just make it glowing that sounds kind of fun set glowing to true that’s gonna make that entity glow maybe it’s like a radioactive poison or something and so now I also want to add just this event so I can show you guys I Want to send a message to the player in chat sort of saying that the Sheep isn’t feeling so well just as like a joke I guess so to do that to send messages to the player like during events you can actually do well I should mention this First in its this is really hard to explain but all events even though this is a client event because we have dissed our client all events are going to be run on the server and the client so actually all these events are running twice so when it comes to sending text To the player it’s actually going to send two text lines to the player because this is actually running twice not very important but if we want to prevent that every time you send text to the player you do want to make sure that it’s only sending client-side at least In this case so to do that and again this is every time that you’re just like setting text it’s not important other times you can check you vent dog get player dots get entity world dot is remote so this is going to check if the world is remote which checks if it’s Like a client or server and then you want to make sure you want to check if it’s false so if it’s not true if the world is not remote then you can send text to the player this will again make sure that text only gets sent one time So to send a message we can let’s first make our message with a string here let’s make our message read so to make a message colored you can do text formatting dot and you can set like a color here I’m gonna set red and then I’m going to make the text say like Let’s let’s do thinness theme so like that sheep isn’t feeling so well there we go so that’s the message and then to send it to the player we need to do player or well did we even get the player no we didn’t get the player so we need to get The player first let’s let’s make a player objects I believe this event has dog get player yeah I get player entities so we can actually do player entity this time instead of living entity much better player is equal to event get player there we go oh what’s going on here Event oh I put new definitely should not I put that event dog get player so that’s gonna get the player here and we can send the player a message by doing player dots send message I mean to send a message we have to also pass in a String text component so you want to pass in a new string text component and this is gonna pass in just the message we set earlier just this variable here message and we also need to pass in the players you you you ID which is a unique ID and we can do that with player dots get unique ID there we go so oh my gosh this event is so freaking long but let’s fast through it really quickly just to sort of really make sure that we understand what’s going on here so this event is going to be fired Every time a entity is attacked if the person attacked who attacks the the entity is holding a poison apple in their main hand so if they’re hitting it with a poison apple it’s going to first make sure the the entity the target is alive if it’s still Alive it’s going to make sure it’s a sheep and if it’s a sheep then it’s going to add a poison effect to that sheep for 10 seconds and it’s going to make the Sheep glowing and making sure that this is only done client-side it’s going to send the player message that is Red and says oh the sheep isn’t feeling so well so yeah there we go all good now the last event we’re actually gonna do before we test these out is I’m going to show you how to cancel an event and we’re gonna do that with like a GUI open Event so let’s come down here once again a new event which you can make a public static void I’m gonna call this on a crafting table open and we’re gonna pass in the GUI open events and just like you would expect this is going to fire every time a GUI Is opened and again we need that at subscribe event tag there we go so what I want to do is cancel this event so what you can do by canceling an event what you’re doing is saying like every time a GUI is opened here if you cancel It you’re making this event like you’re preventing it from happening so I guess the best way I could explain this is if you cancel it a GUI open event every GUI that you opened like a chest or a crafting table it would just never open because you’re canceling it as soon as The event fires you might think well why would you ever want to do that some cool things you could do with canceling is you could cancel a GUI open event if like a player opens a crafting table and you can make it so that like players can’t use crafting tables unless they Like unlock an advancement for example or like maybe they need like a special enchantment to be able to use furnaces all that sort of stuff is what canceling is for so to cancel an event first we need to make sure that the event is actually canceled a bowl so to do that We want to do if event that is cancelable because most events are not cancelable but this one in particular is I know for a fact so you all we always want to check this otherwise you will get errors if it’s not cancelable so now That we know that it is we want to make sure that this is a crafting table that there you’re opening so if the event get GUI is an instance of crafting screen I believe is the name and you could set this to whatever you wanted really you Could do like furnace screen there’s all sorts but I’m gonna make sure that’s a crafting screen so if the player is opening a crafting table then we can just cancel the event so they literally can’t open crafting tables you like an event set cancelled and set this to true That’s how you cancel event is event dot set cancel it and then set it to true I don’t know why you would ever set this to false that doesn’t really make any sense to me unless you were like I’m canceling the event although I don’t think that’s even possible so just that True and that’s pretty much it and maybe we can like set like some info to the log like we can get the logger here and then like send some info that says players rights you open a crafting table what a fool or something like that so just for our information all right so This is a really simple one but this is going to prevent us from opening crafting tables again hopefully you would want something more advanced than this like maybe they have to like unlock certain things I don’t know again it’s all creative process so you can add whatever you like here but yeah that’s What this event does and that’s how you cancel events and you can use this with any event that is cancelable of course okay so all of our events are done now before we test them I just want to mention one more thing here so in our Events package here I’m gonna make a new class and I’m gonna call this mod server events and I’m gonna put in here all of the events that are server-side only now it’s really difficult sometimes to tell if a event is server-side and sometimes you’re just gonna have to do trial and Error because Forge really doesn’t have a good list of these but what you can do let’s just copy this subscriber event bus subscriber here and paste it in so that this class is registered but instead of just our client we want this to be dis da dedicated server because This is a server events so what you can do is you can have these two classes and you can throw all of your client events in here and you can throw all of your server events in here and I’m just gonna show you really quickly had subscribe Events public static void so an example of like a server event like on server chat you could pass in server chat event is an event I believe there’s also server tic event which I think occurs every time the server ticks every every second or so and or I guess there’s 20 Ticks per second so it would occur 20 times per second but we could do like the server chat events and you could put some stuff in here I’m not gonna show this because you know you’re probably not making a server mod but that’s how you add server-side Events and you could you would hopefully throw them all in here but again make sure that you have dedicated servers that’s the dist the value here because otherwise it’s just not going to be a good time for you so definitely do that but now we are done with our events and We can run the game and test this out all right so we’re inside of the game and I’ve got some items to help us test our events now the first thing I want to test is this crafting table here that we cancel it essentially so as you can see If I right-click on the crafting table it actually and no longer opens because we did cancel the event if you are opening a crafting table so yes another crafting table literally just does not work anymore in the game but you can see that like a chest do UI or like the Inventory all that stuff still works so it’s only a crafting table is just like we set which is working great next thing I want to try out is the stick so you can see here that when we’re jumping nothing is happening and it’s just regular minecraft vanilla mechanics but We did said that if you hold a stick it would add a Ruby to the ground a ruby block so let’s try that on right now and there we go so every time we jump with a stick in our hand it does actually add or set the block below us To a ruby block and we can literally just like spam Ruby blocks here and it should actually break that as well so yeah so this is working great and again if we’re holding like other objects it doesn’t work so it’s only sticks just like we said so that’s working great now The last event that we want to check out is the the poison Apple one hitting sheep so if you see here if we hit a a pig for example nothing’s happening if we hit the pig with the poison apple nothing happens Oh where’d you go nothing happens that’s Normal we set it to that this only works on sheep if you remember so if we get a sheep here and we hit it with our hand you can see nothing happens just regular mechanics but if we use our poison apple there we go it says in the chat the Sheep isn’t feeling so well it does have the poison effect as you can see is taking damage and it’s now glowing just like we set and we can keep hitting it till it dies and this will work and you can tell here that it’s just the poison apple that does This effect so it’s sort of like poisoning the sheep I guess but yeah so hopefully this sort of gives you an idea of how you can make really advanced items like you can see the stick here now is like a way to generate Ruby blocks for us and this poison Apple is Now much more advanced than it was before so get creative check out all those events that you can find in the game and the source code and just try out stuff to your heart’s content that’s really the only way you’re gonna learn but yeah thanks guys so much for Watching this episode and it we’ll see you in the next episode Video Information
This video, titled ‘Minecraft 1.16: Forge Modding Tutorial – Events (#15)’, was uploaded by TechnoVision on 2020-07-21 16:01:23. It has garnered 15473 views and 465 likes. The duration of the video is 00:28:23 or 1703 seconds.
Learn to code a Minecraft mod from scratch in this complete tutorial series! In this episode, we use events to add some really unique properties to our game. Make sure to use events to help make your items and blocks more advanced!
— Important Links —
● Discord: https://discord.gg/ZsB3Eha ● GitHub Repository: https://github.com/TechnoVisionDev/Minecraft-1.16-Modding-Tutorial.git
— Music —
● Chill Out Media – Minute Mix
— My Channel —
● Subscribe: http://tinyurl.com/zbc7mwy ● Instagram: https://www.instagram.com/tomm.peters ● Twitter: https://twitter.com/TechnoVisionTV
#minecraft #modding #tutorial