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: Listjava.util.concurrent: ThreadLocalRandomnet.minecraft.core: BlockPos, Directionnet.minecraft.core.particles: ParticleTypesnet.minecraft.core.registries: BuiltInRegistries, Registriesnet.minecraft.network.chat: Componentnet.minecraft.resources: Identifiernet.minecraft.server: MinecraftServernet.minecraft.server.level: ServerLevel, ServerPlayernet.minecraft.sounds: SoundEvents, SoundSourcenet.minecraft.tags: TagKeynet.minecraft.util: Mth, Predictionnet.minecraft.world: InteractionHand, InteractionResultnet.minecraft.world.effect: MobEffectInstance, MobEffectsnet.minecraft.world.entity: Entity, EntitySpawnReason, EntityType, EntityTypes, EquipmentSlot, LightningBolt, LivingEntitynet.minecraft.world.entity.player: Playernet.minecraft.world.entity.projectile: Projectilenet.minecraft.world.item: Item, ItemStack, Itemsnet.minecraft.world.level: ClipContext, Levelnet.minecraft.world.level.block: Block, Blocksnet.minecraft.world.level.block.state: BlockStatenet.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.