Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Server-Sent Events #16

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Final tweaks, remove debug broadcast and add docs
MrToirol committed Aug 26, 2024
commit 5b6fc5460ee78a8fb0cdc9a3aba330d73e211c2b
236 changes: 236 additions & 0 deletions docs/events.md
Original file line number Diff line number Diff line change
@@ -9,6 +9,242 @@ You can easily connect to the event stream from your terminal using
curl -H "Accept:text/event-steam" "https://api.earthmc.net/v3/aurora/events"
````

<br>

### Here is a list of all the events and their JSON structure:
*(Each event carries a UNIX timestamp)*

- PlayerJoin (aurora)
```yaml
{
player: {
name: str
uuid: str
}
}
```
- PlayerQuit (aurora)
```yaml
{
player: {
name: str
uuid: str
}
}
```

<br>

- NewDay
```yaml
{
fallenTowns: str[]
fallenNations: str[]
}
```

<br>

- NewNation
```yaml
{
nation: {
name: str
uuid: str
}
king: {
name: str
uuid: str
}
}
```
- DeleteNation
```yaml
{
nation: {
name: str
uuid: str
}
king: {
name: str
uuid: str
}
}
```
- RenameNation
```yaml
{
nation: {
name: str
uuid: str
}
oldName: str
}
```
- NationKingChange
```yaml
{
nation: {
name: str
uuid: str
}
newKing: {
name: str
uuid: str
}
oldKing: {
name: str
uuid: str
}
isCapitalChange: bool

# if isCapitalChange is true:
newCapital: {
name: str
uuid: str
}
oldCapital: {
name: str
uuid: str
}
}
```
- NationAddTown
```yaml
{
nation: {
name: str
uuid: str
}
town: {
name: str
uuid: str
}
}
```
- NationRemoveTown
```yaml
{
nation: {
name: str
uuid: str
}
town: {
name: str
uuid: str
}
}
```

<br>

- NewTown
```yaml
{
town: {
name: str
uuid: str
}
mayor: {
name: str
uuid: str
}
}
```
- DeleteTown
```yaml
{
town: {
name: str
uuid: str
}
mayor: {
name: str
uuid: str
}
}
```
- RenameTown
```yaml
{
town: {
name: str
uuid: str
}
oldName: str
}
```
- TownMayorChanged
```yaml
{
town: {
name: str
uuid: str
}
newMayor: {
name: str
uuid: str
}
oldMayor: {
name: str
uuid: str
}
}
```
- TownRuined
```yaml
{
town: {
name: str
uuid: str
}
oldMayor: {
name: str
uuid: str
}
}
```
- TownReclaimed
```yaml
{
town: {
name: str
uuid: str
}
newMayor: {
name: str
uuid: str
}
}
```
- TownAddResident
```yaml
{
town: {
name: str
uuid: str
}
resident: {
name: str
uuid: str
}
}
```
- TownRemoveResident
```yaml
{
town: {
name: str
uuid: str
}
resident: {
name: str
uuid: str
}
}
```

<br>

Example `NewNation` event
```json5
2 changes: 0 additions & 2 deletions src/main/java/net/earthmc/emcapi/EMCAPI.java
Original file line number Diff line number Diff line change
@@ -51,12 +51,10 @@ public void onEnable() {

getServer().getPluginManager().registerEvents(new TownyListeners(), this);
getServer().getPluginManager().registerEvents(new PlayerConnectionListener(), this);

}

@Override
public void onDisable() {
//sseManager.stop(); Should I add this or is it enough to simply shut down javalin?
javalin.stop();
}

Original file line number Diff line number Diff line change
@@ -55,6 +55,10 @@ public void onNationKingChange(NationKingChangeEvent event) {
message.add("newKing", EndpointUtils.getResidentJsonObject(event.getNewKing()));
message.add("oldKing", EndpointUtils.getResidentJsonObject(event.getOldKing()));
message.addProperty("isCapitalChange", event.isCapitalChange());
if (event.isCapitalChange()) {
message.add("newCapital", EndpointUtils.getTownJsonObject(event.getNewKing().getTownOrNull()));
message.add("oldCapital", EndpointUtils.getTownJsonObject(event.getOldKing().getTownOrNull()));
}
SSEManager.broadcastMessage("NationKingChange", message);
}

@@ -110,10 +114,10 @@ public void onTownMayorChanged(TownMayorChangedEvent event) {
}

@EventHandler
public void onTownRuined(TownRuinedEvent event) {
public void onTownRuined(TownPreRuinedEvent event) {
JsonObject message = new JsonObject();
message.add("town", EndpointUtils.getTownJsonObject(event.getTown()));
message.addProperty("oldMayor", event.getOldMayorName());
message.add("oldMayor", EndpointUtils.getResidentJsonObject(event.getTown().getMayor()));
SSEManager.broadcastMessage("TownRuined", message);
}

9 changes: 0 additions & 9 deletions src/main/java/net/earthmc/emcapi/manager/SSEManager.java
Original file line number Diff line number Diff line change
@@ -24,15 +24,6 @@ public void loadSSE() {
client.onClose(() -> clients.remove(client));
clients.add(client);
});

// Testing purposes
javalin.get("/broadcast", ctx -> {
JsonObject message = new JsonObject();
message.addProperty("text", "Hello to all connected clients!");
broadcastMessage("Hello", message);
ctx.result("Message broadcasted to all %s clients".formatted(clients.size()));
});
// Testing purposes
}

public static void broadcastMessage(String event, JsonObject data) {