我正在制作一个使用配置存储数据的bukkit插件,但是当我使用plugin.getConfig()时,我得到一个NullPointer。我认为是因为插件的引用,但我该如何解决?
错误发生在我使用插件的Storage类中。实例
主要:http://pastebin.com/d3bFXbiR
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
public void onEnable() {
final FileConfiguration config = this.getConfig();
config.options().copyDefaults(true);
saveConfig();
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (cmd.getName().equalsIgnoreCase("BlockCmd")) {
if (sender.isOp()) {
if (args.length < 1) {
sender.sendMessage(ChatColor.RED + "/BlockCmd [command] | Kijk naar het blok dat je wilt cmd'en");
} else {
Block block = ((LivingEntity) sender).getTargetBlock(null, 100);
Location bl = block.getLocation();
StringBuilder sb = new StringBuilder();
for (int i = 1; i < args.length; i++) {
sb.append(args[i]).append(" ");
}
String allArgs = sb.toString().trim();
Storage.addClickCmd(bl, allArgs);
sender.sendMessage(ChatColor.GRAY + "[BlockCommand] " + ChatColor.BLUE + "Successfully added a command to the block");
sender.sendMessage(ChatColor.GRAY + "[BlockCommand] " + ChatColor.BLUE + "Command: " + ChatColor.GREEN + allArgs);
}
} else {
sender.sendMessage(ChatColor.RED + "Dit is alleen voor operators");
}
}
return true;
}
@EventHandler
public void onRightClick(PlayerInteractEvent event) {
Player p = event.getPlayer();
if ((event.getAction() == Action.RIGHT_CLICK_BLOCK) || (event.getAction() == Action.LEFT_CLICK_BLOCK)) {
Location loc = event.getClickedBlock().getLocation();
int x = loc.getBlockX();
int y = loc.getBlockY();
int z = loc.getBlockZ();
String w = p.getWorld().getName();
String cCc = "Click.Cmd." + w + "." + x + "." + y + "." + z;
if (Storage.getClickCmd(w, x, y, z) != null) {
String cCc2 = Storage.getString(cCc);
p.performCommand(cCc2);
}
}
}
}
存储:http://pastebin.com/wvQS3n57
import org.bukkit.Location;
import org.bukkit.event.Listener;
public class Storage implements Listener {
static Main plugin;
public Storage(Main instance) {
plugin = instance;
}
public static void addClickCmd(Location loc, String text) {
int x = loc.getBlockX();
int y = loc.getBlockY();
int z = loc.getBlockZ();
String w = loc.getWorld().getName();
if (plugin != null && plugin.getConfig() != null) {
System.out.println("Check");
}
//if(plugin.getConfig() !=null){}
//plugin.getConfig().set("Click.Cmd." + w + "." + x + "." + y + "." + z, text);
}
public static String getClickCmd(String w, int x, int y, int z) {
return plugin.getConfig().getString("Click.Cmd." + w + "." + x + "." + y + "." + z);
}
public static String getString(String path) {
return plugin.getConfig().getString(path);
}
}
答案 0 :(得分:1)
您使用static Main plugin;
但它永远不会被初始化,因为您从不实例化Storage
对象,只使用其静态函数。
在你的插件类中创建一个Storage
对象,并在onEnable
方法中初始化它,并为其构造函数传递插件。例如:
public class Main extends JavaPlugin {
Storage myStorage = null;
public void onEnable() {
final FileConfiguration config = this.getConfig();
config.options().copyDefaults(true);
saveConfig();
myStorage = new Storage(this);
}
稍后在您的插件类中使用此对象 - 并且最好使Storage
中的静态方法(以及plugin
成员)非静态,因为您始终需要创建一个实例使用构造函数中的plugin
集。
我认为this文章提供了关于班级成员的一个很好的基本概述。