2424
2525import io .github .axolotlclient .AxolotlClient ;
2626import io .github .axolotlclient .AxolotlclientConfig .options .BooleanOption ;
27+ import io .github .axolotlclient .AxolotlclientConfig .options .EnumOption ;
2728import io .github .axolotlclient .AxolotlclientConfig .options .OptionCategory ;
2829import io .github .axolotlclient .modules .AbstractModule ;
2930import io .github .axolotlclient .util .Logger ;
30- import io . github . axolotlclient . util . Util ;
31+ import lombok . AllArgsConstructor ;
3132import net .minecraft .client .resource .language .I18n ;
3233import net .minecraft .text .*;
3334import net .minecraft .util .Formatting ;
35+ import net .minecraft .util .Util ;
36+ import org .jetbrains .annotations .Nullable ;
3437
3538import java .awt .*;
3639import java .awt .datatransfer .DataFlavor ;
3740import java .awt .datatransfer .Transferable ;
3841import java .io .File ;
3942import java .nio .file .Files ;
4043import java .util .ArrayList ;
44+ import java .util .List ;
4145
4246public class ScreenshotUtils extends AbstractModule {
4347 private static final ScreenshotUtils Instance = new ScreenshotUtils ();
@@ -46,10 +50,58 @@ public class ScreenshotUtils extends AbstractModule {
4650
4751 private final BooleanOption enabled = new BooleanOption ("enabled" , false );
4852
53+ private final List <Action > actions = Util .make (() -> {
54+ List <Action > actions = new ArrayList <>();
55+ actions .add (new Action ("copyAction" ,
56+ Formatting .AQUA ,
57+ new HoverEvent (HoverEvent .Action .SHOW_TEXT ,
58+ Text .translatable ("copy_image" )),
59+ new CustomClickEvent ((file ) -> {
60+ FileTransferable selection = new FileTransferable (file );
61+ Toolkit .getDefaultToolkit ().getSystemClipboard ().setContents (selection , null );
62+ })
63+ ));
64+
65+ actions .add (new Action ("deleteAction" ,
66+ Formatting .LIGHT_PURPLE ,
67+ new HoverEvent (HoverEvent .Action .SHOW_TEXT ,
68+ Text .translatable ("delete_image" )),
69+ new CustomClickEvent ((file ) -> {
70+ try {
71+ Files .delete (file .toPath ());
72+ io .github .axolotlclient .util .Util .sendChatMessage (
73+ Text .literal (I18n .translate ("screenshot_deleted" )
74+ .replace ("<name>" , file .getName ())));
75+ } catch (Exception e ) {
76+ Logger .warn ("Couldn't delete Screenshot " + file .getName ());
77+ }
78+ })
79+ ));
80+
81+ actions .add (new Action ("openAction" ,
82+ Formatting .WHITE ,
83+ new HoverEvent (HoverEvent .Action .SHOW_TEXT ,
84+ Text .translatable ("open_image" )),
85+ new CustomClickEvent ((file ) -> Util .getOperatingSystem ().open (file .toURI ()))
86+ ));
87+
88+ // If you have further ideas to what actions could be added here, please let us know!
89+
90+ return actions ;
91+
92+ });
93+
94+ private final EnumOption autoExec = new EnumOption ("autoExec" , Util .make (() -> {
95+ List <String > names = new ArrayList <>();
96+ names .add ("off" );
97+ actions .forEach (action -> names .add (action .getName ()));
98+ return names .toArray (new String [0 ]);
99+
100+ }), "off" );
101+
49102 @ Override
50103 public void init () {
51-
52- category .add (enabled );
104+ category .add (enabled , autoExec );
53105
54106 AxolotlClient .CONFIG .general .addSubCategory (category );
55107 }
@@ -59,13 +111,32 @@ public static ScreenshotUtils getInstance(){
59111 }
60112
61113 public Text onScreenshotTaken (MutableText text , File shot ){
62- if (enabled .get ()){
63- return text .append ("\n " ).append (getUtilsText (shot ));
114+ if (enabled .get ()) {
115+ Text t = getUtilsText (shot );
116+ if (t != null ) {
117+ return text .append ("\n " ).append (t );
118+ }
64119 }
65120 return text ;
66121 }
67122
68- private Text getUtilsText (File file ){
123+ private @ Nullable Text getUtilsText (File file ) {
124+
125+ if (!autoExec .get ().equals ("off" )) {
126+
127+ actions .parallelStream ().filter (action -> autoExec .get ().equals (action .getName ())).toList ().get (0 ).clickEvent .setFile (file ).doAction ();
128+ return null ;
129+ }
130+
131+ MutableText message = Text .empty ().copy ();
132+ actions .parallelStream ().map (action -> action .getText (file )).iterator ().forEachRemaining (text -> {
133+ message .append (text );
134+ message .append (" " );
135+ });
136+ return message ;
137+ }
138+
139+ /*private Text getUtilsText(File file){
69140
70141 return Text.translatable("copyAction")
71142 .setStyle(Style.EMPTY
@@ -93,14 +164,33 @@ private Text getUtilsText(File file){
93164 }
94165 })))
95166 );
167+ }*/
168+
169+ @ AllArgsConstructor
170+ public static class Action {
171+
172+ private final String translationKey ;
173+ private final Formatting formatting ;
174+ private final HoverEvent hoverEvent ;
175+ private final CustomClickEvent clickEvent ;
176+
177+ public Text getText (File file ) {
178+ return Text .translatable (translationKey )
179+ .setStyle (Style .EMPTY
180+ .withFormatting (formatting )
181+ .withClickEvent (clickEvent .setFile (file ))
182+ .withHoverEvent (hoverEvent ));
183+ }
184+
185+ public String getName () {
186+ return translationKey ;
187+ }
96188 }
97189
190+ @ AllArgsConstructor
98191 protected static class FileTransferable implements Transferable {
99- private final File file ;
100192
101- public FileTransferable (File file ) {
102- this .file = file ;
103- }
193+ private final File file ;
104194
105195 @ Override
106196 public DataFlavor [] getTransferDataFlavors () {
@@ -123,18 +213,30 @@ public Object getTransferData(DataFlavor flavor) {
123213 public static class CustomClickEvent extends ClickEvent {
124214
125215 private final OnActionCall action ;
216+ private File file ;
126217
127218 public CustomClickEvent (OnActionCall action ) {
128219 super (Action .byName ("" ), "" );
129220 this .action = action ;
130221 }
131222
132- public void doAction (){
133- action .doAction ();
223+ public void doAction () {
224+ if (file != null ) {
225+ action .doAction (file );
226+ } else {
227+ Logger .warn ("How'd you manage to do this? " +
228+ "Now there's a screenshot ClickEvent without a File attached to it!" );
229+ }
230+ }
231+
232+ public CustomClickEvent setFile (File file ) {
233+ this .file = file ;
234+ return this ;
134235 }
135236 }
136237
137238 interface OnActionCall {
138- void doAction ();
239+
240+ void doAction (File file );
139241 }
140242}
0 commit comments