我尝试在我的bukkit服务器中加载插件时遇到NullPointerException,但Eclipse中没有错误。我的插件是Rush pluing,很多东西没有使用/丢失。 错误是这样的:
[18:11:10] [Server thread/ERROR]: Could not load 'plugins\rush.jar' in folder 'plugins'
org.bukkit.plugin.InvalidPluginException: java.lang.NullPointerException
at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:182) ~ [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:308) ~ [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:231) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
at org.bukkit.craftbukkit.v1_7_R1.CraftServer.loadPlugins(CraftServer.java:255) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
at org.bukkit.craftbukkit.v1_7_R1.CraftServer.<init>(CraftServer.java:233) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
at net.minecraft.server.v1_7_R1.PlayerList.<init>(PlayerList.java:63) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
at net.minecraft.server.v1_7_R1.DedicatedPlayerList.<init>(SourceFile:14) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
at net.minecraft.server.v1_7_R1.DedicatedServer.init(DedicatedServer.java:126) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:424) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
Caused by: java.lang.NullPointerException
at me.mailh.Rush.MainRush.<init>(MainRush.java:62) ~[?:?]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.7.0_45]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.7.0_45]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~ [?:1.7.0_45]
at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.7.0_45]
at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:178) ~ [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
... 9 more
这是我的代码:
package me.mailh.Rush;
import java.io.File;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.WorldCreator;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.ScoreboardManager;
import org.bukkit.scoreboard.Team;
public class MainRush extends JavaPlugin implements Listener{
public final Logger logger = Logger.getLogger("Minecraft");
public static MainRush plugin;
public void onEnable(){
this.logger.info("Rush is enabled.");
getServer().getPluginManager().registerEvents(this, this);
}
private ScoreboardManager manager = Bukkit.getScoreboardManager();
private Scoreboard board = manager.getNewScoreboard();
private Team bleu = board.registerNewTeam("bleu");
private Team orange = board.registerNewTeam("orange");
public void onDisable(){
this.logger.info("Rush is disabled.");
}
public boolean onCommand(CommandSender s, Command c, String lbl, String[] args){
Player pl = (Player) s;
if (lbl.equalsIgnoreCase("rush")){
if (args.length == 0){
World w = Bukkit.getWorld("rush");
Location rush = new Location( w, -210, 51, -8 );
pl.sendMessage(ChatColor.GOLD + "Teleportation...");
pl.teleport(rush);
}
if (args[0].equalsIgnoreCase("bleu")){
board.resetScores(pl.getPlayer());
orange.removePlayer(pl.getPlayer());
bleu.addPlayer(pl.getPlayer());
pl.sendMessage(ChatColor.AQUA + "Vous êtes dans l'équipe bleu");
bleu.setAllowFriendlyFire(false);
}
if (args[0].equalsIgnoreCase("leave")){
board.resetScores(pl.getPlayer());
bleu.removePlayer(pl.getPlayer());
orange.removePlayer(pl.getPlayer());
pl.sendMessage(ChatColor.RED + "Vous avez quitté votre équipe");
}
if (args[0].equalsIgnoreCase("orange")){
board.resetScores(pl.getPlayer());
bleu.removePlayer(pl.getPlayer());
orange.addPlayer(pl.getPlayer());
pl.sendMessage(ChatColor.GOLD + "Vous êtes dans l'équipe orange");
orange.setAllowFriendlyFire(false);
}
}
return true;
}
public void rollback(){
if(Bukkit.getServer().unloadWorld("rush",false))
{
new File("rush").delete();
Bukkit.getServer().createWorld(new WorldCreator("rush"));
}
}
@EventHandler
public void onLeave(PlayerQuitEvent e){
Player p = (Player) e.getPlayer();
bleu.removePlayer(p);
orange.removePlayer(p);
board.resetScores(p);
}
@EventHandler
public void Death(PlayerDeathEvent event){
if (event.getEntityType() == EntityType.PLAYER){
Player player = event.getEntity();
Team team = board.getPlayerTeam(player);
if(team.equals(bleu)){
player.sendMessage("did it work? bleu");
}
else if (team.equals(orange)){
player.sendMessage("did it work orange?");
}
}
}
}
答案 0 :(得分:7)
似乎这段代码:
private ScoreboardManager manager = Bukkit.getScoreboardManager();
private Scoreboard board = manager.getNewScoreboard();
原因是你没有在方法中声明这些变量,因此,在调用onEnable()
方法之前它们会被加载,这使得你无法使用{{1}获取任何内容因为Bukkit尚未初始化。
我建议做的是做任何方法的外部:
Bukkit
然后在private ScoreboardManager manager;
private Scoreboard board;
:
onEnable()
所以你会有这样的事情:
manager = Bukkit.getScoreboardManager();
board = manager.getNewScoreboard();
通过这样做,您可以在插件加载时消除获得public class MainRush extends JavaPlugin implements Listener{
private ScoreboardManager manager;
private Scoreboard board;
@Override
public void onEnable(){
//plugin enabled
manager = Bukkit.getScoreboardManager();
board = manager.getNewScoreboard();
}
}
的机会。作为一般的经验法则,你永远不应该从方法之外的bukkit获得任何东西。