The Best Resource for Minecraft
The Best Resource for Minecraft

KubeJS Library Wiki

KubeJS is a comprehensive scripting library that allows modpack creators and server administrators to customize Minecraft using the JavaScript language, enabling the creation of custom items, blocks, recipes, and complex event-driven logic.

8 sections · 1,057 words

Overview#

KubeJS is a powerful utility mod designed for Minecraft, and beyond. It utilizes a fork of the Rhino JavaScript engine by Mozilla to convert JS code into Java classes at runtime. This allows for deep integration with Minecraft's internal systems without the need for complex Java development environments.

By wrapping Minecraft classes and utilizing the Architectury framework, KubeJS provides a unified API for both Forge and Fabric/Quilt loaders. It is primarily used to:

  • Create entirely new items and blocks with custom properties.
  • Modify or remove recipes from any mod.
  • Change block and item tags.
  • Handle complex server-side events like player interactions and entity deaths.
  • Customize the user interface and tooltips on the client side.

Script Types and Locations#

KubeJS scripts are organized into three distinct folders within the /kubejs/ directory of your Minecraft instance. Each folder serves a specific purpose and has different reloading rules.

Script Folders

Folder Purpose Reload Method
startup_scripts/ Used for registries (blocks, items, fluids) and worldgen modifications. Requires game restart or /kubejs reload startup_scripts.
server_scripts/ Used for recipes, tags, loot tables, and server events. Use /reload or /kubejs reload server_scripts.
client_scripts/ Used for tooltips, JEI/REI hiding, and client-side assets. Use F3 + T or /kubejs reload client_scripts.

Reloading Mechanics

  • Server Scripts: These are the most commonly used. Running /reload in-game will re-read all scripts in this folder, allowing you to test recipe changes instantly.
  • Startup Scripts: Because these scripts register new objects into the game's registry, they generally require a full game restart to take effect, though some simple modifications can be reloaded via command.
  • Client Scripts: These handle visual elements. Reloading them usually requires refreshing the game's resource manager.

Custom Blocks and Items#

KubeJS allows you to register new content in the startup_scripts folder. These custom objects are added to the kubejs namespace (e.g., kubejs:example_item).

Custom Items

Items can be created with various properties such as rarity, stack size, and special effects.

Common Item Properties: Property Method Description
Display Name .displayName('Name') Sets the name shown in-game.
Max Stack .maxStackSize(int) Sets the maximum stack size (default 64).
Rarity .rarity('rare') Changes the color of the item name (common, uncommon, rare, epic).
Glow .glow(true) Gives the item a permanent enchantment glint.
Tooltip .tooltip('Text') Adds a static description to the item.

Custom Blocks

Blocks are more complex and support properties like hardness, blast resistance, and tool requirements.

Common Block Properties: Property Method Description
Material .mapColor('color') Sets the color on maps and material type.
Hardness .hardness(float) Determines how long it takes to mine.
Resistance .resistance(float) Determines explosion resistance.
Light Level .lightLevel(float) Sets the amount of light emitted (0.0 to 1.0).
Tool Tier .tagBlock('mineable/pickaxe') Assigns the block to a specific mining category.

Recipe Management#

One of the core features of KubeJS is the ability to manipulate recipes from any mod that uses the standard Minecraft datapack system. This is done in the server_scripts folder using the ServerEvents.recipes event.

Adding Recipes

KubeJS supports all vanilla recipe types and many modded ones.

  • Shaped: event.shaped('output', ['AAA', ' B ', 'AAA'], {A: 'ingredient1', B: 'ingredient2'})
  • Shapeless: event.shapeless('output', ['input1', 'input2'])
  • Smelting/Blasting: event.smelting('output', 'input')

Removing Recipes

You can remove recipes by output, ID, mod, or type.

Removal Type Syntax Example
By Output event.remove({output: 'minecraft:stick'})
By ID event.remove({id: 'minecraft:glowstone'})
By Mod event.remove({mod: 'ironchest'})
By Type event.remove({type: 'minecraft:campfire_cooking'})

Modifying Existing Recipes

You can replace specific inputs or outputs across all recipes in the game:

  • event.replaceInput({input: 'minecraft:iron_ingot'}, 'minecraft:iron_ingot', 'minecraft:gold_ingot')
  • event.replaceOutput({}, 'minecraft:stone', 'minecraft:cobblestone')

Entity and Mob Modification#

While KubeJS is a library and does not add new mob entities by default, it provides an exhaustive event system to modify every creature in the game. This is handled via EntityEvents in server scripts.

Mob Mechanics

Event Description Use Case
entity.spawned Fired when a mob enters the world. Increase health or speed of specific mobs.
entity.hurt Fired when a mob takes damage. Create custom damage resistances or thorns effects.
entity.death Fired when a mob dies. Add custom drops or trigger explosions on death.
entity.check_spawn Fired before a mob spawns. Prevent specific mobs from spawning in certain biomes.

Example Behaviors:

  • Custom Drops: By listening to entity.death, you can check the entity type and manually spawn items at its location, effectively creating custom loot tables.
  • Stat Scaling: You can use entity.spawned to check the player's progression (using Game Stages) and buff mobs accordingly by modifying their attributes.

Worldgen and Biomes#

KubeJS can modify the world generation process through WorldgenEvents. This allows modpack creators to fine-tune the distribution of resources and features.

  • Adding Features: You can add new ore veins, trees, or lake features to specific biomes or biome categories.
  • Removing Features: You can remove vanilla features like diorite/andesite veins or specific structures to clean up the world generation.
  • Biome Properties: Through client scripts, you can modify the visual properties of biomes, such as changing the water color, fog color, or grass tint to create a unique atmosphere.

Mechanics and Utilities#

KubeJS includes several built-in utilities to simplify complex tasks.

Text and Chat

The Text utility allows for advanced chat formatting, including colors, click events, and hover tooltips.

  • Text.green('Success!').bold
  • Text.of('Click here').click('https://example.com') (Note: External links are handled by the Minecraft client).

Scheduling

You can schedule tasks to run after a certain amount of time:

  • server.scheduleInTicks(20, callback) - Runs code after 1 second (20 ticks).

NBT Handling

KubeJS provides full access to NBT (Named Binary Tag) data. You can read, modify, and write NBT to item stacks, entities, and block entities (like chests or furnaces).

Inventory and Items

  • Item Stacks: Use Item.of('id') to create item objects with custom NBT or counts.
  • Ingredients: Use Ingredient.of('#tag') to refer to groups of items in recipes.

Commands#

KubeJS adds several administrative commands to help with script development and debugging.

Command Description
/kubejs reload Reloads all scripts and data.
/kubejs hand Displays the ID, tags, and NBT of the item in your hand.
/kubejs inventory Dumps your current inventory into a JSON file in the kubejs/exported folder.
/kubejs export Exports various game data, such as all registered recipes or tags, for reference.
/kubejs custom_command A shorthand for creating custom commands within scripts.