无法从JavaPlugin类型对非静态方法getConfig()进行静态引用

问题描述 投票:0回答:1

我正在尝试创建一个Minecraft插件,其命令将世界名称设置为config.yml。当我尝试设置配置时,我不断收到“Cannot make a static reference to the non-static method getConfig() from the type JavaPlugin”。我已经搜索了几种方法来解决这个问题,但我还没有理解必须将其他情况实施到我的中。

这是我的代码:

main.Java:

package me.Liam22840.MurderRun;

import org.bukkit.plugin.java.JavaPlugin;

import me.Liam22840.MurderRun.commands.HelpCommand;
import me.Liam22840.MurderRun.commands.SetmapCommand;

public class Main extends JavaPlugin {

@Override
public void onEnable(){
    loadConfig();
    new HelpCommand(this);
    new SetmapCommand(this);
}   

public void loadConfig(){
    getConfig().options().copyDefaults(true);
    saveConfig();
    }
}

set map command.Java:

package me.Liam22840.MurderRun.commands;

import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;

import Utils.Utils;
import me.Liam22840.MurderRun.Main;
import me.Liam22840.MurderRun.getConfig;


public class SetmapCommand implements CommandExecutor{
   private int count;
   public SetmapCommand(Main plugin){
       plugin.getCommand("Setmap").setExecutor(this);

}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (!(sender instanceof Player)){
        sender.sendMessage("Only players can execute this command!");
        return true;
    }
    Player p = (Player) sender; 
    Location b_loc = p.getLocation();
    if(p.hasPermission("MurderRun.Setworld")){
        Main.getConfig().set("Maps." + p.getName() + count + ".World", b_loc.getWorld().getName());
        Main.saveConfig();
        p.sendMessage(Utils.chat("&4Map Set"));
        return true;
    } else{
        p.sendMessage("You do not have the required permissions to execute this command!");             
    }
    return false;

    }

}
static bukkit non-static
1个回答
1
投票

您无法直接调用Main类,因为它不是静态的。要调用它,您应该在Setmap类和构造函数中执行此操作:

private Main plugin;

public SetmapCommand(Main plugin){
   this.plugin = plugin;
   plugin.getCommand("Setmap").setExecutor(this);
}

完成此操作后,您可以在Setmap类中使用:plugin.saveConfig();

© www.soinside.com 2019 - 2024. All rights reserved.