Skip to content

Commit

Permalink
fix: /pk link on non-existant system
Browse files Browse the repository at this point in the history
Fixed bug where when /pk link is ran on a user without a loaded system, an error is created. Now, /pk link will automatically load the attached system.
  • Loading branch information
Allymonies committed Jun 7, 2021
1 parent 4d0d2c6 commit 57e5719
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
20 changes: 17 additions & 3 deletions src/main/java/com/omnipico/pluralkitmc/PluralKitData.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ void setSystemId(UUID uuid, String systemId) {
userCache.put(uuid, user);
}

void setSystemId(UUID uuid, String systemId, String token) {
config.set("players." + uuid.toString() + ".system", systemId);
plugin.saveConfig();

userCache.remove(uuid);
UserCache user = new UserCache(uuid, systemId, token, plugin);
userCache.put(uuid, user);
}

UserCache getCacheOrCreate(UUID uuid) {
if (userCache.containsKey(uuid)) {
UserCache user = userCache.get(uuid);
Expand All @@ -85,9 +94,14 @@ boolean setToken(UUID uuid, String token) {
config.set("players." + uuid.toString() + ".token", token);
plugin.saveConfig();
UserCache user = getCacheOrCreate(uuid);
if (user.verifyToken(token)) {
user.setToken(token);
user.Update();
String systemId = UserCache.verifyToken(token);
if (systemId != null) {
if (user == null) {
setSystemId(uuid, systemId, token);
} else {
user.setToken(token);
user.Update();
}
plugin.saveConfig();
return true;
}
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/omnipico/pluralkitmc/UserCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ public void run() {
}
}

public boolean verifyToken(String token) {
public static String verifyToken(String token) {
URL url = null;
try {
url = new URL("https://api.pluralkit.me/v1/s/");
} catch (MalformedURLException e) {
return false;
return null;
}
InputStreamReader reader = null;
try {
Expand All @@ -166,9 +166,10 @@ public boolean verifyToken(String token) {
conn.addRequestProperty("User-Agent", "PluralKitMC");
reader = new InputStreamReader(conn.getInputStream());
} catch (IOException e) {
return false;
return null;
}
return true;
PluralKitSystem system = new Gson().fromJson(reader, PluralKitSystem.class);
return system.id;
}

public List<PluralKitMember> getMembers() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: PluralKitMC
version: 0.4.8
version: 0.4.9
author: Allymonies
main: com.omnipico.pluralkitmc.PluralKitMC
api-version: 1.16
Expand Down

0 comments on commit 57e5719

Please sign in to comment.