Skip to content
Martin Hövre edited this page Dec 4, 2018 · 12 revisions

Boxlin comes with some utilities as documented below.

Table of contents

NBT

For NBT there are 5 additions operator functions get, set, contains, plus and plus equals. So how do they work?

var x: Int = 2

fun tags(tag: NBTTagCompound) {
    // Set
    tag["x"] = 1
    tag.set("hello", "world")

    // Get
    x = tag["x"]
    val hello1 = tag.get<String>("hello")
    val hello2: String = tag.get("hello")
    val hello3: String = tag["hello"]

    // If the type is not obvious you have to specify it.

    // Contains

    if ("hello" in tag) {
      // Yap
    } else {
      // Nope
    }

    // Merges

    val otherTag = NBTTagCompound()

    val mergedTag = tag + otherTag // Merges to new tag
    tag += otherTag // Merges into tag
}

Back to top

logger

The logger property gets a logger with your mod name.

Back to top

String.localize

Uses the string to call I18n to format the string to the localized version.

val dirtName = "tile.${Blocks.DIRT.unlocalizedName}".localize()

Back to top

setName

This is an extension method on Item and Block. What it does is set the registry name and unlocalized name to the name you pass in. The second parameter is your modid. It gets it from the current container ie. your modid during initialization. If you want to be certain it gets your modid you can pass it here.

Warning

This function is not compatible with the stable_39 mappings as unlocalizedname is now called translationKey.

val item = Item().setName("some_item")
val block = Block().setName("some_block")

Back to top

Variants

Get all item variants of a item

itemWithVariants.variants.forEach { /* ... */ }

Back to top

getGuiConfigScreen

This generates a GuiConfig from your configuration.

What it needs is the parent screen, the configuration, your modid and the title.

val config: Configuration

class GuiFactoryBoxlin : IModGuiFactory {
    
    // ...

    override fun createConfigGui(parentScreen: GuiScreen) =
            getGuiConfigScreen(parentScreen, config, "mod", "Boxlin configurations")

    // ...
}

Back to top

ConfigurationHandler

ConfigurationHandler makes you write your configuration and not the logic behind it. It requires a modid and the configuration file. The file is 9/10 times going to be the suggestedConfigurationFile from the pre initialization event.

The third argument is a function where you set the configuration properties.

ConfigurationHandler can be extended. When you do you can override the method config. It gets the configuration object as a parameter.

ConfigurationHandler registers itself on the changed event as when you save the configuration inside the gui it saves the configuration to file.

It has a method named guiConfig that takes the parent screen and a title. And returns the configured GuiConfig. This is to be a shorthand for the getGuiConfigScreen util method.

lateinit var configHandler: ConfigurationHandler
var sayHello = true

@EventHandler
fun preInit(e: FMLPreInitializationEvent) {
    configHandler = ConfigurationHandler("mod", e.suggestedConfigurationFile) {
        sayHello = this["general", "sayHello", sayHello, "Says hello on startup"].boolean
    }

    if (sayHello)
        logger.info("Greetings")
}

Back to top

World

Boxlin adds two more descriptive properties isServer and isClient. They are basically !world.isRemote and world.isRemote.

Back to top

ProxyInjector

ProxyInjector is a delegate for removing the magic behind the annotation @SidedProxy. The proxy classes can also be Kotlin objects as a bonus. The field holding the delegate can be of any visibility type.

// ...
// From
@SidedProxy(clientSide = "path.to.ClientProxy", serverSide = "path.to.ServerProxy")
lateinit var proxy: IProxy

// To
val proxy by useProxy(ClientProxy::class, ServerProxy::class)

Back to top

Capabilities

I have included a helper function for getting capabilities. The function is an extension on the objects that have capabilities and returns an Optional with a supplier when called gets the capability.

val stack: ItemStack
val optional = stack.capability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
if (optional.isPresent) {
  val itemHandler = optional.get().get()
}

stack.capability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent { 
  val itemHandler = it.get()
}

Back to top

Clone this wiki locally