alecgorge / jsonapi

A JSON API for Minecraft: http://forums.bukkit.org/threads/admn-info-jsonapi-v1-3-1-json-http-and-socket-api-for-controlling-a-server-740.14270/
http://mcjsonapi.com
MIT License
231 stars 88 forks source link

Stream without a name? #169

Closed buschtoens closed 12 years ago

buschtoens commented 12 years ago

I try to initialize a stream, but get this error:

You just created a JSONAPIStream without a name! You shouldn't be doing that! Location:
java.lang.Exception: Stack trace
at java.lang.Thread.dumpStack(Unknown Source)
    at com.alecgorge.minecraft.jsonapi.api.JSONAPIStream.<init>(JSONAPIStream.java:30)
    at com.github.silvinci.JSONAPI.Commands.Stream.<init>(Stream.java:5)
    at com.github.silvinci.JSONAPI.Commands.Commands.onEnable(Commands.java:49)
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337)
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
    at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:257)
    at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:239)
    at net.minecraft.server.MinecraftServer.t(MinecraftServer.java:383)
    at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:370)
    at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:199)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:434)
at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
End JSONAPIStream invalid constructor location.

I use this (stripped down) code:

public class Commands extends JavaPlugin {
    // ...
    stream = new Stream();
    jsonapi.getStreamManager().registerStream("commands", stream);
    // ...
}

public class Stream extends JSONAPIStream { }

public class StreamCommand extends JSONAPIStreamMessage {
    private String command;
    private String args[];
    private Player player;

    public StreamCommand(String _command, String _args[], Player _player) {
        command = _command;
        args = _args;
        player = _player;
    }

    @Override
    public String streamName() {
        return "commands";
    }

    @Override
    public JSONObject toJSONObject() {
        JSONObject o = new JSONObject();
        o.put("time", Long.valueOf(getTime()));
        o.put("command", command);
        o.put("args", args);
        o.put("player", player);
        return o;
    }

}
alecgorge commented 12 years ago
stream = new Stream();

Needs to be:

stream = new Stream("Stream name here!");

I left the old constructor in for binary backwards compatibility, but it won't actually function.

You also may need to change Stream to include this:

public Stream(String name) {
    super(name);
}
buschtoens commented 12 years ago

Thanks. I'll update the wiki accordingly. But isn't this all redundant? Why can't we give the name only once?

Psithief commented 12 years ago

I'm fairly certain if you don't create that constructor in the subclass that the super constructor is called implicitly.

A new jsonapi.getStreamManager().registerStream(JSONAPISteam) perhaps?

alecgorge commented 12 years ago

@silvinci Thanks. I needed to be able to tell which stream stream messages came from when I listened to the event for all messages when I added the ability for the server to push streams to a HTTP endpoint.

@Psithief That might be true, I just have hazy memories of the Java compiler complaining about something when I didn't. I am probably wrong though.