NeoForge P3 - 命令注册
528 字
3 分钟
NeoForge P3 - 命令注册
一、相关文档与介绍
NeoForge 官方文档:
命令:https://docs.neoforged.net/docs/misc/commands/ Brigadier:https://github.com/Mojang/brigadier
对于命令注册,这里直接开始制作先前地图的粒子特效进行入手。首先编写一段粒子特效,而后将其绑定命令可用于调用。
二、粒子效果实现
由于研究方向仍然是把mcfunction内容迁移到NeoForge内,所以粒子效果仍然是以实体(Entity) 作为执行目标。
那么就先写一个圆(钢铁)的粒子效果吧,首先需要写圆环的效果,而后再将多个圆环叠加到一起,就成了圆。
// 环函数实现public static void spawnCircle(ServerLevel level, double x, double y, double z, float radius, ParticleOptions particle, int density){ for (int i = 0; i < density; i++) { double angle = i * Math.PI * 2.0 / density; double px = x + Math.cos(angle) * radius; double pz = z + Math.sin(angle) * radius; level.sendParticles(particle, px, y, pz, 1, 0, 0, 0, 0); }}
// 钢铁函数模块public static void spawnFilledCircle(ServerLevel level, double x, double y, double z, float radius, ParticleOptions particle, float particleSpacing){ float step = 0.5f; // 每层环间隔 0.5 格 for (float r = step; r <= radius; r += step) { // 周长 / 间距 = 这一环需要多少粒子 int count = Math.max(8, (int) (2.0 * Math.PI * r / particleSpacing)); spawnCircle(level, x, y, z, r, particle, count); }}
// ===== 语义别名 =====// 钢铁提示范围 同时 自定义粒子效果public static void steelAreaWarning(ServerLevel level, Entity entity, float radius){ var dust = new DustParticleOptions(new Vector3f(1.0f, 0.5f, 0.0f), 2.0f); spawnFilledCircleAtEntity(level, entity, radius, dust, 0.5f);}
public static void steelAreaCheck(ServerLevel level, Entity entity, float radius) { var dust = new DustParticleOptions(new Vector3f(1.0f, 0.0f, 0.0f), 2.0f); spawnFilledCircleAtEntity(level, entity, radius, dust, 0.5f);}三、命令注册效果实现
粒子效果写好了,接下来注册 debug 的 Command 吧!
public static void register(CommandDispatcher<CommandSourceStack> dispatcher){ dispatcher.register(Commands.literal("springtsuki") .then(Commands.literal("lib") // 钢铁测试 .then(Commands.literal("warning_steel") .then(Commands.argument("radius", FloatArgumentType.floatArg(1f, 20f)) .executes(ctx -> { var player = ctx.getSource().getPlayerOrException(); var radius = FloatArgumentType.getFloat(ctx, "radius"); var dust = new DustParticleOptions(new Vector3f(1.0f, 0.5f, 0.0f), 1.5f); EffectLib.steelAreaWarning(player.serverLevel(), player, radius); player.sendSystemMessage(Component.literal("§a✓ 钢铁半径=" + radius)); return 1; })))
.then(Commands.literal("check_steel") .then(Commands.argument("radius", FloatArgumentType.floatArg(1f, 20f)) .executes(ctx -> { var player = ctx.getSource().getPlayerOrException(); var radius = FloatArgumentType.getFloat(ctx, "radius"); var dust = new DustParticleOptions(new Vector3f(1.0f, 0.5f, 0.0f), 1.5f); EffectLib.steelAreaCheck(player.serverLevel(), player, radius); player.sendSystemMessage(Component.literal("§a✓ 钢铁半径=" + radius)); return 1; })))}这样就可以在游戏内使用 /springtsuki lib warning_stell 进行特效测试了。且绑定实体为执行角色本身。
支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!
NeoForge P3 - 命令注册
https://springtsuki.xucjno.com/posts/springtsuki/notes/java/2026/811-1/












