All the Java you need for Minecraft Modding — Transcript
Full transcript
- 0:00Thought you want to make a Minecraft
- 0:01mod? You opened the tutorial, looked at
- 0:02the code, and went, "What is any of
- 0:04this? What's a class? Why is everything
- 0:06wrapped in angle brackets?" That's what
- 0:09this video fixes. This is not a full
- 0:10Java course. I'm not teaching you to
- 0:12build a banking app. I'm teaching you
- 0:14exactly the Java that shows up in mods,
- 0:17nothing more. With Minecraft examples
- 0:19the entire way through. By the end,
- 0:21you'll be able to open any mod source
- 0:23code and actually read it. Let's go. A
- 0:25variable is a named box that holds a
- 0:27value. That's technically the whole
- 0:29concept. Integer is the type, basically
- 0:31what kind of value goes into that box.
- 0:34Block hardness in here in our example is
- 0:36the name, three puts in the box,
- 0:38semicolon [music] ends that statement,
- 0:40and if you forget the semicolon in Java,
- 0:43you'll die. The types you'll be using in
- 0:45modding are the following ones. Integer,
- 0:47which are whole numbers, float for
- 0:49decimals, double, more precise decimals
- 0:53used for coordinates for example,
- 0:54boolean is true or false, and string
- 0:56[music] is text, a capital S with double
- 0:59quotes. Java is strictly typed. So, once
- 1:01a variable is an integer, it stays an
- 1:03integer. You can change the value, but
- 1:06not the type. Now, the one capital
- 1:07suddenly destroy your code. Now, this
- 1:09might look fine, but it doesn't work.
- 1:12With strings, the double equal [music]
- 1:13sign compares memory addresses, not
- 1:15text. So, always use dot [music] equals
- 1:18to compare two strings with each other.
- 1:20Burn this into your brain. Numbers and
- 1:22booleans use double equal signs,
- 1:25>> [music]
- 1:25>> and strings use dot equals. Forget this,
- 1:28and you'll spend hours wondering why
- 1:29nothing works. If and else loop. Every
- 1:32mechanic in Minecraft is built on if
- 1:34check. If the block below you is air,
- 1:36you fall. If your health is zero, you
- 1:38die. Right here we have some code where
- 1:41it shows that if a player's health is
- 1:43below six, it gives you a regeneration
- 1:46effect. But if the player's health is
- 1:49below 10, it gives you absorption
- 1:50hearts. Else, it does nothing. Condition
- 1:53in parentheses, A in curly braces, and
- 1:57the chain else if for multiple cases.
- 2:00These are the operators you can use.
- 2:02Basically, if it equals that, if it
- 2:04doesn't equal that, if it's bigger, if
- 2:07it's smaller, if it's bigger and equals
- 2:09that, if it's smaller and equals that,
- 2:11then the double and and is an end, and
- 2:15the double lines is for an or, and the
- 2:20exclamation point is a not. Loops. Do
- 2:23something multiple times without writing
- 2:24it five times. [music] The one you use
- 2:26the most is for each. By looking at the
- 2:28code, we can see that for each item
- 2:30stack called stack in the inventory, if
- 2:32the stack is diamonds, it's to shrink it
- 2:35by one. Then we have the classic for
- 2:37loop, when you know the count.
- 2:39Basically, we have the four, we state
- 2:41the variable the integer e equals zero,
- 2:44then if e is smaller than five, and then
- 2:47also e plus plus, which basically adds
- 2:50plus one to the e every time it runs.
- 2:52So, this will run five times. The
- 2:54control keywords break exits the loop
- 2:56entirely, and continue skips the next
- 2:59time around. Both save you from deeply
- 3:01nested ifs. Methods. A method is a named
- 3:03block of code that does one job. You
- 3:05write it once, call it whenever you need
- 3:07it, like a crafting recipe. It has a
- 3:09name, inputs, basically parameters, and
- 3:12a result, a return value. Right here we
- 3:15have a method that's basically an
- 3:17integer.
- 3:18With integer, it's basically what it
- 3:20returns after the action is done. In our
- 3:23case, it's an integer. The get damage is
- 3:26the name of the method, and the boolean
- 3:29is critical is the parameter, or yeah,
- 3:32is the parameter, or what we named it
- 3:34for, it's a variable, which is checked,
- 3:36and return sends the value back. Let's
- 3:38say we call that method, it would, in
- 3:41our case, maybe return a 10 if it's a
- 3:44critical. And if a method returns
- 3:46nothing, you should use a void. So, void
- 3:48apply poison, basically, it just applies
- 3:52poison to the player, and it does not
- 3:53return a value. Quick bonus,
- 3:55ternary operator
- 3:58is a compact if else that returns a
- 4:00value. You'll see it everywhere. So
- 4:03basically it's again the classic integer
- 4:06damage equals is critical. This is where
- 4:09everything changes. Is critical
- 4:10{question mark} 10 {double point} five.
- 4:13Basically if it's a critical use 10
- 4:17otherwise use five. It's a built-in
- 4:19statement. Moving on now to the classes
- 4:22and objects. Now this is the big one.
- 4:24This is what Minecraft is built on. All
- 4:26blocks share properties. Hardness,
- 4:28sound, can be broken but each time has
- 4:31different values. Dirt itself obsidian
- 4:34is hard. Minecraft is a block class, a
- 4:36blueprint. It defines what a what all
- 4:39blocks have. Each specific block is an
- 4:41object, a concrete thing built from what
- 4:43the blueprint within its own values. A
- 4:45class equals a blueprint. An object is
- 4:48the thing built from that class or the
- 4:50blueprint. Right here we have a class
- 4:52where basically we're giving it a bunch
- 4:55of properties. Right here we have three
- 4:57things. We have fields. Basically this
- 4:59private float destroy speed and private
- 5:01sound type sound type. Fields are
- 5:02basically variables each object owns. It
- 5:05has own and different variables and yes
- 5:08then the constructor is a special method
- 5:10that runs when you create the object.
- 5:12Same name as the class and no return
- 5:15type. And last but not least the methods
- 5:17we also mentioned before is the actions
- 5:19the objects can perform. But now let's
- 5:21say you want to create an object. You're
- 5:23going to use new. So basically a block
- 5:26is the class name then you name what you
- 5:29want to name the block basically a dirt
- 5:30block equals a new block and
- 5:33properties.of.strength
- 5:35and then you set all the different
- 5:36properties you want which we also have
- 5:38inside of the constructor from before.
- 5:40Now in our case we have changed the
- 5:41strength basically the hardness of the
- 5:43block and we put 0.5 and for a stone
- 5:46block it would be 1.5 since it's harder
- 5:47than a dirt block. So, two separate
- 5:49objects, but it's the same class and
- 5:52different values. The keyword this we
- 5:54saw before refers to the current object.
- 5:56You'll see it when a parameter has the
- 5:58same name as a field. So, a public void
- 6:01set energy integer energy, this refers
- 6:05to the object. So, for example, dirt
- 6:07block.energy equals energy. This is the
- 6:10field on the object, basically the
- 6:12parameter. It's something you set
- 6:13yourself later. Then, moving on, we have
- 6:16the inheritance. The most important
- 6:18concept we're going to probably be
- 6:19covering today. If there's one thing you
- 6:21need to be understand, it's this.
- 6:22Inheritance lets one class be built on
- 6:25top of another. The child class inherits
- 6:27everything from the parent, all fields,
- 6:30all methods, and can add or change
- 6:32things on top. When you make a custom
- 6:34block, you extend block, you get
- 6:36everything block does for free. You only
- 6:39write the parts that are different. That
- 6:41way you don't need to write everything
- 6:43from zero. Instead, you can just use
- 6:45what is already there. Let's say we have
- 6:46a public class my block extends block.
- 6:49Basically, now you have everything
- 6:51[music]
- 6:51that the block class has, and you can
- 6:54just add whatever you want after that.
- 6:56You can change the hardness, the sounds,
- 6:58the placing, the breaking. You can also
- 7:00just add a custom method in there that
- 7:02will give it a custom property. Super
- 7:05means the parent class. [music] You must
- 7:07call super in your constructor to pass
- 7:08values up. Forget it and Java errors
- 7:11out. Want to change one behavior? You
- 7:13are going to use override to override
- 7:15that method. And welcome block equals
- 7:17speed boost. [music] So, for example,
- 7:18let's say we have a speed block that
- 7:20extends block again. Basically, we have
- 7:23everything that's from the block class,
- 7:25and we're going to override the step on
- 7:28method. At override is an annotation. It
- 7:31tells the compiler this should be
- 7:32overriding something. So, [music] typo
- 7:35your method name and the compiler
- 7:37catches this instead of suddenly
- 7:38creating a useless method. Always to o
- 7:41at override when overriding, [music]
- 7:42always. The super.dot.step on calls the
- 7:45parent's original version, useful when
- 7:47you want to add a behavior without
- 7:49replacing it. And this is, to be honest,
- 7:52the entire secret to modding. Modding
- 7:54growth tons of classes, you extend
- 7:56[music] them, you only write what's
- 7:57different. That's inheritance. Anyways,
- 8:00let's go on to interfaces, but just
- 8:02briefly, a quick one. An interface is a
- 8:05contract that says you must be able to
- 8:07do these things. A class signs the
- 8:09contract with implement. It can only
- 8:11extend one class, but implement many
- 8:13interfaces. For example, right here we
- 8:15have a an armor. Basically, it extends
- 8:19the item. And since but because we
- 8:22cannot extend more than the item, we're
- 8:24going to use implement, which implements
- 8:26interfaces. Some interfaces are just
- 8:28markers, like vanishable. Basically,
- 8:30it's just labeled something up for the
- 8:32curse of vanishing. Others require
- 8:35methods. Recognizing implements in code
- 8:37is what matters. You add it
- 8:39occasionally, not often, but it's also
- 8:41quite useful every once in a while,
- 8:43especially when you use it for armor or
- 8:47some any other stuff you can think of.
- 8:49Now, static versus instance. This
- 8:52confuses everyone. Instance, each object
- 8:55has its own copy. Each block's hardness
- 8:57is player's health. Basically,
- 8:59everything is different. For static,
- 9:01it's one copy, [music]
- 9:02shared by the whole class. It belongs to
- 9:04the class itself, not to any other
- 9:06object. Why is Minecraft's blocks
- 9:08registry static? Well, public static
- 9:10final block magic block equals register
- 9:12register register. Public registers and
- 9:15registerable block reduce location from
- 9:16space. Built-in registry.dot.block is
- 9:19itself a static field. There's only one
- 9:21game, one block registry, one list of
- 9:24all blocks, not one per object. It
- 9:26belongs to the class. So, you call the
- 9:28static methods on the class name, not on
- 9:31an object. Right here we have two
- 9:32examples for both static and an
- 9:34instance. Integer clamp equals
- 9:36math.clamp value zero to 100 as a static
- 9:40on the class and float speed equals my
- 9:42block the destroy speed is an instance
- 9:45on an on a on an object and it's
- 9:48specified for for that specific block
- 9:50which is my block. Rule thumb,
- 9:51registries, constants, utility methods
- 9:55are static. Block properties, entity
- 9:57possessions, player health are
- 9:58instances. Also, public static final is
- 10:01the standard pair for these registry
- 10:03entries. Final just means it can't be
- 10:05assigned after creation. It's a
- 10:07constant. It's [music] not changeable.
- 10:09Now, let's move on with null. And no, I
- 10:11don't mean the unstable SMP [music]
- 10:13null empire, but I mean null, the number
- 10:15one mod crash, the null pointer
- 10:17exception, the most common crash in
- 10:19modding. Null means no object. A null
- 10:22variable points at nothing. Well, a
- 10:24method on nothing is a crash. So, when
- 10:27there's a player in the instance and
- 10:29there [music] is nothing found, there is
- 10:31a null and the game crashes. The fix to
- 10:33all of this is a null check. Basically,
- 10:35we set an if statement so when the
- 10:37player is null, [music] if it enters,
- 10:39exit. That way, it won't execute what
- 10:42comes after it. How do you know what
- 10:43might be null? There are two ways.
- 10:45First, look for the nullable annotation
- 10:48in the method signature. If you see
- 10:50this, check for null. Second, common
- 10:52sense. Anything that searches might find
- 10:54nothing. For example, get the player
- 10:56name. What if they're online? Get block
- 10:59entity. What if there isn't one? These
- 11:01might return null. When you do crash,
- 11:03read the stack trace. Look for the first
- 11:05line mentioning your mod's code. For
- 11:08example, if there is a
- 11:09java.lang.NullPointerException,
- 11:12[music] there should be something
- 11:13pointing towards a line in your own mod.
- 11:15For example, in this case, it would be
- 11:17magicblock.java:47.
- 11:19Open the file, go to line 47, figure out
- 11:22what's null. This is the most valuable
- 11:24debugging skill you'll learn in this
- 11:25entire crash course. Casting and
- 11:27instance of. Minecraft works with
- 11:29general types. Events give you [music]
- 11:31an entity, but you usually want a
- 11:32specific type like the player.
- 11:34Converting from general to specific is
- 11:37casting. Now, if we have entity entity
- 11:41equals any.getentity,
- 11:42there are like tens of [music] different
- 11:44entities Minecraft can pick from. But,
- 11:46since we only want to have an instance
- 11:49of a player, we uh in [music] if
- 11:51statement. So, this pattern is matching.
- 11:54Modern Java does the check and the cast
- 11:56in one line. Mojang uses everywhere in
- 11:591. 20 plus. So, use this everywhere,
- 12:02too. Instance of also safely handles
- 12:04nulls. So, if entity is null, it returns
- 12:07false instead of crashing. That way, if
- 12:09you have an if entity instance of player
- 12:11player, it checks on [music] if the
- 12:13entity is a player and it passes to the
- 12:16next line, which would be sending us
- 12:18this message. Collections. [music]
- 12:20List and map. Two collections you need.
- 12:22List and map. List, ordered items at
- 12:25positions. Duplicates allowed like an
- 12:27inventory. You can think of it like
- 12:30this. Here we have an array list, which
- 12:32basically includes an item stack of
- 12:34diamonds and three gold ingot. And for
- 12:37[music] each item stack, it should do
- 12:39something. There we have a for loop.
- 12:40Then, map key value [music] lookup. Map
- 12:43is basically like a dictionary. Use list
- 12:46when order matters and you iterate. Use
- 12:48map when you want fast lookup by name or
- 12:50ID. The item stack and string integer
- 12:52[music] are generics. They tell Java
- 12:54what type the collection loads. Try
- 12:57adding a string to a list item stack and
- 12:59the compiler stops you, but caught
- 13:01before the game runs. That's all you
- 13:03need to know about generics. When
- 13:04[music] you see something after a type,
- 13:06it means this works with something. Now,
- 13:09the weird syntax. Lambdas, chaining, and
- 13:12enum. Three patterns that might look
- 13:14intimidating, but are simple once you
- 13:16see them. Number one, method chaining.
- 13:18What happens here? So, basically right
- 13:19here, we have the block behavior, for
- 13:21example, for properties of a block,
- 13:23where we have three different methods
- 13:25that are being called from the block
- 13:27behavior. Right here we have the
- 13:28strength, light level, and the sound. We
- 13:30saw that before when we were creating a
- 13:32new object. Each method returns the same
- 13:34object, so you can immediately call
- 13:36another method on it, like Lego. You see
- 13:38this everywhere, block properties, item
- 13:40properties, entity builders, just read
- 13:42top to bottom. Number two, lambdas. The
- 13:45bracket with an arrow syntax. Basically,
- 13:47right here we have a lambda, a server
- 13:48living entity.afterdeath.register
- 13:51entity, {comma} source, and then we have
- 13:54an arrow, which basically runs whenever
- 13:56something dies. A lambda is a timely
- 13:58nameless method passed as a value. Here
- 14:01we have the event system a chunk of code
- 14:03to run later. When something dies, run
- 14:05this. The bracket with an arrow means
- 14:07lambda with no parameters. State with an
- 14:09arrow means lambda with one parameter
- 14:11called state. The stuff after the arrow
- 14:14is the body. Moving on, enums, number
- 14:16three. The fixed sets of named values.
- 14:19As already mentioned, enums are the
- 14:20fixed sets of named values. Basically,
- 14:22right here we have two, where we have
- 14:24the direction and the rarity. For
- 14:26direction, the direction to north, which
- 14:28is basically a number, which is
- 14:30basically zero, since we are facing at
- 14:32the default direction, the
- 14:33straightforward, and the rarity, which
- 14:36is rarity.epic, [music]
- 14:37which means something else. So,
- 14:38basically, instead of zero means north
- 14:41and one means south. You get readable
- 14:43and possible to misstep names, and this,
- 14:45of course, pairs up beautifully with
- 14:47switch. Right here we have the switch,
- 14:50where, basically, we can automate what
- 14:53happens when something [music] happens
- 14:54uh for specific stuff. For example, if
- 14:57something is facing towards north, it
- 14:59should do the north stuff. If it's
- 15:00[music] facing south, it should do the
- 15:01south stuff. And, of course, the default
- 15:04if. That's it. Three patterns [music]
- 15:05demystified. Next on, we have events,
- 15:07where it all comes together, the finale.
- 15:09Events are our fabric. Forge and your
- 15:11forge lets you hook into everything
- 15:12Minecraft does. Every player death,
- 15:14every block break, every [music] tick,
- 15:16the Fabric API and all the other APIs
- 15:18fire events. Your mod listens and runs
- 15:20code. In Fabric, there are no
- 15:22annotations for this. You register a
- 15:23listener, usually a lambda, inside your
- 15:25mod's initializer. The pattern is always
- 15:28the same. Grab the event, call dot
- 15:30register, and hand in the code to run. A
- 15:32real event listener, and what you have
- 15:34reconceptualized shows up, is this one.
- 15:36This this here is an example of
- 15:37everything we spent today implemented
- 15:39[music] into one class. Basically, on
- 15:42initialize, which I think probably you
- 15:44have seen this already multiple times,
- 15:45the server living and living entities
- 15:47events after death that register, entity
- 15:49damage source. Basically, every time
- 15:52anything dies, it should run this code.
- 15:54And if the entity instance of is a
- 15:56player, so basically every every time
- 15:59something dies and it's a player, it
- 16:01should run this code. Basically, we also
- 16:03have a null check. We have again another
- 16:06if statement. If the killer is the
- 16:07instance of zombie, so if the player
- 16:09dies and the killer is a zombie, it
- 16:11should send a system message which says
- 16:13a zombie got you. So, as you can see, a
- 16:16lot of stuff we mentioned in this
- 16:17[music] video gets mentioned into this.
- 16:19A class implementing an interface,
- 16:20basically the mod's initializer, an
- 16:22overridden method on initialize, there's
- 16:24your at override, a lambda passed to dot
- 16:26register, the listener [music] itself,
- 16:28instance of, pattern matching with
- 16:30casting twice, a null check, an early
- 16:33return, a method call, basically send
- 16:35system message. Almost every concept
- 16:37from the video in one listener. [music]
- 16:39And for block registration, the lines
- 16:40you write in every mod are public static
- 16:42final block magic block equals
- 16:45registry.dot.register,
- 16:47built-in registry.dot.block, resource
- 16:50>> [music]
- 16:50>> and path, my mod, magic block, new magic
- 16:53block, block
- 16:57Basically, public static final is the
- 16:59constant on the class,
- 17:00registry.dot.register
- 17:01is a method [music] call into the shared
- 17:03registry, built-in registry.dot.block,
- 17:05the one global block registry, resource
- 17:07location.dot.from space [music] and
- 17:09path, your mod's namespace ID, and
- 17:12is the method chaining we mentioned
- 17:13before, and state
- 17:14>> [music]
- 17:15>> method chaining we mentioned before, and
- 17:16state 10 is a lambda with a parameter. A
- 17:19few lines is basically the whole video.
- 17:22So, that's the whole crash course,
- 17:23variables, conditions, loops, classes
- 17:25and objects, inheritance, the secret to
- 17:28modding, interfaces, static versus
- 17:30instance, null checks, casting with
- 17:32instance of, collections, [music] the
- 17:34width syntax, chaining, lambdas and
- 17:36enums, and events tying it all together.
- 17:39You now speak Minecraft Java, not all of
- 17:41Java, just the parts that really matter.
- 17:43Now, you can go to Kaupenjoe's tutorials
- 17:45or any other Java Fabric and Forge
- 17:47NeoForge tutorial you can think of and
- 17:49be able to read the code. Or even
- 17:52better, open a real mod on GitHub and
- 17:54see how much you actually can understand
- 17:55from it. The same comes also for the
- 17:57Minecraft code. Now, go build something,
- 17:59break it, fix it. That's how you
- 18:01actually learn. See you in the next one.
About this transcript
This page contains the full transcript of All the Java you need for Minecraft Modding by ChaosMC, generated from the public captions YouTube serves with the video. The transcript has 3,255 words across 499 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.