Skip to content

Commit

Permalink
Add link events, toggle for DM link message, and code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
RhysB committed Jan 27, 2025
1 parent afa1e63 commit dde1381
Show file tree
Hide file tree
Showing 18 changed files with 620 additions and 303 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,5 @@ modules.xml


# End of https://www.gitignore.io/api/intellij,intellij+all,intellij+iml

libs/
Binary file removed src/.vs/slnx.sqlite
Binary file not shown.
45 changes: 33 additions & 12 deletions src/com/johnymuffin/beta/discordauth/DiscordAuthCache.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.johnymuffin.beta.discordauth;

import org.bukkit.entity.Player;

import java.util.HashMap;
import java.util.UUID;


public class DiscordAuthCache {
DiscordAuthentication plugin;

private HashMap<String, HashMap<String, String>> codeCache = new HashMap<String, HashMap<String, String> >();
private HashMap<String, HashMap<String, String>> codeCache = new HashMap<String, HashMap<String, String>>();


public DiscordAuthCache(DiscordAuthentication plugin) {
Expand All @@ -18,7 +17,7 @@ public DiscordAuthCache(DiscordAuthentication plugin) {

public void addCodeToken(String uuid, String code, String discordID) {
//Replace possible existing code
if(codeCache.containsKey(uuid)) {
if (codeCache.containsKey(uuid)) {
codeCache.remove(uuid);
}
//Store code and ID
Expand All @@ -29,32 +28,54 @@ public void addCodeToken(String uuid, String code, String discordID) {

}

@Deprecated
public boolean isUserPending(String uuid) {
if(codeCache.containsKey(uuid)) {
if (codeCache.containsKey(uuid)) {
return true;
}
return false;
}

public boolean verifyCode(String uuid, String codeAttempt){
if(!codeCache.containsKey(uuid)) {
public boolean isUserPending(UUID uuid) {
return isUserPending(uuid.toString());
}

@Deprecated
public boolean verifyCode(String uuid, String codeAttempt) {
if (!codeCache.containsKey(uuid)) {
return false;
}
if(codeCache.get(uuid).get("code").equalsIgnoreCase(codeAttempt)) {
if (codeCache.get(uuid).get("code").equalsIgnoreCase(codeAttempt)) {
return true;
}

return false;
}

public boolean verifyCode(UUID uuid, String codeAttempt) {
return verifyCode(uuid.toString(), codeAttempt);
}

// Remove the code from the cache
public void removeCode(UUID uuid, String code) {
String uuidString = uuid.toString();

if (codeCache.containsKey(uuidString)) {
if (codeCache.get(uuidString).get("code").equalsIgnoreCase(code)) {
codeCache.remove(uuidString);
}
}
}

@Deprecated
public String getUUIDDiscordID(String uuid) {
if(!codeCache.containsKey(uuid)) {
if (!codeCache.containsKey(uuid)) {
return "0";
}
return codeCache.get(uuid).get("id");

return codeCache.get(uuid).get("id");
}


public long getUUIDDiscordID(UUID uuid) {
return Long.parseLong(getUUIDDiscordID(uuid.toString()));
}
}
5 changes: 5 additions & 0 deletions src/com/johnymuffin/beta/discordauth/DiscordAuthConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ private void write() {

generateConfigOption("settings.discord.automatic-nickname.enabled", false);
generateConfigOption("settings.discord.automatic-nickname.info", "This will automatically set the nickname of the user to their minecraft username.");

// Setting that sends linking message via DM as opposed to linking channel
generateConfigOption("settings.discord.send-linking-code-via-dm.info", "This will send the linking code via DM instead of in the channel the command was run in. Sending via DMs can be problematic if users have DMs disabled.");
generateConfigOption("settings.discord.send-linking-code-via-dm.enabled", false);

getAutomaticNicknameGuilds();

}
Expand Down
68 changes: 64 additions & 4 deletions src/com/johnymuffin/beta/discordauth/DiscordAuthDatafile.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.johnymuffin.beta.discordauth;

import com.johnymuffin.beta.discordauth.events.DiscordAuthenticationLinkEvent;
import com.johnymuffin.beta.discordauth.events.DiscordAuthenticationUnlinkEvent;
import org.bukkit.Bukkit;
import org.bukkit.util.config.Configuration;

Expand Down Expand Up @@ -31,13 +33,19 @@ public void run() {

}

@Deprecated
public boolean isUUIDAlreadyLinked(String uuid) {
if (discordAuthDatabase.containsKey(uuid)) {
return true;
}
return false;
}

public boolean isUUIDAlreadyLinked(UUID uuid) {
return isUUIDAlreadyLinked(uuid.toString());
}

@Deprecated
public boolean isDiscordIDAlreadyLinked(String discordID) {

//Iterate through all keys
Expand All @@ -49,26 +57,60 @@ public boolean isDiscordIDAlreadyLinked(String discordID) {
return false;
}

public boolean isDiscordIDAlreadyLinked(long discordID) {
return isDiscordIDAlreadyLinked(String.valueOf(discordID));
}

@Deprecated
public boolean removeLinkFromDiscordID(String discordID) {
//Iterate through all keys
for (String key1 : discordAuthDatabase.keySet()) {
if (discordAuthDatabase.get(key1).get("discordID").equals(discordID)) {
long DiscordID = Long.parseLong(discordID);
UUID minecraftUUID = UUID.fromString(key1);

// Call DiscordAuthenticationUnlinkEvent event in the next tick
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, () -> {
// Call DiscordAuthenticationUnlinkEvent event
DiscordAuthenticationUnlinkEvent event = new DiscordAuthenticationUnlinkEvent(minecraftUUID, DiscordID);
Bukkit.getServer().getPluginManager().callEvent(event);
});

discordAuthDatabase.remove(key1);
return true;
}
}
return false;
}

public boolean removeLinkFromDiscordID(long discordID) {
return removeLinkFromDiscordID(String.valueOf(discordID));
}

@Deprecated
public boolean removeLinkByUUID(String uuid) {
if (discordAuthDatabase.containsKey(uuid)) {
long discordID = Long.parseLong(discordAuthDatabase.get(uuid).get("discordID"));
discordAuthDatabase.remove(uuid);

// Call DiscordAuthenticationUnlinkEvent event in the next tick
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, () -> {
// Call DiscordAuthenticationUnlinkEvent event
DiscordAuthenticationUnlinkEvent event = new DiscordAuthenticationUnlinkEvent(UUID.fromString(uuid), discordID);
Bukkit.getServer().getPluginManager().callEvent(event);
});

return true;
}
return false;
}

public boolean removeLinkByUUID(UUID uuid) {
return removeLinkByUUID(uuid.toString());
}


@Deprecated
public boolean addLinkedUser(String username, String uuid, String discordID) {
if (isUUIDAlreadyLinked(uuid) || isDiscordIDAlreadyLinked(discordID)) {
//If user is already linked return false
Expand All @@ -80,24 +122,37 @@ public boolean addLinkedUser(String username, String uuid, String discordID) {
tmp.put("discordID", discordID);

discordAuthDatabase.put(uuid, tmp);

// Call DiscordAuthenticationLinkEvent event
DiscordAuthenticationLinkEvent event = new DiscordAuthenticationLinkEvent(UUID.fromString(uuid), Long.parseLong(discordID));
Bukkit.getServer().getPluginManager().callEvent(event);

return true;
}

public boolean addLinkedUser(String username, UUID uuid, long discordID) {
return addLinkedUser(username, uuid.toString(), String.valueOf(discordID));
}

@Deprecated
public String getDiscordIDFromUUID(String uuid) {
if (!discordAuthDatabase.containsKey(uuid)) {
return null;
} else {
return discordAuthDatabase.get(uuid).get("discordID");
}
}


public long getDiscordIDFromUUID(UUID uuid) {
return Long.parseLong(getDiscordIDFromUUID(uuid.toString()));
}

@Deprecated
public String getInitialUsernameFromDiscordID(String discordID) {
return getLastUsernameFromDiscordID(discordID);
}

@Deprecated
public String getLastUsernameFromDiscordID(String discordID) {
if (!isDiscordIDAlreadyLinked(discordID)) {
return null;
Expand All @@ -109,7 +164,11 @@ public String getLastUsernameFromDiscordID(String discordID) {
return discordAuthDatabase.get(uuid).get("username");
}

public String getLastUsernameFromDiscordID(long discordID) {
return getLastUsernameFromDiscordID(String.valueOf(discordID));
}

@Deprecated
public String getUUIDFromDiscordID(String discordID) {
for (String key1 : discordAuthDatabase.keySet()) {
if (discordAuthDatabase.get(key1).get("discordID").equals(discordID)) {
Expand All @@ -119,6 +178,10 @@ public String getUUIDFromDiscordID(String discordID) {
return null;
}

public UUID getUUIDFromDiscordID(long discordID) {
return UUID.fromString(getUUIDFromDiscordID(String.valueOf(discordID)));
}

public void updateLastKnownUsername(UUID uuid, String username) {
String uuid2 = uuid.toString();
if (!discordAuthDatabase.containsKey(uuid2)) {
Expand All @@ -127,11 +190,8 @@ public void updateLastKnownUsername(UUID uuid, String username) {
if (!discordAuthDatabase.get(uuid2).get("username").equals(username)) {
discordAuthDatabase.get(uuid2).replace("username", username);
}


}


public void saveConfig() {
config.setProperty("authentication", discordAuthDatabase);
config.save();
Expand Down
Loading

0 comments on commit dde1381

Please sign in to comment.