diff --git a/src/main/java/com/cleanroommc/groovyscript/api/infocommand/InfoParserPackage.java b/src/main/java/com/cleanroommc/groovyscript/api/infocommand/InfoParserPackage.java index 4ca760f4d..94128006f 100644 --- a/src/main/java/com/cleanroommc/groovyscript/api/infocommand/InfoParserPackage.java +++ b/src/main/java/com/cleanroommc/groovyscript/api/infocommand/InfoParserPackage.java @@ -1,16 +1,18 @@ package com.cleanroommc.groovyscript.api.infocommand; import com.cleanroommc.groovyscript.event.GsHandEvent; +import com.cleanroommc.groovyscript.helper.RayTracingHelper; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; -import net.minecraft.client.Minecraft; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; import net.minecraft.server.MinecraftServer; import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.EnumHand; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.RayTraceResult; import net.minecraft.util.text.ITextComponent; import net.minecraftforge.common.MinecraftForge; import org.jetbrains.annotations.NotNull; @@ -156,15 +158,36 @@ public void setTileEntity(@Nullable TileEntity tileEntity) { this.tileEntity = tileEntity; } - public void copyFromPos(BlockPos pos) { - if (pos == null) return; - this.pos = pos; + public void copyFromPlayer(@NotNull EntityPlayer player) { + // mainhand -> offhand -> entity being looked at -> block being looked at -> self + var stack = player.getHeldItem(EnumHand.MAIN_HAND); + if (stack.isEmpty()) stack = player.getHeldItem(EnumHand.OFF_HAND); + if (stack.isEmpty()) { + var entity = RayTracingHelper.getEntityLookingAt(player); + if (entity == null) { + var rayTrace = RayTracingHelper.getBlockLookingAt(player); + if (rayTrace == null) { + setEntity(player); + } else { + copyFromPos(rayTrace); + } + } else { + setEntity(entity); + } + } else { + setStack(stack); + } + } + + public void copyFromPos(RayTraceResult rayTrace) { + if (rayTrace == null) return; + this.pos = rayTrace.getBlockPos(); this.blockState = player.world.getBlockState(pos); this.block = blockState.getBlock(); this.tileEntity = player.world.getTileEntity(pos); - this.stack = block.getPickBlock(blockState, Minecraft.getMinecraft().objectMouseOver, player.world, pos, player); - if (this.stack.isEmpty()) this.stack = new ItemStack(block, 1, block.getMetaFromState(blockState)); + stack = block.getPickBlock(blockState, rayTrace, player.world, pos, player); + if (stack.isEmpty()) stack = new ItemStack(block, 1, block.getMetaFromState(blockState)); } public void parse() { diff --git a/src/main/java/com/cleanroommc/groovyscript/command/InfoInfoCommand.java b/src/main/java/com/cleanroommc/groovyscript/command/InfoInfoCommand.java index 2f2d8b7ad..9eda6c01f 100644 --- a/src/main/java/com/cleanroommc/groovyscript/command/InfoInfoCommand.java +++ b/src/main/java/com/cleanroommc/groovyscript/command/InfoInfoCommand.java @@ -1,9 +1,7 @@ package com.cleanroommc.groovyscript.command; import com.cleanroommc.groovyscript.api.infocommand.InfoParserPackage; -import com.cleanroommc.groovyscript.helper.RayTracingHelper; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.util.EnumHand; import org.jetbrains.annotations.NotNull; public class InfoInfoCommand extends BaseInfoCommand { @@ -20,19 +18,6 @@ protected String targetDescription() { @Override void gatherInfo(InfoParserPackage info, EntityPlayer player) { - info.setStack(player.getHeldItem(EnumHand.MAIN_HAND)); - if (info.getStack().isEmpty()) info.setStack(player.getHeldItem(EnumHand.OFF_HAND)); - - // if there's nothing in the player's hands, get the entity being looked at and then the block position - // because entity should be preferred - if (info.getStack().isEmpty()) { - info.setEntity(RayTracingHelper.getEntityLookingAt(player)); - if (info.getEntity() == null) { - info.copyFromPos(RayTracingHelper.getBlockLookingAt(player)); - if (info.getPos() == null) { - info.setEntity(player); - } - } - } + info.copyFromPlayer(player); } } diff --git a/src/main/java/com/cleanroommc/groovyscript/helper/RayTracingHelper.java b/src/main/java/com/cleanroommc/groovyscript/helper/RayTracingHelper.java index f6abdee96..75a95bd7c 100644 --- a/src/main/java/com/cleanroommc/groovyscript/helper/RayTracingHelper.java +++ b/src/main/java/com/cleanroommc/groovyscript/helper/RayTracingHelper.java @@ -5,7 +5,6 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.EntitySelectors; import net.minecraft.util.math.AxisAlignedBB; -import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.RayTraceResult; import net.minecraft.util.math.Vec3d; @@ -16,7 +15,7 @@ public class RayTracingHelper { /** * gets the block being looked at, stopping on fluid blocks */ - public static BlockPos getBlockLookingAt(EntityPlayer player) { + public static RayTraceResult getBlockLookingAt(EntityPlayer player) { double distance = player.getEntityAttribute(EntityPlayer.REACH_DISTANCE).getAttributeValue(); Vec3d eyes = player.getPositionEyes(0.0F); Vec3d look = player.getLook(0.0F); @@ -24,7 +23,7 @@ public static BlockPos getBlockLookingAt(EntityPlayer player) { RayTraceResult result = player.getEntityWorld().rayTraceBlocks(eyes, end, true); if (result != null && result.typeOfHit == RayTraceResult.Type.BLOCK) { - return result.getBlockPos(); + return result; } return null; } diff --git a/src/main/java/com/cleanroommc/groovyscript/keybinds/CopyKey.java b/src/main/java/com/cleanroommc/groovyscript/keybinds/CopyKey.java index 93a7b4e44..b1a4c7ff3 100644 --- a/src/main/java/com/cleanroommc/groovyscript/keybinds/CopyKey.java +++ b/src/main/java/com/cleanroommc/groovyscript/keybinds/CopyKey.java @@ -3,7 +3,6 @@ import com.cleanroommc.groovyscript.api.infocommand.InfoParserPackage; import com.cleanroommc.groovyscript.compat.mods.ModSupport; import com.cleanroommc.groovyscript.compat.mods.jei.JeiPlugin; -import com.cleanroommc.groovyscript.helper.RayTracingHelper; import com.cleanroommc.groovyscript.helper.StyleConstant; import com.google.common.collect.ImmutableList; import mezz.jei.api.IRecipesGui; @@ -11,7 +10,6 @@ import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; -import net.minecraft.util.EnumHand; import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.TextComponentString; import net.minecraftforge.client.settings.KeyModifier; @@ -23,6 +21,7 @@ public class CopyKey extends GroovyScriptKeybinds.Key { private static final Minecraft mc = Minecraft.getMinecraft(); + private static final List ARGS = ImmutableList.of("all"); public CopyKey() { super("copy", KeyModifier.CONTROL, Keyboard.KEY_C); @@ -34,22 +33,21 @@ public static CopyKey createKeybind() { private static void gatherInfo(InfoParserPackage info, EntityPlayer player) { if (mc.inGameHasFocus) { - info.setStack(player.getHeldItem(EnumHand.MAIN_HAND)); - if (info.getStack().isEmpty()) info.setStack(player.getHeldItem(EnumHand.OFF_HAND)); - if (info.getStack().isEmpty()) { - info.setEntity(RayTracingHelper.getEntityLookingAt(player)); - if (info.getEntity() == null) { - info.copyFromPos(RayTracingHelper.getBlockLookingAt(player)); - if (info.getPos() == null) { - info.setEntity(player); - } - } - } + info.copyFromPlayer(player); } else { - if (mc.currentScreen instanceof GuiContainer container && container.hoveredSlot != null) { - info.setStack(container.hoveredSlot.getStack()); + var jei = ModSupport.JEI.isLoaded(); + if (mc.currentScreen instanceof GuiContainer container) { + var slot = container.getSlotUnderMouse(); + if (slot != null) { + info.setStack(slot.getStack()); + } else if (jei && info.getStack().isEmpty()) { + // check sidebars of normal guis + info.setStack(getJeiStack()); + } + } else if (jei && getJeiRecipesObject() != null) { + // have to check this separately for if IRecipesGui is open, since its GuiScreen not GuiContainer + info.setStack(getJeiStack()); } - if (info.getStack().isEmpty() && ModSupport.JEI.isLoaded()) info.setStack(getJeiStack()); } } @@ -61,15 +59,20 @@ private static ItemStack getJeiStack() { } private static Object getJeiObject() { - if (mc.currentScreen instanceof IRecipesGui gui) { - var entry = gui.getIngredientUnderMouse(); - if (entry != null) return entry; - } - var entry = JeiPlugin.jeiRuntime.getBookmarkOverlay().getIngredientUnderMouse(); + var entry = getJeiRecipesObject(); + if (entry != null) return entry; + entry = JeiPlugin.jeiRuntime.getBookmarkOverlay().getIngredientUnderMouse(); if (entry != null) return entry; return JeiPlugin.jeiRuntime.getIngredientListOverlay().getIngredientUnderMouse(); } + private static Object getJeiRecipesObject() { + if (mc.currentScreen instanceof IRecipesGui gui) { + return gui.getIngredientUnderMouse(); + } + return null; + } + private static void print(EntityPlayer player, List messages) { if (messages.isEmpty()) { player.sendMessage(new TextComponentString("Couldn't find anything being focused!").setStyle(StyleConstant.getErrorStyle())); @@ -80,18 +83,22 @@ private static void print(EntityPlayer player, List messages) { } } + public static boolean isCapturingKeyboard() { + return mc.currentScreen != null && mc.currentScreen.isFocused(); + } + @Override public boolean isValid() { - return mc.isIntegratedServerRunning(); + return mc.player != null && !isCapturingKeyboard(); } - // only runs if isIntegratedServerRunning() is true, so getIntegratedServer() cannot be null - @SuppressWarnings("DataFlowIssue") @Override public void runOperation() { var player = mc.player; List messages = new ArrayList<>(); - InfoParserPackage info = new InfoParserPackage(mc.getIntegratedServer(), player, ImmutableList.of("all"), messages, false); + var server = mc.isIntegratedServerRunning() ? mc.getIntegratedServer() : player.getServer(); + if (server == null) return; // unsure how this would happen, should be mutually exclusive + InfoParserPackage info = new InfoParserPackage(server, player, ARGS, messages, false); gatherInfo(info, player); info.parse(true); print(player, messages);