RunFunction .java

net.minecraft.world.item.enchantment.effects.RunFunction (record, 49 lines): members: function, apply, LOGGER, codec, CODEC. strings: "function".

net.minecraft.world.item.enchantment.effectsrecord49 linescommon2.0 KB

Implements

API Surface (2)

void apply(final ServerLevel serverLevel, final int enchantmentLevel, final EnchantedItemInUse item, final Entity entity, final Vec3 position)
MapCodec<RunFunction> codec()

Fields & Constants (2)

LOGGER
CODEC

String literals (1)

"function"

Full Source

RunFunction.java49 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.item.enchantment.effects;
2
3
import com.mojang.logging.LogUtils;
4
import com.mojang.serialization.MapCodec;
5
import com.mojang.serialization.codecs.RecordCodecBuilder;
6
import java.util.Optional;
7
import net.minecraft.commands.CommandSourceStack;
8
import net.minecraft.commands.functions.CommandFunction;
9
import net.minecraft.resources.Identifier;
10
import net.minecraft.server.MinecraftServer;
11
import net.minecraft.server.ServerFunctionManager;
12
import net.minecraft.server.level.ServerLevel;
13
import net.minecraft.server.permissions.LevelBasedPermissionSet;
14
import net.minecraft.world.entity.Entity;
15
import net.minecraft.world.item.enchantment.EnchantedItemInUse;
16
import net.minecraft.world.phys.Vec3;
17
import org.slf4j.Logger;
18
19
public record RunFunction(Identifier function) implements EnchantmentEntityEffect {
20
	private static final Logger LOGGER = LogUtils.getLogger();
21
	public static final MapCodec<RunFunction> CODEC = RecordCodecBuilder.mapCodec(
22
		i -> i.group(Identifier.CODEC.fieldOf("function").forGetter(RunFunction::function)).apply(i, RunFunction::new)
23
	);
24
25
	@Override
26
	public void apply(final ServerLevel serverLevel, final int enchantmentLevel, final EnchantedItemInUse item, final Entity entity, final Vec3 position) {
27
		MinecraftServer server = serverLevel.getServer();
28
		ServerFunctionManager functions = server.getFunctions();
29
		Optional<CommandFunction<CommandSourceStack>> function = functions.get(this.function);
30
		if (function.isPresent()) {
31
			CommandSourceStack source = server.createCommandSourceStack()
32
				.withPermission(LevelBasedPermissionSet.GAMEMASTER)
33
				.withSuppressedOutput()
34
				.withEntity(entity)
35
				.withLevel(serverLevel)
36
				.withPosition(position)
37
				.withRotation(entity.getRotationVector());
38
			functions.execute(function.get(), source);
39
		} else {
40
			LOGGER.error("Enchantment run_function effect failed for non-existent function {}", this.function);
41
		}
42
	}
43
44
	@Override
45
	public MapCodec<RunFunction> codec() {
46
		return CODEC;
47
	}
48
}
49

Decompiled Minecraft 26.3 source of RunFunction (net/minecraft/world/item/enchantment/effects/RunFunction.java) with Mojang official mappings, stored verbatim on the site.