gpt4 book ai didi

Java NullPointErexception Bukkit 插件

转载 作者:行者123 更新时间:2023-12-04 17:45:26 25 4
gpt4 key购买 nike

尝试在我的 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?");
}
}
}
}

最佳答案

看来这段代码:

private ScoreboardManager manager = Bukkit.getScoreboardManager();
private Scoreboard board = manager.getNewScoreboard();

原因是您没有在方法内部声明这些变量,因此,它们会在您的 onEnable() 方法被调用之前加载,这样您就什么也得不到使用 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();
}
}

这样做,您就消除了在加载插件时出现 NullPointerException 的可能性。作为一般经验法则,您永远不应从 bukkit 获取方法之外的任何内容。

关于Java NullPointErexception Bukkit 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22493171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com