Skip to content

Commit

Permalink
feat: ported buttons support
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelHillcox committed May 15, 2024
1 parent b108337 commit bd48592
Show file tree
Hide file tree
Showing 15 changed files with 289 additions and 50 deletions.
21 changes: 19 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
## 86.0.0
## 86.0.1

### Changed

- Updated to 1.20.6
Buttons can be added by adding the following to the mods config file. You can have up to 2 buttons. Each button can have a label up to 32 characters long.

If you do not want to have buttons, you can leave the buttons array empty.

```json
{
"buttons": [
{
"label": "Google",
"url": "https://www.google.com"
},
{
"label": "Yahoo",
"url": "https://www.yahoo.com"
}
]
}
```
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,39 @@ We omit the `modid` from the dimension name and use the `name` instead. So `mine
}
```

### Config Buttons (6.0.2+)

Buttons can be added by adding the following to the mods config file.

**Restrictions**
- You can have up to 2 buttons.
- Each button can have a label up to 32 characters long.

If you do not want to have buttons, you can leave the buttons array empty.

```json5
{
"...": "See above", // See above
"buttons": [
{
"label": "Google",
"url": "https://www.google.com"
},
{
"label": "Yahoo",
"url": "https://www.yahoo.com"
}
]
}
```

## KubeJS Integration

> **Note!**
>
> KubeJS Support is only support in 3.0.0+ for `1.19.2` and `4.0.3+` for `1.20.1+`
> KubeJS Support is only support in 3.0.0+ for `1.19.2` and `4.0.3+` for `1.20.1+`.
>
> KubeJS no longer supports Forge or Fabric so support for KubeJS as of `1.20.4+` is limited to just the `NeoForge` version of our mod
>
> Only `KubeJS 6+` is supported!
Expand Down
5 changes: 0 additions & 5 deletions common/src/main/java/com/sunekaer/sdrp/SDRP.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.sunekaer.sdrp.config.SDRPConfig;
import com.sunekaer.sdrp.discord.RPClient;
import com.sunekaer.sdrp.discord.State;
//import com.sunekaer.sdrp.integration.kubejs.SDRPKubeJSIntegration;
import dev.architectury.event.EventResult;
import dev.architectury.event.events.client.ClientGuiEvent;
import dev.architectury.event.events.client.ClientLifecycleEvent;
Expand Down Expand Up @@ -43,10 +42,6 @@ public static void init() {

EntityEvent.ADD.register(SDRP::clientJoinEvent);
ClientGuiEvent.INIT_POST.register(SDRP::screenEvent);

// if (Platform.isModLoaded("kubejs")) {
// SDRPKubeJSIntegration.setup();
// }
}

/**
Expand Down
10 changes: 10 additions & 0 deletions common/src/main/java/com/sunekaer/sdrp/config/SDRPConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Config(name = SDRP.MOD_ID + "-common")
Expand All @@ -30,6 +32,9 @@ public class SDRPConfig implements ConfigData {
@Comment("When enabled, the mod will log the current state being sent to Discord")
public boolean logState = false;

@Comment("Set custom buttons for the Discord Rich Presences. You can only have 2 buttons, each button has a label and a URL.")
public List<Button> buttons = new ArrayList<>();

@Override
public void validatePostLoad() {
var oldConfig = SDRPCrossPlatform.getConfigDirectory().resolve("sdrp.json");
Expand Down Expand Up @@ -60,4 +65,9 @@ public static final class OldConfigEntry<T> {
private @Nullable T value;
private String comment;
}

public static final class Button {
public String label;
public String url;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package com.sunekaer.sdrp.discord;

import com.jagrosh.discordipc.entities.RichPresence;
import org.jetbrains.annotations.Nullable;
import org.json.JSONArray;
import org.json.JSONObject;

import java.time.OffsetDateTime;

public class ExtendedRichPresence extends RichPresence {
public @Nullable String button_label_1;
public @Nullable String button_url_1;
public @Nullable String button_label_2;
public @Nullable String button_url_2;

public ExtendedRichPresence(String state, String details, OffsetDateTime startTimestamp, OffsetDateTime endTimestamp, String largeImageKey, String largeImageText, String smallImageKey, String smallImageText, String partyId, int partySize, int partyMax, String matchSecret, String joinSecret, String spectateSecret, boolean instance) {
super(state, details, startTimestamp, endTimestamp, largeImageKey, largeImageText, smallImageKey, smallImageText, partyId, partySize, partyMax, matchSecret, joinSecret, spectateSecret, instance);
}

public ExtendedRichPresence(String state, String details, OffsetDateTime startTimestamp, OffsetDateTime endTimestamp, String largeImageKey, String largeImageText, String smallImageKey, String smallImageText, String partyId, int partySize, int partyMax, String matchSecret, String joinSecret, String spectateSecret, boolean instance, @Nullable String button_label_1, @Nullable String button_url_1, @Nullable String button_label_2, @Nullable String button_url_2) {
super(state, details, startTimestamp, endTimestamp, largeImageKey, largeImageText, smallImageKey, smallImageText, partyId, partySize, partyMax, matchSecret, joinSecret, spectateSecret, instance);
this.button_label_1 = button_label_1;
this.button_url_1 = button_url_1;
this.button_label_2 = button_label_2;
this.button_url_2 = button_url_2;
}

@Override
public JSONObject toJson() {
var jsonObject = super.toJson();
var buttonsObj = new JSONArray();
if (button_label_1 != null && button_url_1 != null) {
var btn1 = new JSONObject();
btn1.put("label", button_label_1);
btn1.put("url", button_url_1);
buttonsObj.put(btn1);
}
if (button_label_2 != null && button_url_2 != null) {
var btn2 = new JSONObject();
btn2.put("label", button_label_2);
btn2.put("url", button_url_2);
buttonsObj.put(btn2);
}
if (!buttonsObj.isEmpty()) {
jsonObject.put("buttons", buttonsObj);
if (jsonObject.has("secrets")) {
jsonObject.remove("secrets");
}
}
return jsonObject;
}

public static class ExtendedBuilder {
private String state;
private String details;
private OffsetDateTime startTimestamp;
private OffsetDateTime endTimestamp;
private String largeImageKey;
private String largeImageText;
private String smallImageKey;
private String smallImageText;
private String partyId;
private int partySize;
private int partyMax;
private String matchSecret;
private String joinSecret;
private String spectateSecret;
private boolean instance;

private String button_label_1;
private String button_url_1;
private String button_label_2;
private String button_url_2;

public ExtendedBuilder setState(String state) {
this.state = state;
return this;
}

public ExtendedBuilder setDetails(String details) {
this.details = details;
return this;
}

public ExtendedBuilder setStartTimestamp(OffsetDateTime startTimestamp) {
this.startTimestamp = startTimestamp;
return this;
}

public ExtendedBuilder setEndTimestamp(OffsetDateTime endTimestamp) {
this.endTimestamp = endTimestamp;
return this;
}

public ExtendedBuilder setLargeImage(String largeImageKey, String largeImageText) {
this.largeImageKey = largeImageKey;
this.largeImageText = largeImageText;
return this;
}

public ExtendedBuilder setLargeImage(String largeImageKey) {
return setLargeImage(largeImageKey, null);
}

public ExtendedBuilder setSmallImage(String smallImageKey, String smallImageText) {
this.smallImageKey = smallImageKey;
this.smallImageText = smallImageText;
return this;
}

public ExtendedBuilder setSmallImage(String smallImageKey) {
return setSmallImage(smallImageKey, null);
}

public ExtendedBuilder setParty(String partyId, int partySize, int partyMax) {
this.partyId = partyId;
this.partySize = partySize;
this.partyMax = partyMax;
return this;
}

public ExtendedBuilder setMatchSecret(String matchSecret) {
this.matchSecret = matchSecret;
return this;
}

public ExtendedBuilder setJoinSecret(String joinSecret) {
this.joinSecret = joinSecret;
return this;
}

public ExtendedBuilder setSpectateSecret(String spectateSecret) {
this.spectateSecret = spectateSecret;
return this;
}

public ExtendedBuilder setInstance(boolean instance) {
this.instance = instance;
return this;
}

public ExtendedBuilder setButton1(String label, String url) {
// If the label is greater than 32 characters, it will be truncated.
if (label.length() > 32) {
// This is a failsafe
label = label.substring(0, 32);
}

this.button_label_1 = label;
this.button_url_1 = url;
return this;
}

public ExtendedBuilder setButton2(String label, String url) {
// If the label is greater than 32 characters, it will be truncated.
if (label.length() > 32) {
// This is a failsafe
label = label.substring(0, 32);
}

this.button_label_2 = label;
this.button_url_2 = url;
return this;
}

public ExtendedRichPresence build() {
return new ExtendedRichPresence(state, details, startTimestamp, endTimestamp, largeImageKey, largeImageText, smallImageKey, smallImageText, partyId, partySize, partyMax, matchSecret, joinSecret, spectateSecret, instance, button_label_1, button_url_1, button_label_2, button_url_2);
}
}
}
8 changes: 7 additions & 1 deletion common/src/main/java/com/sunekaer/sdrp/discord/RPClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.jagrosh.discordipc.IPCClient;
import com.jagrosh.discordipc.IPCListener;
import com.jagrosh.discordipc.entities.Callback;
import com.jagrosh.discordipc.entities.RichPresence;
import com.jagrosh.discordipc.entities.User;
import com.jagrosh.discordipc.entities.pipe.PipeStatus;
Expand Down Expand Up @@ -88,7 +89,12 @@ private void processStateQueue() {
return;
}

this.client.sendRichPresence(state);
this.client.sendRichPresence(state, new Callback((s) -> {}, (e) -> {
if (SDRP.config.logState) {
LOGGER.error("Failed to send state to discord: {}\n {}", state.toJson().toString(), e);
}
}));

if (SDRP.config.logState) {
LOGGER.info("Sent state to discord: {}", state.toJson().toString());
}
Expand Down
18 changes: 15 additions & 3 deletions common/src/main/java/com/sunekaer/sdrp/discord/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,23 @@ public State(String m, String n, String k) {
}

public RichPresence createPresence() {
return new RichPresence.Builder()
ExtendedRichPresence.ExtendedBuilder presence = new ExtendedRichPresence.ExtendedBuilder()
.setState(I18n.get(message))
.setStartTimestamp(SDRP.START_TIME)
.setLargeImage("logo", I18n.get("sdrp.logo"))
.setSmallImage(imageKey, I18n.get(imageName))
.build();
.setSmallImage(imageKey, I18n.get(imageName));

if (!SDRP.config.buttons.isEmpty()) {
var buttonOne = SDRP.config.buttons.get(0);
presence.setButton1(buttonOne.label, buttonOne.url);

if (SDRP.config.buttons.size() > 1) {
var buttonTwo = SDRP.config.buttons.get(1);
presence.setButton2(buttonTwo.label, buttonTwo.url);
}
}

return presence.build();
}

}

This file was deleted.

1 change: 0 additions & 1 deletion common/src/main/resources/kubejs.plugins.txt

This file was deleted.

2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ minecraft_version=1.20.6
enabled_platforms=fabric,neoforge

archives_base_name=SimpleDiscordRichPresence
mod_version=86.0.0
mod_version=86.0.1
maven_group=com.sunekaer.mods

curseforge_id=334853
Expand Down
17 changes: 0 additions & 17 deletions neoforge/src/main/java/com/sunekaer/sdrp/neoforge/SDRPForge.java

This file was deleted.

Loading

0 comments on commit bd48592

Please sign in to comment.