[CHORE] Code cleanup and housekeeping

This commit is contained in:
2024-03-17 13:18:56 +02:00
parent 9da589f63b
commit e3573f3f6e
10 changed files with 76 additions and 123 deletions

View File

@@ -1,6 +1,6 @@
MIT License
Copyright (c) 2021-2023 HypherionSA and Contributors
Copyright (c) 2021-2024 HypherionSA and Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -3,7 +3,7 @@
Minecraft independent code used by Simple Discord Link. This library contains all the core discord code, and code that is not
dependent on Minecraft.
Requires JAVA 16 and Above!
Requires JAVA 17 and Above!
***

View File

@@ -8,23 +8,14 @@ import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
@AllArgsConstructor(staticName = "of")
public class DiscordUser {
@Getter
@Setter
private String effectiveName;
@Getter
@Setter
private String avatarUrl;
@Getter
@Setter
private long userId;
@Getter
@Setter
private String asMention;
}

View File

@@ -14,6 +14,7 @@ import com.hypherionmc.sdlink.core.services.SDLinkPlatform;
import com.hypherionmc.sdlink.core.util.Profiler;
import com.hypherionmc.sdlink.core.util.SDLinkUtils;
import com.mojang.authlib.GameProfile;
import lombok.Getter;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Role;
@@ -47,7 +48,9 @@ import static com.hypherionmc.sdlink.core.managers.DatabaseManager.sdlinkDatabas
*/
public class MinecraftAccount {
@Getter
private final String username;
@Getter
private final UUID uuid;
private final boolean isOffline;
private final boolean isValid;
@@ -122,52 +125,6 @@ public class MinecraftAccount {
}
//<editor-fold desc="Helper Methods">
private static Pair<String, UUID> fetchPlayer(String name) {
OkHttpClient client = new OkHttpClient.Builder()
.callTimeout(20, TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS)
.connectTimeout(20, TimeUnit.SECONDS)
.build();
try {
Request request = new Request.Builder()
.url("https://api.mojang.com/users/profiles/minecraft/" + name)
.cacheControl(new CacheControl.Builder().noCache().build())
.build();
Response response = client.newCall(request).execute();
if (response.isSuccessful() && response.body() != null) {
JSONObject obj = new JSONObject(new JSONTokener(response.body().string()));
String uuid = "";
String returnname = name;
if (obj.has("name") && !obj.getString("name").isEmpty()) {
returnname = obj.getString("name");
}
if (obj.has("id") && !obj.getString("id").isEmpty()) {
uuid = obj.getString("id");
}
response.close();
return Pair.of(returnname, uuid.isEmpty() ? null : mojangIdToUUID(uuid));
}
} catch (IOException | JSONException e) {
e.printStackTrace();
}
return Pair.of("", null);
}
private static UUID mojangIdToUUID(String id) {
final List<String> strings = new ArrayList<>();
strings.add(id.substring(0, 8));
strings.add(id.substring(8, 12));
strings.add(id.substring(12, 16));
strings.add(id.substring(16, 20));
strings.add(id.substring(20, 32));
return UUID.fromString(String.join("-", strings));
}
private static Pair<String, UUID> offlinePlayer(String offlineName) {
return Pair.of(offlineName, UUID.nameUUIDFromBytes(("OfflinePlayer:" + offlineName).getBytes(StandardCharsets.UTF_8)));
}
@@ -417,14 +374,6 @@ public class MinecraftAccount {
}
}
public String getUsername() {
return username;
}
public UUID getUuid() {
return uuid;
}
public boolean isValid() {
return isValid;
}
@@ -432,5 +381,51 @@ public class MinecraftAccount {
public boolean isOffline() {
return isOffline;
}
private static Pair<String, UUID> fetchPlayer(String name) {
OkHttpClient client = new OkHttpClient.Builder()
.callTimeout(20, TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS)
.connectTimeout(20, TimeUnit.SECONDS)
.build();
try {
Request request = new Request.Builder()
.url("https://api.mojang.com/users/profiles/minecraft/" + name)
.cacheControl(new CacheControl.Builder().noCache().build())
.build();
Response response = client.newCall(request).execute();
if (response.isSuccessful() && response.body() != null) {
JSONObject obj = new JSONObject(new JSONTokener(response.body().string()));
String uuid = "";
String returnname = name;
if (obj.has("name") && !obj.getString("name").isEmpty()) {
returnname = obj.getString("name");
}
if (obj.has("id") && !obj.getString("id").isEmpty()) {
uuid = obj.getString("id");
}
response.close();
return Pair.of(returnname, uuid.isEmpty() ? null : mojangIdToUUID(uuid));
}
} catch (IOException | JSONException e) {
e.printStackTrace();
}
return Pair.of("", null);
}
private static UUID mojangIdToUUID(String id) {
final List<String> strings = new ArrayList<>();
strings.add(id.substring(0, 8));
strings.add(id.substring(8, 12));
strings.add(id.substring(12, 16));
strings.add(id.substring(16, 20));
strings.add(id.substring(20, 32));
return UUID.fromString(String.join("-", strings));
}
//</editor-fold>
}

View File

@@ -9,28 +9,16 @@ import io.jsondb.annotation.Id;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
@Document(collection = "verifiedaccounts", schemaVersion = "1.0")
public class SDLinkAccount {
@Id
@Getter
@Setter
private String uuid;
@Getter
@Setter
private String username;
@Getter
@Setter
private String discordID;
@Getter
@Setter
private String verifyCode;
@Getter
@Setter
private boolean isOffline;
}

View File

@@ -16,6 +16,7 @@ import com.hypherionmc.sdlink.core.util.ThreadedEventManager;
import com.jagrosh.jdautilities.command.CommandClient;
import com.jagrosh.jdautilities.command.CommandClientBuilder;
import com.jagrosh.jdautilities.commons.waiter.EventWaiter;
import lombok.Getter;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.requests.GatewayIntent;
@@ -37,10 +38,15 @@ public class BotController {
// Thread Execution Manager
public static final ScheduledExecutorService taskManager = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors());
// Public instance of this class that can be called anywhere
public static BotController INSTANCE;
@Getter
private final EventWaiter eventWaiter = new EventWaiter();
@Getter
private final Logger logger;
// Required Variables
private JDA _jda;
@@ -199,15 +205,8 @@ public class BotController {
}
}
public Logger getLogger() {
return logger;
}
public JDA getJDA() {
return this._jda;
}
public EventWaiter getEventWaiter() {
return eventWaiter;
}
}

View File

@@ -5,6 +5,7 @@
package com.hypherionmc.sdlink.core.managers;
import com.hypherionmc.sdlink.core.discord.BotController;
import lombok.Getter;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.channel.ChannelType;
@@ -15,10 +16,14 @@ import java.util.Set;
public class CacheManager {
@Getter
private static final HashMap<String, String> serverChannels = new HashMap<>();
@Getter
private static final HashMap<String, String> serverRoles = new HashMap<>();
@Getter
private static final HashMap<String, String> userCache = new HashMap<>();
private static Set<Member> discordMembers = new HashSet<>();
@Getter
private static final Set<Member> discordMembers = new HashSet<>();
public static void loadCache() {
loadChannelCache();
@@ -71,19 +76,4 @@ public class CacheManager {
});
}
public static HashMap<String, String> getServerChannels() {
return serverChannels;
}
public static HashMap<String, String> getServerRoles() {
return serverRoles;
}
public static HashMap<String, String> getUserCache() {
return userCache;
}
public static Set<Member> getDiscordMembers() {
return discordMembers;
}
}

View File

@@ -7,6 +7,7 @@ package com.hypherionmc.sdlink.core.managers;
import com.hypherionmc.sdlink.core.config.SDLinkConfig;
import com.hypherionmc.sdlink.core.discord.BotController;
import com.hypherionmc.sdlink.core.messaging.MessageDestination;
import lombok.Getter;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.channel.middleman.StandardGuildMessageChannel;
import org.apache.commons.lang3.tuple.Pair;
@@ -20,6 +21,7 @@ import java.util.HashMap;
public class ChannelManager {
private static final HashMap<MessageDestination, Pair<StandardGuildMessageChannel, Boolean>> channelMap = new HashMap<>();
@Getter
private static StandardGuildMessageChannel consoleChannel;
/**
@@ -42,10 +44,6 @@ public class ChannelManager {
channelMap.put(MessageDestination.CONSOLE, consoleChannel != null ? Pair.of(consoleChannel, true) : Pair.of(chatChannel, false));
}
public static StandardGuildMessageChannel getConsoleChannel() {
return consoleChannel;
}
public static StandardGuildMessageChannel getDestinationChannel(MessageDestination destination) {
return channelMap.get(destination).getLeft();
}

View File

@@ -8,6 +8,7 @@ import com.hypherionmc.sdlink.core.config.SDLinkConfig;
import com.hypherionmc.sdlink.core.discord.BotController;
import com.hypherionmc.sdlink.core.util.SDLinkUtils;
import com.hypherionmc.sdlink.core.util.SystemUtils;
import lombok.Getter;
import net.dv8tion.jda.api.entities.Role;
import java.util.HashSet;
@@ -21,9 +22,12 @@ import java.util.concurrent.atomic.AtomicInteger;
*/
public class RoleManager {
@Getter
private static final Set<Role> verificationRoles = new HashSet<>();
@Getter
private static final Set<Role> deniedRoles = new HashSet<>();
@Getter
private static Role verifiedRole = null;
/**
@@ -91,15 +95,4 @@ public class RoleManager {
return null;
}
public static Set<Role> getVerificationRoles() {
return verificationRoles;
}
public static Role getVerifiedRole() {
return verifiedRole;
}
public static Set<Role> getDeniedRoles() {
return deniedRoles;
}
}

View File

@@ -4,6 +4,8 @@
*/
package com.hypherionmc.sdlink.core.messaging;
import lombok.Getter;
/**
* @author HypherionSA
* Helper Class to return the result of interactions between Discord and Minecraft
@@ -11,6 +13,7 @@ package com.hypherionmc.sdlink.core.messaging;
public class Result {
private final Type type;
@Getter
private final String message;
private Result(Type type, String message) {
this.type = type;
@@ -29,10 +32,6 @@ public class Result {
return this.type == Type.ERROR;
}
public String getMessage() {
return message;
}
enum Type {
ERROR,
SUCCESS