Web-API [v4] - AdminPanel & RESTful web server [now with screenshots!]

@tridaak Thanks for testing the plugin :slight_smile:

  • I am assuming with “command nodes” you mean permissions? These are already provided (I forgot to document them). The permission name is “webapi.command.notify.[name]” where name is the name of the command.

  • I’m still not sure about the aliases, it feels like it’s not part of the scope of the project, but I’ll think about.

  • I fixed the json output of the players, that will be in the next update.

  • I’m not sure if the server closing should be part of the sponge functionality, but I’ll look into it

1 Like

I released version 2.1.0 on Ore and Github with the following changes:

  • Add ALL, PLAYER_KICK and PLAYER_BAN hooks
  • Add hook aliases (there you go @tridaak ;))
  • Add examples for swagger docs
  • Add class name and parent class name to /class endpoint

I did not make the server closing call PLAYER_LEAVE hooks, however there is a SERVER_STOP hook which can be subscribed to. (Sponge itself doesn’t fire PlayerLeave events when the server is closing, so it didn’t feel right to do that. Maybe I’ll add this later)

1 Like

Nice :slight_smile: Also, any chance you could add optional parameters for the notify commands?

1 Like

Finally got the chance to update webapi, getting the following:

[03:02:47] [Server thread/INFO] [webapi]: Starting Web Server...
[03:02:47] [Server thread/INFO] [webapi]: jetty-9.4.z-SNAPSHOT
[03:02:47] [Server thread/INFO] [webapi]: Started o.e.j.s.h.ContextHandler@6342efb1{/,null,AVAILABLE}
[03:02:47] [Server thread/INFO] [webapi]: Started o.e.j.s.h.ContextHandler@13708ff3{/docs,null,AVAILABLE}
[03:02:47] [Server thread/INFO] [webapi]: Started o.e.j.s.ServletContextHandler@28878ab7{/api,null,AVAILABLE}
[03:02:47] [Server thread/INFO] [webapi]: Started ServerConnector@d6a4865{HTTP/1.1,[http/1.1]}{localhost:8080}
[03:02:47] [Server thread/INFO] [webapi]: Started @53385ms
[03:02:47] [Server thread/INFO] [webapi]: Web server running on http://localhost:8080/
[03:02:47] [Server thread/ERROR] [Sponge]: Could not pass FMLServerStartedEvent to Plugin{id=webapi, name=Web-API, version=2.1.0-S5.1, description=Access Minecraft through a Web API, url=ht
tps://github.com/Valandur/Web-API, authors=[Valandur], source=/home/minecraft/mc/mods/webapi-2.1.0-S5.1.jar}
java.lang.AbstractMethodError: Method net/minecraftforge/fml/common/event/FMLServerStartedEvent.getCause()Lorg/spongepowered/api/event/cause/Cause; is abstract
        at net.minecraftforge.fml.common.event.FMLServerStartedEvent.getCause(FMLServerStartedEvent.java) ~[FMLServerStartedEvent.class:?]
        at valandur.webapi.WebAPI.onServerStart(WebAPI.java:287) ~[WebAPI.class:?]
        at org.spongepowered.common.event.listener.GameStartedServerEventListener_WebAPI_onServerStart74.handle(Unknown Source) ~[?:?]
        at org.spongepowered.common.event.RegisteredListener.handle(RegisteredListener.java:95) ~[RegisteredListener.class:1.10.2-2254-5.2.0-BETA-2272]
        at org.spongepowered.mod.event.SpongeModEventManager.post(SpongeModEventManager.java:313) [SpongeModEventManager.class:1.10.2-2254-5.2.0-BETA-2272]
        at org.spongepowered.mod.event.SpongeModEventManager.post(SpongeModEventManager.java:342) [SpongeModEventManager.class:1.10.2-2254-5.2.0-BETA-2272]
        at org.spongepowered.mod.SpongeMod.onStateEvent(SpongeMod.java:234) [SpongeMod.class:1.10.2-2254-5.2.0-BETA-2272]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_111]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_111]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_111]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_111]
        at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74) [minecraft_server.1.10.2.jar:?]
        at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47) [minecraft_server.1.10.2.jar:?]
        at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322) [minecraft_server.1.10.2.jar:?]
        at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304) [minecraft_server.1.10.2.jar:?]
        at com.google.common.eventbus.EventBus.post(EventBus.java:275) [minecraft_server.1.10.2.jar:?]
        at net.minecraftforge.fml.common.LoadController.sendEventToModContainer(LoadController.java:243) [LoadController.class:?]
        at net.minecraftforge.fml.common.LoadController.propogateStateMessage(LoadController.java:221) [LoadController.class:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_111]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_111]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_111]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_111]
        at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74) [minecraft_server.1.10.2.jar:?]
        at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47) [minecraft_server.1.10.2.jar:?]
        at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322) [minecraft_server.1.10.2.jar:?]
        at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304) [minecraft_server.1.10.2.jar:?]
        at com.google.common.eventbus.EventBus.post(EventBus.java:275) [minecraft_server.1.10.2.jar:?]
        at net.minecraftforge.fml.common.LoadController.redirect$onPost$zzb000(LoadController.java:552) [LoadController.class:?]
        at net.minecraftforge.fml.common.LoadController.distributeStateMessage(LoadController.java:145) [LoadController.class:?]
        at net.minecraftforge.fml.common.Loader.serverStarted(Loader.java:868) [Loader.class:?]
        at net.minecraftforge.fml.common.FMLCommonHandler.handleServerStarted(FMLCommonHandler.java:297) [FMLCommonHandler.class:?]
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:433) [MinecraftServer.class:?]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_111]

Same here, I had to go back to Sponge 5.1 to remove this problem.

Hey everyone

I released a first version of v3, although I marked it as a pre-release because it might still have some issues, it should be mostly stable.

Quite a few response objects from /entity, /tile-entity, /player and /world have changed. Please make sure your code still works with the new endpoints when updating.

I have implemented the following changes:

  • Add /block endpoint to query & set blocks in the world
  • Rework endpoints to provide more consistent and better structured data
  • Add fine grained permission nodes for commands run via /cmd endpoint
  • Add 100% more error messages for endpoints

The releases are for SpongeAPI 5.2 and 6.0. 5.2 should work for 5.1 aswell.

Cheers
Valandur

I will reply in this topic.

Quick release, thank you for everything :smiley:

Could you explain them ? I would like to send a post when a player logs in. Is it possible with Web-API or should I make a small plugin to do so ?

That is exactly what hooks are meant for. If you check in the webapi/hooks.conf config file you will find some description there. I will try and explain it real quick.

The hooks listed in events are called when certain minecraft events happen. So for example all the hooks listed in events.player_join are called when a player joins.

A hook is basically just another endpoint (URL) which the Web-API contacts. A simple example could look like this:

events {
    player_join=[
        {
            address="http://localhost:25000"
            method=POST
            #dataType=FORM   # Add this if you're using PHP since it makes it easier to read the data
        }
    ]
}

This would send a POST request to http://localhost:25000 whenever a player joins. The request would contain a json body containing information about the player that joined.

I hope that clears things up, and I will try to add more tutorials as soon as I can :smile:

1 Like

This is fantastic, I didn’t see hooks.conf and it’s crystal clear :smiley:

Can I add headers as well ?

    headers=[
        {
            name=X-WEBAPI-KEY
            value=MY-SUPER-SECRET-KEY
        },
        {
            name=X-SOURCE-HEADER
            value="{source}"
        }
    ]

This is so perfect, huge thanks !

You can add constant headers (such as the X-WEBAPI-KEY). Parameters in headers, such as {source} don’t work in events (the data should already be included in the request body), they only work with the command hooks.

1 Like

I have released a bug fix which takes care of problems with config files. Please update to version 3.0.1 if you are using 3.0.0

And v3.0.2 should fix errors when starting the server related to a Sponge bug

Well, nope :stuck_out_tongue:

I can only get the events-all hook to work.

Here is my config : https://pastebin.com/Qmb7ka3v
event_join.php : https://pastebin.com/NMViJXDi
event_log.txt (tried with login, suicide, get wood, logout) : https://pastebin.com/rxHk3uzq
console output : [14:59:25 INFO]: Done (3,813s)! For help, type "help" or "?"[14:59:26 INFO] [w - Pastebin.com

No error during startup, still using a fresh install with latest vanilla sponge 5.2

What is wrong in my config ?

Edit : added console ouptut pastebin

Thank you for all the log files, I found the error, it has to do with the config files (once again, I honestly HATE how Sponge currently solved this…). I’ll try and fix this as soon as I can.

@Keuterio for now I suggest using the all event hook, and then checking $_SERVER for the “X-WEBAPI-EVENT” header that will tell you which event it is

I do my best to provide enough information it’s easier for you to fix the problem :slight_smile:

Nice suggestion, I now listen to $_SERVER['HTTP_X_WEBAPI_EVENT'] and it works like a charm :

However, I can wait for next update as I am not in a hurry.

Don’t you have any support button ?

Edit : By chance, could you had hook “use” (button press) ? And if possible, “open” (open chest) ?. Only if they are not fired too often, I don’t want to overload the requests.

Was gonna ask, is it possible for us to add sponge (or other plugin) events onto the hooks file in order to notify the server about these events being triggered and respond to it accordingly.
(I believe GriefPrevention is adding claimEnter and claimLeave events and want the server to be notified when they happen)

2 Likes

Very interesting idea. I could definitely add that. I’ll have to look into how to turn the event data into nice json.

3 Likes

Even better if we can hook in whatever event we like. That could lead to a real interest for a server website.

I hope @Valandur will find an easy way to implement it :slight_smile:

Sorry for the late reply @Keuterio and @tridaak.

I’ve been working on a new version that supports custom events and serializers, and am happy to say that I’m almost done. Most of the stuff is in the git repository but I haven’t released a new version yet because it needs more testing and documentation.

How this is going to work is that you can specify any event you want to subscribe to. In case you’re not sure about the events you can check the /class endpoint which now supports looking up subclasses of a certain class. The Web-API will serialize all data which it knows how to into json. But if that’s not good enough you can now write your own serializer which will get compiled and loaded when the server runs.

Let me make a short example:

You have to create a class that extends from the Jackson StdSerializer. The file has to have the same name as the class. E.g. the following file could be placed into the /webapi/serializers folder (not the config folder):

package serializers;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import me.ryanhamshire.griefprevention.api.event.ClaimEvent;

import java.io.IOException;

public class ClaimEventSerializer extends StdSerializer<ClaimEvent> {

    public ClaimEventSerializer() {
        this(null);
    }

    public ClaimEventSerializer(Class<ClaimEvent> t) {
        super(t);
    }

    @Override
    public void serialize(ClaimEvent value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        gen.writeStartObject();
        gen.writeStringField("owner", value.getOwnerName());
        gen.writeEndObject();
    }
}

Together with a custom event in config/webapi/hooks.conf along the lines of:

custom {
    "me.ryanhamshire.griefprevention.api.event.ClaimEvent"=[
        {
            address="http://localhost:25000?event=claim"
            method=POST
        }
    ]
}

Would then cause the ClaimEvent from GriefPrevention to be serialized and sent to the hook.

I’m probably going to set up a repository or something to collect some common serializers.

Tell me what you guys think of that, and I’ll probably be releasing a new version sometime this weekend.

:open_mouth: Amazing work, can’t wait till it’s done!