Question Why do i get this

  • After careful consideration and due to limited usage, we’ve made the decision to discontinue the PaperMC forums. Moving forward, we recommend using Hangar for plugin uploads, and for all other community discussions and support, please join us on Discord.

Cabbage

New member
Apr 5, 2025
3
1
3
error: cannot find symbol
commands.registrar().register(buildCommand);
^
symbol: variable buildCommand

Code: package com.cabbage.testplugin;

import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
import org.bukkit.plugin.java.JavaPlugin;

public final class Testplugin extends JavaPlugin {

@Override
public void onEnable() {
this.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, commands -> {
commands.registrar().register(buildCommand);
});
}

@Override
public void onDisable() {
// Plugin shutdown logic
}
}



Code 2:
package com.cabbage.testplugin;

import com.mojang.brigadier.Command;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.tree.LiteralCommandNode;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;

public class coolcommand {


public static void command(){
LiteralCommandNode<CommandSourceStack> buildCommand = Commands.literal("testcmd")
.then(Commands.literal("argument_one"))
.then(Commands.literal("argument_two"))
.build();
}

}
 
  • Like
Reactions: cozyHousecat

stefvanschie

Moderator
Staff member
Dec 17, 2021
145
7
29
28
You can't access variables in a method from outside that method. What you can do is make the command method return a LiteralCommandNode<CommandSourceStack> and then call coolcommand.command() in your onEnable method.