首页 > 解决方案 > IllegalArgumentException,插件不能为空

问题描述

当我在其他类的顶部初始化插件时,插件的主类返回 null:

java.lang.IllegalArgumentException: Plugin cannot be null

BeforeGame 类中的初始化:

    Main instance;
public BeforeGame(Main instance) {
    this.instance = instance;
}

在主课中:

公共类 Main 扩展 JavaPlugin 实现 Listener {

private Main instance;
public Main getInstance(){
    return instance;
}

@Override
public void onEnable() {
    instance = this;
    Bukkit.getServer().getPluginManager().registerEvents(new LobbyHandlers(this), this);
    Bukkit.getServer().getPluginManager().registerEvents(new GameListener(this), this);
    BeforeGame bfGame = new BeforeGame(this);
    LobbyCountdown lobbyCountdown = new LobbyCountdown(this);
}

@Override
public void onDisable() {
    instance = null;
}

部分代码有错误:

public void loadWaiter(World w){
    List<Player> players = w.getPlayers();
    BukkitTask waitplz = new BukkitRunnable(){
        @Override
        public void run() {
            for(Player p : players){
                p.sendTitle(ChatColor.YELLOW + "Wait", ChatColor.YELLOW + "loading other players!", 0, 40, 10);
                p.getInventory().clear();
                blocksSet(players);

            }
        }
    }.runTaskLater(instance, 50);
}

控制台告诉,问题出在 java:39 上,它是:

}.runTaskLater(instance, 50);

错误是:

[23:38:05 WARN]: [BridgeBattle_Reworked] Task #36 for 
BridgeBattle_Reworked v0.1 generated an exception 
java.lang.IllegalArgumentException: Plugin cannot be null at 
org.apache.commons.lang.Validate.notNull(Validate.java:192) ~[spigot. 
jar:git-Spigot-dcd1643-e60fc34] at 
org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftScheduler.validate(Cra 
ftScheduler.java:410) ~[spigot.jar:git-Spigot-dcd1643-e60fc34] at 
org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftScheduler.runTaskTimer 
(CraftScheduler.java:130) ~[spigot.jar:git-Spigot-dcd1643-e60fc34] at 
org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftScheduler.runTaskLater 
(CraftScheduler.java:113) ~[spigot.jar:git-Spigot-dcd1643-e60fc34] at 
org.bukkit.scheduler.BukkitRunnable.runTaskLater(BukkitRunnable.java: 75) 
~[spigot.jar:git-Spigot-dcd1643-e60fc34] at 
ru.vladis.bridgebattle.game.BeforeGame.loadWaiter(BeforeGame.java:39) ~ 
[?:?] at 
ru.vladis.bridgebattle.countdowns.LobbyCountdown$1.run(LobbyCountdown 
.java:51) ~[?:?] at 
org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftTask.run(CraftTask.jav 
a:76) ~[spigot.jar:git-Spigot-dcd1643-e60fc34] at 
org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftScheduler.mainThreadHe 
artbeat(CraftScheduler.java:361) [spigot.jar:git-Spigot-dcd1643-e60fc34] 
at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java: 
739) [spigot.jar:git-Spigot-dcd1643-e60fc34] at 
net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java: 406) 
[spigot.jar:git-Spigot-dcd1643-e60fc34] at 
net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java: 679) 
[spigot.jar:git-Spigot-dcd1643-e60fc34] at 
net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.jav 
a:577) [spigot.jar:git-Spigot-dcd1643-e60fc34] at 
java.lang.Thread.run(Unknown Source) [?:1.8.0_191]

标签: javaminecraftbukkit

解决方案


在 onEnable() 方法的构造函数中定义实例和其他所有操作。这样做的原因是因为 Bukkit 清除了变量,但是由于 java.classLoader 的限制,每次重新加载服务器时都不会创建主类的新实例。


推荐阅读