diff --git a/build.gradle b/build.gradle index cbd05f1..ac08ec9 100644 --- a/build.gradle +++ b/build.gradle @@ -18,7 +18,6 @@ dependencies { implementation "dev.masecla:Modrinth4J:2.2.0" // https://github.com/masecla22/Modrinth4J implementation "ch.qos.logback:logback-classic:1.5.6" // https://mvnrepository.com/artifact/ch.qos.logback/logback-classic implementation "com.google.code.gson:gson:2.10.1" // https://github.com/google/gson/releases - implementation "com.jcraft:jsch:0.1.55" // https://mvnrepository.com/artifact/com.jcraft/jsch } tasks.withType(JavaCompile).configureEach { diff --git a/src/main/java/ru/pinkgoosik/kitsun/command/KitsunCommands.java b/src/main/java/ru/pinkgoosik/kitsun/command/KitsunCommands.java index 31c133d..5c81349 100644 --- a/src/main/java/ru/pinkgoosik/kitsun/command/KitsunCommands.java +++ b/src/main/java/ru/pinkgoosik/kitsun/command/KitsunCommands.java @@ -13,8 +13,6 @@ public class KitsunCommands { public static final List COMMANDS = new ArrayList<>(); public static void init() { - add(RegisterCommands.reg()); - add(RegisterCommands.unreg()); add(new ModUpdatesCommand()); add(new ImportFabricCommand()); add(new ImportQuiltCommand()); diff --git a/src/main/java/ru/pinkgoosik/kitsun/command/member/RegisterCommands.java b/src/main/java/ru/pinkgoosik/kitsun/command/member/RegisterCommands.java deleted file mode 100644 index d925d10..0000000 --- a/src/main/java/ru/pinkgoosik/kitsun/command/member/RegisterCommands.java +++ /dev/null @@ -1,105 +0,0 @@ -package ru.pinkgoosik.kitsun.command.member; - -import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; -import net.dv8tion.jda.api.interactions.commands.OptionType; -import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData; -import ru.pinkgoosik.kitsun.api.mojang.MojangAPI; -import ru.pinkgoosik.kitsun.command.CommandHelper; -import ru.pinkgoosik.kitsun.command.KitsunCommand; -import ru.pinkgoosik.kitsun.cosmetics.CosmeticsData; -import ru.pinkgoosik.kitsun.cosmetics.FtpConnection; -import ru.pinkgoosik.kitsun.util.Embeds; - -import java.util.Objects; - -public class RegisterCommands { - - public static KitsunCommand reg() { - return new KitsunCommand() { - @Override - public String getName() { - return "reg"; - } - - @Override - public String getDescription() { - return "Links your Discord account and Minecraft nickname."; - } - - @Override - public boolean isTLExclusive() { - return true; - } - - @Override - public void build(SlashCommandData data) { - data.addOption(OptionType.STRING, "nickname", "Your Minecraft nickname.", true); - } - - @Override - public void respond(SlashCommandInteractionEvent ctx, CommandHelper helper) { - String name = Objects.requireNonNull(ctx.getOption("nickname")).getAsString().replaceAll("[^a-zA-Z0-9_]", ""); - ctx.deferReply().queue(); - - var member = helper.member; - - if(CosmeticsData.getEntry(member.getId()).isPresent()) { - helper.followup(Embeds.error("You already registered!")); - return; - } - if(CosmeticsData.getEntryByName(name).isPresent()) { - helper.followup(Embeds.error("Player `" + name + "` is already registered!")); - return; - } - if(MojangAPI.getUuid(name).isPresent()) { - CosmeticsData.register(member.getId(), name, MojangAPI.getUuid(name).get()); - FtpConnection.updateData(); - helper.followup(Embeds.success("Player Registration", "You've been successfully registered. Use `/unreg` command if you'd like to unregister or change your nickname.")); - } - else { - helper.followup(Embeds.error("Player `" + name + "` is not found. Write down your Minecraft username.")); - } - } - }; - } - - public static KitsunCommand unreg() { - return new KitsunCommand() { - @Override - public String getName() { - return "unreg"; - } - - @Override - public String getDescription() { - return "Unlinks your Discord account and Minecraft nickname."; - } - - @Override - public boolean isTLExclusive() { - return true; - } - - @Override - public void build(SlashCommandData data) { - } - - @Override - public void respond(SlashCommandInteractionEvent ctx, CommandHelper helper) { - ctx.deferReply().queue(); - - var member = helper.member; - var entry = CosmeticsData.getEntry(member.getId()); - if(entry.isPresent()) { - CosmeticsData.unregister(member.getId()); - FtpConnection.updateData(); - String text = "Player " + entry.get().user.name + " is successfully unregistered. \nHope to see you soon later!"; - helper.followup(Embeds.success("Player Unregistering", text)); - } - else { - helper.followup(Embeds.error("You have not registered yet!")); - } - } - }; - } -} diff --git a/src/main/java/ru/pinkgoosik/kitsun/config/Secrets.java b/src/main/java/ru/pinkgoosik/kitsun/config/Secrets.java index 49974c7..c91d3ae 100644 --- a/src/main/java/ru/pinkgoosik/kitsun/config/Secrets.java +++ b/src/main/java/ru/pinkgoosik/kitsun/config/Secrets.java @@ -5,13 +5,13 @@ public class Secrets { public String modrinthApiKey = ""; public String discordToken = ""; public String note = ""; - public String host = ""; - public String user = ""; - public String password = ""; - public String dir = ""; public String activity = ""; - public int httpPort = 0; - public String httpToken = ""; + public HttpConfig http = new HttpConfig(); public static final Secrets DEFAULT = new Secrets(); + + public static class HttpConfig { + public int port = 0; + public String token = ""; + } } diff --git a/src/main/java/ru/pinkgoosik/kitsun/cosmetics/Capes.java b/src/main/java/ru/pinkgoosik/kitsun/cosmetics/Capes.java deleted file mode 100644 index fe7b15a..0000000 --- a/src/main/java/ru/pinkgoosik/kitsun/cosmetics/Capes.java +++ /dev/null @@ -1,12 +0,0 @@ -package ru.pinkgoosik.kitsun.cosmetics; - -import java.util.ArrayList; -import java.util.List; - -public class Capes { - public static final List COLORED_CLOAKS = new ArrayList<>(List.of("azure", "crimson", "flamingo", "golden", "lapis", "military", "mint", "mystic", "pumpkin", "smoky", "turtle", "violet", "void", "coffee")); - - public static boolean exist(String cloak) { - return Capes.COLORED_CLOAKS.contains(cloak); - } -} diff --git a/src/main/java/ru/pinkgoosik/kitsun/cosmetics/CosmeticsData.java b/src/main/java/ru/pinkgoosik/kitsun/cosmetics/CosmeticsData.java deleted file mode 100644 index 9dbd813..0000000 --- a/src/main/java/ru/pinkgoosik/kitsun/cosmetics/CosmeticsData.java +++ /dev/null @@ -1,89 +0,0 @@ -package ru.pinkgoosik.kitsun.cosmetics; - -import ru.pinkgoosik.kitsun.util.UrlParser; - -import java.util.*; - -public class CosmeticsData { - private static final String URL_STRING = "https://pinkgoosik-assets.akamaized.net/cosmetics/data.json?_=%random%"; - public static final List ENTRIES = new ArrayList<>(); - - public static void register(String discord, String name, String uuid) { - EntryData entry = new EntryData(); - EntryData.UserData user = new EntryData.UserData(); - user.discord = discord; - user.name = name; - user.uuid = uuid; - entry.user = user; - - EntryData.CapeData cape = new EntryData.CapeData(); - cape.name = ""; - cape.glint = false; - entry.cape = cape; - - ENTRIES.add(entry); - } - - public static void unregister(String discord) { - ENTRIES.removeIf(entry -> entry.user.discord.equals(discord)); - } - - public static Optional getEntry(String discord) { - for(var entry : CosmeticsData.ENTRIES) { - if(entry.user.discord.equals(discord)) return Optional.of(entry); - } - return Optional.empty(); - } - - public static Optional getEntryByName(String name) { - for(var entry : CosmeticsData.ENTRIES) { - if(entry.user.name.equals(name)) return Optional.of(entry); - } - return Optional.empty(); - } - - public static void setCape(String username, String capeName) { - for(var entry : ENTRIES) { - if(entry.user.name.equals(username)) { - entry.cape.name = capeName; - } - } - } - - public static void clearCape(String username) { - for(var entry : ENTRIES) { - if(entry.user.name.equals(username)) { - EntryData.CapeData cape = new EntryData.CapeData(); - cape.name = ""; - cape.glint = false; - entry.cape = cape; - } - } - } - - public static boolean hasCape(String nickname) { - for(var entry : ENTRIES) { - if(entry.user.name.equals(nickname)) { - return !entry.cape.name.isBlank(); - } - } - return false; - } - - public static void fetch() { - try { - String urlStr = URL_STRING; - urlStr = urlStr.replaceAll("%random%", random()); - EntryData[] entries = UrlParser.get(urlStr, EntryData[].class); - ENTRIES.addAll(Arrays.asList(entries)); - } - catch(Exception e) { - e.printStackTrace(); - } - } - - static String random() { - UUID uuid = UUID.randomUUID(); - return uuid.toString().replaceAll("_", ""); - } -} diff --git a/src/main/java/ru/pinkgoosik/kitsun/cosmetics/EntryData.java b/src/main/java/ru/pinkgoosik/kitsun/cosmetics/EntryData.java deleted file mode 100644 index d0ba76b..0000000 --- a/src/main/java/ru/pinkgoosik/kitsun/cosmetics/EntryData.java +++ /dev/null @@ -1,19 +0,0 @@ -package ru.pinkgoosik.kitsun.cosmetics; - -import java.util.ArrayList; -import java.util.List; - -public class EntryData { - public UserData user; - public CapeData cape; - public List cosmetics = new ArrayList<>(); - - public static class UserData { - public String name, uuid, discord; - } - - public static class CapeData { - public String name; - public boolean glint; - } -} diff --git a/src/main/java/ru/pinkgoosik/kitsun/cosmetics/FtpConnection.java b/src/main/java/ru/pinkgoosik/kitsun/cosmetics/FtpConnection.java deleted file mode 100644 index 8c46b63..0000000 --- a/src/main/java/ru/pinkgoosik/kitsun/cosmetics/FtpConnection.java +++ /dev/null @@ -1,64 +0,0 @@ -package ru.pinkgoosik.kitsun.cosmetics; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.jcraft.jsch.*; -import ru.pinkgoosik.kitsun.Bot; -import ru.pinkgoosik.kitsun.feature.KitsunDebugger; -import ru.pinkgoosik.kitsun.util.FileUtils; - -import java.io.*; -import java.util.Properties; - -public class FtpConnection { - private static final Gson GSON = new GsonBuilder().setLenient().setPrettyPrinting().create(); - private static Channel channel; - - public static void updateData() { - FtpConnection.connect(); - FtpConnection.createJson(); - try { - ChannelSftp channelSftp = (ChannelSftp) channel; - channelSftp.cd(Bot.secrets.get().dir); - File file = new File(System.getProperty("user.dir") + "/cosmetics/data.json"); - channelSftp.put(new FileInputStream(file), "data.json"); - Bot.LOGGER.info("Remote cosmetics data successfully updated."); - } - catch(Exception e) { - KitsunDebugger.ping("Failed to update remote cosmetics data due to an exception:\n" + e); - e.printStackTrace(); - } - } - - private static void connect() { - try { - JSch jSch = new JSch(); - Session session = jSch.getSession(Bot.secrets.get().user, Bot.secrets.get().host, 22); - session.setPassword(Bot.secrets.get().password); - Properties props = new Properties(); - props.put("StrictHostKeyChecking", "no"); - session.setConfig(props); - session.connect(); - channel = session.openChannel("sftp"); - channel.connect(); - Bot.LOGGER.info("Sftp channel opened and connected."); - } - catch(Exception e) { - KitsunDebugger.ping("Failed connection to sftp due to an exception:\n" + e); - e.printStackTrace(); - } - } - - private static void createJson() { - try { - FileUtils.createDir("cosmetics"); - try(FileWriter writer = new FileWriter("cosmetics/data.json")) { - writer.write(GSON.toJson(CosmeticsData.ENTRIES)); - } - } - catch(Exception e) { - KitsunDebugger.ping("Failed to create cosmetics data json due to an exception:\n" + e); - } - } - -} diff --git a/src/main/java/ru/pinkgoosik/kitsun/http/KitsunHttpServer.java b/src/main/java/ru/pinkgoosik/kitsun/http/KitsunHttpServer.java index 3f9c8f4..ba8006d 100644 --- a/src/main/java/ru/pinkgoosik/kitsun/http/KitsunHttpServer.java +++ b/src/main/java/ru/pinkgoosik/kitsun/http/KitsunHttpServer.java @@ -4,6 +4,7 @@ import com.sun.net.httpserver.HttpServer; import ru.pinkgoosik.kitsun.Bot; import ru.pinkgoosik.kitsun.cache.ServerData; +import ru.pinkgoosik.kitsun.util.ServerUtils; import java.io.OutputStream; import java.net.InetSocketAddress; @@ -16,15 +17,34 @@ public class KitsunHttpServer { public static HttpServer server; public static void init() { - if(Bot.secrets.get().httpPort > 0) { + if(Bot.secrets.get().http.port > 0) { try { - HttpServer server = HttpServer.create(new InetSocketAddress(Bot.secrets.get().httpPort), 0); + HttpServer server = HttpServer.create(new InetSocketAddress(Bot.secrets.get().http.port), 0); server.createContext("/mod_update", exchange -> { var map = parseParams(exchange); - if (map.containsKey("token") && Bot.secrets.get().httpToken.equals(map.get("token"))) { - success(exchange, "Success"); - if(map.containsKey("server") && map.containsKey("project")) modUpdateWebhook(map.get("server"), map.get("project")); + if (map.containsKey("token") && Bot.secrets.get().http.token.equals(map.get("token"))) { + if(map.containsKey("server") && map.containsKey("project")) { + if (!ServerUtils.exist(map.get("server"))) { + success(exchange, "Server not found"); + return; + } + var data = ServerData.get(map.get("server")); + + for(var publisher : data.modUpdates.get()) { + if(publisher.project.equals(map.get("project"))) { + success(exchange, "Success"); + try { + Thread.sleep(5 * 1000); + publisher.check(0); + } + catch (Exception ignored) {} + return; + } + } + success(exchange, "Project not found"); + return; + } } notFound(exchange); @@ -40,21 +60,6 @@ public static void init() { } } - static void modUpdateWebhook(String server, String project) { - var data = ServerData.get(server); - - for(var publisher : data.modUpdates.get()) { - if(publisher.project.equals(project)) { - try { - Thread.sleep(5 * 1000); - publisher.check(0); - } - catch (Exception ignored) {} - return; - } - } - } - static Map parseParams(HttpExchange exchange) { String[] split = exchange.getRequestURI().toString().split("\\?"); if (split.length >= 2) { diff --git a/src/main/java/ru/pinkgoosik/kitsun/util/ServerUtils.java b/src/main/java/ru/pinkgoosik/kitsun/util/ServerUtils.java index 9dbf3fd..955cdc4 100644 --- a/src/main/java/ru/pinkgoosik/kitsun/util/ServerUtils.java +++ b/src/main/java/ru/pinkgoosik/kitsun/util/ServerUtils.java @@ -20,6 +20,13 @@ public static void runFor(String serverId, ServerRunnable runnable) { }); } + public static boolean exist(String serverId) { + for(var guild : Bot.jda.getGuilds()) { + if(guild.getId().equals(serverId)) return true; + } + return false; + } + @FunctionalInterface public interface ServerRunnable { void run(ServerData serverData);