1111import io .github .codeutilities .sys .player .chat .ChatType ;
1212import io .github .codeutilities .sys .player .chat .ChatUtil ;
1313import io .github .codeutilities .sys .util .ItemUtil ;
14- import java .awt .Dialog ;
15- import java .awt .FileDialog ;
16- import java .awt .Frame ;
14+ import net .fabricmc .fabric .api .client .command .v1 .FabricClientCommandSource ;
15+ import net .minecraft .client .MinecraftClient ;
16+ import net .minecraft .item .ItemStack ;
17+ import net .minecraft .item .Items ;
18+
19+ import java .awt .*;
1720import java .io .File ;
1821import java .io .IOException ;
1922import java .nio .charset .StandardCharsets ;
2023import java .util .ArrayList ;
2124import java .util .List ;
2225import java .util .Scanner ;
23- import net .fabricmc .fabric .api .client .command .v1 .FabricClientCommandSource ;
24- import net .minecraft .client .MinecraftClient ;
25- import net .minecraft .item .ItemStack ;
26- import net .minecraft .item .Items ;
2726
2827public class ImportFileCommand extends Command {
2928
3029 @ Override
3130 public void register (MinecraftClient mc , CommandDispatcher <FabricClientCommandSource > cd ) {
3231 cd .register (ArgBuilder .literal ("importfile" )
33- .executes (ctx -> {
34- if (!isCreative (mc )) return -1 ;
35-
36- ChatUtil .sendMessage ("Opening File Picker" , ChatType .INFO_BLUE );
37- CodeUtilities .EXECUTOR .submit (() -> {
38- try {
39- FileDialog fd = new FileDialog ((Dialog ) null , "Choose a text file" , FileDialog .LOAD );
40- fd .setMultipleMode (true );
41- fd .setVisible (true );
42- File [] files = fd .getFiles ();
43- fd .dispose ();
44- if (files == null || files .length ==0 ) {
45- ChatUtil .sendMessage ("You didnt choose a file!" ,ChatType .FAIL );
46- return ;
47- }
48-
49- int valid = 0 ;
50- files :
51- for (File f : files ) {
52- if (files .length != 1 ) ChatUtil .sendMessage ("Loading file: " + f .getName (),ChatType .INFO_BLUE );
53- Scanner sc = new Scanner (f );
32+ .executes (ctx -> {
33+ if (!isCreative (mc )) return -1 ;
34+
35+ ChatUtil .sendMessage ("Opening File Picker" , ChatType .INFO_BLUE );
36+ CodeUtilities .EXECUTOR .submit (() -> {
37+ try {
38+ FileDialog fd = new FileDialog ((Dialog ) null , "Choose a text file" , FileDialog .LOAD );
39+ fd .setMultipleMode (true );
40+ fd .setVisible (true );
41+ File [] files = fd .getFiles ();
42+ fd .dispose ();
43+ if (files == null || files .length == 0 ) {
44+ ChatUtil .sendMessage ("You didnt choose a file!" , ChatType .FAIL );
45+ return ;
46+ }
5447
55- List <String > lines = new ArrayList <>();
48+ int valid = 0 ;
49+ files :
50+ for (File f : files ) {
51+ if (files .length != 1 )
52+ ChatUtil .sendMessage ("Loading file: " + f .getName (), ChatType .INFO_BLUE );
53+ Scanner sc = new Scanner (f , "utf-8" );
54+
55+ List <String > lines = new ArrayList <>();
56+
57+ while (sc .hasNextLine ()) {
58+ String line = sc .nextLine ();
59+ if (line .length () > 2000 ) {
60+ ChatUtil .sendMessage ("Line " + (lines .size () + 1 ) + " is too long! (" + line .length () + " > 2000)" , ChatType .FAIL );
61+ continue files ;
62+ }
63+ lines .add (line );
64+ if (lines .size () > 10000 ) {
65+ ChatUtil .sendMessage ("File contains contains too many lines! (Max: 10000)" , ChatType .FAIL );
66+ continue files ;
67+ }
68+ }
5669
57- while (sc .hasNextLine ()) {
58- String line = sc .nextLine ();
59- if (line .length ()>2000 ) {
60- ChatUtil .sendMessage ("Line " + (lines .size ()+1 ) + " is too long! (" + line .length () + " > 2000)" ,ChatType .FAIL );
61- continue files ;
70+ List <JsonObject > blocks = new ArrayList <>();
71+ List <String > current = new ArrayList <>();
72+
73+ boolean first = true ;
74+ for (String line : lines ) {
75+ current .add (line );
76+ if (current .size () >= 26 ) {
77+ blocks .add (block (current , first ));
78+ first = false ;
79+ current = new ArrayList <>();
80+ }
6281 }
63- lines .add (line );
64- if (lines .size () > 10000 ) {
65- ChatUtil .sendMessage ("File contains contains too many lines! (Max: 10000)" ,ChatType .FAIL );
66- continue files ;
82+ if (current .size () != 0 ) blocks .add (block (current , first ));
83+
84+ String template = template (blocks );
85+ if (template .getBytes ().length > 65536 ) {//i have no idea what the actual limit is it just seems to be close to this
86+ ChatUtil .sendMessage ("Your file is too large!" , ChatType .FAIL );
87+ } else {
88+ ItemStack item = new ItemStack (Items .ENDER_CHEST );
89+ TemplateUtils .applyRawTemplateNBT (item , f .getName (), "CodeUtilities" , template );
90+ ItemUtil .giveCreativeItem (item , files .length == 1 );
91+ if (files .length != 1 ) Thread .sleep (500 );
92+ valid ++;
6793 }
6894 }
95+ if (files .length != 1 && valid > 0 )
96+ ChatUtil .sendMessage ("Loaded " + valid + " files!" , ChatType .SUCCESS );
6997
70- List <JsonObject > blocks = new ArrayList <>();
71- List <String > current = new ArrayList <>();
72-
73- boolean first = true ;
74- for (String line : lines ) {
75- current .add (line );
76- if (current .size ()>=26 ) {
77- blocks .add (block (current ,first ));
78- first = false ;
79- current = new ArrayList <>();
80- }
81- }
82- if (current .size () != 0 ) blocks .add (block (current ,first ));
83-
84- String template = template (blocks );
85- if (template .getBytes ().length > 65536 ) {//i have no idea what the actual limit is it just seems to be close to this
86- ChatUtil .sendMessage ("Your file is too large!" ,ChatType .FAIL );
87- } else {
88- ItemStack item = new ItemStack (Items .ENDER_CHEST );
89- TemplateUtils .applyRawTemplateNBT (item ,f .getName (),"CodeUtilities" ,template );
90- ItemUtil .giveCreativeItem (item ,files .length ==1 );
91- if (files .length !=1 ) Thread .sleep (500 );
92- valid ++;
93- }
98+ } catch (Exception err ) {
99+ err .printStackTrace ();
100+ ChatUtil .sendMessage ("Unexpected Error." , ChatType .FAIL );
94101 }
95- if (files .length !=1 && valid >0 ) ChatUtil .sendMessage ("Loaded " + valid + " files!" ,ChatType .SUCCESS );
96-
97- } catch (Exception err ) {
98- err .printStackTrace ();
99- ChatUtil .sendMessage ("Unexpected Error." ,ChatType .FAIL );
100- }
101- });
102- return 1 ;
103- })
102+ });
103+ return 1 ;
104+ })
104105 );
105106 }
106107
@@ -111,7 +112,7 @@ private String template(List<JsonObject> iblocks) throws IOException {
111112 blocks .add (block );
112113 }
113114 JsonObject root = new JsonObject ();
114- root .add ("blocks" ,blocks );
115+ root .add ("blocks" , blocks );
115116 byte [] b64 = CompressionUtil .toBase64 (CompressionUtil .toGZIP (root .toString ().getBytes (StandardCharsets .UTF_8 )));
116117 return new String (b64 );
117118 }
@@ -121,27 +122,27 @@ private JsonObject block(List<String> texts, boolean first) {
121122 JsonArray items = new JsonArray ();
122123 items .add (var );
123124 for (String text : texts ) {
124- items .add (textItem (text ,items .size ()));
125+ items .add (textItem (text , items .size ()));
125126 }
126127 JsonObject args = new JsonObject ();
127- args .add ("items" ,items );
128+ args .add ("items" , items );
128129 JsonObject root = new JsonObject ();
129- root .add ("args" ,args );
130- root .addProperty ("id" ,"block" );
131- root .addProperty ("block" ,"set_var" );
132- root .addProperty ("action" ,first ? "CreateList" : "AppendValue" );
130+ root .add ("args" , args );
131+ root .addProperty ("id" , "block" );
132+ root .addProperty ("block" , "set_var" );
133+ root .addProperty ("action" , first ? "CreateList" : "AppendValue" );
133134 return root ;
134135 }
135136
136- private JsonObject textItem (String text ,int slot ) {
137+ private JsonObject textItem (String text , int slot ) {
137138 JsonObject data = new JsonObject ();
138- data .addProperty ("name" ,text );
139+ data .addProperty ("name" , text );
139140 JsonObject item = new JsonObject ();
140- item .addProperty ("id" ,"txt" );
141- item .add ("data" ,data );
141+ item .addProperty ("id" , "txt" );
142+ item .add ("data" , data );
142143 JsonObject root = new JsonObject ();
143- root .add ("item" ,item );
144- root .addProperty ("slot" ,slot );
144+ root .add ("item" , item );
145+ root .addProperty ("slot" , slot );
145146 return root ;
146147 }
147148
0 commit comments