Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)));
}
}