Signed-off-by: Etzelia <etzelia@hotmail.com>
mcm
Etzelia 2020-10-31 22:18:36 -05:00
parent fbf285fb4f
commit fb030fe62e
No known key found for this signature in database
GPG Key ID: 708511AE7ABC5314
9 changed files with 94 additions and 1 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,6 +118,11 @@ 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());

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;