2

我正在尝试为我的世界 bukkit 服务器创建一个插件。目标是读取 html 页面第一行的字符串。如果结果为 True,它将执行命令。

这是我现在拥有的代码:

import java.net.URLConnection;
import java.util.Scanner;
import java.util.logging.Logger;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

public class main extends JavaPlugin{
public final Logger logger = Logger.getLogger("Minecraft");

public void onEnable(){
    logger.info("[First] Has Been Enabled.");
}

public void onDisable(){
    logger.info("[First] Has Been Disabled.");
}

public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
    Player player = (Player) sender;
    if(commandLabel.equalsIgnoreCase("hello")){
        player.sendMessage(ChatColor.GOLD + "Hello");
    }
    else if(commandLabel.equalsIgnoreCase("world")){
        player.sendMessage(ChatColor.GOLD + "World");
    }
    else if(commandLabel.equalsIgnoreCase("coolman")){
        player.setPlayerListName("coolman");
    }
    else if(commandLabel.equalsIgnoreCase("vote")){
        String sourceLine = null;

        // The URL address of the page to open.
        URL address = new URL("http://www.koolflashgames.com/test.php");

        // Open the address and create a BufferedReader with the source code.
        InputStreamReader pageInput = new InputStreamReader(address.openStream());
        BufferedReader source = new BufferedReader(pageInput);

        // Append each new HTML line into one string. Add a tab character.
        try {
            sourceLine = source.readLine();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if(sourceLine == "False"){
            player.sendMessage("Thanks for voting!");
        }

    }
    return false;

}

}

在日志中创建以下错误:

2012-11-02 16:18:30 [SEVERE] null
org.bukkit.command.CommandException: Unhandled exception executing command 'vote' in        plugin first v1.0
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180)
at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:502)
at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:915)
at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:828)
at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:810)
at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
at net.minecraft.server.NetworkManager.b(NetworkManager.java:282)
at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:111)
at net.minecraft.server.ServerConnection.b(SourceFile:35)
at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:561)
at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:213)
at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:474)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:406)
at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
Caused by: java.lang.NullPointerException
at java.io.Reader.<init>(Unknown Source)
at java.io.InputStreamReader.<init>(Unknown Source)
at me.storminmormon30.first.main.onCommand(main.java:43)
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
... 15 more
4

2 回答 2

2

看起来错误在其他地方: 未处理的异常执行命令“投票”
我在您给出的代码片段中看不到该命令

于 2012-11-02T23:50:34.183 回答
0

您应该将每个命令拆分为它自己的类并在 plugin.yml 中注册,我要做的是在onEnable()方法中,设置如下:

getCommand("command").setExecutor(new CommandExecutor());

这会将所有命令分成他们自己的类,这样会更好,我会建议这样做。

main您的代码问题在第 43 行的类中看起来像 NPE (在您的onEnable()方法中)

于 2013-10-27T20:49:16.017 回答