Compare commits

..

1 Commits
post ... master

Author SHA1 Message Date
Etzelia f2a6249af1 schedule (#1)
Schedule kick

Signed-off-by: Etzelia <etzelia@hotmail.com>

Chat API (#7)

Chat API

Signed-off-by: Etzelia <etzelia@hotmail.com>

Reviewed-on: https://git.etztech.xyz/Minecraft/ServerAPI/pulls/7
Reviewed-by: ZeroHD <joey@ahines.net>

Add token auth and POST endpoints (#6)

Add token auth and POST endpoints

Signed-off-by: Etzelia <etzelia@hotmail.com>

Reviewed-on: https://git.etztech.xyz/Minecraft/ServerAPI/pulls/6
Reviewed-by: ZeroHD <joey@ahines.net>

Change QueryAPI to PingAPI and add PluginAPI (#4)

typo

Fix null fields

Signed-off-by: Etzelia <etzelia@hotmail.com>

Change QueryAPI to PingAPI and add PluginAPI

Signed-off-by: Etzelia <etzelia@hotmail.com>

Reviewed-on: https://git.etztech.xyz/Minecraft/ServerAPI/pulls/4
Reviewed-by: ZeroHD <joey@ahines.net>

Fix conflicting name (#3)

Fix conflicting name

Signed-off-by: Etzelia <etzelia@hotmail.com>

Reviewed-on: https://git.etztech.xyz/Minecraft/ServerAPI/pulls/3

Reviewed-on: https://git.birbmc.com/BirbMC/serverapi/pulls/1
Co-Authored-By: Etzelia <etzelia@hotmail.com>
Co-Committed-By: Etzelia <etzelia@hotmail.com>
2021-03-16 01:54:15 +00:00
9 changed files with 100 additions and 3 deletions

View File

@ -9,6 +9,7 @@ import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import xyz.etztech.serverapi.commands.MainCommand; import xyz.etztech.serverapi.commands.MainCommand;
import xyz.etztech.serverapi.listeners.AsyncPlayerChatListener;
import xyz.etztech.serverapi.token.TokenList; import xyz.etztech.serverapi.token.TokenList;
import xyz.etztech.serverapi.tps.TPS; import xyz.etztech.serverapi.tps.TPS;
import xyz.etztech.serverapi.web.IProvider; import xyz.etztech.serverapi.web.IProvider;
@ -25,6 +26,8 @@ public class ServerAPI extends JavaPlugin implements IProvider {
private Web web = new Web(this); private Web web = new Web(this);
private final Logger log = Logger.getLogger( "Minecraft" ); private final Logger log = Logger.getLogger( "Minecraft" );
private static final List<ChatAPI> chat = new ArrayList<>();
@Override @Override
public void onEnable() { public void onEnable() {
instance = this; instance = this;
@ -33,6 +36,7 @@ public class ServerAPI extends JavaPlugin implements IProvider {
reloadConfig(); reloadConfig();
if (isEnabled()) { if (isEnabled()) {
new AsyncPlayerChatListener(this);
new MainCommand(this); new MainCommand(this);
tps = new TPS(); tps = new TPS();
@ -68,6 +72,10 @@ public class ServerAPI extends JavaPlugin implements IProvider {
return tps; return tps;
} }
public static List<ChatAPI> getChat() {
return chat;
}
@Override @Override
public TPSAPI TPS() { public TPSAPI TPS() {
return new TPSAPI(tps.getHistory()); return new TPSAPI(tps.getHistory());
@ -110,11 +118,18 @@ public class ServerAPI extends JavaPlugin implements IProvider {
return bans; return bans;
} }
@Override
public List<ChatAPI> chat() {
return chat;
}
@Override @Override
public void kick(BanAPI kick) { public void kick(BanAPI kick) {
Player player = Bukkit.getPlayerExact(kick.getTarget()); Player player = Bukkit.getPlayerExact(kick.getTarget());
if (player != null) { if (player != null) {
getServer().getScheduler().runTask(this, () -> {
player.kickPlayer("You have been kicked: " + kick.getReason()); player.kickPlayer("You have been kicked: " + kick.getReason());
});
} }
} }
@ -127,7 +142,9 @@ public class ServerAPI extends JavaPlugin implements IProvider {
Bukkit.getBanList(BanList.Type.NAME).addBan(ban.getTarget(), ban.getReason(), expires, "ServerAPI"); Bukkit.getBanList(BanList.Type.NAME).addBan(ban.getTarget(), ban.getReason(), expires, "ServerAPI");
Player player = Bukkit.getPlayerExact(ban.getTarget()); Player player = Bukkit.getPlayerExact(ban.getTarget());
if (player != null) { if (player != null) {
getServer().getScheduler().runTask(this, () -> {
player.kickPlayer("You have been banned: " + ban.getReason()); player.kickPlayer("You have been banned: " + ban.getReason());
});
} }
} }

View File

@ -0,0 +1,31 @@
package xyz.etztech.serverapi.listeners;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import xyz.etztech.serverapi.ServerAPI;
import xyz.etztech.serverapi.web.api.ChatAPI;
import java.util.Date;
public class AsyncPlayerChatListener implements Listener {
private final ServerAPI plugin;
public AsyncPlayerChatListener(ServerAPI plugin) {
this.plugin = plugin;
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
@EventHandler(priority= EventPriority.MONITOR, ignoreCancelled=true)
public void onChat(AsyncPlayerChatEvent event) {
int chatLimit = plugin.getConfig().getInt("chat", 100);
if (ServerAPI.getChat().size() >= chatLimit) {
ServerAPI.getChat().remove(0);
}
ServerAPI.getChat().add(new ChatAPI(
String.format(event.getFormat(), event.getPlayer().getDisplayName(), event.getMessage()),
new Date().getTime()
));
}
}

View File

@ -45,4 +45,9 @@ public class GraphQL implements QueryGraphql {
public TPSAPI getTps() { public TPSAPI getTps() {
return provider.TPS(); return provider.TPS();
} }
@GraphQLName("chat")
public ChatAPI[] getChat() {
return provider.chat().toArray(new ChatAPI[0]);
}
} }

View File

@ -15,6 +15,7 @@ public interface IProvider {
List<WorldAPI> worlds(); List<WorldAPI> worlds();
WorldAPI world(String name); WorldAPI world(String name);
List<PluginAPI> plugins(); List<PluginAPI> plugins();
List<ChatAPI> chat();
// POST // POST
void kick(BanAPI kick); void kick(BanAPI kick);

View File

@ -59,6 +59,7 @@ public class Web {
app.get("/tps", this::tps); app.get("/tps", this::tps);
app.get("/worlds", this::worlds); app.get("/worlds", this::worlds);
app.get("/worlds/:name", this::world); app.get("/worlds/:name", this::world);
app.get("/chat", this::chat);
app.post("/kick", this::kick); app.post("/kick", this::kick);
app.post("/ban", this::ban); app.post("/ban", this::ban);
@ -135,6 +136,10 @@ public class Web {
ctx.json(provider.world(ctx.pathParam("name"))); ctx.json(provider.world(ctx.pathParam("name")));
} }
public void chat(Context ctx) {
ctx.json(provider.chat());
}
public void players(Context ctx) { public void players(Context ctx) {
ctx.json(provider.players()); ctx.json(provider.players());
} }

View File

@ -0,0 +1,26 @@
package xyz.etztech.serverapi.web.api;
import com.expediagroup.graphql.annotations.GraphQLDescription;
import com.expediagroup.graphql.annotations.GraphQLName;
@GraphQLName("Chat")
@GraphQLDescription("Chat GraphQL")
public class ChatAPI {
private final String message;
private final long timestamp;
public ChatAPI(String message, long timestamp) {
this.message = message;
this.timestamp = timestamp;
}
@GraphQLName("message")
public String getMessage() {
return message;
}
@GraphQLName("timestamp")
public long getTimestamp() {
return timestamp;
}
}

View File

@ -1,6 +1,9 @@
# The port to run on # The port to run on
port: 8080 port: 8080
# Number of chat logs to retain
chat: 100
# Authentication # Authentication
auth: auth:
# Protect GET routes. If false, GET routes are public. # Protect GET routes. If false, GET routes are public.

View File

@ -84,6 +84,16 @@ public class MockProvider implements IProvider {
); );
} }
@Override
public List<ChatAPI> chat() {
long now = new Date().getTime();
return Arrays.asList(
new ChatAPI("message 1", now-2),
new ChatAPI("message 2", now-1),
new ChatAPI("message 3", now)
);
}
@Override @Override
public void log(String message) { public void log(String message) {
System.out.println(message); System.out.println(message);

View File

@ -5,7 +5,6 @@ import xyz.etztech.serverapi.token.TokenList;
import xyz.etztech.serverapi.token.TokenScope; import xyz.etztech.serverapi.token.TokenScope;
import xyz.etztech.serverapi.web.Web; import xyz.etztech.serverapi.web.Web;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;