Java code

A Java code node is a Java method. You list its inputs and outputs, the editor writes the method’s first line, and Code is its body. Use it only for logic no other node can do.

Name

Name is the method’s name: a lowercase letter, then letters and digits, such as smite. Two Java code nodes can’t share one.

Ports

Inputs and Outputs are rows of a name and a type. Each row is a port on the node, wired like any other: an input named target is the port target.

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

Inputs are the method’s parameters, and the only variables in scope. Player, entity, world and position inputs left unwired take the event’s own. The others must be wired.

Returning

Outputs are all flow or all values.

Outputs The method
One flow returns nothing. Flow goes on through it.
Several flows returns the one to go on from
One value returns the value
Several values returns a record of them

With several flows, return a constant of the node’s exit enum. An output named missedTarget on a node named smite is SmiteExit.MISSED_TARGET.

enum SmiteExit {
  HIT,
  MISSED_TARGET
}

static SmiteExit smite(Player target, float amount) {

With value outputs the node is a value: it has no flow in. Several values go in a record, in output order.

record LightResult(boolean dark, float level) {}

static LightResult light() {

Here Code ends with return new LightResult(dark, level);.

Classes

Classes from java.lang and these need no import:

  • 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, Level
  • net.minecraft.world.level.block: Block, Blocks
  • net.minecraft.world.level.block.state: BlockState
  • net.minecraft.world.phys: AABB, BlockHitResult, HitResult, Vec3

The mod’s own classes, such as ModItems and ModBlocks, need none either.

Imports

List any other class in Imports, one per line, with its package:

java.util.Map
java.util.ArrayList

Never write import in Code. An import can’t share its name with a class the generated code uses, or with another node’s import.

Bedrock Edition

Java code is not available on Bedrock Edition: the Add node list doesn’t offer it there, and a project with Java code doesn’t build for Bedrock.