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

Setting Up Your Proxies

Requirements

  • Requires a mod to already be defined.

Instructions

  • Ok, before we can truly start doing things, we first need to create some proxies.

  • Proxies are how code is separated in Minecraft Forge and forge-clj. There are two types of proxies: Common and Client proxies. They are used to separate things that run on the Integrated Client (what you normally download and run via the launcher), and the things that run on the Dedicated Server (what people who create a Minecraft server use).

  • There are two types of proxies. Common Proxies run on both the Integrated Client AND the Dedicated Server. Client Proxies run on only the Integrated Client.

  • For now, we only need a Common Proxy. Client proxies will be defined in a latter tutorial, when we get to complex things such as custom rendering and guis.

  • To add a proxy, we should first create another namespace for it. Create a new file called common.clj (or whatever you want really) in the same folder as the core file. Then make the namespace declaration as usual.

(ns <yourusername>.<yourmodname>.common)
  • No need to require anything at the moment, since we're going to do something simple.

  • Now, each proxy can have 3 functions: pre-init, init, and post-init. We're only doing one thing here, so we'll only use init now.

  • Each of these functions should take 2 arguments. The first is an instance of the mod, and the second is the forge event. You usually won't use either of these, but it's nice to have them available for the few cases you need them.

  • So, in our common namespace, let's make ourselves an init function:

(defn commmon-init [this event]
  )
  • Now that we have that, let's make it do something. For something simple, try this:
(defn common-init [this event]
  (println "\n\nHello World!\n\n"))
  • This will make your mod print out the phrase "Hello World" when Minecraft boots up. The "\n" is a new line, and will help make our message a bit easier for us to see.

  • Now that we've done that, we need to tell our mod to use this function as our mod's init function for the common proxy. Add this to your defmod in your core file:

(defmod <namespace> <modname> "<modversion>"
  :common {:init <common-namespace>/common-init})

FOR CLOJURE NEWBIES: Things that start with a ":" in Clojure are keywords. They don't really have any meaning alone other than serving as a way of naming things. The brackets ({}) specify a map, which consists of groups of two: A key, and a value. The key doesn't HAVE to be a keyword, but it is a common practice.

  • The keyword :common is telling forge-clj that you have a map of functions that you wish to include in the common proxy. The keyword inside the brackets :init is telling forge-clj what the function is that you wish to add as an init function.

  • Normally in this case, you would use :require to require your common namespace, and use :refer in that to get the init function. Then you would be able to simply enter init instead of the full name. HOWEVER, this does not work in this case. This is due to the fact that forge-clj requires this namespace for you, and needs the full name in order to do so. It does this because it needs to be able to NOT require things in the client namespace when this is running on a dedicated server.

  • Now that the init function has been specified, this code should run. Run this and look at your console. You should see the words "Hello World", with some white space around it (you might need to scroll up a bit). If that happens, it worked!

  • Here's some more example code with the <>s filled in, assuming my user name is "yoshiquest" and my mod is called "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)

(defn common-init [this event]
  (println "\n\nHello World!\n\n"))

Clone this wiki locally