-
Notifications
You must be signed in to change notification settings - Fork 1
Utils
Boxlin comes with some utilities as documented below.
- NBT
- logger
- setName
- Variants
- String.localize
- getGuiConfigScreen
- ConfigurationHandler
- World
- ProxyInjector
- Capabilities
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
}The logger property gets a logger with your mod name.
Uses the string to call I18n to format the string to the localized version.
val dirtName = "tile.${Blocks.DIRT.unlocalizedName}".localize()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.
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")Get all item variants of a item
itemWithVariants.variants.forEach { /* ... */ }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")
// ...
}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")
}Boxlin adds two more descriptive properties isServer and isClient. They are basically !world.isRemote and world.isRemote.
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)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()
}