Question

I'm trying to create a plugin for minecraft bukkit servers. The goal is to read the string on the first line of an html page. If the result is True it will execute a command.

Here is the code I have right now:

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;

}

}

Creates the following error in the log:

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
Was it helpful?

Solution

Looks like the error is somewhere else: Unhandled exception executing command 'vote'
I don't see that command in the code snippet you have given

OTHER TIPS

You should split each command into it's own class and register in in the plugin.yml, What I would do, is in the onEnable() method, set something like this:

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

That will split off all of the commands into their own class, which would work better, and I would suggest it.

The issue with your code looks like a NPE in your class main on line 43 (its in your onEnable() method)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top