YouTube2Text

All the Java you need for Minecraft Modding — Transcript

by ChaosMC · 3,255 words · 499 segments · language en · Watch on YouTube

Full transcript

  1. 0:00Thought you want to make a Minecraft
  2. 0:01mod? You opened the tutorial, looked at
  3. 0:02the code, and went, "What is any of
  4. 0:04this? What's a class? Why is everything
  5. 0:06wrapped in angle brackets?" That's what
  6. 0:09this video fixes. This is not a full
  7. 0:10Java course. I'm not teaching you to
  8. 0:12build a banking app. I'm teaching you
  9. 0:14exactly the Java that shows up in mods,
  10. 0:17nothing more. With Minecraft examples
  11. 0:19the entire way through. By the end,
  12. 0:21you'll be able to open any mod source
  13. 0:23code and actually read it. Let's go. A
  14. 0:25variable is a named box that holds a
  15. 0:27value. That's technically the whole
  16. 0:29concept. Integer is the type, basically
  17. 0:31what kind of value goes into that box.
  18. 0:34Block hardness in here in our example is
  19. 0:36the name, three puts in the box,
  20. 0:38semicolon [music] ends that statement,
  21. 0:40and if you forget the semicolon in Java,
  22. 0:43you'll die. The types you'll be using in
  23. 0:45modding are the following ones. Integer,
  24. 0:47which are whole numbers, float for
  25. 0:49decimals, double, more precise decimals
  26. 0:53used for coordinates for example,
  27. 0:54boolean is true or false, and string
  28. 0:56[music] is text, a capital S with double
  29. 0:59quotes. Java is strictly typed. So, once
  30. 1:01a variable is an integer, it stays an
  31. 1:03integer. You can change the value, but
  32. 1:06not the type. Now, the one capital
  33. 1:07suddenly destroy your code. Now, this
  34. 1:09might look fine, but it doesn't work.
  35. 1:12With strings, the double equal [music]
  36. 1:13sign compares memory addresses, not
  37. 1:15text. So, always use dot [music] equals
  38. 1:18to compare two strings with each other.
  39. 1:20Burn this into your brain. Numbers and
  40. 1:22booleans use double equal signs,
  41. 1:25>> [music]
  42. 1:25>> and strings use dot equals. Forget this,
  43. 1:28and you'll spend hours wondering why
  44. 1:29nothing works. If and else loop. Every
  45. 1:32mechanic in Minecraft is built on if
  46. 1:34check. If the block below you is air,
  47. 1:36you fall. If your health is zero, you
  48. 1:38die. Right here we have some code where
  49. 1:41it shows that if a player's health is
  50. 1:43below six, it gives you a regeneration
  51. 1:46effect. But if the player's health is
  52. 1:49below 10, it gives you absorption
  53. 1:50hearts. Else, it does nothing. Condition
  54. 1:53in parentheses, A in curly braces, and
  55. 1:57the chain else if for multiple cases.
  56. 2:00These are the operators you can use.
  57. 2:02Basically, if it equals that, if it
  58. 2:04doesn't equal that, if it's bigger, if
  59. 2:07it's smaller, if it's bigger and equals
  60. 2:09that, if it's smaller and equals that,
  61. 2:11then the double and and is an end, and
  62. 2:15the double lines is for an or, and the
  63. 2:20exclamation point is a not. Loops. Do
  64. 2:23something multiple times without writing
  65. 2:24it five times. [music] The one you use
  66. 2:26the most is for each. By looking at the
  67. 2:28code, we can see that for each item
  68. 2:30stack called stack in the inventory, if
  69. 2:32the stack is diamonds, it's to shrink it
  70. 2:35by one. Then we have the classic for
  71. 2:37loop, when you know the count.
  72. 2:39Basically, we have the four, we state
  73. 2:41the variable the integer e equals zero,
  74. 2:44then if e is smaller than five, and then
  75. 2:47also e plus plus, which basically adds
  76. 2:50plus one to the e every time it runs.
  77. 2:52So, this will run five times. The
  78. 2:54control keywords break exits the loop
  79. 2:56entirely, and continue skips the next
  80. 2:59time around. Both save you from deeply
  81. 3:01nested ifs. Methods. A method is a named
  82. 3:03block of code that does one job. You
  83. 3:05write it once, call it whenever you need
  84. 3:07it, like a crafting recipe. It has a
  85. 3:09name, inputs, basically parameters, and
  86. 3:12a result, a return value. Right here we
  87. 3:15have a method that's basically an
  88. 3:17integer.
  89. 3:18With integer, it's basically what it
  90. 3:20returns after the action is done. In our
  91. 3:23case, it's an integer. The get damage is
  92. 3:26the name of the method, and the boolean
  93. 3:29is critical is the parameter, or yeah,
  94. 3:32is the parameter, or what we named it
  95. 3:34for, it's a variable, which is checked,
  96. 3:36and return sends the value back. Let's
  97. 3:38say we call that method, it would, in
  98. 3:41our case, maybe return a 10 if it's a
  99. 3:44critical. And if a method returns
  100. 3:46nothing, you should use a void. So, void
  101. 3:48apply poison, basically, it just applies
  102. 3:52poison to the player, and it does not
  103. 3:53return a value. Quick bonus,
  104. 3:55ternary operator
  105. 3:58is a compact if else that returns a
  106. 4:00value. You'll see it everywhere. So
  107. 4:03basically it's again the classic integer
  108. 4:06damage equals is critical. This is where
  109. 4:09everything changes. Is critical
  110. 4:10{question mark} 10 {double point} five.
  111. 4:13Basically if it's a critical use 10
  112. 4:17otherwise use five. It's a built-in
  113. 4:19statement. Moving on now to the classes
  114. 4:22and objects. Now this is the big one.
  115. 4:24This is what Minecraft is built on. All
  116. 4:26blocks share properties. Hardness,
  117. 4:28sound, can be broken but each time has
  118. 4:31different values. Dirt itself obsidian
  119. 4:34is hard. Minecraft is a block class, a
  120. 4:36blueprint. It defines what a what all
  121. 4:39blocks have. Each specific block is an
  122. 4:41object, a concrete thing built from what
  123. 4:43the blueprint within its own values. A
  124. 4:45class equals a blueprint. An object is
  125. 4:48the thing built from that class or the
  126. 4:50blueprint. Right here we have a class
  127. 4:52where basically we're giving it a bunch
  128. 4:55of properties. Right here we have three
  129. 4:57things. We have fields. Basically this
  130. 4:59private float destroy speed and private
  131. 5:01sound type sound type. Fields are
  132. 5:02basically variables each object owns. It
  133. 5:05has own and different variables and yes
  134. 5:08then the constructor is a special method
  135. 5:10that runs when you create the object.
  136. 5:12Same name as the class and no return
  137. 5:15type. And last but not least the methods
  138. 5:17we also mentioned before is the actions
  139. 5:19the objects can perform. But now let's
  140. 5:21say you want to create an object. You're
  141. 5:23going to use new. So basically a block
  142. 5:26is the class name then you name what you
  143. 5:29want to name the block basically a dirt
  144. 5:30block equals a new block and
  145. 5:33properties.of.strength
  146. 5:35and then you set all the different
  147. 5:36properties you want which we also have
  148. 5:38inside of the constructor from before.
  149. 5:40Now in our case we have changed the
  150. 5:41strength basically the hardness of the
  151. 5:43block and we put 0.5 and for a stone
  152. 5:46block it would be 1.5 since it's harder
  153. 5:47than a dirt block. So, two separate
  154. 5:49objects, but it's the same class and
  155. 5:52different values. The keyword this we
  156. 5:54saw before refers to the current object.
  157. 5:56You'll see it when a parameter has the
  158. 5:58same name as a field. So, a public void
  159. 6:01set energy integer energy, this refers
  160. 6:05to the object. So, for example, dirt
  161. 6:07block.energy equals energy. This is the
  162. 6:10field on the object, basically the
  163. 6:12parameter. It's something you set
  164. 6:13yourself later. Then, moving on, we have
  165. 6:16the inheritance. The most important
  166. 6:18concept we're going to probably be
  167. 6:19covering today. If there's one thing you
  168. 6:21need to be understand, it's this.
  169. 6:22Inheritance lets one class be built on
  170. 6:25top of another. The child class inherits
  171. 6:27everything from the parent, all fields,
  172. 6:30all methods, and can add or change
  173. 6:32things on top. When you make a custom
  174. 6:34block, you extend block, you get
  175. 6:36everything block does for free. You only
  176. 6:39write the parts that are different. That
  177. 6:41way you don't need to write everything
  178. 6:43from zero. Instead, you can just use
  179. 6:45what is already there. Let's say we have
  180. 6:46a public class my block extends block.
  181. 6:49Basically, now you have everything
  182. 6:51[music]
  183. 6:51that the block class has, and you can
  184. 6:54just add whatever you want after that.
  185. 6:56You can change the hardness, the sounds,
  186. 6:58the placing, the breaking. You can also
  187. 7:00just add a custom method in there that
  188. 7:02will give it a custom property. Super
  189. 7:05means the parent class. [music] You must
  190. 7:07call super in your constructor to pass
  191. 7:08values up. Forget it and Java errors
  192. 7:11out. Want to change one behavior? You
  193. 7:13are going to use override to override
  194. 7:15that method. And welcome block equals
  195. 7:17speed boost. [music] So, for example,
  196. 7:18let's say we have a speed block that
  197. 7:20extends block again. Basically, we have
  198. 7:23everything that's from the block class,
  199. 7:25and we're going to override the step on
  200. 7:28method. At override is an annotation. It
  201. 7:31tells the compiler this should be
  202. 7:32overriding something. So, [music] typo
  203. 7:35your method name and the compiler
  204. 7:37catches this instead of suddenly
  205. 7:38creating a useless method. Always to o
  206. 7:41at override when overriding, [music]
  207. 7:42always. The super.dot.step on calls the
  208. 7:45parent's original version, useful when
  209. 7:47you want to add a behavior without
  210. 7:49replacing it. And this is, to be honest,
  211. 7:52the entire secret to modding. Modding
  212. 7:54growth tons of classes, you extend
  213. 7:56[music] them, you only write what's
  214. 7:57different. That's inheritance. Anyways,
  215. 8:00let's go on to interfaces, but just
  216. 8:02briefly, a quick one. An interface is a
  217. 8:05contract that says you must be able to
  218. 8:07do these things. A class signs the
  219. 8:09contract with implement. It can only
  220. 8:11extend one class, but implement many
  221. 8:13interfaces. For example, right here we
  222. 8:15have a an armor. Basically, it extends
  223. 8:19the item. And since but because we
  224. 8:22cannot extend more than the item, we're
  225. 8:24going to use implement, which implements
  226. 8:26interfaces. Some interfaces are just
  227. 8:28markers, like vanishable. Basically,
  228. 8:30it's just labeled something up for the
  229. 8:32curse of vanishing. Others require
  230. 8:35methods. Recognizing implements in code
  231. 8:37is what matters. You add it
  232. 8:39occasionally, not often, but it's also
  233. 8:41quite useful every once in a while,
  234. 8:43especially when you use it for armor or
  235. 8:47some any other stuff you can think of.
  236. 8:49Now, static versus instance. This
  237. 8:52confuses everyone. Instance, each object
  238. 8:55has its own copy. Each block's hardness
  239. 8:57is player's health. Basically,
  240. 8:59everything is different. For static,
  241. 9:01it's one copy, [music]
  242. 9:02shared by the whole class. It belongs to
  243. 9:04the class itself, not to any other
  244. 9:06object. Why is Minecraft's blocks
  245. 9:08registry static? Well, public static
  246. 9:10final block magic block equals register
  247. 9:12register register. Public registers and
  248. 9:15registerable block reduce location from
  249. 9:16space. Built-in registry.dot.block is
  250. 9:19itself a static field. There's only one
  251. 9:21game, one block registry, one list of
  252. 9:24all blocks, not one per object. It
  253. 9:26belongs to the class. So, you call the
  254. 9:28static methods on the class name, not on
  255. 9:31an object. Right here we have two
  256. 9:32examples for both static and an
  257. 9:34instance. Integer clamp equals
  258. 9:36math.clamp value zero to 100 as a static
  259. 9:40on the class and float speed equals my
  260. 9:42block the destroy speed is an instance
  261. 9:45on an on a on an object and it's
  262. 9:48specified for for that specific block
  263. 9:50which is my block. Rule thumb,
  264. 9:51registries, constants, utility methods
  265. 9:55are static. Block properties, entity
  266. 9:57possessions, player health are
  267. 9:58instances. Also, public static final is
  268. 10:01the standard pair for these registry
  269. 10:03entries. Final just means it can't be
  270. 10:05assigned after creation. It's a
  271. 10:07constant. It's [music] not changeable.
  272. 10:09Now, let's move on with null. And no, I
  273. 10:11don't mean the unstable SMP [music]
  274. 10:13null empire, but I mean null, the number
  275. 10:15one mod crash, the null pointer
  276. 10:17exception, the most common crash in
  277. 10:19modding. Null means no object. A null
  278. 10:22variable points at nothing. Well, a
  279. 10:24method on nothing is a crash. So, when
  280. 10:27there's a player in the instance and
  281. 10:29there [music] is nothing found, there is
  282. 10:31a null and the game crashes. The fix to
  283. 10:33all of this is a null check. Basically,
  284. 10:35we set an if statement so when the
  285. 10:37player is null, [music] if it enters,
  286. 10:39exit. That way, it won't execute what
  287. 10:42comes after it. How do you know what
  288. 10:43might be null? There are two ways.
  289. 10:45First, look for the nullable annotation
  290. 10:48in the method signature. If you see
  291. 10:50this, check for null. Second, common
  292. 10:52sense. Anything that searches might find
  293. 10:54nothing. For example, get the player
  294. 10:56name. What if they're online? Get block
  295. 10:59entity. What if there isn't one? These
  296. 11:01might return null. When you do crash,
  297. 11:03read the stack trace. Look for the first
  298. 11:05line mentioning your mod's code. For
  299. 11:08example, if there is a
  300. 11:09java.lang.NullPointerException,
  301. 11:12[music] there should be something
  302. 11:13pointing towards a line in your own mod.
  303. 11:15For example, in this case, it would be
  304. 11:17magicblock.java:47.
  305. 11:19Open the file, go to line 47, figure out
  306. 11:22what's null. This is the most valuable
  307. 11:24debugging skill you'll learn in this
  308. 11:25entire crash course. Casting and
  309. 11:27instance of. Minecraft works with
  310. 11:29general types. Events give you [music]
  311. 11:31an entity, but you usually want a
  312. 11:32specific type like the player.
  313. 11:34Converting from general to specific is
  314. 11:37casting. Now, if we have entity entity
  315. 11:41equals any.getentity,
  316. 11:42there are like tens of [music] different
  317. 11:44entities Minecraft can pick from. But,
  318. 11:46since we only want to have an instance
  319. 11:49of a player, we uh in [music] if
  320. 11:51statement. So, this pattern is matching.
  321. 11:54Modern Java does the check and the cast
  322. 11:56in one line. Mojang uses everywhere in
  323. 11:591. 20 plus. So, use this everywhere,
  324. 12:02too. Instance of also safely handles
  325. 12:04nulls. So, if entity is null, it returns
  326. 12:07false instead of crashing. That way, if
  327. 12:09you have an if entity instance of player
  328. 12:11player, it checks on [music] if the
  329. 12:13entity is a player and it passes to the
  330. 12:16next line, which would be sending us
  331. 12:18this message. Collections. [music]
  332. 12:20List and map. Two collections you need.
  333. 12:22List and map. List, ordered items at
  334. 12:25positions. Duplicates allowed like an
  335. 12:27inventory. You can think of it like
  336. 12:30this. Here we have an array list, which
  337. 12:32basically includes an item stack of
  338. 12:34diamonds and three gold ingot. And for
  339. 12:37[music] each item stack, it should do
  340. 12:39something. There we have a for loop.
  341. 12:40Then, map key value [music] lookup. Map
  342. 12:43is basically like a dictionary. Use list
  343. 12:46when order matters and you iterate. Use
  344. 12:48map when you want fast lookup by name or
  345. 12:50ID. The item stack and string integer
  346. 12:52[music] are generics. They tell Java
  347. 12:54what type the collection loads. Try
  348. 12:57adding a string to a list item stack and
  349. 12:59the compiler stops you, but caught
  350. 13:01before the game runs. That's all you
  351. 13:03need to know about generics. When
  352. 13:04[music] you see something after a type,
  353. 13:06it means this works with something. Now,
  354. 13:09the weird syntax. Lambdas, chaining, and
  355. 13:12enum. Three patterns that might look
  356. 13:14intimidating, but are simple once you
  357. 13:16see them. Number one, method chaining.
  358. 13:18What happens here? So, basically right
  359. 13:19here, we have the block behavior, for
  360. 13:21example, for properties of a block,
  361. 13:23where we have three different methods
  362. 13:25that are being called from the block
  363. 13:27behavior. Right here we have the
  364. 13:28strength, light level, and the sound. We
  365. 13:30saw that before when we were creating a
  366. 13:32new object. Each method returns the same
  367. 13:34object, so you can immediately call
  368. 13:36another method on it, like Lego. You see
  369. 13:38this everywhere, block properties, item
  370. 13:40properties, entity builders, just read
  371. 13:42top to bottom. Number two, lambdas. The
  372. 13:45bracket with an arrow syntax. Basically,
  373. 13:47right here we have a lambda, a server
  374. 13:48living entity.afterdeath.register
  375. 13:51entity, {comma} source, and then we have
  376. 13:54an arrow, which basically runs whenever
  377. 13:56something dies. A lambda is a timely
  378. 13:58nameless method passed as a value. Here
  379. 14:01we have the event system a chunk of code
  380. 14:03to run later. When something dies, run
  381. 14:05this. The bracket with an arrow means
  382. 14:07lambda with no parameters. State with an
  383. 14:09arrow means lambda with one parameter
  384. 14:11called state. The stuff after the arrow
  385. 14:14is the body. Moving on, enums, number
  386. 14:16three. The fixed sets of named values.
  387. 14:19As already mentioned, enums are the
  388. 14:20fixed sets of named values. Basically,
  389. 14:22right here we have two, where we have
  390. 14:24the direction and the rarity. For
  391. 14:26direction, the direction to north, which
  392. 14:28is basically a number, which is
  393. 14:30basically zero, since we are facing at
  394. 14:32the default direction, the
  395. 14:33straightforward, and the rarity, which
  396. 14:36is rarity.epic, [music]
  397. 14:37which means something else. So,
  398. 14:38basically, instead of zero means north
  399. 14:41and one means south. You get readable
  400. 14:43and possible to misstep names, and this,
  401. 14:45of course, pairs up beautifully with
  402. 14:47switch. Right here we have the switch,
  403. 14:50where, basically, we can automate what
  404. 14:53happens when something [music] happens
  405. 14:54uh for specific stuff. For example, if
  406. 14:57something is facing towards north, it
  407. 14:59should do the north stuff. If it's
  408. 15:00[music] facing south, it should do the
  409. 15:01south stuff. And, of course, the default
  410. 15:04if. That's it. Three patterns [music]
  411. 15:05demystified. Next on, we have events,
  412. 15:07where it all comes together, the finale.
  413. 15:09Events are our fabric. Forge and your
  414. 15:11forge lets you hook into everything
  415. 15:12Minecraft does. Every player death,
  416. 15:14every block break, every [music] tick,
  417. 15:16the Fabric API and all the other APIs
  418. 15:18fire events. Your mod listens and runs
  419. 15:20code. In Fabric, there are no
  420. 15:22annotations for this. You register a
  421. 15:23listener, usually a lambda, inside your
  422. 15:25mod's initializer. The pattern is always
  423. 15:28the same. Grab the event, call dot
  424. 15:30register, and hand in the code to run. A
  425. 15:32real event listener, and what you have
  426. 15:34reconceptualized shows up, is this one.
  427. 15:36This this here is an example of
  428. 15:37everything we spent today implemented
  429. 15:39[music] into one class. Basically, on
  430. 15:42initialize, which I think probably you
  431. 15:44have seen this already multiple times,
  432. 15:45the server living and living entities
  433. 15:47events after death that register, entity
  434. 15:49damage source. Basically, every time
  435. 15:52anything dies, it should run this code.
  436. 15:54And if the entity instance of is a
  437. 15:56player, so basically every every time
  438. 15:59something dies and it's a player, it
  439. 16:01should run this code. Basically, we also
  440. 16:03have a null check. We have again another
  441. 16:06if statement. If the killer is the
  442. 16:07instance of zombie, so if the player
  443. 16:09dies and the killer is a zombie, it
  444. 16:11should send a system message which says
  445. 16:13a zombie got you. So, as you can see, a
  446. 16:16lot of stuff we mentioned in this
  447. 16:17[music] video gets mentioned into this.
  448. 16:19A class implementing an interface,
  449. 16:20basically the mod's initializer, an
  450. 16:22overridden method on initialize, there's
  451. 16:24your at override, a lambda passed to dot
  452. 16:26register, the listener [music] itself,
  453. 16:28instance of, pattern matching with
  454. 16:30casting twice, a null check, an early
  455. 16:33return, a method call, basically send
  456. 16:35system message. Almost every concept
  457. 16:37from the video in one listener. [music]
  458. 16:39And for block registration, the lines
  459. 16:40you write in every mod are public static
  460. 16:42final block magic block equals
  461. 16:45registry.dot.register,
  462. 16:47built-in registry.dot.block, resource
  463. 16:50>> [music]
  464. 16:50>> and path, my mod, magic block, new magic
  465. 16:53block, block
  466. 16:57Basically, public static final is the
  467. 16:59constant on the class,
  468. 17:00registry.dot.register
  469. 17:01is a method [music] call into the shared
  470. 17:03registry, built-in registry.dot.block,
  471. 17:05the one global block registry, resource
  472. 17:07location.dot.from space [music] and
  473. 17:09path, your mod's namespace ID, and
  474. 17:12is the method chaining we mentioned
  475. 17:13before, and state
  476. 17:14>> [music]
  477. 17:15>> method chaining we mentioned before, and
  478. 17:16state 10 is a lambda with a parameter. A
  479. 17:19few lines is basically the whole video.
  480. 17:22So, that's the whole crash course,
  481. 17:23variables, conditions, loops, classes
  482. 17:25and objects, inheritance, the secret to
  483. 17:28modding, interfaces, static versus
  484. 17:30instance, null checks, casting with
  485. 17:32instance of, collections, [music] the
  486. 17:34width syntax, chaining, lambdas and
  487. 17:36enums, and events tying it all together.
  488. 17:39You now speak Minecraft Java, not all of
  489. 17:41Java, just the parts that really matter.
  490. 17:43Now, you can go to Kaupenjoe's tutorials
  491. 17:45or any other Java Fabric and Forge
  492. 17:47NeoForge tutorial you can think of and
  493. 17:49be able to read the code. Or even
  494. 17:52better, open a real mod on GitHub and
  495. 17:54see how much you actually can understand
  496. 17:55from it. The same comes also for the
  497. 17:57Minecraft code. Now, go build something,
  498. 17:59break it, fix it. That's how you
  499. 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.