diff --git a/src/main/java/mcjty/theoneprobe/apiimpl/providers/ChestInfoTools.java b/src/main/java/mcjty/theoneprobe/apiimpl/providers/ChestInfoTools.java index e005a9fe..a1304d6d 100644 --- a/src/main/java/mcjty/theoneprobe/apiimpl/providers/ChestInfoTools.java +++ b/src/main/java/mcjty/theoneprobe/apiimpl/providers/ChestInfoTools.java @@ -6,12 +6,15 @@ import mcjty.theoneprobe.apiimpl.styles.LayoutStyle; import mcjty.theoneprobe.config.Config; import net.minecraft.core.BlockPos; +import net.minecraft.resources.ResourceLocation; import net.minecraft.world.Container; +import net.minecraft.world.entity.Entity; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.entity.BlockEntity; -import net.minecraftforge.items.CapabilityItemHandler; +import net.minecraftforge.common.capabilities.ForgeCapabilities; +import net.minecraftforge.common.capabilities.ICapabilityProvider; import net.minecraftforge.items.ItemHandlerHelper; import net.minecraftforge.registries.ForgeRegistries; @@ -25,20 +28,31 @@ public class ChestInfoTools { static void showChestInfo(ProbeMode mode, IProbeInfo probeInfo, Level world, BlockPos pos, IProbeConfig config) { + ResourceLocation registryKey = ForgeRegistries.BLOCKS.getKey(world.getBlockState(pos).getBlock()); + BlockEntity te = world.getBlockEntity(pos); + showChestInfo(mode, probeInfo, registryKey, te, config); + } + + static void showChestInfo(ProbeMode mode, IProbeInfo probeInfo, Entity entity, IProbeConfig config) { + ResourceLocation registryKey = ForgeRegistries.ENTITY_TYPES.getKey(entity.getType()); + showChestInfo(mode, probeInfo, registryKey, entity, config); + } + + private static void showChestInfo(ProbeMode mode, IProbeInfo probeInfo, ResourceLocation registryKey, ICapabilityProvider entity, IProbeConfig config) { List stacks = null; IProbeConfig.ConfigMode chestMode = config.getShowChestContents(); if (chestMode == IProbeConfig.ConfigMode.EXTENDED && (Config.showSmallChestContentsWithoutSneaking.get() > 0 || !Config.getInventoriesToShow().isEmpty())) { - if (Config.getInventoriesToShow().contains(ForgeRegistries.BLOCKS.getKey(world.getBlockState(pos).getBlock()))) { + if (Config.getInventoriesToShow().contains(registryKey)) { chestMode = IProbeConfig.ConfigMode.NORMAL; } else if (Config.showSmallChestContentsWithoutSneaking.get() > 0) { stacks = new ArrayList<>(); - int slots = getChestContents(world, pos, stacks); + int slots = getChestContents(entity, registryKey, stacks); if (slots <= Config.showSmallChestContentsWithoutSneaking.get()) { chestMode = IProbeConfig.ConfigMode.NORMAL; } } } else if (chestMode == IProbeConfig.ConfigMode.NORMAL && !Config.getInventoriesToNotShow().isEmpty()) { - if (Config.getInventoriesToNotShow().contains(ForgeRegistries.BLOCKS.getKey(world.getBlockState(pos).getBlock()))) { + if (Config.getInventoriesToNotShow().contains(registryKey)) { chestMode = IProbeConfig.ConfigMode.EXTENDED; } } @@ -46,12 +60,12 @@ static void showChestInfo(ProbeMode mode, IProbeInfo probeInfo, Level world, Blo if (Tools.show(mode, chestMode)) { if (stacks == null) { stacks = new ArrayList<>(); - getChestContents(world, pos, stacks); + getChestContents(entity, registryKey, stacks); } if (!stacks.isEmpty()) { boolean showDetailed = Tools.show(mode, config.getShowChestContentsDetailed()) && stacks.size() <= Config.showItemDetailThresshold.get(); - showChestContents(probeInfo, world, pos, stacks, showDetailed); + showChestContents(probeInfo, stacks, showDetailed); } } } @@ -75,7 +89,7 @@ private static void addItemStack(List stacks, Set foundItems, @ } } - private static void showChestContents(IProbeInfo probeInfo, Level world, BlockPos pos, List stacks, boolean detailed) { + private static void showChestContents(IProbeInfo probeInfo, List stacks, boolean detailed) { int rows = 0; int idx = 0; @@ -103,28 +117,26 @@ private static void showChestContents(IProbeInfo probeInfo, Level world, BlockPo } } - private static int getChestContents(Level world, BlockPos pos, List stacks) { - BlockEntity te = world.getBlockEntity(pos); - + private static int getChestContents(ICapabilityProvider inventory, ResourceLocation registryKey, List stacks) { Set foundItems = Config.compactEqualStacks.get() ? new HashSet<>() : null; AtomicInteger maxSlots = new AtomicInteger(); try { - if (te != null && te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).isPresent()) { - te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(capability -> { + if (inventory != null && inventory.getCapability(ForgeCapabilities.ITEM_HANDLER).isPresent()) { + inventory.getCapability(ForgeCapabilities.ITEM_HANDLER).ifPresent(capability -> { maxSlots.set(capability.getSlots()); for (int i = 0; i < maxSlots.get(); i++) { addItemStack(stacks, foundItems, capability.getStackInSlot(i)); } }); - } else if (te instanceof Container inventory) { - maxSlots.set(inventory.getContainerSize()); + } else if (inventory instanceof Container container) { + maxSlots.set(container.getContainerSize()); for (int i = 0; i < maxSlots.get(); i++) { - addItemStack(stacks, foundItems, inventory.getItem(i)); + addItemStack(stacks, foundItems, container.getItem(i)); } } } catch (RuntimeException e) { - throw new RuntimeException("Getting the contents of a " + ForgeRegistries.BLOCKS.getKey(world.getBlockState(pos).getBlock()) + " (" + te.getClass().getName() + ")", e); + throw new RuntimeException("Getting the contents of a " + registryKey + " (" + inventory.getClass().getName() + ")", e); } return maxSlots.get(); } diff --git a/src/main/java/mcjty/theoneprobe/apiimpl/providers/DefaultProbeInfoEntityProvider.java b/src/main/java/mcjty/theoneprobe/apiimpl/providers/DefaultProbeInfoEntityProvider.java index 805a2712..208c4c7a 100644 --- a/src/main/java/mcjty/theoneprobe/apiimpl/providers/DefaultProbeInfoEntityProvider.java +++ b/src/main/java/mcjty/theoneprobe/apiimpl/providers/DefaultProbeInfoEntityProvider.java @@ -19,6 +19,7 @@ import net.minecraft.world.entity.animal.horse.Horse; import net.minecraft.world.entity.decoration.ItemFrame; import net.minecraft.world.entity.player.Player; +import net.minecraft.world.entity.vehicle.ContainerEntity; import net.minecraft.world.item.DyeColor; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; @@ -156,6 +157,10 @@ public void addProbeEntityInfo(ProbeMode mode, IProbeInfo probeInfo, Player play probeInfo.text(CompoundText.createLabelInfo("Collar: ", collarColor.getSerializedName())); } } + + if (entity instanceof ContainerEntity) { + ChestInfoTools.showChestInfo(mode, probeInfo, entity, config); + } } public static String getPotionDurationString(MobEffectInstance effect, float factor) { diff --git a/src/main/java/mcjty/theoneprobe/config/Config.java b/src/main/java/mcjty/theoneprobe/config/Config.java index 8df23fad..94b670d6 100644 --- a/src/main/java/mcjty/theoneprobe/config/Config.java +++ b/src/main/java/mcjty/theoneprobe/config/Config.java @@ -255,11 +255,11 @@ public static IProbeConfig getRealConfig() { .comment("The maximum amount of slots (empty or not) to show without sneaking") .defineInRange("showSmallChestContentsWithoutSneaking", 0, 0, 1000); showContentsWithoutSneaking = COMMON_BUILDER - .comment("A list of blocks for which we automatically show chest contents even if not sneaking") + .comment("A list of blocks and entities for which we automatically show contents even if not sneaking") .defineList("showContentsWithoutSneaking", new ArrayList<>(Arrays.asList("storagedrawers:basicdrawers", "storagedrawersextra:extra_drawers")), s -> s instanceof String); dontShowContentsUnlessSneaking = COMMON_BUILDER - .comment("A list of blocks for which we don't show chest contents automatically except if sneaking") + .comment("A list of blocks and entities for which we don't show contents automatically except if sneaking") .defineList("dontShowContentsUnlessSneaking", new ArrayList<>(), s -> s instanceof String);