Skip to content
Matias Bjarland edited this page Dec 6, 2017 · 4 revisions

Adding a Block

Requirements

  • A working common proxy.

Instructions

  • Now that we have a working proxy, we can finally add our first block.

  • I personally prefer to put all of my blocks in their own namespace, so I'd recommend to create a new file called blocks.clj in your namespace. You don't HAVE to do this (you could just as easily add it in your common proxy namespace), but I'd recommend it to keep your code organized.

(ns <yourusername>.<yourmodname>.blocks)
  • Before we go any further though, we need a few things. The first of which is the defblock macro, which is in forge-clj.blocks.
(ns <yourusername>.<yourmodname>.blocks
  (:require
    [forge-clj.blocks :refer [defblock]]))
  • However, we need a couple more things after this. This is because we will want to put our block in one of the vanilla creative tabs, and we also would like to add a sound to our block. So we need to import the CreativeTabs class and the Block class. This is done as follows:
(ns <yourusername>.<yourmodname>.blocks
  (:require
    [forge-clj.blocks :refer [defblock]])
  (:import
    [net.minecraft.block Block]
    [net.minecraft.creativetab CreativeTabs]))
  • That will import the Minecraft classes that we need. The difference is that require is used for Clojure namespaces, while import is used for Java classes (such as those in Minecraft).

  • Now that we have done that, we can finally make a block.

  • The defblock macro, and many other macros in forge-clj, make use of a macro called defobj. The defobj macro will take the name of the block, and a series of arguments. These arguments are turned into a java setter. For example, :creative-tab gets turned into .setCreativeTab().

  • Here is an example of a simple block:

(defblock test-block
  :block-name "test-block"
  :hardness 0.5
  :step-sound Block/soundTypeStone
  :creative-tab CreativeTabs/tabBlock)
  • That's a lot of code, so let's go over each piece one at a time:

  • :block-name turns into the java method .setBlockName(). This is used by Minecraft to figure out what the block's unlocalized name is (more on that latter).

  • :hardness turns into the java method .setHardness(). This sets how hard the block is, and therefore how long it takes to break this block.

  • step-sound turns into the java method .setStepSound(). This sets what sounds the block will make when interacted with. The Block/soundTypeStone means that we're using a static field from Minecraft's Block class as the sound type.

  • creative-tab turns into the java method .setCreativeTab(). This takes an instance of CreativeTabs (in this case CreativeTabs/tabBlock), in order to say where this block is in the Minecraft's Creative menu. You can create your own creative tabs as well, but that is for another tutorial.

  • That was nice, but this still won't work just yet. While we have made a block successfully, Minecraft doesn't know it exists yet, so we have to register this block. We do this in our common proxy namespace, like so:

(ns <yourusername>.<yourmodname>.common
  (:require
    [forge-clj.registry :refer [register]]
    [<yourusername>.<yourmodname>.blocks :refer [test-block]]))

(defn common-init [this event]
  (register test-block "test-block"))
  • We first had to require a few things. The register function in forge-clj.registry is used to register a block to Minecraft Forge's registry. It takes a block to register, and a name for that block, which will be used within the Minecraft Forge registry, and should be unique. We also had to require the test-block we created earlier in order to register it within the common-init function.

  • Minecraft should run now! Go ahead and run the client, and look inside of the Blocks tab for your block (it'll probably be near the bottom). It will have a purple/black square texture (because we never added a texture to our block, we'll do that latter), and will have a weird looking name (something along "tile.test-block.name"), which is because we haven't specified what the localized name should be (we'll also do that latter).

  • Finally, here's a full version containing everything, with the name being "yoshiquest", and the mod name being "test-mod":

core.clj:

(ns yoshiquest.test-mod.core
  (:require
    [forge-clj.core :refer [defmod]]))

(defmod yoshiquest.test-mod.core test-mod "0.0.1"
  :common {:init yoshiquest.test-mod.common/common-init})

common.clj:

(ns yoshiquest.test-mod.common
  (:require
    [forge-clj.registry :refer [register]]
    [yoshiquest.test-mod.blocks :refer [test-block]]))

(defn common-init [this event]
  (register test-block "test-block"))

blocks.clj:

(ns yoshiquest.test-mod.blocks
  (:require
    [forge-clj.blocks :refer [defblock]])
  (:import
    [net.minecraft.block Block]
    [net.minecraft.creativetab CreativeTabs]))

(defblock test-block
  :block-name "test-block"
  :hardness 0.5
  :step-sound Block/soundTypeStone
  :creative-tab CreativeTabs/tabBlock)

Up next, we'll try adding an item!

Clone this wiki locally