Java code

A Java code node compiles into a static Java method. You define its input and output ports, the editor generates the method signature, and Code provides the method body. Use it when you need custom logic that built-in nodes don’t cover.

Name

Name sets the Java method name. Start with a lowercase letter and use only letters and digits, such as smite. Every Java code node in a project needs a unique name.

Ports

Inputs and Outputs are rows of a name and a type. Each row becomes a port on the node that wires up like any other port (for example, an input named target appears as target).

Type In Java
player Player
entity LivingEntity
world Level
position BlockPos
number float
bool boolean
item Item
block Block
flow outputs only

Inputs become the method’s parameters and are the only local variables initially in scope. Unwired player, entity, world, and position inputs default to the triggering event’s values; all other inputs must be wired.

Returning

Outputs must be either all flow or all value types.

Outputs Method behavior
One flow Returns void; execution continues through that port.
Multiple flows Returns an exit enum constant choosing which branch runs next.
One value Returns a single value of that Java type.
Multiple values Returns a generated Java record holding each output in order.

When a node has multiple flow outputs, return a constant from its generated exit enum. For example, an output named missedTarget on a node named smite becomes SmiteExit.MISSED_TARGET:

enum SmiteExit {
  HIT,
  MISSED_TARGET
}

static SmiteExit smite(Player target, float amount) {

When a node has value outputs, it acts as a value node with no flow input. Multiple value outputs are bundled into a generated record in port order:

record LightResult(boolean dark, float level) {}

static LightResult light() {

In this case, Code finishes with return new LightResult(dark, level);.

Classes

Classes from java.lang and the following packages are imported automatically:

  • java.util: List
  • java.util.concurrent: ThreadLocalRandom
  • net.minecraft.core: BlockPos, Direction
  • net.minecraft.core.particles: ParticleTypes
  • net.minecraft.core.registries: BuiltInRegistries, Registries
  • net.minecraft.network.chat: Component
  • net.minecraft.resources: Identifier
  • net.minecraft.server: MinecraftServer
  • net.minecraft.server.level: ServerLevel, ServerPlayer
  • net.minecraft.sounds: SoundEvents, SoundSource
  • net.minecraft.tags: TagKey
  • net.minecraft.util: Mth, Prediction
  • net.minecraft.world: InteractionHand, InteractionResult
  • net.minecraft.world.effect: MobEffectInstance, MobEffects
  • net.minecraft.world.entity: Entity, EntitySpawnReason, EntityType, EntityTypes, EquipmentSlot, LightningBolt, LivingEntity
  • net.minecraft.world.entity.player: Player
  • net.minecraft.world.entity.projectile: Projectile
  • net.minecraft.world.item: Item, ItemStack, Items
  • net.minecraft.world.level: ClipContext, GameType, Level
  • net.minecraft.world.level.block: Block, Blocks
  • net.minecraft.world.level.block.state: BlockState
  • net.minecraft.world.phys: AABB, BlockHitResult, HitResult, Vec3

Your mod’s own generated classes, such as ModItems and ModBlocks, can also be used without imports.

Imports

Add any other classes you need in Imports, one fully qualified class name per line:

java.util.Map
java.util.ArrayList

Do not write import statements inside Code. An imported class cannot share its short name with any of the pre-imported classes above or with a different class imported by another node; if two classes share a name, reference one by its full package name in Code.

Java code from the chat

When the chat assistant adds or edits a Java code node, it shows the code for review first and flags potential risks outside the game, such as starting external programs, connecting to the internet, or modifying files. The code is added to your mod only if you click Put it in; clicking Leave it out or stopping the reply leaves your project unchanged. Because custom Java code runs on the computer of anyone using the mod, only approve code you trust.

Bedrock Edition

Java code is not available on Bedrock Edition: it does not appear in the Add Node menu on Bedrock projects, and projects containing Java code nodes cannot build for Bedrock.