88import com .diamondfire .helpbot .bot .command .reply .feature .informative .*;
99import com .diamondfire .helpbot .bot .events .CommandEvent ;
1010import com .diamondfire .helpbot .util .nbs .*;
11- import com .google .gson .*;
1211import net .dv8tion .jda .api .EmbedBuilder ;
1312import net .dv8tion .jda .api .entities .*;
1413
1514import java .awt .*;
1615import java .io .*;
17- import java .net .*;
18- import java .nio .charset .StandardCharsets ;
19- import java .util .Scanner ;
2016
2117public class NbsCommand extends Command {
2218
19+ private static final String NBS_IMAGE = "https://static.wikia.nocookie.net/minecraft/images/9/9b/Note_Block.png/revision/latest?cb=20190921170620" ;
20+
2321 @ Override
2422 public String getName () {
2523 return "nbs" ;
@@ -33,78 +31,59 @@ public HelpContext getHelpContext() {
3331 }
3432
3533 @ Override
36- protected ArgumentSet compileArguments () { return new ArgumentSet (); }
34+ protected ArgumentSet compileArguments () {
35+ return new ArgumentSet ();
36+ }
3737
3838 @ Override
39- public Permission getPermission () { return Permission .USER ; }
39+ public Permission getPermission () {
40+ return Permission .USER ;
41+ }
4042
4143 @ Override
4244 public void run (CommandEvent event ) {
4345 TextChannel channel = event .getChannel ();
44- PresetBuilder nbsPreset = new PresetBuilder ()
45- .withPreset (new InformativeReply (InformativeReplyType .ERROR ,"You need to attach an nbs file!" ));
46- PresetBuilder error = new PresetBuilder ()
47- .withPreset (new InformativeReply (InformativeReplyType .ERROR ,"Something went wrong while processing/generating!" ));
46+ PresetBuilder attachNbsMsg = new PresetBuilder ().withPreset (new InformativeReply (InformativeReplyType .ERROR ,"You need to attach an nbs file!" ));
47+ PresetBuilder errorMsg = new PresetBuilder ().withPreset (new InformativeReply (InformativeReplyType .ERROR ,"Something went wrong while generating!" ));
4848
49- if (event .getMessage ().getAttachments ().isEmpty ()) {
50- event .reply (nbsPreset );
49+ if (event .getMessage ().getAttachments ().isEmpty ()) {
50+ event .reply (attachNbsMsg );
5151 return ;
5252 }
53+
5354 Message .Attachment attachment = event .getMessage ().getAttachments ().get (0 );
54- if (!attachment .getFileExtension ().equals ("nbs" )) {
55- event .reply (nbsPreset );
55+ if (!attachment .getFileExtension ().equals ("nbs" )) {
56+ event .reply (attachNbsMsg );
5657 return ;
5758 }
5859
59- try {
60- File file = new File ("input.nbs" );
61- attachment .downloadToFile (file ).thenAccept ((downloadedFile ) -> {
62- try {
63- String code = new NBSToTemplate (NBSDecoder .parse (file )).convert ();
64- byte [] b64 = CompressionUtil .toBase64 (CompressionUtil .toGZIP (code .getBytes (StandardCharsets .UTF_8 )));
65- String templateJson = String .format ("{\" template\" : \" %s\" ,\" temp\" : true}" ,new String (b64 ));
66- JsonObject json = new Gson ().fromJson (generateLink (templateJson ),JsonObject .class );
67-
68- EmbedBuilder embed = new EmbedBuilder ()
69- .setColor (new Color (70 ,199 ,82 ))
70- .setTitle ("Template Generated!" )
71- .setThumbnail ("https://static.wikia.nocookie.net/minecraft/images/9/9b/Note_Block.png/revision/latest?cb=20190921170620" )
72- .addField ("Link: __Expires in 2 minutes__" ,"[Template Link](https://derpystuff.gitlab.io/code/l?link=" + json .get ("link" ).getAsString () + ")" ,false )
73- .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 );
74-
75- channel .sendMessageEmbeds (embed .build ()).queue ();
76- } catch (OutdatedNBSException | IOException e ) {
77- e .printStackTrace ();
78- event .reply (error );
79- }
80- });
81- } catch (Exception e ) {
82- e .printStackTrace ();
83- event .reply (error );
84- }
60+ File file = new File ("input.nbs" );
8561
62+
63+ attachment .downloadToFile (file ).thenAccept (downloadedFile -> {
64+ try {
65+ byte [] b64 = new NBSToTemplate (NBSDecoder .parse (file )).convert ();
66+ File templateOutputfile = File .createTempFile ("nbs_output" , ".txt" );
67+ BufferedWriter writer = new BufferedWriter (new FileWriter (templateOutputfile ));
68+ 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 )));
69+
70+ writer .close ();
71+
72+
73+ EmbedBuilder embed = new EmbedBuilder ()
74+ .setColor (new Color (70 ,199 ,82 ))
75+ .setTitle ("Template Generated!" )
76+ .setThumbnail (NBS_IMAGE )
77+ .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 );
78+
79+
80+ channel .sendFile (templateOutputfile ).setEmbeds (embed .build ()).queue ();
81+ file .deleteOnExit ();
82+ } catch (OutdatedNBSException | IOException e ) {
83+ e .printStackTrace ();
84+ event .reply (errorMsg );
85+ }
86+ });
8687 }
8788
88- private static String generateLink (String templateData ) throws IOException {
89- URL url = new URL ("https://twv.vercel.app/v2/create" );
90- URLConnection con = url .openConnection ();
91- HttpURLConnection http = (HttpURLConnection ) con ;
92- http .setRequestMethod ("POST" );
93- http .setDoOutput (true );
94- byte [] out = templateData .getBytes (StandardCharsets .UTF_8 );
95- int length = out .length ;
96- http .setFixedLengthStreamingMode (length );
97- http .setRequestProperty ("Content-Type" , "application/json; charset=UTF-8" );
98- http .setRequestProperty ("Accept" ,"application/json; charset=UTF-8" );
99- http .connect ();
100- try (OutputStream os = http .getOutputStream ()) {
101- os .write (out );
102- }
103- Scanner s = new Scanner (http .getInputStream ()).useDelimiter ("\\ A" );
104- String result = s .hasNext () ? s .next () : "" ;
105- s .close ();
106- http .disconnect ();
107-
108- return result ;
109- }
11089}
0 commit comments