@@ -109,71 +109,30 @@ public boolean mouseClicked(double x, double y, int button) {
109109
110110 panel .add (enableDisable );
111111
112- if (s .getOwner () != null && s .getOwner ().equals (DFScript .PLAYER_UUID ) && allowEditAndUpload ) {
113- // Edit Button
114- CButton edit = new CTexturedButton (addedX , y + addedY , 8 , 8 , DFScript .MOD_ID + ":wrench.png" , () -> {
115- DFScript .MC .setScreen (new ScriptEditScreen (s ));
116- }, 0 ,0 ,1 ,0.5f ,0 ,0.5f );
117-
118- panel .add (edit );
119-
120- // Upload or Remove Button
121- CButton upload = new CTexturedButton (10 + addedX , y + addedY , 8 , 8 , DFScript .MOD_ID + ":upload.png" , () -> {
122- try {
123- // Encode the script JSON to GZIP Base64
124- byte [] bytes = Files .readAllBytes (s .getFile ().toPath ());
125-
126- ByteArrayOutputStream rstBao = new ByteArrayOutputStream ();
127- GZIPOutputStream zos = new GZIPOutputStream (rstBao );
128- zos .write (bytes );
129- zos .close ();
112+ if (allowEditAndUpload ) {
113+ if (s .getOwner () != null && s .getOwner ().equals (DFScript .PLAYER_UUID )) {
114+ // Edit Button
115+ CButton edit = new CTexturedButton (addedX , y + addedY , 8 , 8 , DFScript .MOD_ID + ":wrench.png" , () -> {
116+ DFScript .MC .setScreen (new ScriptEditScreen (s ));
117+ }, 0 , 0 , 1 , 0.5f , 0 , 0.5f );
130118
131- String scriptData = Base64 .encodeBase64String (rstBao .toByteArray ());
132-
133- // Upload the script to the server
134- URL url = new URL ("https://DFScript-Server.techstreetdev.repl.co/scripts/upload" );
135- HttpURLConnection con = (HttpURLConnection ) url .openConnection ();
136- con .setRequestMethod ("POST" );
137- con .setRequestProperty ("Content-Type" , "application/json" );
138- con .setRequestProperty ("Accept" , "application/json" );
139- con .setRequestProperty ("authorization" , AuthHandler .getAuthCode ());
140- con .setDoOutput (true );
141-
142- JsonObject obj = new JsonObject ();
143- obj .addProperty ("data" , scriptData );
144-
145- try (OutputStream os = con .getOutputStream ()) {
146- byte [] input = obj .toString ().getBytes ("utf-8" );
147- os .write (input , 0 , input .length );
148- }
119+ panel .add (edit );
149120
150- // Parse the response and get the scripts ID
151- try (BufferedReader br = new BufferedReader (new InputStreamReader (con .getInputStream (), "utf-8" ))) {
152- StringBuilder response = new StringBuilder ();
153- String responseLine ;
121+ // Upload or Remove Button
122+ CButton upload = new CTexturedButton (10 + addedX , y + addedY , 8 , 8 , DFScript .MOD_ID + ":upload.png" , () -> {
123+ try {
124+ // Encode the script JSON to GZIP Base64
125+ byte [] bytes = Files .readAllBytes (s .getFile ().toPath ());
154126
155- while ((responseLine = br .readLine ()) != null ) {
156- response .append (responseLine .trim ());
157- }
127+ ByteArrayOutputStream rstBao = new ByteArrayOutputStream ();
128+ GZIPOutputStream zos = new GZIPOutputStream (rstBao );
129+ zos .write (bytes );
130+ zos .close ();
158131
159- UploadResponse uploadResponse = DFScript .GSON .fromJson (response .toString (), UploadResponse .class );
160- System .out .println (uploadResponse .getId ());
161- s .setServer (uploadResponse .getId ());
132+ String scriptData = Base64 .encodeBase64String (rstBao .toByteArray ());
162133
163- ScriptManager .getInstance ().saveScript (s );
164- DFScript .MC .setScreen (new ScriptMessageScreen (new ScriptListScreen (allowEditAndUpload ), "Successfully uploaded the script to the server!" ));
165- }
166- } catch (Exception e ) {
167- DFScript .MC .setScreen (new ScriptMessageScreen (new ScriptListScreen (allowEditAndUpload ), "Failed to upload script to the server, please report this to a DFScript developer!" ));
168- e .printStackTrace ();
169- }
170- }, 0 ,0 ,1 ,0.5f ,0 ,0.5f );
171-
172- if (!Objects .equals (s .getServer (), "None" )) {
173- upload = new CTexturedButton (10 + addedX , y + addedY , 8 , 8 , DFScript .MOD_ID + ":unupload.png" , () -> {
174- try {
175- // Remove the script to the server
176- URL url = new URL ("https://DFScript-Server.techstreetdev.repl.co/scripts/remove/" );
134+ // Upload the script to the server
135+ URL url = new URL ("https://DFScript-Server.techstreetdev.repl.co/scripts/upload" );
177136 HttpURLConnection con = (HttpURLConnection ) url .openConnection ();
178137 con .setRequestMethod ("POST" );
179138 con .setRequestProperty ("Content-Type" , "application/json" );
@@ -182,34 +141,85 @@ public boolean mouseClicked(double x, double y, int button) {
182141 con .setDoOutput (true );
183142
184143 JsonObject obj = new JsonObject ();
185- obj .addProperty ("id " , s . getServer () );
144+ obj .addProperty ("data " , scriptData );
186145
187146 try (OutputStream os = con .getOutputStream ()) {
188147 byte [] input = obj .toString ().getBytes ("utf-8" );
189148 os .write (input , 0 , input .length );
190149 }
191150
192- try {
193- try (BufferedReader br = new BufferedReader (new InputStreamReader (con .getInputStream (), "utf-8" ))) {
194- s .setServer ("None" );
195- ScriptManager .getInstance ().saveScript (s );
196- DFScript .MC .setScreen (new ScriptMessageScreen (new ScriptListScreen (allowEditAndUpload ), "Successfully removed the script from the server!" ));
197- }
198- } catch (IOException e ) {
199- if (e .getMessage ().contains ("401" )) {
200- DFScript .MC .setScreen (new ScriptMessageScreen (new ScriptListScreen (allowEditAndUpload ), "You don't have permission to delete this script!" ));
201- } else {
202- e .printStackTrace ();
151+ // Parse the response and get the scripts ID
152+ try (BufferedReader br = new BufferedReader (new InputStreamReader (con .getInputStream (), "utf-8" ))) {
153+ StringBuilder response = new StringBuilder ();
154+ String responseLine ;
155+
156+ while ((responseLine = br .readLine ()) != null ) {
157+ response .append (responseLine .trim ());
203158 }
159+
160+ UploadResponse uploadResponse = DFScript .GSON .fromJson (response .toString (), UploadResponse .class );
161+ System .out .println (uploadResponse .getId ());
162+ s .setServer (uploadResponse .getId ());
163+
164+ ScriptManager .getInstance ().saveScript (s );
165+ DFScript .MC .setScreen (new ScriptMessageScreen (new ScriptListScreen (allowEditAndUpload ), "Successfully uploaded the script to the server!" ));
204166 }
205167 } catch (Exception e ) {
206- DFScript .MC .setScreen (new ScriptMessageScreen (new ScriptListScreen (allowEditAndUpload ), "Failed to remove the script from the server, please try again !" ));
168+ DFScript .MC .setScreen (new ScriptMessageScreen (new ScriptListScreen (allowEditAndUpload ), "Failed to upload script to the server, please report this to a DFScript developer !" ));
207169 e .printStackTrace ();
208170 }
209- }, 0 ,0 ,1 ,0.5f ,0 ,0.5f );
171+ }, 0 , 0 , 1 , 0.5f , 0 , 0.5f );
172+
173+ if (!Objects .equals (s .getServer (), "None" )) {
174+ upload = new CTexturedButton (10 + addedX , y + addedY , 8 , 8 , DFScript .MOD_ID + ":unupload.png" , () -> {
175+ try {
176+ // Remove the script to the server
177+ URL url = new URL ("https://DFScript-Server.techstreetdev.repl.co/scripts/remove/" );
178+ HttpURLConnection con = (HttpURLConnection ) url .openConnection ();
179+ con .setRequestMethod ("POST" );
180+ con .setRequestProperty ("Content-Type" , "application/json" );
181+ con .setRequestProperty ("Accept" , "application/json" );
182+ con .setRequestProperty ("authorization" , AuthHandler .getAuthCode ());
183+ con .setDoOutput (true );
184+
185+ JsonObject obj = new JsonObject ();
186+ obj .addProperty ("id" , s .getServer ());
187+
188+ try (OutputStream os = con .getOutputStream ()) {
189+ byte [] input = obj .toString ().getBytes ("utf-8" );
190+ os .write (input , 0 , input .length );
191+ }
192+
193+ try {
194+ try (BufferedReader br = new BufferedReader (new InputStreamReader (con .getInputStream (), "utf-8" ))) {
195+ s .setServer ("None" );
196+ ScriptManager .getInstance ().saveScript (s );
197+ DFScript .MC .setScreen (new ScriptMessageScreen (new ScriptListScreen (allowEditAndUpload ), "Successfully removed the script from the server!" ));
198+ }
199+ } catch (IOException e ) {
200+ if (e .getMessage ().contains ("401" )) {
201+ DFScript .MC .setScreen (new ScriptMessageScreen (new ScriptListScreen (allowEditAndUpload ), "You don't have permission to delete this script!" ));
202+ } else {
203+ e .printStackTrace ();
204+ }
205+ }
206+ } catch (Exception e ) {
207+ DFScript .MC .setScreen (new ScriptMessageScreen (new ScriptListScreen (allowEditAndUpload ), "Failed to remove the script from the server, please try again!" ));
208+ e .printStackTrace ();
209+ }
210+ }, 0 , 0 , 1 , 0.5f , 0 , 0.5f );
211+ }
212+
213+ panel .add (upload );
210214 }
215+ else {
216+ //Script Settings Button
217+ CButton settings = new CTexturedButton (10 + addedX , y + addedY , 8 , 8 , DFScript .MOD_ID + ":wrench.png" , () -> {
218+ DFScript .MC .setScreen (new ScriptSettingsScreen (s , false ));
219+ }, 0 , 0 , 1 , 0.5f , 0 , 0.5f );
211220
212- panel .add (upload );
221+ panel .add (settings );
222+ }
213223 }
214224
215225 y += 12 ;
0 commit comments