From 54cc20813638bceafa12da74f32f119731c433e3 Mon Sep 17 00:00:00 2001 From: ProjectDF <61924259+ProjectDF@users.noreply.github.com> Date: Thu, 11 Feb 2021 15:37:03 -0600 Subject: [PATCH 01/17] big jere crazy --- .../command/impl/other/JerePicsCommand.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/main/java/com/diamondfire/helpbot/bot/command/impl/other/JerePicsCommand.java diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/JerePicsCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/JerePicsCommand.java new file mode 100644 index 00000000..795267cb --- /dev/null +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/JerePicsCommand.java @@ -0,0 +1,61 @@ +package com.diamondfire.helpbot.bot.command.impl.other; + +import com.diamondfire.helpbot.bot.command.argument.ArgumentSet; +import com.diamondfire.helpbot.bot.command.help.CommandCategory; +import com.diamondfire.helpbot.bot.command.help.HelpContext; +import com.diamondfire.helpbot.bot.command.impl.Command; +import com.diamondfire.helpbot.bot.command.permissions.Permission; +import com.diamondfire.helpbot.bot.events.CommandEvent; +import net.dv8tion.jda.api.EmbedBuilder; + +import java.awt.*; + +public class JerePicsCommand extends Command { + + @Override + public String getName() { + return "jerepic"; + } + + @Override + public HelpContext getHelpContext() { + return new HelpContext() + .description("Very hot jeremaster pictures.") + .category(CommandCategory.OTHER); + + } + + @Override + protected ArgumentSet compileArguments() { + return new ArgumentSet(); + } + + @Override + public Permission getPermission() { + return Permission.USER; + } + + @Override + public void run(CommandEvent event) { + EmbedBuilder embed = new EmbedBuilder(); + embed.setColor(Color.RED); + embed.setTitle("Jere Pic"); + int rngIndex = (int) (Math.random() * links.length); + embed.setImage(links[rngIndex]); + event.getChannel().sendMessage(embed.build()).queue(); + + } + private static final String[] links = { + "https://imgur.com/cZZHMWY.png", + "https://imgur.com/BGcPKv8.png", + "https://imgur.com/seRPPR2.png", + "https://imgur.com/e0aHeLp.png", + "https://imgur.com/m6MjDbk.png", + "https://imgur.com/3yaNUzm.png", + "https://cdn.discordapp.com/attachments/180793115223916544/539188769480507426/unknown.png", + "https://cdn.discordapp.com/attachments/789266995672907818/809169563543273512/jeremaster.png", + "https://external-content.duckduckgo.com/iu/?reload=1613077331588&u=https://mc-heads.net/body/jeremaster/180.png" + }; + + +} From f29843e57a94b017874f6b6dbb7aa745d115663a Mon Sep 17 00:00:00 2001 From: ProjectDF <61924259+ProjectDF@users.noreply.github.com> Date: Thu, 11 Feb 2021 15:38:46 -0600 Subject: [PATCH 02/17] Add files via upload crazy --- .../helpbot/bot/HelpBotInstance.java | 284 +++++++++--------- 1 file changed, 142 insertions(+), 142 deletions(-) diff --git a/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java b/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java index f55c5e83..87edbf79 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java +++ b/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java @@ -1,142 +1,142 @@ -package com.diamondfire.helpbot.bot; - -import com.diamondfire.helpbot.HelpBot; -import com.diamondfire.helpbot.bot.command.CommandHandler; -import com.diamondfire.helpbot.bot.command.impl.codeblock.*; -import com.diamondfire.helpbot.bot.command.impl.other.*; -import com.diamondfire.helpbot.bot.command.impl.stats.*; -import com.diamondfire.helpbot.bot.command.impl.stats.graph.*; -import com.diamondfire.helpbot.bot.command.impl.stats.individualized.*; -import com.diamondfire.helpbot.bot.command.impl.stats.plot.*; -import com.diamondfire.helpbot.bot.command.impl.stats.support.*; -import com.diamondfire.helpbot.bot.config.Config; -import com.diamondfire.helpbot.bot.events.*; -import com.diamondfire.helpbot.sys.tasks.TaskRegistry; -import net.dv8tion.jda.api.*; -import net.dv8tion.jda.api.entities.Activity; -import net.dv8tion.jda.api.requests.GatewayIntent; -import net.dv8tion.jda.api.utils.MemberCachePolicy; -import net.dv8tion.jda.api.utils.cache.CacheFlag; - -import javax.security.auth.login.LoginException; - -public class HelpBotInstance { - - private static final Config config = new Config(); - public static final long DF_GUILD = config.getGuild(); - public static final long LOG_CHANNEL = config.getLogChannel(); - private static final CommandHandler handler = new CommandHandler(); - - private static JDA jda; - private static final TaskRegistry loop = new TaskRegistry(); - - public static void initialize() throws LoginException { - - handler.register( - // codeblock commands - new CodeCommand(), - new RankCommand(), - new BlockCommand(), - new SearchCommand(), - new TagsCommand(), - // others - new MimicCommand(), - new FetchDataCommand(), - new InfoCommand(), - new EvalCommand(), - new GarfieldCommand(), - new HelpCommand(), - new RestartCommand(), - new ActionDumpCommand(), - new RawCommand(), - new SamQuotesCommand(), - new PolicyCommand(), - new SkinCommand(), - new DisableCommand(), - new EnableCommand(), - new DisableCommand(), - new ImageDumpCommand(), - new SoundListCommand(), - new QueryCommand(), - new RulesCommand(), - new BulkExecuteCommand(), - new PermUnlocksCommand(), - new MuteCommand(), - new MutedCommand(), - new UnmuteCommand(), - new VerifyCommand(), - // statsbot - new StatsCommand(), - new SupportBadCommand(), - new JoinBadCommand(), - new PlotCommand(), - new ProfileCommand(), - new ActivePlotsCommand(), - new TrendingPlotsCommand(), - new PlotsCommand(), - new CpTopCommand(), - new RetiredListCommand(), - new StaffListCommand(), - new SessionTopCommand(), - new LastJoinedCommand(), - new SupporteeStatsCommand(), - new NewPlayersCommand(), - new StatsGraphCommand(), - new NewJoinGraphCommand(), - new PlayersCommand(), - new BoostersCommand(), - new DiscordBoostersCommand(), - new TimeTopCommand(), - new QueueCommand(), - new WhoHelpedCommand(), - new HelpedByCommand(), - new NamesCommand(), - new PlayerJoinGraphCommand(), - new CpCommand(), - new CpRequirementsCommand(), - new VoteGivenLeaderboard(), - new PlotVoteGraphCommand(), - new JoinDataCommand(), - new TotalStatsCommand(), - new TokenTopCommand(), - new HistoryCommand(), - new UuidCommand(), - new PlotLocCommand(), - new FindSupporteeNamesCommand(), - new SessionsCommand(), - new SupporteeSessionsCommand(), - new ExcuseStaffCommand(), - new ExcusedStaffCommand(), - new SupportBannedPlayersCommand(), - new DiscussionMuteCommand() - ); - - JDABuilder builder = JDABuilder.createDefault(config.getToken()) - .enableIntents(GatewayIntent.GUILD_MEMBERS) - .setStatus(OnlineStatus.ONLINE) - .setMemberCachePolicy(MemberCachePolicy.NONE) - .setActivity(Activity.watching("for " + getConfig().getPrefix() + "help")) - .setGatewayEncoding(GatewayEncoding.ETF) - .disableCache(CacheFlag.ACTIVITY, CacheFlag.VOICE_STATE, CacheFlag.CLIENT_STATUS) - .addEventListeners(new MessageEvent(), new ReactionEvent(), new ReadyEvent(), new GuildJoinEvent()); - - jda = builder.build(); - handler.initialize(); - } - - public static JDA getJda() { - return jda; - } - - public static CommandHandler getHandler() { - return handler; - } - - public static Config getConfig() { - return config; - } - - public static TaskRegistry getScheduler() { - return loop; - } -} +package com.diamondfire.helpbot.bot; + +import com.diamondfire.helpbot.bot.command.CommandHandler; +import com.diamondfire.helpbot.bot.command.impl.codeblock.*; +import com.diamondfire.helpbot.bot.command.impl.other.*; +import com.diamondfire.helpbot.bot.command.impl.stats.*; +import com.diamondfire.helpbot.bot.command.impl.stats.graph.*; +import com.diamondfire.helpbot.bot.command.impl.stats.individualized.*; +import com.diamondfire.helpbot.bot.command.impl.stats.plot.*; +import com.diamondfire.helpbot.bot.command.impl.stats.support.*; +import com.diamondfire.helpbot.bot.config.Config; +import com.diamondfire.helpbot.bot.events.*; +import com.diamondfire.helpbot.sys.tasks.TaskRegistry; +import net.dv8tion.jda.api.*; +import net.dv8tion.jda.api.entities.Activity; +import net.dv8tion.jda.api.requests.GatewayIntent; +import net.dv8tion.jda.api.utils.MemberCachePolicy; +import net.dv8tion.jda.api.utils.cache.CacheFlag; + +import javax.security.auth.login.LoginException; + +public class HelpBotInstance { + + private static final Config config = new Config(); + public static final long DF_GUILD = config.getGuild(); + public static final long LOG_CHANNEL = config.getLogChannel(); + private static final CommandHandler handler = new CommandHandler(); + + private static JDA jda; + private static final TaskRegistry loop = new TaskRegistry(); + + public static void initialize() throws LoginException { + + handler.register( + // codeblock commands + new CodeCommand(), + new RankCommand(), + new BlockCommand(), + new SearchCommand(), + new TagsCommand(), + // others + new MimicCommand(), + new FetchDataCommand(), + new InfoCommand(), + new EvalCommand(), + new GarfieldCommand(), + new HelpCommand(), + new RestartCommand(), + new ActionDumpCommand(), + new RawCommand(), + new SamQuotesCommand(), + new PolicyCommand(), + new SkinCommand(), + new DisableCommand(), + new EnableCommand(), + new DisableCommand(), + new ImageDumpCommand(), + new SoundListCommand(), + new QueryCommand(), + new RulesCommand(), + new BulkExecuteCommand(), + new PermUnlocksCommand(), + new MuteCommand(), + new MutedCommand(), + new UnmuteCommand(), + new VerifyCommand(), + // statsbot + new StatsCommand(), + new SupportBadCommand(), + new JoinBadCommand(), + new PlotCommand(), + new ProfileCommand(), + new ActivePlotsCommand(), + new TrendingPlotsCommand(), + new PlotsCommand(), + new CpTopCommand(), + new RetiredListCommand(), + new StaffListCommand(), + new SessionTopCommand(), + new LastJoinedCommand(), + new SupporteeStatsCommand(), + new NewPlayersCommand(), + new StatsGraphCommand(), + new NewJoinGraphCommand(), + new PlayersCommand(), + new BoostersCommand(), + new DiscordBoostersCommand(), + new TimeTopCommand(), + new QueueCommand(), + new WhoHelpedCommand(), + new HelpedByCommand(), + new NamesCommand(), + new PlayerJoinGraphCommand(), + new CpCommand(), + new CpRequirementsCommand(), + new VoteGivenLeaderboard(), + new PlotVoteGraphCommand(), + new JoinDataCommand(), + new TotalStatsCommand(), + new TokenTopCommand(), + new HistoryCommand(), + new UuidCommand(), + new PlotLocCommand(), + new FindSupporteeNamesCommand(), + new SessionsCommand(), + new SupporteeSessionsCommand(), + new ExcuseStaffCommand(), + new ExcusedStaffCommand(), + new SupportBannedPlayersCommand(), + new DiscussionMuteCommand(), + new JerePicsCommand() + ); + + JDABuilder builder = JDABuilder.createDefault(config.getToken()) + .enableIntents(GatewayIntent.GUILD_MEMBERS) + .setStatus(OnlineStatus.ONLINE) + .setMemberCachePolicy(MemberCachePolicy.NONE) + .setActivity(Activity.watching("for " + getConfig().getPrefix() + "help")) + .setGatewayEncoding(GatewayEncoding.ETF) + .disableCache(CacheFlag.ACTIVITY, CacheFlag.VOICE_STATE, CacheFlag.CLIENT_STATUS) + .addEventListeners(new MessageEvent(), new ReactionEvent(), new ReadyEvent(), new GuildJoinEvent()); + + jda = builder.build(); + handler.initialize(); + } + + public static JDA getJda() { + return jda; + } + + public static CommandHandler getHandler() { + return handler; + } + + public static Config getConfig() { + return config; + } + + public static TaskRegistry getScheduler() { + return loop; + } +} From 6f5073e211ef42e21ac74207c3e0e9035f77a32c Mon Sep 17 00:00:00 2001 From: ProjectDF <61924259+ProjectDF@users.noreply.github.com> Date: Fri, 12 Feb 2021 10:44:23 -0600 Subject: [PATCH 03/17] Update JerePicsCommand.java --- .../helpbot/bot/command/impl/other/JerePicsCommand.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/JerePicsCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/JerePicsCommand.java index 795267cb..c2fb60af 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/JerePicsCommand.java +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/JerePicsCommand.java @@ -54,7 +54,9 @@ public void run(CommandEvent event) { "https://imgur.com/3yaNUzm.png", "https://cdn.discordapp.com/attachments/180793115223916544/539188769480507426/unknown.png", "https://cdn.discordapp.com/attachments/789266995672907818/809169563543273512/jeremaster.png", - "https://external-content.duckduckgo.com/iu/?reload=1613077331588&u=https://mc-heads.net/body/jeremaster/180.png" + "https://external-content.duckduckgo.com/iu/?reload=1613077331588&u=https://mc-heads.net/body/jeremaster/180.png", + "https://imgur.com/LFldK2p.png", + "https://imgur.com/aEq7voY.png" }; From fa687d78da28e08e90764b21ba62776f9c2e36a2 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sat, 13 Feb 2021 14:48:08 -0600 Subject: [PATCH 04/17] jere moment --- .../helpbot/bot/command/impl/other/JerePicsCommand.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/JerePicsCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/JerePicsCommand.java index c2fb60af..838cf8e9 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/JerePicsCommand.java +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/JerePicsCommand.java @@ -38,7 +38,7 @@ public Permission getPermission() { @Override public void run(CommandEvent event) { EmbedBuilder embed = new EmbedBuilder(); - embed.setColor(Color.RED); + embed.setColor(new Color(184,45,40)); embed.setTitle("Jere Pic"); int rngIndex = (int) (Math.random() * links.length); embed.setImage(links[rngIndex]); @@ -56,7 +56,8 @@ public void run(CommandEvent event) { "https://cdn.discordapp.com/attachments/789266995672907818/809169563543273512/jeremaster.png", "https://external-content.duckduckgo.com/iu/?reload=1613077331588&u=https://mc-heads.net/body/jeremaster/180.png", "https://imgur.com/LFldK2p.png", - "https://imgur.com/aEq7voY.png" + "https://imgur.com/aEq7voY.png", + "https://render.namemc.com/skin/3d/body.png?skin=8c8cd7a1a2c52ca4&model=classic&theta=180&phi=0&time=0&width=600&height=800" }; From a307a1b0489da69ba549e2abe84f861fc8af05d1 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sat, 13 Feb 2021 14:59:50 -0600 Subject: [PATCH 05/17] nbs stuff --- .../helpbot/bot/HelpBotInstance.java | 3 +- .../bot/command/impl/other/NbsCommand.java | 122 ++++++++ .../helpbot/util/CompressionUtil.java | 46 +++ .../diamondfire/helpbot/util/NBSDecoder.java | 291 ++++++++++++++++++ .../helpbot/util/NBSToTemplate.java | 157 ++++++++++ .../helpbot/util/OutdatedNBSException.java | 4 + .../diamondfire/helpbot/util/SongData.java | 88 ++++++ 7 files changed, 710 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/diamondfire/helpbot/bot/command/impl/other/NbsCommand.java create mode 100644 src/main/java/com/diamondfire/helpbot/util/CompressionUtil.java create mode 100644 src/main/java/com/diamondfire/helpbot/util/NBSDecoder.java create mode 100644 src/main/java/com/diamondfire/helpbot/util/NBSToTemplate.java create mode 100644 src/main/java/com/diamondfire/helpbot/util/OutdatedNBSException.java create mode 100644 src/main/java/com/diamondfire/helpbot/util/SongData.java diff --git a/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java b/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java index 87edbf79..d7f4c748 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java +++ b/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java @@ -108,7 +108,8 @@ public static void initialize() throws LoginException { new ExcusedStaffCommand(), new SupportBannedPlayersCommand(), new DiscussionMuteCommand(), - new JerePicsCommand() + new JerePicsCommand(), + new NbsCommand() ); JDABuilder builder = JDABuilder.createDefault(config.getToken()) diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/NbsCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/NbsCommand.java new file mode 100644 index 00000000..b46ccd71 --- /dev/null +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/NbsCommand.java @@ -0,0 +1,122 @@ +package com.diamondfire.helpbot.bot.command.impl.other; + +import com.diamondfire.helpbot.bot.command.argument.ArgumentSet; +import com.diamondfire.helpbot.bot.command.help.CommandCategory; +import com.diamondfire.helpbot.bot.command.help.HelpContext; +import com.diamondfire.helpbot.bot.command.impl.Command; +import com.diamondfire.helpbot.bot.command.permissions.Permission; +import com.diamondfire.helpbot.bot.events.CommandEvent; +import com.diamondfire.helpbot.util.*; +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.entities.TextChannel; + +import java.awt.*; +import java.io.*; +import java.net.*; +import java.nio.charset.StandardCharsets; + +import java.util.Scanner; +import java.util.concurrent.CompletableFuture; + +public class NbsCommand extends Command { + private static String generateLink(String templateData) throws IOException { + URL url = new URL("https://twv.vercel.app/v2/create"); + URLConnection con = url.openConnection(); + HttpURLConnection http = (HttpURLConnection) con; + http.setRequestMethod("POST"); + http.setDoOutput(true); + byte[] out = templateData.getBytes(StandardCharsets.UTF_8); + int length = out.length; + + http.setFixedLengthStreamingMode(length); + http.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); + http.setRequestProperty("Accept","application/json; charset=UTF-8"); + http.connect(); + try(OutputStream os = http.getOutputStream()) { + os.write(out); + } + Scanner s = new Scanner(http.getInputStream()).useDelimiter("\\A"); + String result = s.hasNext() ? s.next() : ""; + s.close(); + http.disconnect(); + + return result; + } + @Override + public String getName() { + return "nbs"; + } + + @Override + public HelpContext getHelpContext() { + return new HelpContext() + .description("Generates Codeutils song function.") + .category(CommandCategory.OTHER); + + } + + @Override + protected ArgumentSet compileArguments() { return new ArgumentSet(); } + + @Override + public Permission getPermission() { return Permission.USER; } + + @Override + public void run(CommandEvent event) { + TextChannel channel = event.getChannel(); + if(event.getMessage().getAttachments().isEmpty()) { + EmbedBuilder warning = new EmbedBuilder() + .addField("Warning!","You need to attach an nbs file!",false) + .setColor(Color.RED); + event.getChannel().sendMessage(warning.build()).queue(); + return; + } + + EmbedBuilder errorEmbed = new EmbedBuilder() + .setColor(Color.RED) + .setTitle("An error occurred!"); + try { + File file = new File("input.nbs"); + CompletableFuture.runAsync(() -> { + CompletableFuture task = event.getMessage().getAttachments().get(0).downloadToFile(file); + while(!task.isDone()) { } + try { + SongData d = NBSDecoder.parse(file); + String code = new NBSToTemplate(d).convert(); + byte[] b64 = CompressionUtil.toBase64(CompressionUtil.toGZIP(code.getBytes(StandardCharsets.UTF_8))); + String exported = new String(b64); + String jsonInputString = String.format("{\"template\": \"%s\",\"temp\": true}",exported); + String result = generateLink(jsonInputString); + JsonObject json = new Gson().fromJson(result,JsonObject.class); + + EmbedBuilder embed = new EmbedBuilder() + .setColor(new Color(70,199,82)) + .setTitle("Function Generated!") + .setThumbnail("https://static.wikia.nocookie.net/minecraft/images/9/9b/Note_Block.png/revision/latest?cb=20190921170620") + .addField("Link(Expires in 2 mins):","https://derpystuff.gitlab.io/code/l?link=" + json.get("link").getAsString(),false) + .addField("Music Player:","https://derpystuff.gitlab.io/code/l?link=7cf5d91c35bbde31c28567d8d8945c40",false); + + + + channel.sendMessage(embed.build()).queue(); + System.out.println(exported); + } catch(OutdatedNBSException e) { + e.printStackTrace(); + channel.sendMessage(errorEmbed.build()).queue(); + } catch(IOException e) { + e.printStackTrace(); + channel.sendMessage(errorEmbed.build()).queue(); + } + + }); + } catch(Exception e) { + e.printStackTrace(); + channel.sendMessage(errorEmbed.build()).queue(); + } + + + } +} diff --git a/src/main/java/com/diamondfire/helpbot/util/CompressionUtil.java b/src/main/java/com/diamondfire/helpbot/util/CompressionUtil.java new file mode 100644 index 00000000..4e3de232 --- /dev/null +++ b/src/main/java/com/diamondfire/helpbot/util/CompressionUtil.java @@ -0,0 +1,46 @@ +package com.diamondfire.helpbot.util; + +import java.io.*; +import java.nio.charset.StandardCharsets; +import java.util.Base64; +import java.util.zip.*; + +public class CompressionUtil { + + public static byte[] fromBase64(byte[] bytes) { + return Base64.getDecoder().decode(bytes); + } + + public static byte[] toBase64(byte[] bytes) { + return Base64.getEncoder().encode(bytes); + } + + public static byte[] fromGZIP(byte[] bytes) throws IOException { + if (bytes == null) { + return null; + } + + GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(bytes)); + BufferedReader bf = new BufferedReader(new InputStreamReader(gis, StandardCharsets.UTF_8)); + StringBuilder outStr = new StringBuilder(); + String line; + while ((line = bf.readLine()) != null) { + outStr.append(line); + } + + return outStr.toString().getBytes(); + } + + public static byte[] toGZIP(byte[] bytes) throws IOException { + if (bytes == null) { + return null; + } + ByteArrayOutputStream obj = new ByteArrayOutputStream(); + GZIPOutputStream gzip = new GZIPOutputStream(obj); + gzip.write(bytes); + gzip.close(); + + return obj.toByteArray(); + } + +} diff --git a/src/main/java/com/diamondfire/helpbot/util/NBSDecoder.java b/src/main/java/com/diamondfire/helpbot/util/NBSDecoder.java new file mode 100644 index 00000000..0e2cbcc0 --- /dev/null +++ b/src/main/java/com/diamondfire/helpbot/util/NBSDecoder.java @@ -0,0 +1,291 @@ +package com.diamondfire.helpbot.util; +import java.io.*; +import java.math.BigDecimal; +//stolen from Codeutils + +public class NBSDecoder { + + public static SongData parse(File songFile) throws IOException, OutdatedNBSException { + try { + return parse(new FileInputStream(songFile), songFile); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + return null; + } + + private static SongData parse(InputStream inputStream, File songFile) throws IOException, OutdatedNBSException { + String title = ""; + String author = ""; + String file = songFile.getName(); + float speed = 0f; + float actualSpeed = 0f; + short timeSignature = 4; + int loopTick = 0; + int loopCount = 0; + int vanillaInstruments = 9; + + StringBuilder stringBuilder = new StringBuilder(); + StringBuilder layerStringBuilder = new StringBuilder(); + + DataInputStream dataInputStream = new DataInputStream(inputStream); + short length = readShort(dataInputStream); + int nbsversion = 0; + if (length == 0) { + nbsversion = dataInputStream.readByte(); + vanillaInstruments = dataInputStream.readByte(); + if (nbsversion >= 3) { + length = readShort(dataInputStream); + } else if (nbsversion == 1 || nbsversion == 2) { + throw new OutdatedNBSException(); + } + } else { + // poop ChatUtil.sendMessage("§a§lHEY!§r Looks like you are using original Note Block Studio. I recommend you to use §bOpen Note Block Studio§r, which is an unofficial continuation of Note Block Studio!"); + } + short layers = readShort(dataInputStream); //song height + title = readString(dataInputStream); //title + author = readString(dataInputStream); //author + readString(dataInputStream); //original author + String description = readString(dataInputStream); //description + actualSpeed = readShort(dataInputStream); //speed + speed = actualSpeed / 100f; //speed + dataInputStream.readBoolean(); //autosave + dataInputStream.readByte(); //autosave duration + timeSignature = (short) dataInputStream.readByte(); //time signature + readInt(dataInputStream); //minutes spent + readInt(dataInputStream); //left clicks + readInt(dataInputStream); //right clicks + readInt(dataInputStream); //blocks added + readInt(dataInputStream); //nlocks removed + readString(dataInputStream); // mid or schematic filename + if (nbsversion >= 4) { + dataInputStream.readByte(); //loop on/off + loopCount = dataInputStream.readByte(); //loop count + loopTick = readShort(dataInputStream); //loop start tick + } + + short tick = -1; + String[][] addStringList = new String[layers][length + 1]; + int[][] instrumentList = new int[layers][length + 1]; + int[][] pitchList = new int[layers][length + 1]; + int[][] finepitchList = new int[layers][length + 1]; + int[][] velocityList = new int[layers][length + 1]; + int[][] panningList = new int[layers][length + 1]; + boolean[] columnExistence = new boolean[length + 1]; + boolean[][] noteExistence = new boolean[layers][length + 1]; + boolean firstNoted = false; + + while (true) { //Read notes + short t = readShort(dataInputStream); + if (t == 0) { + break; + } + tick += t; + + columnExistence[tick] = true; + + short layer = -1; + while (true) { + short jumpLayers = readShort(dataInputStream); + if (jumpLayers == 0) { + break; + } + layer += jumpLayers; + byte instrument = dataInputStream.readByte(); + byte note = dataInputStream.readByte(); + byte velocity = 100; + int panning = 100; + short finepitch = 0; + if (nbsversion >= 4) { + velocity = dataInputStream.readByte(); + panning = Byte.toUnsignedInt(dataInputStream.readByte()); + finepitch = readShort(dataInputStream); + } + + //System.out.println("==NOTE #" + debugNoteCount + "=="); + //System.out.println(" Tick: " + tick); + //System.out.println(" Instrument ID: " + instrument); + //System.out.println(" Jump to the next tick: " + tCopy); + //System.out.println(" Jump to the next layer: " + jumpLayers); + + instrumentList[layer][tick] = instrument; + pitchList[layer][tick] = note; + finepitchList[layer][tick] = finepitch; + velocityList[layer][tick] = velocity; + panningList[layer][tick] = panning; + noteExistence[layer][tick] = true; + } + } + + for (int i = 0; i < layers; i++) { //Read layer data + + String name = readString(dataInputStream); + + if (nbsversion >= 4) { + dataInputStream.readByte(); + } + + byte volume = dataInputStream.readByte(); + int panning = 100; + + if (nbsversion >= 2) { + panning = Byte.toUnsignedInt(dataInputStream.readByte()); + } + + for (int currentTick = 0; currentTick < length + 1; currentTick++) { + boolean noteExists = noteExistence[i][currentTick]; + if (noteExists == true) { + + int noteVelocity = velocityList[i][currentTick]; + int notePanning = panningList[i][currentTick]; + + double averageVelocity = noteVelocity * (volume / 100d); + double averagePanning = (notePanning + panning) / 2d; + + double preFinalPanning = (averagePanning - 100) / 50; + + String finalVelocity = new BigDecimal(averageVelocity).setScale(3, BigDecimal.ROUND_FLOOR).stripTrailingZeros().toPlainString(); + String finalPanning = new BigDecimal(preFinalPanning).setScale(3, BigDecimal.ROUND_FLOOR).stripTrailingZeros().toPlainString(); + + //COOL DEBUG CODE STARTS FROM HERE + //System.out.println("Pre-Final Panning: " + preFinalPanning); + //System.out.println("Final Panning: " + finalPanning); + //COOL DEBUG CODE ENDS HERE + + String finalString; + if (preFinalPanning == 0) { + finalString = "," + finalVelocity; + } else { + finalString = "," + finalVelocity + "," + finalPanning; + } + addStringList[i][currentTick] = finalString; + } + } + + String finalLayerVolume = new BigDecimal(volume).setScale(3, BigDecimal.ROUND_FLOOR).stripTrailingZeros().toPlainString(); + String finalLayerPanning = new BigDecimal(panning).setScale(3, BigDecimal.ROUND_FLOOR).stripTrailingZeros().toPlainString(); + + layerStringBuilder.append("=" + finalLayerVolume + "," + finalLayerPanning); + } + + int customInstruments = 0; + customInstruments = dataInputStream.readByte(); + + int[] customPitchList = new int[customInstruments]; + + if (customInstruments >= 1) { + for (int i = 0; i < customInstruments; i++) { + int instrumentOffset = vanillaInstruments + customInstruments; + int instrumentPitch = 0; + + readString(dataInputStream); //Instrument name + readString(dataInputStream); //Sound file + + instrumentPitch = dataInputStream.readByte(); //Sound pitch + + customPitchList[i] = instrumentPitch; + + dataInputStream.readByte(); //Press key + } + } + + //EPIC DEBUG CODE STARTS FROM HERE + //System.out.println("List length: " + jumpTickList.size()); + //System.out.println("Array length: " + jumpTickArray.length); + //EPIC DEBUG CODE ENDS HERE + + dataInputStream.close(); + + for (int currentTick = 0; currentTick < length + 1; currentTick++) { + boolean columnExists = columnExistence[currentTick]; + if (columnExists == true) { + StringBuilder columnStringBuilder = new StringBuilder(); + if (!firstNoted) { + columnStringBuilder.append(currentTick + 1); + firstNoted = true; + } else { + columnStringBuilder.append("=" + (currentTick + 1)); + } + boolean firstAppend = true; + for (int i = 0; i < layers; i++) { + boolean noteExists = noteExistence[i][currentTick]; + if (noteExists == true) { + String laterNoteString = addStringList[i][currentTick]; + + int noteInstrument = instrumentList[i][currentTick]; + int noteKey = pitchList[i][currentTick]; + int noteFinePitch = finepitchList[i][currentTick]; + int noteKeyOffset = 0; + + //ANOTHER EPIC DEBUG CODE STARTS FROM HERE + //System.out.println("Note ID: " + noteID); + //ANOTHER EPIC DEBUG CODE ENDS HERE + + if (noteInstrument >= vanillaInstruments) { + int instrumentId = noteInstrument - vanillaInstruments; + noteKeyOffset = customPitchList[instrumentId] - 45; + } + if (firstAppend == true) { + columnStringBuilder.append(":" + (noteInstrument + 1) + "," + getMinecraftPitch(noteKey + (double) noteFinePitch / 100d, noteKeyOffset) + laterNoteString); + firstAppend = false; + } else { + columnStringBuilder.append(";" + (noteInstrument + 1) + "," + getMinecraftPitch(noteKey + (double) noteFinePitch / 100d, noteKeyOffset) + laterNoteString); + } + } + } + stringBuilder.append(columnStringBuilder.toString()); + } + } + + //YET ANOTHER EPIC DEBUG CODE STARTS FROM HERE + //System.out.println("Note Data: " + stringBuilder.toString()); + //System.out.println("Layer Data: " + layerStringBuilder.toString()); + //YET ANOTHER EPIC DEBUG CODE ENDS HERE + + return new SongData(title, author, speed, (int) ((Math.ceil((length + 1) / timeSignature) + 1) * timeSignature), stringBuilder.toString(), file, layerStringBuilder.toString(), (loopTick + 1), loopCount, customInstruments); + } + + private static short readShort(DataInputStream dataInputStream) throws IOException { + int byte1 = dataInputStream.readUnsignedByte(); + int byte2 = dataInputStream.readUnsignedByte(); + return (short) (byte1 + (byte2 << 8)); + } + + private static int readInt(DataInputStream dataInputStream) throws IOException { + int byte1 = dataInputStream.readUnsignedByte(); + int byte2 = dataInputStream.readUnsignedByte(); + int byte3 = dataInputStream.readUnsignedByte(); + int byte4 = dataInputStream.readUnsignedByte(); + return (byte1 + (byte2 << 8) + (byte3 << 16) + (byte4 << 24)); + } + + private static String readString(DataInputStream dataInputStream) throws IOException { + int length = readInt(dataInputStream); + StringBuilder builder = new StringBuilder(length); + for (; length > 0; --length) { + char c = (char) dataInputStream.readByte(); + if (c == (char) 0x0D) { + c = ' '; + } + builder.append(c); + } + return builder.toString(); + } + + private static int getMinecraftPitch(double key, double offset) { + + if (key < 33) key -= 9; + else if (key > 57) key -= 57; + else key -= 33; + + key += offset; + + double finalValue = (0.5 * (Math.pow(2, (key / 12)))) * 1000; + + return (int) finalValue; + } + + public SongData parse(InputStream inputStream) throws IOException, OutdatedNBSException { + return parse(inputStream, null); + } +} diff --git a/src/main/java/com/diamondfire/helpbot/util/NBSToTemplate.java b/src/main/java/com/diamondfire/helpbot/util/NBSToTemplate.java new file mode 100644 index 00000000..a5359dc9 --- /dev/null +++ b/src/main/java/com/diamondfire/helpbot/util/NBSToTemplate.java @@ -0,0 +1,157 @@ +package com.diamondfire.helpbot.util; +import java.math.BigDecimal; +// stolen from Codeutils + + +public class NBSToTemplate { + + private static final String SONG_PARSER_VERSION = "4"; + private static final String SONG_NBS_FORMAT_VERSION = "4"; + + String song; + String name; + String author; + String filename; + String layers; + String version; + float speed; + int length; + int loopTick; + int loopCount; + int customInstrumentCount; + boolean multipleChests = false; + + public NBSToTemplate(SongData song) { + + this.version = "v" + SONG_PARSER_VERSION + "-nbs" + SONG_NBS_FORMAT_VERSION; + + this.song = song.getNotes(); + this.author = song.getAuthor(); + this.name = song.getName(); + this.filename = song.getFileName(); + this.layers = song.getLayers(); + this.length = song.getLength(); + this.speed = song.getSpeed(); + this.loopTick = song.getLoopTick(); + this.loopCount = song.getLoopCount(); + this.customInstrumentCount = song.getCustomInstrumentCount(); + } + + public String convert() { + String[] songData = song.split("="); + StringBuilder currentNotes = new StringBuilder(); + StringBuilder code = new StringBuilder(); + StringBuilder currentBlock = new StringBuilder(); + StringBuilder instList = new StringBuilder(); + + String songTempo = new BigDecimal(this.speed).stripTrailingZeros().toPlainString(); + + if (name.length() == 0) { + if (filename.indexOf(".") > 0) { + name = filename.substring(0, filename.lastIndexOf(".")); + } else { + name = filename; + } + } + if (author.length() == 0) author = "N/A"; + + code.append(String.format("{\"id\":\"block\",\"block\":\"func\",\"args\":{\"items\":[{\"item\":{\"id\":\"bl_tag\",\"data\":{\"option\":\"False\",\"tag\":\"Is Hidden\",\"action\":\"dynamic\",\"block\":\"func\"}},\"slot\":26}]},\"data\":\"%s\"},", name)); + + int slot = 1; + int chestCount = 1; + boolean chestInited = false; + int noteCount = 0; + boolean finalNote = false; + + for (int i = 0; i < songData.length; i++) { + boolean closeChest = false; + if (slot == 1) { + if (!chestInited) { + chestInited = true; + currentBlock.append("{\"id\":\"block\",\"block\":\"set_var\",\"args\":{\"items\":[{\"item\":{\"id\":\"var\",\"data\":{\"name\":\"notes\",\"scope\":\"local\"}},\"slot\":0}"); + } + } + + if (slot >= 27) { + closeChest = true; + } + + if (!closeChest) { + String currentNote = songData[i]; + String revertString = currentNotes.toString(); + + if (noteCount == 0) { + currentNotes.append(currentNote); + } else { + currentNotes.append("=" + currentNote); + } + noteCount++; + + if (currentNotes.length() > 1930) { + currentNotes = new StringBuilder(revertString); + currentBlock.append(String.format(",{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"%s\"}},\"slot\":%d}", currentNotes.toString(), slot)); + currentNotes.setLength(0); + noteCount = 0; + finalNote = true; + i -= 1; + slot++; + } + + if (i >= songData.length - 1) { + if (!finalNote) { + currentBlock.append(String.format(",{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"%s\"}},\"slot\":%d}", currentNotes.toString(), slot)); + currentNotes.setLength(0); + } + closeChest = true; + } + finalNote = false; + } + if (closeChest) { + String varActionType; + if (chestCount == 1) { + varActionType = "CreateList"; + } else { + varActionType = "AppendValue"; + } + + currentBlock.append(String.format("]},\"action\":\"%s\"},", varActionType)); + code.append(currentBlock.toString()); + currentBlock.setLength(0); + currentNotes.setLength(0); + + chestInited = false; + noteCount = 0; + finalNote = false; + chestCount++; + closeChest = false; + slot = 1; + } + } + + //CreateList: instrumentNames + if (customInstrumentCount == 0) { + code.append("{\"id\":\"block\",\"block\":\"set_var\",\"args\":{\"items\":[{\"item\":{\"id\":\"var\",\"data\":{\"name\":\"instrumentNames\",\"scope\":\"local\"}},\"slot\":0},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Harp\"}},\"slot\":1},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Bass\"}},\"slot\":2},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Bass Drum\"}},\"slot\":3},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Snare Drum\"}},\"slot\":4},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Click\"}},\"slot\":5},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Guitar\"}},\"slot\":6},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Flute\"}},\"slot\":7},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Bell\"}},\"slot\":8},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Chime\"}},\"slot\":9},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Xylophone\"}},\"slot\":10},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Iron Xylophone\"}},\"slot\":11},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Cow Bell\"}},\"slot\":12},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Didgeridoo\"}},\"slot\":13},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Bit\"}},\"slot\":14},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Banjo\"}},\"slot\":15},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Pling\"}},\"slot\":16}]},\"action\":\"CreateList\"},"); + } else { + instList.append("{\"id\":\"block\",\"block\":\"set_var\",\"args\":{\"items\":[{\"item\":{\"id\":\"var\",\"data\":{\"name\":\"instrumentNames\",\"scope\":\"local\"}},\"slot\":0},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Harp\"}},\"slot\":1},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Bass\"}},\"slot\":2},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Bass Drum\"}},\"slot\":3},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Snare Drum\"}},\"slot\":4},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Click\"}},\"slot\":5},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Guitar\"}},\"slot\":6},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Flute\"}},\"slot\":7},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Bell\"}},\"slot\":8},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Chime\"}},\"slot\":9},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Xylophone\"}},\"slot\":10},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Iron Xylophone\"}},\"slot\":11},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Cow Bell\"}},\"slot\":12},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Didgeridoo\"}},\"slot\":13},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Bit\"}},\"slot\":14},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Banjo\"}},\"slot\":15},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"Pling\"}},\"slot\":16},"); + + int currentSlot; + + currentSlot = 17; + for (int currentInstID = 1; currentInstID <= customInstrumentCount; currentInstID++) { + if (currentInstID == customInstrumentCount) { + instList.append(String.format("{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"\"}},\"slot\":%d}", currentInstID, currentSlot)); + } else { + instList.append(String.format("{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"\"}},\"slot\":%d},", currentInstID, currentSlot)); + } + currentSlot++; + } + instList.append("]},\"action\":\"CreateList\"},"); + code.append(instList.toString()); + } + + //CreateList: songData + code.append(String.format("{\"id\":\"block\",\"block\":\"set_var\",\"args\":{\"items\":[{\"item\":{\"id\":\"var\",\"data\":{\"name\":\"songData\",\"scope\":\"local\"}},\"slot\":0},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"%s\"}},\"slot\":1},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"%s\"}},\"slot\":2},{\"item\":{\"id\":\"num\",\"data\":{\"name\":\"%s\"}},\"slot\":3}, {\"item\":{\"id\":\"num\",\"data\":{\"name\":\"%d\"}},\"slot\":4}, {\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"%s\"}},\"slot\":5}, {\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"%s\"}},\"slot\":6},{\"item\":{\"id\":\"num\",\"data\":{\"name\":\"%d\"}},\"slot\":7},{\"item\":{\"id\":\"num\",\"data\":{\"name\":\"%d\"}},\"slot\":8}]},\"action\":\"CreateList\"}", name, author, songTempo, length, layers, version, loopTick, loopCount)); + + return "{\"blocks\": [" + code + "]}"; + } +} diff --git a/src/main/java/com/diamondfire/helpbot/util/OutdatedNBSException.java b/src/main/java/com/diamondfire/helpbot/util/OutdatedNBSException.java new file mode 100644 index 00000000..24314b2d --- /dev/null +++ b/src/main/java/com/diamondfire/helpbot/util/OutdatedNBSException.java @@ -0,0 +1,4 @@ +package com.diamondfire.helpbot.util; + +public class OutdatedNBSException extends Exception{ +} diff --git a/src/main/java/com/diamondfire/helpbot/util/SongData.java b/src/main/java/com/diamondfire/helpbot/util/SongData.java new file mode 100644 index 00000000..194ee0fd --- /dev/null +++ b/src/main/java/com/diamondfire/helpbot/util/SongData.java @@ -0,0 +1,88 @@ +package com.diamondfire.helpbot.util; +// stole from codeutils lol +public class SongData { + + String name; + String author; + float speed; + int length; + String fileName; + String notes; + String layers; + int loopTick; + int loopCount; + int customInstrumentCount; + + public SongData(String name, String author, float speed, int length, String notes, String fileName, String layers, int loopTick, int loopCount, int customInstrumentCount) { + this.name = name; + this.author = author; + this.speed = speed; + this.length = length; + this.notes = notes; + this.fileName = fileName; + this.layers = layers; + this.loopTick = loopTick; + this.loopCount = loopCount; + this.customInstrumentCount = customInstrumentCount; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public float getSpeed() { + return speed; + } + + public void setSpeed(float speed) { + this.speed = speed; + } + + public int getLength() { + return length; + } + + public String getNotes() { + return notes; + } + + public void setNotes(String notes) { + this.notes = notes; + } + + public String getFileName() { + return fileName; + } + + public void setFileName(String fileName) { + this.fileName = fileName; + } + + public String getLayers() { + return layers; + } + + public int getLoopTick() { + return loopTick; + } + + public int getLoopCount() { + return loopCount; + } + + public int getCustomInstrumentCount() { + return customInstrumentCount; + } +} \ No newline at end of file From 60ec0cfc3985bd612e86f25ced3fa0a5fda1d713 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sat, 13 Feb 2021 15:11:19 -0600 Subject: [PATCH 06/17] nbs stuff --- input.nbs | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 input.nbs diff --git a/input.nbs b/input.nbs new file mode 100644 index 00000000..e69de29b From b90f5d546f9f0c6c61d2a9c40fa55b0b118ab488 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sat, 13 Feb 2021 20:13:08 -0600 Subject: [PATCH 07/17] nbs things --- src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java b/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java index d7f4c748..364ae7b1 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java +++ b/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java @@ -108,7 +108,6 @@ public static void initialize() throws LoginException { new ExcusedStaffCommand(), new SupportBannedPlayersCommand(), new DiscussionMuteCommand(), - new JerePicsCommand(), new NbsCommand() ); From 8242dea84c43b5da36425b82b7be8cc1622bbe18 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sat, 13 Feb 2021 20:14:08 -0600 Subject: [PATCH 08/17] nbs things --- .../helpbot/util/OutdatedNBSException.java | 4 --- .../util/{ => nbs}/CompressionUtil.java | 3 +- .../helpbot/util/{ => nbs}/NBSDecoder.java | 32 ++++++------------- .../helpbot/util/{ => nbs}/NBSToTemplate.java | 6 ++-- .../util/nbs/OutdatedNBSException.java | 4 +++ .../helpbot/util/{ => nbs}/SongData.java | 4 +-- 6 files changed, 21 insertions(+), 32 deletions(-) delete mode 100644 src/main/java/com/diamondfire/helpbot/util/OutdatedNBSException.java rename src/main/java/com/diamondfire/helpbot/util/{ => nbs}/CompressionUtil.java (92%) rename src/main/java/com/diamondfire/helpbot/util/{ => nbs}/NBSDecoder.java (87%) rename src/main/java/com/diamondfire/helpbot/util/{ => nbs}/NBSToTemplate.java (98%) create mode 100644 src/main/java/com/diamondfire/helpbot/util/nbs/OutdatedNBSException.java rename src/main/java/com/diamondfire/helpbot/util/{ => nbs}/SongData.java (94%) diff --git a/src/main/java/com/diamondfire/helpbot/util/OutdatedNBSException.java b/src/main/java/com/diamondfire/helpbot/util/OutdatedNBSException.java deleted file mode 100644 index 24314b2d..00000000 --- a/src/main/java/com/diamondfire/helpbot/util/OutdatedNBSException.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.diamondfire.helpbot.util; - -public class OutdatedNBSException extends Exception{ -} diff --git a/src/main/java/com/diamondfire/helpbot/util/CompressionUtil.java b/src/main/java/com/diamondfire/helpbot/util/nbs/CompressionUtil.java similarity index 92% rename from src/main/java/com/diamondfire/helpbot/util/CompressionUtil.java rename to src/main/java/com/diamondfire/helpbot/util/nbs/CompressionUtil.java index 4e3de232..76feef15 100644 --- a/src/main/java/com/diamondfire/helpbot/util/CompressionUtil.java +++ b/src/main/java/com/diamondfire/helpbot/util/nbs/CompressionUtil.java @@ -1,9 +1,10 @@ -package com.diamondfire.helpbot.util; +package com.diamondfire.helpbot.util.nbs; import java.io.*; import java.nio.charset.StandardCharsets; import java.util.Base64; import java.util.zip.*; +//from https://github.com/CodeUtilities/CodeUtilities public class CompressionUtil { diff --git a/src/main/java/com/diamondfire/helpbot/util/NBSDecoder.java b/src/main/java/com/diamondfire/helpbot/util/nbs/NBSDecoder.java similarity index 87% rename from src/main/java/com/diamondfire/helpbot/util/NBSDecoder.java rename to src/main/java/com/diamondfire/helpbot/util/nbs/NBSDecoder.java index 0e2cbcc0..a38628cf 100644 --- a/src/main/java/com/diamondfire/helpbot/util/NBSDecoder.java +++ b/src/main/java/com/diamondfire/helpbot/util/nbs/NBSDecoder.java @@ -1,7 +1,8 @@ -package com.diamondfire.helpbot.util; +package com.diamondfire.helpbot.util.nbs; + import java.io.*; import java.math.BigDecimal; -//stolen from Codeutils +//from https://github.com/CodeUtilities/CodeUtilities public class NBSDecoder { @@ -40,7 +41,7 @@ private static SongData parse(InputStream inputStream, File songFile) throws IOE throw new OutdatedNBSException(); } } else { - // poop ChatUtil.sendMessage("§a§lHEY!§r Looks like you are using original Note Block Studio. I recommend you to use §bOpen Note Block Studio§r, which is an unofficial continuation of Note Block Studio!"); + } short layers = readShort(dataInputStream); //song height title = readString(dataInputStream); //title @@ -102,11 +103,7 @@ private static SongData parse(InputStream inputStream, File songFile) throws IOE finepitch = readShort(dataInputStream); } - //System.out.println("==NOTE #" + debugNoteCount + "=="); - //System.out.println(" Tick: " + tick); - //System.out.println(" Instrument ID: " + instrument); - //System.out.println(" Jump to the next tick: " + tCopy); - //System.out.println(" Jump to the next layer: " + jumpLayers); + instrumentList[layer][tick] = instrument; pitchList[layer][tick] = note; @@ -147,10 +144,7 @@ private static SongData parse(InputStream inputStream, File songFile) throws IOE String finalVelocity = new BigDecimal(averageVelocity).setScale(3, BigDecimal.ROUND_FLOOR).stripTrailingZeros().toPlainString(); String finalPanning = new BigDecimal(preFinalPanning).setScale(3, BigDecimal.ROUND_FLOOR).stripTrailingZeros().toPlainString(); - //COOL DEBUG CODE STARTS FROM HERE - //System.out.println("Pre-Final Panning: " + preFinalPanning); - //System.out.println("Final Panning: " + finalPanning); - //COOL DEBUG CODE ENDS HERE + String finalString; if (preFinalPanning == 0) { @@ -189,10 +183,7 @@ private static SongData parse(InputStream inputStream, File songFile) throws IOE } } - //EPIC DEBUG CODE STARTS FROM HERE - //System.out.println("List length: " + jumpTickList.size()); - //System.out.println("Array length: " + jumpTickArray.length); - //EPIC DEBUG CODE ENDS HERE + dataInputStream.close(); @@ -217,9 +208,7 @@ private static SongData parse(InputStream inputStream, File songFile) throws IOE int noteFinePitch = finepitchList[i][currentTick]; int noteKeyOffset = 0; - //ANOTHER EPIC DEBUG CODE STARTS FROM HERE - //System.out.println("Note ID: " + noteID); - //ANOTHER EPIC DEBUG CODE ENDS HERE + if (noteInstrument >= vanillaInstruments) { int instrumentId = noteInstrument - vanillaInstruments; @@ -237,10 +226,7 @@ private static SongData parse(InputStream inputStream, File songFile) throws IOE } } - //YET ANOTHER EPIC DEBUG CODE STARTS FROM HERE - //System.out.println("Note Data: " + stringBuilder.toString()); - //System.out.println("Layer Data: " + layerStringBuilder.toString()); - //YET ANOTHER EPIC DEBUG CODE ENDS HERE + return new SongData(title, author, speed, (int) ((Math.ceil((length + 1) / timeSignature) + 1) * timeSignature), stringBuilder.toString(), file, layerStringBuilder.toString(), (loopTick + 1), loopCount, customInstruments); } diff --git a/src/main/java/com/diamondfire/helpbot/util/NBSToTemplate.java b/src/main/java/com/diamondfire/helpbot/util/nbs/NBSToTemplate.java similarity index 98% rename from src/main/java/com/diamondfire/helpbot/util/NBSToTemplate.java rename to src/main/java/com/diamondfire/helpbot/util/nbs/NBSToTemplate.java index a5359dc9..6eee3ec0 100644 --- a/src/main/java/com/diamondfire/helpbot/util/NBSToTemplate.java +++ b/src/main/java/com/diamondfire/helpbot/util/nbs/NBSToTemplate.java @@ -1,6 +1,8 @@ -package com.diamondfire.helpbot.util; +package com.diamondfire.helpbot.util.nbs; + import java.math.BigDecimal; -// stolen from Codeutils +// from https://github.com/CodeUtilities/CodeUtilities + public class NBSToTemplate { diff --git a/src/main/java/com/diamondfire/helpbot/util/nbs/OutdatedNBSException.java b/src/main/java/com/diamondfire/helpbot/util/nbs/OutdatedNBSException.java new file mode 100644 index 00000000..1c4245c8 --- /dev/null +++ b/src/main/java/com/diamondfire/helpbot/util/nbs/OutdatedNBSException.java @@ -0,0 +1,4 @@ +package com.diamondfire.helpbot.util.nbs; +//from https://github.com/CodeUtilities/CodeUtilities +public class OutdatedNBSException extends Exception{ +} diff --git a/src/main/java/com/diamondfire/helpbot/util/SongData.java b/src/main/java/com/diamondfire/helpbot/util/nbs/SongData.java similarity index 94% rename from src/main/java/com/diamondfire/helpbot/util/SongData.java rename to src/main/java/com/diamondfire/helpbot/util/nbs/SongData.java index 194ee0fd..ad939a07 100644 --- a/src/main/java/com/diamondfire/helpbot/util/SongData.java +++ b/src/main/java/com/diamondfire/helpbot/util/nbs/SongData.java @@ -1,5 +1,5 @@ -package com.diamondfire.helpbot.util; -// stole from codeutils lol +package com.diamondfire.helpbot.util.nbs; +// from https://github.com/CodeUtilities/CodeUtilities public class SongData { String name; From 997957250a21af5e952525e3f17864933ae4f5ba Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sat, 13 Feb 2021 20:16:19 -0600 Subject: [PATCH 09/17] nbs things --- .../bot/command/impl/other/NbsCommand.java | 83 +++++++++---------- 1 file changed, 40 insertions(+), 43 deletions(-) diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/NbsCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/NbsCommand.java index b46ccd71..a5601a6c 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/NbsCommand.java +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/NbsCommand.java @@ -6,7 +6,7 @@ import com.diamondfire.helpbot.bot.command.impl.Command; import com.diamondfire.helpbot.bot.command.permissions.Permission; import com.diamondfire.helpbot.bot.events.CommandEvent; -import com.diamondfire.helpbot.util.*; +import com.diamondfire.helpbot.util.nbs.*; import com.google.gson.Gson; import com.google.gson.JsonObject; import net.dv8tion.jda.api.EmbedBuilder; @@ -22,29 +22,7 @@ import java.util.concurrent.CompletableFuture; public class NbsCommand extends Command { - private static String generateLink(String templateData) throws IOException { - URL url = new URL("https://twv.vercel.app/v2/create"); - URLConnection con = url.openConnection(); - HttpURLConnection http = (HttpURLConnection) con; - http.setRequestMethod("POST"); - http.setDoOutput(true); - byte[] out = templateData.getBytes(StandardCharsets.UTF_8); - int length = out.length; - - http.setFixedLengthStreamingMode(length); - http.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); - http.setRequestProperty("Accept","application/json; charset=UTF-8"); - http.connect(); - try(OutputStream os = http.getOutputStream()) { - os.write(out); - } - Scanner s = new Scanner(http.getInputStream()).useDelimiter("\\A"); - String result = s.hasNext() ? s.next() : ""; - s.close(); - http.disconnect(); - return result; - } @Override public String getName() { return "nbs"; @@ -53,9 +31,8 @@ public String getName() { @Override public HelpContext getHelpContext() { return new HelpContext() - .description("Generates Codeutils song function.") + .description("Generates a Codeutils song function.") .category(CommandCategory.OTHER); - } @Override @@ -67,42 +44,41 @@ public HelpContext getHelpContext() { @Override public void run(CommandEvent event) { TextChannel channel = event.getChannel(); + EmbedBuilder warning = new EmbedBuilder() + .addField("Warning!","You need to attach an nbs file!",false) + .setColor(Color.RED); if(event.getMessage().getAttachments().isEmpty()) { - EmbedBuilder warning = new EmbedBuilder() - .addField("Warning!","You need to attach an nbs file!",false) - .setColor(Color.RED); - event.getChannel().sendMessage(warning.build()).queue(); + channel.sendMessage(warning.build()).queue(); + return; + } + Message.Attachment attachment = event.getMessage().getAttachments().get(0); + if(!attachment.getFileExtension().equals("nbs")) { + channel.sendMessage(warning.build()).queue(); return; } - EmbedBuilder errorEmbed = new EmbedBuilder() .setColor(Color.RED) .setTitle("An error occurred!"); try { File file = new File("input.nbs"); + System.out.println(event.getMessage().getAttachments().get(0).getFileExtension()); CompletableFuture.runAsync(() -> { - CompletableFuture task = event.getMessage().getAttachments().get(0).downloadToFile(file); + CompletableFuture task = attachment.downloadToFile(file); while(!task.isDone()) { } try { - SongData d = NBSDecoder.parse(file); - String code = new NBSToTemplate(d).convert(); + String code = new NBSToTemplate(NBSDecoder.parse(file)).convert(); byte[] b64 = CompressionUtil.toBase64(CompressionUtil.toGZIP(code.getBytes(StandardCharsets.UTF_8))); - String exported = new String(b64); - String jsonInputString = String.format("{\"template\": \"%s\",\"temp\": true}",exported); - String result = generateLink(jsonInputString); - JsonObject json = new Gson().fromJson(result,JsonObject.class); + String jsonInputString = String.format("{\"template\": \"%s\",\"temp\": true}",new String(b64)); + JsonObject json = new Gson().fromJson(generateLink(jsonInputString),JsonObject.class); EmbedBuilder embed = new EmbedBuilder() .setColor(new Color(70,199,82)) .setTitle("Function Generated!") .setThumbnail("https://static.wikia.nocookie.net/minecraft/images/9/9b/Note_Block.png/revision/latest?cb=20190921170620") - .addField("Link(Expires in 2 mins):","https://derpystuff.gitlab.io/code/l?link=" + json.get("link").getAsString(),false) - .addField("Music Player:","https://derpystuff.gitlab.io/code/l?link=7cf5d91c35bbde31c28567d8d8945c40",false); - - + .addField("Link: *Expires in 2 mins*","https://derpystuff.gitlab.io/code/l?link=" + json.get("link").getAsString(),false) + .addField("Info:","Click the link shown above and click the button in the bottom left corner to copy the give command for the template. You will need this function to play songs: https://derpystuff.gitlab.io/code/l?link=7cf5d91c35bbde31c28567d8d8945c40",false); channel.sendMessage(embed.build()).queue(); - System.out.println(exported); } catch(OutdatedNBSException e) { e.printStackTrace(); channel.sendMessage(errorEmbed.build()).queue(); @@ -110,13 +86,34 @@ public void run(CommandEvent event) { e.printStackTrace(); channel.sendMessage(errorEmbed.build()).queue(); } - }); } catch(Exception e) { e.printStackTrace(); channel.sendMessage(errorEmbed.build()).queue(); } + } + private static String generateLink(String templateData) throws IOException { + URL url = new URL("https://twv.vercel.app/v2/create"); + URLConnection con = url.openConnection(); + HttpURLConnection http = (HttpURLConnection) con; + http.setRequestMethod("POST"); + http.setDoOutput(true); + byte[] out = templateData.getBytes(StandardCharsets.UTF_8); + int length = out.length; + http.setFixedLengthStreamingMode(length); + http.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); + http.setRequestProperty("Accept","application/json; charset=UTF-8"); + http.connect(); + try(OutputStream os = http.getOutputStream()) { + os.write(out); + } + Scanner s = new Scanner(http.getInputStream()).useDelimiter("\\A"); + String result = s.hasNext() ? s.next() : ""; + s.close(); + http.disconnect(); + + return result; } } From 62c1c8e829bd8a8f4eeb6c2482562f1cfd6295e1 Mon Sep 17 00:00:00 2001 From: ProjectDF <61924259+ProjectDF@users.noreply.github.com> Date: Sat, 13 Feb 2021 20:20:44 -0600 Subject: [PATCH 10/17] Delete JerePicsCommand.java --- .../command/impl/other/JerePicsCommand.java | 64 ------------------- 1 file changed, 64 deletions(-) delete mode 100644 src/main/java/com/diamondfire/helpbot/bot/command/impl/other/JerePicsCommand.java diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/JerePicsCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/JerePicsCommand.java deleted file mode 100644 index 838cf8e9..00000000 --- a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/JerePicsCommand.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.diamondfire.helpbot.bot.command.impl.other; - -import com.diamondfire.helpbot.bot.command.argument.ArgumentSet; -import com.diamondfire.helpbot.bot.command.help.CommandCategory; -import com.diamondfire.helpbot.bot.command.help.HelpContext; -import com.diamondfire.helpbot.bot.command.impl.Command; -import com.diamondfire.helpbot.bot.command.permissions.Permission; -import com.diamondfire.helpbot.bot.events.CommandEvent; -import net.dv8tion.jda.api.EmbedBuilder; - -import java.awt.*; - -public class JerePicsCommand extends Command { - - @Override - public String getName() { - return "jerepic"; - } - - @Override - public HelpContext getHelpContext() { - return new HelpContext() - .description("Very hot jeremaster pictures.") - .category(CommandCategory.OTHER); - - } - - @Override - protected ArgumentSet compileArguments() { - return new ArgumentSet(); - } - - @Override - public Permission getPermission() { - return Permission.USER; - } - - @Override - public void run(CommandEvent event) { - EmbedBuilder embed = new EmbedBuilder(); - embed.setColor(new Color(184,45,40)); - embed.setTitle("Jere Pic"); - int rngIndex = (int) (Math.random() * links.length); - embed.setImage(links[rngIndex]); - event.getChannel().sendMessage(embed.build()).queue(); - - } - private static final String[] links = { - "https://imgur.com/cZZHMWY.png", - "https://imgur.com/BGcPKv8.png", - "https://imgur.com/seRPPR2.png", - "https://imgur.com/e0aHeLp.png", - "https://imgur.com/m6MjDbk.png", - "https://imgur.com/3yaNUzm.png", - "https://cdn.discordapp.com/attachments/180793115223916544/539188769480507426/unknown.png", - "https://cdn.discordapp.com/attachments/789266995672907818/809169563543273512/jeremaster.png", - "https://external-content.duckduckgo.com/iu/?reload=1613077331588&u=https://mc-heads.net/body/jeremaster/180.png", - "https://imgur.com/LFldK2p.png", - "https://imgur.com/aEq7voY.png", - "https://render.namemc.com/skin/3d/body.png?skin=8c8cd7a1a2c52ca4&model=classic&theta=180&phi=0&time=0&width=600&height=800" - }; - - -} From cd7af5a9695fcfdb59150d5d54e07ea20723bc5f Mon Sep 17 00:00:00 2001 From: ProjectDF <61924259+ProjectDF@users.noreply.github.com> Date: Sun, 14 Feb 2021 09:37:29 -0600 Subject: [PATCH 11/17] Update HelpBotInstance.java --- src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java b/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java index 364ae7b1..d43be811 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java +++ b/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java @@ -64,6 +64,7 @@ public static void initialize() throws LoginException { new MutedCommand(), new UnmuteCommand(), new VerifyCommand(), + new PollCommand(), // statsbot new StatsCommand(), new SupportBadCommand(), From 696835825c95b66b87eefaeee80839dfcbc241a6 Mon Sep 17 00:00:00 2001 From: ProjectDF <61924259+ProjectDF@users.noreply.github.com> Date: Mon, 15 Feb 2021 18:24:33 -0600 Subject: [PATCH 12/17] Update HelpBotInstance.java --- .../java/com/diamondfire/helpbot/bot/HelpBotInstance.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java b/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java index 699b064a..c5f761c5 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java +++ b/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java @@ -1,6 +1,5 @@ package com.diamondfire.helpbot.bot; -import com.diamondfire.helpbot.HelpBot; import com.diamondfire.helpbot.bot.command.CommandHandler; import com.diamondfire.helpbot.bot.command.impl.codeblock.*; import com.diamondfire.helpbot.bot.command.impl.other.*; @@ -110,7 +109,8 @@ public static void initialize() throws LoginException { new ExcusedStaffCommand(), new SupportBannedPlayersCommand(), new DiscussionMuteCommand(), - new NbsCommand() + new NbsCommand(), + new DailySessionsCommand() ); JDABuilder builder = JDABuilder.createDefault(config.getToken()) @@ -141,4 +141,4 @@ public static Config getConfig() { public static TaskRegistry getScheduler() { return loop; } -} \ No newline at end of file +} From 7bf5b5d4a9e17a714091185e0023c53b6e6fe17e Mon Sep 17 00:00:00 2001 From: ProjectDF <61924259+ProjectDF@users.noreply.github.com> Date: Mon, 15 Feb 2021 18:25:19 -0600 Subject: [PATCH 13/17] Update NbsCommand.java --- .../bot/command/impl/other/NbsCommand.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/NbsCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/NbsCommand.java index a5601a6c..ecf23771 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/NbsCommand.java +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/NbsCommand.java @@ -5,6 +5,9 @@ import com.diamondfire.helpbot.bot.command.help.HelpContext; import com.diamondfire.helpbot.bot.command.impl.Command; import com.diamondfire.helpbot.bot.command.permissions.Permission; +import com.diamondfire.helpbot.bot.command.reply.PresetBuilder; +import com.diamondfire.helpbot.bot.command.reply.feature.informative.InformativeReply; +import com.diamondfire.helpbot.bot.command.reply.feature.informative.InformativeReplyType; import com.diamondfire.helpbot.bot.events.CommandEvent; import com.diamondfire.helpbot.util.nbs.*; import com.google.gson.Gson; @@ -44,16 +47,15 @@ public HelpContext getHelpContext() { @Override public void run(CommandEvent event) { TextChannel channel = event.getChannel(); - EmbedBuilder warning = new EmbedBuilder() - .addField("Warning!","You need to attach an nbs file!",false) - .setColor(Color.RED); + PresetBuilder preset = new PresetBuilder(); + preset.withPreset(new InformativeReply(InformativeReplyType.ERROR,"You need to attach an nbs file!")); if(event.getMessage().getAttachments().isEmpty()) { - channel.sendMessage(warning.build()).queue(); + event.reply(preset); return; } Message.Attachment attachment = event.getMessage().getAttachments().get(0); if(!attachment.getFileExtension().equals("nbs")) { - channel.sendMessage(warning.build()).queue(); + event.reply(preset); return; } EmbedBuilder errorEmbed = new EmbedBuilder() @@ -75,8 +77,8 @@ public void run(CommandEvent event) { .setColor(new Color(70,199,82)) .setTitle("Function Generated!") .setThumbnail("https://static.wikia.nocookie.net/minecraft/images/9/9b/Note_Block.png/revision/latest?cb=20190921170620") - .addField("Link: *Expires in 2 mins*","https://derpystuff.gitlab.io/code/l?link=" + json.get("link").getAsString(),false) - .addField("Info:","Click the link shown above and click the button in the bottom left corner to copy the give command for the template. You will need this function to play songs: https://derpystuff.gitlab.io/code/l?link=7cf5d91c35bbde31c28567d8d8945c40",false); + .addField("Link: __Expires in 2 mins__","[Function Link](https://derpystuff.gitlab.io/code/l?link=" + json.get("link").getAsString() + ")",false) + .addField("Info:","Click the link shown above and click the button in the bottom left corner to copy the give command for the template. You will need [this function](https://derpystuff.gitlab.io/code/l?link=7cf5d91c35bbde31c28567d8d8945c40) to play songs.",false); channel.sendMessage(embed.build()).queue(); } catch(OutdatedNBSException e) { From 98a976c3f27424b72493357721a087be6927c914 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Mon, 19 Apr 2021 16:21:32 -0500 Subject: [PATCH 14/17] Update to nbs command --- .../bot/command/impl/other/NbsCommand.java | 64 +++++++++---------- 1 file changed, 29 insertions(+), 35 deletions(-) diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/NbsCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/NbsCommand.java index ecf23771..5da1bebe 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/NbsCommand.java +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/NbsCommand.java @@ -22,79 +22,74 @@ import java.nio.charset.StandardCharsets; import java.util.Scanner; -import java.util.concurrent.CompletableFuture; public class NbsCommand extends Command { - + @Override public String getName() { return "nbs"; } - + @Override public HelpContext getHelpContext() { return new HelpContext() .description("Generates a Codeutils song function.") .category(CommandCategory.OTHER); } - + @Override protected ArgumentSet compileArguments() { return new ArgumentSet(); } - + @Override public Permission getPermission() { return Permission.USER; } - + @Override public void run(CommandEvent event) { TextChannel channel = event.getChannel(); - PresetBuilder preset = new PresetBuilder(); - preset.withPreset(new InformativeReply(InformativeReplyType.ERROR,"You need to attach an nbs file!")); + PresetBuilder nbsPreset = new PresetBuilder() + .withPreset(new InformativeReply(InformativeReplyType.ERROR,"You need to attach an nbs file!")); + PresetBuilder error = new PresetBuilder() + .withPreset(new InformativeReply(InformativeReplyType.ERROR,"Something went wrong while processing/generating!")); + if(event.getMessage().getAttachments().isEmpty()) { - event.reply(preset); + event.reply(nbsPreset); return; } Message.Attachment attachment = event.getMessage().getAttachments().get(0); if(!attachment.getFileExtension().equals("nbs")) { - event.reply(preset); + event.reply(nbsPreset); return; } - EmbedBuilder errorEmbed = new EmbedBuilder() - .setColor(Color.RED) - .setTitle("An error occurred!"); + try { File file = new File("input.nbs"); - System.out.println(event.getMessage().getAttachments().get(0).getFileExtension()); - CompletableFuture.runAsync(() -> { - CompletableFuture task = attachment.downloadToFile(file); - while(!task.isDone()) { } + attachment.downloadToFile(file).thenAccept((downloadedFile) -> { try { String code = new NBSToTemplate(NBSDecoder.parse(file)).convert(); byte[] b64 = CompressionUtil.toBase64(CompressionUtil.toGZIP(code.getBytes(StandardCharsets.UTF_8))); - String jsonInputString = String.format("{\"template\": \"%s\",\"temp\": true}",new String(b64)); - JsonObject json = new Gson().fromJson(generateLink(jsonInputString),JsonObject.class); - + String templateJson = String.format("{\"template\": \"%s\",\"temp\": true}",new String(b64)); + JsonObject json = new Gson().fromJson(generateLink(templateJson),JsonObject.class); + EmbedBuilder embed = new EmbedBuilder() - .setColor(new Color(70,199,82)) - .setTitle("Function Generated!") - .setThumbnail("https://static.wikia.nocookie.net/minecraft/images/9/9b/Note_Block.png/revision/latest?cb=20190921170620") - .addField("Link: __Expires in 2 mins__","[Function Link](https://derpystuff.gitlab.io/code/l?link=" + json.get("link").getAsString() + ")",false) - .addField("Info:","Click the link shown above and click the button in the bottom left corner to copy the give command for the template. You will need [this function](https://derpystuff.gitlab.io/code/l?link=7cf5d91c35bbde31c28567d8d8945c40) to play songs.",false); - + .setColor(new Color(70,199,82)) + .setTitle("Template Generated!") + .setThumbnail("https://static.wikia.nocookie.net/minecraft/images/9/9b/Note_Block.png/revision/latest?cb=20190921170620") + .addField("Link: __Expires in 2 minutes__","[Template Link](https://derpystuff.gitlab.io/code/l?link=" + json.get("link").getAsString() + ")",false) + .addField("Info:","Click the link shown above and click the button in the bottom left corner to copy the give command for the template. You will need [this function](https://derpystuff.gitlab.io/code/l?link=7cf5d91c35bbde31c28567d8d8945c40) to play songs.",false); + channel.sendMessage(embed.build()).queue(); - } catch(OutdatedNBSException e) { - e.printStackTrace(); - channel.sendMessage(errorEmbed.build()).queue(); - } catch(IOException e) { + } catch(OutdatedNBSException | IOException e) { e.printStackTrace(); - channel.sendMessage(errorEmbed.build()).queue(); + event.reply(error); } }); } catch(Exception e) { e.printStackTrace(); - channel.sendMessage(errorEmbed.build()).queue(); + event.reply(error); } - + } + private static String generateLink(String templateData) throws IOException { URL url = new URL("https://twv.vercel.app/v2/create"); URLConnection con = url.openConnection(); @@ -103,7 +98,6 @@ private static String generateLink(String templateData) throws IOException { http.setDoOutput(true); byte[] out = templateData.getBytes(StandardCharsets.UTF_8); int length = out.length; - http.setFixedLengthStreamingMode(length); http.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); http.setRequestProperty("Accept","application/json; charset=UTF-8"); @@ -115,7 +109,7 @@ private static String generateLink(String templateData) throws IOException { String result = s.hasNext() ? s.next() : ""; s.close(); http.disconnect(); - + return result; } } From d6b7cc3ff37a2ac5d27c9c79757cac1403a8cb84 Mon Sep 17 00:00:00 2001 From: ProjectDF <61924259+ProjectDF@users.noreply.github.com> Date: Tue, 17 Aug 2021 20:34:55 -0500 Subject: [PATCH 15/17] Delete NbsCommand.java --- .../bot/command/impl/other/NbsCommand.java | 115 ------------------ 1 file changed, 115 deletions(-) delete mode 100644 src/main/java/com/diamondfire/helpbot/bot/command/impl/other/NbsCommand.java diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/NbsCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/NbsCommand.java deleted file mode 100644 index 5da1bebe..00000000 --- a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/NbsCommand.java +++ /dev/null @@ -1,115 +0,0 @@ -package com.diamondfire.helpbot.bot.command.impl.other; - -import com.diamondfire.helpbot.bot.command.argument.ArgumentSet; -import com.diamondfire.helpbot.bot.command.help.CommandCategory; -import com.diamondfire.helpbot.bot.command.help.HelpContext; -import com.diamondfire.helpbot.bot.command.impl.Command; -import com.diamondfire.helpbot.bot.command.permissions.Permission; -import com.diamondfire.helpbot.bot.command.reply.PresetBuilder; -import com.diamondfire.helpbot.bot.command.reply.feature.informative.InformativeReply; -import com.diamondfire.helpbot.bot.command.reply.feature.informative.InformativeReplyType; -import com.diamondfire.helpbot.bot.events.CommandEvent; -import com.diamondfire.helpbot.util.nbs.*; -import com.google.gson.Gson; -import com.google.gson.JsonObject; -import net.dv8tion.jda.api.EmbedBuilder; -import net.dv8tion.jda.api.entities.Message; -import net.dv8tion.jda.api.entities.TextChannel; - -import java.awt.*; -import java.io.*; -import java.net.*; -import java.nio.charset.StandardCharsets; - -import java.util.Scanner; - -public class NbsCommand extends Command { - - @Override - public String getName() { - return "nbs"; - } - - @Override - public HelpContext getHelpContext() { - return new HelpContext() - .description("Generates a Codeutils song function.") - .category(CommandCategory.OTHER); - } - - @Override - protected ArgumentSet compileArguments() { return new ArgumentSet(); } - - @Override - public Permission getPermission() { return Permission.USER; } - - @Override - public void run(CommandEvent event) { - TextChannel channel = event.getChannel(); - PresetBuilder nbsPreset = new PresetBuilder() - .withPreset(new InformativeReply(InformativeReplyType.ERROR,"You need to attach an nbs file!")); - PresetBuilder error = new PresetBuilder() - .withPreset(new InformativeReply(InformativeReplyType.ERROR,"Something went wrong while processing/generating!")); - - if(event.getMessage().getAttachments().isEmpty()) { - event.reply(nbsPreset); - return; - } - Message.Attachment attachment = event.getMessage().getAttachments().get(0); - if(!attachment.getFileExtension().equals("nbs")) { - event.reply(nbsPreset); - return; - } - - try { - File file = new File("input.nbs"); - attachment.downloadToFile(file).thenAccept((downloadedFile) -> { - try { - String code = new NBSToTemplate(NBSDecoder.parse(file)).convert(); - byte[] b64 = CompressionUtil.toBase64(CompressionUtil.toGZIP(code.getBytes(StandardCharsets.UTF_8))); - String templateJson = String.format("{\"template\": \"%s\",\"temp\": true}",new String(b64)); - JsonObject json = new Gson().fromJson(generateLink(templateJson),JsonObject.class); - - EmbedBuilder embed = new EmbedBuilder() - .setColor(new Color(70,199,82)) - .setTitle("Template Generated!") - .setThumbnail("https://static.wikia.nocookie.net/minecraft/images/9/9b/Note_Block.png/revision/latest?cb=20190921170620") - .addField("Link: __Expires in 2 minutes__","[Template Link](https://derpystuff.gitlab.io/code/l?link=" + json.get("link").getAsString() + ")",false) - .addField("Info:","Click the link shown above and click the button in the bottom left corner to copy the give command for the template. You will need [this function](https://derpystuff.gitlab.io/code/l?link=7cf5d91c35bbde31c28567d8d8945c40) to play songs.",false); - - channel.sendMessage(embed.build()).queue(); - } catch(OutdatedNBSException | IOException e) { - e.printStackTrace(); - event.reply(error); - } - }); - } catch(Exception e) { - e.printStackTrace(); - event.reply(error); - } - - } - - private static String generateLink(String templateData) throws IOException { - URL url = new URL("https://twv.vercel.app/v2/create"); - URLConnection con = url.openConnection(); - HttpURLConnection http = (HttpURLConnection) con; - http.setRequestMethod("POST"); - http.setDoOutput(true); - byte[] out = templateData.getBytes(StandardCharsets.UTF_8); - int length = out.length; - http.setFixedLengthStreamingMode(length); - http.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); - http.setRequestProperty("Accept","application/json; charset=UTF-8"); - http.connect(); - try(OutputStream os = http.getOutputStream()) { - os.write(out); - } - Scanner s = new Scanner(http.getInputStream()).useDelimiter("\\A"); - String result = s.hasNext() ? s.next() : ""; - s.close(); - http.disconnect(); - - return result; - } -} From 819fb040e47614118d0837ede4cf1f00df8e0a71 Mon Sep 17 00:00:00 2001 From: ProjectDF <61924259+ProjectDF@users.noreply.github.com> Date: Sat, 26 Mar 2022 19:39:31 -0500 Subject: [PATCH 16/17] make nbs command attach a file with the command --- .../command/impl/other/fun/NbsCommand.java | 103 +++++++----------- 1 file changed, 41 insertions(+), 62 deletions(-) diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/fun/NbsCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/fun/NbsCommand.java index 5348c249..d35ce74b 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/fun/NbsCommand.java +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/fun/NbsCommand.java @@ -8,18 +8,16 @@ import com.diamondfire.helpbot.bot.command.reply.feature.informative.*; import com.diamondfire.helpbot.bot.events.CommandEvent; import com.diamondfire.helpbot.util.nbs.*; -import com.google.gson.*; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.*; import java.awt.*; import java.io.*; -import java.net.*; -import java.nio.charset.StandardCharsets; -import java.util.Scanner; public class NbsCommand extends Command { + private static final String NBS_IMAGE = "https://static.wikia.nocookie.net/minecraft/images/9/9b/Note_Block.png/revision/latest?cb=20190921170620"; + @Override public String getName() { return "nbs"; @@ -33,78 +31,59 @@ public HelpContext getHelpContext() { } @Override - protected ArgumentSet compileArguments() { return new ArgumentSet(); } + protected ArgumentSet compileArguments() { + return new ArgumentSet(); + } @Override - public Permission getPermission() { return Permission.USER; } + public Permission getPermission() { + return Permission.USER; + } @Override public void run(CommandEvent event) { TextChannel channel = event.getChannel(); - PresetBuilder nbsPreset = new PresetBuilder() - .withPreset(new InformativeReply(InformativeReplyType.ERROR,"You need to attach an nbs file!")); - PresetBuilder error = new PresetBuilder() - .withPreset(new InformativeReply(InformativeReplyType.ERROR,"Something went wrong while processing/generating!")); + PresetBuilder attachNbsMsg = new PresetBuilder().withPreset(new InformativeReply(InformativeReplyType.ERROR,"You need to attach an nbs file!")); + PresetBuilder errorMsg = new PresetBuilder().withPreset(new InformativeReply(InformativeReplyType.ERROR,"Something went wrong while generating!")); - if(event.getMessage().getAttachments().isEmpty()) { - event.reply(nbsPreset); + if (event.getMessage().getAttachments().isEmpty()) { + event.reply(attachNbsMsg); return; } + Message.Attachment attachment = event.getMessage().getAttachments().get(0); - if(!attachment.getFileExtension().equals("nbs")) { - event.reply(nbsPreset); + if (!attachment.getFileExtension().equals("nbs")) { + event.reply(attachNbsMsg); return; } - try { - File file = new File("input.nbs"); - attachment.downloadToFile(file).thenAccept((downloadedFile) -> { - try { - String code = new NBSToTemplate(NBSDecoder.parse(file)).convert(); - byte[] b64 = CompressionUtil.toBase64(CompressionUtil.toGZIP(code.getBytes(StandardCharsets.UTF_8))); - String templateJson = String.format("{\"template\": \"%s\",\"temp\": true}",new String(b64)); - JsonObject json = new Gson().fromJson(generateLink(templateJson),JsonObject.class); - - EmbedBuilder embed = new EmbedBuilder() - .setColor(new Color(70,199,82)) - .setTitle("Template Generated!") - .setThumbnail("https://static.wikia.nocookie.net/minecraft/images/9/9b/Note_Block.png/revision/latest?cb=20190921170620") - .addField("Link: __Expires in 2 minutes__","[Template Link](https://derpystuff.gitlab.io/code/l?link=" + json.get("link").getAsString() + ")",false) - .addField("Info:","Click the link shown above and click the button in the bottom left corner to copy the give command for the template. You will need [this function](https://derpystuff.gitlab.io/code/l?link=7cf5d91c35bbde31c28567d8d8945c40) to play songs.",false); - - channel.sendMessageEmbeds(embed.build()).queue(); - } catch(OutdatedNBSException | IOException e) { - e.printStackTrace(); - event.reply(error); - } - }); - } catch(Exception e) { - e.printStackTrace(); - event.reply(error); - } + File file = new File("input.nbs"); + + attachment.downloadToFile(file).thenAccept(downloadedFile -> { + try { + byte[] b64 = new NBSToTemplate(NBSDecoder.parse(file)).convert(); + File templateOutputfile = File.createTempFile("nbs_output", ".txt"); + BufferedWriter writer = new BufferedWriter(new FileWriter(templateOutputfile)); + writer.write(String.format("/give @p minecraft:ender_chest{display:{Name:'[{\"text\":\"» \",\"color\":\"gold\"},{\"text\":\"Code Template\",\"color\":\"yellow\",\"bold\":true}]'},PublicBukkitValues:{\"hypercube:codetemplatedata\":'{\"name\":\"&6» &e&lCode Template\",\"version\":1,\"code\":\"%s\",\"author\":\"helpbot\"}'}} 1", new String(b64))); + + writer.close(); + + + EmbedBuilder embed = new EmbedBuilder() + .setColor(new Color(70,199,82)) + .setTitle("Template Generated!") + .setThumbnail(NBS_IMAGE) + .addField("Information","You can copy the command above and give it to yourself in singleplayer. Use toolbars to transfer it to Diamondfire. You will need a [Music Player](https://derpystuff.gitlab.io/code/l?link=7cf5d91c35bbde31c28567d8d8945c40) function to play this song!", false); + + + channel.sendFile(templateOutputfile).setEmbeds(embed.build()).queue(); + file.deleteOnExit(); + } catch (OutdatedNBSException | IOException e) { + e.printStackTrace(); + event.reply(errorMsg); + } + }); } - private static String generateLink(String templateData) throws IOException { - URL url = new URL("https://twv.vercel.app/v2/create"); - URLConnection con = url.openConnection(); - HttpURLConnection http = (HttpURLConnection) con; - http.setRequestMethod("POST"); - http.setDoOutput(true); - byte[] out = templateData.getBytes(StandardCharsets.UTF_8); - int length = out.length; - http.setFixedLengthStreamingMode(length); - http.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); - http.setRequestProperty("Accept","application/json; charset=UTF-8"); - http.connect(); - try(OutputStream os = http.getOutputStream()) { - os.write(out); - } - Scanner s = new Scanner(http.getInputStream()).useDelimiter("\\A"); - String result = s.hasNext() ? s.next() : ""; - s.close(); - http.disconnect(); - - return result; - } } From 5024a58f82cf8ecdd54f1e9584e1b965fe4651e8 Mon Sep 17 00:00:00 2001 From: ProjectDF <61924259+ProjectDF@users.noreply.github.com> Date: Sat, 26 Mar 2022 19:43:21 -0500 Subject: [PATCH 17/17] Update NBSToTemplate.java --- .../com/diamondfire/helpbot/util/nbs/NBSToTemplate.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/diamondfire/helpbot/util/nbs/NBSToTemplate.java b/src/main/java/com/diamondfire/helpbot/util/nbs/NBSToTemplate.java index c5bf54e7..2dcc8375 100644 --- a/src/main/java/com/diamondfire/helpbot/util/nbs/NBSToTemplate.java +++ b/src/main/java/com/diamondfire/helpbot/util/nbs/NBSToTemplate.java @@ -1,6 +1,8 @@ package com.diamondfire.helpbot.util.nbs; +import java.io.IOException; import java.math.BigDecimal; +import java.nio.charset.StandardCharsets; // from https://github.com/CodeUtilities/CodeUtilities @@ -39,7 +41,7 @@ public NBSToTemplate(SongData song) { this.customInstrumentCount = song.getCustomInstrumentCount(); } - public String convert() { + public byte[] convert() throws IOException { String[] songData = song.split("="); StringBuilder currentNotes = new StringBuilder(); StringBuilder code = new StringBuilder(); @@ -154,6 +156,7 @@ public String convert() { //CreateList: songData code.append(String.format("{\"id\":\"block\",\"block\":\"set_var\",\"args\":{\"items\":[{\"item\":{\"id\":\"var\",\"data\":{\"name\":\"songData\",\"scope\":\"local\"}},\"slot\":0},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"%s\"}},\"slot\":1},{\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"%s\"}},\"slot\":2},{\"item\":{\"id\":\"num\",\"data\":{\"name\":\"%s\"}},\"slot\":3}, {\"item\":{\"id\":\"num\",\"data\":{\"name\":\"%d\"}},\"slot\":4}, {\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"%s\"}},\"slot\":5}, {\"item\":{\"id\":\"txt\",\"data\":{\"name\":\"%s\"}},\"slot\":6},{\"item\":{\"id\":\"num\",\"data\":{\"name\":\"%d\"}},\"slot\":7},{\"item\":{\"id\":\"num\",\"data\":{\"name\":\"%d\"}},\"slot\":8}]},\"action\":\"CreateList\"}", name, author, songTempo, length, layers, version, loopTick, loopCount)); - return "{\"blocks\": [" + code + "]}"; + String data = "{\"blocks\": [" + code + "]}"; + return CompressionUtil.toBase64(CompressionUtil.toGZIP(data.getBytes(StandardCharsets.UTF_8))); } }