33import com .cleanroommc .groovyscript .api .infocommand .InfoParserPackage ;
44import com .cleanroommc .groovyscript .compat .mods .ModSupport ;
55import com .cleanroommc .groovyscript .compat .mods .jei .JeiPlugin ;
6- import com .cleanroommc .groovyscript .helper .RayTracingHelper ;
76import com .cleanroommc .groovyscript .helper .StyleConstant ;
87import com .google .common .collect .ImmutableList ;
98import mezz .jei .api .IRecipesGui ;
109import net .minecraft .client .Minecraft ;
1110import net .minecraft .client .gui .inventory .GuiContainer ;
1211import net .minecraft .entity .player .EntityPlayer ;
1312import net .minecraft .item .ItemStack ;
14- import net .minecraft .util .EnumHand ;
1513import net .minecraft .util .text .ITextComponent ;
1614import net .minecraft .util .text .TextComponentString ;
1715import net .minecraftforge .client .settings .KeyModifier ;
2321public class CopyKey extends GroovyScriptKeybinds .Key {
2422
2523 private static final Minecraft mc = Minecraft .getMinecraft ();
24+ private static final List <String > ARGS = ImmutableList .of ("all" );
2625
2726 public CopyKey () {
2827 super ("copy" , KeyModifier .CONTROL , Keyboard .KEY_C );
@@ -34,22 +33,21 @@ public static CopyKey createKeybind() {
3433
3534 private static void gatherInfo (InfoParserPackage info , EntityPlayer player ) {
3635 if (mc .inGameHasFocus ) {
37- info .setStack (player .getHeldItem (EnumHand .MAIN_HAND ));
38- if (info .getStack ().isEmpty ()) info .setStack (player .getHeldItem (EnumHand .OFF_HAND ));
39- if (info .getStack ().isEmpty ()) {
40- info .setEntity (RayTracingHelper .getEntityLookingAt (player ));
41- if (info .getEntity () == null ) {
42- info .copyFromPos (RayTracingHelper .getBlockLookingAt (player ));
43- if (info .getPos () == null ) {
44- info .setEntity (player );
45- }
46- }
47- }
36+ info .copyFromPlayer (player );
4837 } else {
49- if (mc .currentScreen instanceof GuiContainer container && container .hoveredSlot != null ) {
50- info .setStack (container .hoveredSlot .getStack ());
38+ var jei = ModSupport .JEI .isLoaded ();
39+ if (mc .currentScreen instanceof GuiContainer container ) {
40+ var slot = container .getSlotUnderMouse ();
41+ if (slot != null ) {
42+ info .setStack (slot .getStack ());
43+ } else if (jei && info .getStack ().isEmpty ()) {
44+ // check sidebars of normal guis
45+ info .setStack (getJeiStack ());
46+ }
47+ } else if (jei && getJeiRecipesObject () != null ) {
48+ // have to check this separately for if IRecipesGui is open, since its GuiScreen not GuiContainer
49+ info .setStack (getJeiStack ());
5150 }
52- if (info .getStack ().isEmpty () && ModSupport .JEI .isLoaded ()) info .setStack (getJeiStack ());
5351 }
5452 }
5553
@@ -61,15 +59,20 @@ private static ItemStack getJeiStack() {
6159 }
6260
6361 private static Object getJeiObject () {
64- if (mc .currentScreen instanceof IRecipesGui gui ) {
65- var entry = gui .getIngredientUnderMouse ();
66- if (entry != null ) return entry ;
67- }
68- var entry = JeiPlugin .jeiRuntime .getBookmarkOverlay ().getIngredientUnderMouse ();
62+ var entry = getJeiRecipesObject ();
63+ if (entry != null ) return entry ;
64+ entry = JeiPlugin .jeiRuntime .getBookmarkOverlay ().getIngredientUnderMouse ();
6965 if (entry != null ) return entry ;
7066 return JeiPlugin .jeiRuntime .getIngredientListOverlay ().getIngredientUnderMouse ();
7167 }
7268
69+ private static Object getJeiRecipesObject () {
70+ if (mc .currentScreen instanceof IRecipesGui gui ) {
71+ return gui .getIngredientUnderMouse ();
72+ }
73+ return null ;
74+ }
75+
7376 private static void print (EntityPlayer player , List <ITextComponent > messages ) {
7477 if (messages .isEmpty ()) {
7578 player .sendMessage (new TextComponentString ("Couldn't find anything being focused!" ).setStyle (StyleConstant .getErrorStyle ()));
@@ -80,18 +83,22 @@ private static void print(EntityPlayer player, List<ITextComponent> messages) {
8083 }
8184 }
8285
86+ public static boolean isCapturingKeyboard () {
87+ return mc .currentScreen != null && mc .currentScreen .isFocused ();
88+ }
89+
8390 @ Override
8491 public boolean isValid () {
85- return mc .isIntegratedServerRunning ();
92+ return mc .player != null && ! isCapturingKeyboard ();
8693 }
8794
88- // only runs if isIntegratedServerRunning() is true, so getIntegratedServer() cannot be null
89- @ SuppressWarnings ("DataFlowIssue" )
9095 @ Override
9196 public void runOperation () {
9297 var player = mc .player ;
9398 List <ITextComponent > messages = new ArrayList <>();
94- InfoParserPackage info = new InfoParserPackage (mc .getIntegratedServer (), player , ImmutableList .of ("all" ), messages , false );
99+ var server = mc .isIntegratedServerRunning () ? mc .getIntegratedServer () : player .getServer ();
100+ if (server == null ) return ; // unsure how this would happen, should be mutually exclusive
101+ InfoParserPackage info = new InfoParserPackage (server , player , ARGS , messages , false );
95102 gatherInfo (info , player );
96103 info .parse (true );
97104 print (player , messages );
0 commit comments