我正在制作一个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);
}
}发布于 2014-07-23 19:28:11
您使用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集。
我认为这的文章给出了一个很好的、基本的关于类成员的概述。
https://stackoverflow.com/questions/24917153
复制相似问题