Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show chest contents for boats and minecarts #603

Open
wants to merge 1 commit into
base: 1.19.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -25,33 +28,44 @@
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<ItemStack> 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;
}
}

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);
}
}
}
Expand All @@ -75,7 +89,7 @@ private static void addItemStack(List<ItemStack> stacks, Set<Item> foundItems, @
}
}

private static void showChestContents(IProbeInfo probeInfo, Level world, BlockPos pos, List<ItemStack> stacks, boolean detailed) {
private static void showChestContents(IProbeInfo probeInfo, List<ItemStack> stacks, boolean detailed) {
int rows = 0;
int idx = 0;

Expand Down Expand Up @@ -103,28 +117,26 @@ private static void showChestContents(IProbeInfo probeInfo, Level world, BlockPo
}
}

private static int getChestContents(Level world, BlockPos pos, List<ItemStack> stacks) {
BlockEntity te = world.getBlockEntity(pos);

private static int getChestContents(ICapabilityProvider inventory, ResourceLocation registryKey, List<ItemStack> stacks) {
Set<Item> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/mcjty/theoneprobe/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down