forked from OrekiWoof/ChestPreview
support for groundstorable containers and bloomery
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"total": 66247,
|
||||
"total": 73071,
|
||||
"sessions": [
|
||||
{
|
||||
"begin": "2026-03-11T23:50:47+01:00",
|
||||
@@ -65,6 +65,11 @@
|
||||
"begin": "2026-03-15T20:33:08+01:00",
|
||||
"end": "2026-03-15T20:53:09+01:00",
|
||||
"duration": 1201
|
||||
},
|
||||
{
|
||||
"begin": "2026-03-15T21:00:39+01:00",
|
||||
"end": "2026-03-15T23:04:35+01:00",
|
||||
"duration": 7435
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -14,7 +14,9 @@ public sealed class Config
|
||||
|
||||
public bool WhitelistedContainersOnly { get; set; } = true;
|
||||
|
||||
public string WhitelistedContainers { get; set; } = "barrel, crate, chest-*, trunk-*, storagevessel-*, stationarybasket-*, labeledchest-*";
|
||||
public string WhitelistedContainers { get; set; } = "barrel, crate, chest-*, trunk-*, storagevessel-*, stationarybasket-*, labeledchest-*, groundstorage";
|
||||
|
||||
public string BlacklistedContainers { get; set; } = "";
|
||||
|
||||
public bool GroundStorageOnlyContainers { get; set; } = true;
|
||||
}
|
||||
@@ -11,6 +11,7 @@ using Vintagestory.API.Client;
|
||||
using Vintagestory.API.Common;
|
||||
using Vintagestory.API.MathTools;
|
||||
using Vintagestory.API.Util;
|
||||
using Vintagestory.GameContent;
|
||||
|
||||
namespace ChestPreview;
|
||||
|
||||
@@ -39,7 +40,7 @@ internal partial class PreviewTargetProvider(ICoreClientAPI api, Config config)
|
||||
if (!TryGetHoveredContainer(out Block hoveredBlock, out BlockEntity hoveredBlockEntity))
|
||||
return;
|
||||
|
||||
if (!CanAccessContainer(hoveredBlockEntity))
|
||||
if (!CanAccessContainer(hoveredBlock, hoveredBlockEntity))
|
||||
return;
|
||||
|
||||
if (!IsContainerAllowed(hoveredBlock))
|
||||
@@ -70,7 +71,7 @@ internal partial class PreviewTargetProvider(ICoreClientAPI api, Config config)
|
||||
if (block.Id == 0)
|
||||
continue;
|
||||
|
||||
if (!CanAccessContainer(blockEntity))
|
||||
if (!CanAccessContainer(block, blockEntity))
|
||||
continue;
|
||||
|
||||
if (!IsContainerAllowed(block))
|
||||
@@ -134,10 +135,14 @@ internal partial class PreviewTargetProvider(ICoreClientAPI api, Config config)
|
||||
if (currentBlockSelection == null)
|
||||
return false;
|
||||
|
||||
BlockEntity? currentBlockEntity = api.World.BlockAccessor.GetBlockEntity(currentBlockSelection.Position);
|
||||
if (currentBlockEntity is IBlockEntityContainer)
|
||||
BlockPos hoveredPos = currentBlockSelection.Position;
|
||||
BlockEntity? currentBlockEntity = api.World.BlockAccessor.GetBlockEntity(hoveredPos);
|
||||
if (HasPreviewableStorage(currentBlockSelection.Block, hoveredPos, currentBlockEntity))
|
||||
{
|
||||
block = currentBlockSelection.Block;
|
||||
if (currentBlockEntity == null)
|
||||
return false;
|
||||
|
||||
blockEntity = currentBlockEntity;
|
||||
return true;
|
||||
}
|
||||
@@ -159,14 +164,17 @@ internal partial class PreviewTargetProvider(ICoreClientAPI api, Config config)
|
||||
return false;
|
||||
|
||||
BlockPos controllerPos = multiblockOffset.GetControlBlockPos(blockSelection.Position);
|
||||
BlockEntity? blockEntity = api.World.BlockAccessor.GetBlockEntity(controllerPos);
|
||||
if (blockEntity is not IBlockEntityContainer)
|
||||
return false;
|
||||
|
||||
controllerBlock = api.World.BlockAccessor.GetBlock(controllerPos);
|
||||
if (controllerBlock.Id == 0)
|
||||
return false;
|
||||
|
||||
BlockEntity? blockEntity = api.World.BlockAccessor.GetBlockEntity(controllerPos);
|
||||
if (!HasPreviewableStorage(controllerBlock, controllerPos, blockEntity))
|
||||
return false;
|
||||
|
||||
if (blockEntity == null)
|
||||
return false;
|
||||
|
||||
controllerBlockEntity = blockEntity;
|
||||
return true;
|
||||
}
|
||||
@@ -196,11 +204,15 @@ internal partial class PreviewTargetProvider(ICoreClientAPI api, Config config)
|
||||
if (Math.Abs(y + 0.5d - playerEntity.CameraPos.Y) > nearbyRadius)
|
||||
return;
|
||||
|
||||
BlockEntity? blockEntity = api.World.BlockAccessor.GetBlockEntity(new BlockPos(x, y, z));
|
||||
if (blockEntity is not IBlockEntityContainer)
|
||||
if (block.Id == 0)
|
||||
return;
|
||||
|
||||
if (block.Id == 0)
|
||||
BlockPos blockPos = new(x, y, z);
|
||||
BlockEntity? blockEntity = api.World.BlockAccessor.GetBlockEntity(blockPos);
|
||||
if (!HasPreviewableStorage(block, blockPos, blockEntity))
|
||||
return;
|
||||
|
||||
if (blockEntity == null)
|
||||
return;
|
||||
|
||||
nearbyContainerEntities.Add(blockEntity);
|
||||
@@ -232,17 +244,57 @@ internal partial class PreviewTargetProvider(ICoreClientAPI api, Config config)
|
||||
}
|
||||
}
|
||||
|
||||
private bool CanAccessContainer(BlockEntity blockEntity)
|
||||
private bool CanAccessContainer(Block block, BlockEntity blockEntity)
|
||||
{
|
||||
if (blockEntity is not IBlockEntityContainer container || container.Inventory is not InventoryBase inventory)
|
||||
if (config.GroundStorageOnlyContainers && blockEntity is BlockEntityGroundStorage && !HasHeldBagInGroundStorage(blockEntity))
|
||||
return false;
|
||||
|
||||
IPlayer? player = api.World.Player;
|
||||
EntityPlayer? playerEntity = player?.Entity;
|
||||
if (player == null || playerEntity == null)
|
||||
BlockPos blockPos = blockEntity.Pos;
|
||||
if (block.GetInterface<IBlockEntityContainer>(api.World, blockPos) is { } container && container.Inventory is InventoryBase inventory)
|
||||
{
|
||||
IPlayer? player = api.World.Player;
|
||||
EntityPlayer? playerEntity = player?.Entity;
|
||||
if (player == null || playerEntity == null)
|
||||
return false;
|
||||
|
||||
return inventory.CanPlayerAccess(player, playerEntity.GetPos());
|
||||
}
|
||||
|
||||
if (blockEntity is BlockEntityBloomery)
|
||||
return true;
|
||||
|
||||
return HasHeldBagInGroundStorage(blockEntity);
|
||||
}
|
||||
|
||||
private bool HasPreviewableStorage(Block block, BlockPos blockPos, BlockEntity? blockEntity)
|
||||
{
|
||||
if (config.GroundStorageOnlyContainers && blockEntity is BlockEntityGroundStorage)
|
||||
return HasHeldBagInGroundStorage(blockEntity);
|
||||
|
||||
return block.GetInterface<IBlockEntityContainer>(api.World, blockPos) != null || HasHeldBagInGroundStorage(blockEntity) || HasBloomeryInventory(blockEntity);
|
||||
}
|
||||
|
||||
private static bool HasBloomeryInventory(BlockEntity? blockEntity)
|
||||
{
|
||||
return blockEntity is BlockEntityBloomery;
|
||||
}
|
||||
|
||||
private static bool HasHeldBagInGroundStorage(BlockEntity? blockEntity)
|
||||
{
|
||||
if (blockEntity is not BlockEntityGroundStorage groundStorage)
|
||||
return false;
|
||||
|
||||
return inventory.CanPlayerAccess(player, playerEntity.GetPos());
|
||||
foreach (ItemSlot? slot in groundStorage.Inventory)
|
||||
{
|
||||
if (slot?.Itemstack?.Collectible is not { } collectible)
|
||||
continue;
|
||||
|
||||
IHeldBag? heldBag = collectible.GetCollectibleInterface<IHeldBag>();
|
||||
if (heldBag != null)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool IsContainerAllowed(Block block)
|
||||
|
||||
@@ -4,12 +4,15 @@ using ChestPreview.Models;
|
||||
using ChestPreview.Utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using Vintagestory.API.Client;
|
||||
using Vintagestory.API.Common;
|
||||
using Vintagestory.API.Config;
|
||||
using Vintagestory.API.Datastructures;
|
||||
using Vintagestory.API.MathTools;
|
||||
using Vintagestory.GameContent;
|
||||
|
||||
namespace ChestPreview.Rendering;
|
||||
|
||||
@@ -28,6 +31,7 @@ internal class CardRenderer(ICoreClientAPI api, Config config) : IDisposable
|
||||
|
||||
private readonly ICoreClientAPI api = api;
|
||||
private readonly Config config = config;
|
||||
private readonly TreeAttribute tempTree = new();
|
||||
private readonly Dictionary<string, CachedCardTexture> cardTextureByTarget = [];
|
||||
private readonly Dictionary<int, LoadedTexture> atlasTextureById = [];
|
||||
private readonly Dictionary<string, RenderedIconEntry> renderedIconByStack = [];
|
||||
@@ -46,9 +50,33 @@ internal class CardRenderer(ICoreClientAPI api, Config config) : IDisposable
|
||||
{
|
||||
texture = null!;
|
||||
|
||||
if (target.BlockEntity is not IBlockEntityContainer container || container.Inventory is not InventoryBase inventory)
|
||||
if (TryResolveHeldBagContents(target.BlockEntity, out List<ItemStack>? bagContents))
|
||||
{
|
||||
string bagTargetKey = CreateTargetKey(target.BlockEntity.Pos);
|
||||
if (!cardTextureByTarget.TryGetValue(bagTargetKey, out CachedCardTexture? bagCachedTexture))
|
||||
{
|
||||
bagCachedTexture = new CachedCardTexture(new LoadedTexture(api), string.Empty);
|
||||
cardTextureByTarget[bagTargetKey] = bagCachedTexture;
|
||||
}
|
||||
|
||||
BuildLayoutFromStacks(bagContents, maxColumns, out List<GroupedItemCell> bagCells, out int bagUsedColumns, out int bagUsedRows, out string bagFingerprint);
|
||||
if (bagCachedTexture.Fingerprint != bagFingerprint)
|
||||
{
|
||||
ComposeTexture(bagCachedTexture.Texture, bagCells, bagUsedColumns, bagUsedRows);
|
||||
bagCachedTexture.Fingerprint = bagFingerprint;
|
||||
}
|
||||
|
||||
texture = bagCachedTexture.Texture;
|
||||
return texture.TextureId != 0;
|
||||
}
|
||||
|
||||
if (config.GroundStorageOnlyContainers && target.BlockEntity is BlockEntityGroundStorage)
|
||||
return false;
|
||||
|
||||
if (!TryResolveInventory(target, out InventoryBase? resolvedInventory))
|
||||
return false;
|
||||
|
||||
InventoryBase inventory = resolvedInventory;
|
||||
IPlayer? player = api.World.Player;
|
||||
EntityPlayer? playerEntity = player?.Entity;
|
||||
if (player == null || playerEntity == null || !inventory.CanPlayerAccess(player, playerEntity.GetPos()))
|
||||
@@ -61,17 +89,85 @@ internal class CardRenderer(ICoreClientAPI api, Config config) : IDisposable
|
||||
cardTextureByTarget[targetKey] = cachedTexture;
|
||||
}
|
||||
|
||||
BuildLayout(inventory, maxColumns, out List<GroupedItemCell> cells, out int usedColumns, out int usedRows, out string fingerprint);
|
||||
if (cachedTexture.Fingerprint != fingerprint)
|
||||
BuildLayout(inventory, maxColumns, out List<GroupedItemCell> cellsFromInventory, out int usedColumnsFromInventory, out int usedRowsFromInventory, out string fingerprintFromInventory);
|
||||
if (cachedTexture.Fingerprint != fingerprintFromInventory)
|
||||
{
|
||||
ComposeTexture(cachedTexture.Texture, cells, usedColumns, usedRows);
|
||||
cachedTexture.Fingerprint = fingerprint;
|
||||
ComposeTexture(cachedTexture.Texture, cellsFromInventory, usedColumnsFromInventory, usedRowsFromInventory);
|
||||
cachedTexture.Fingerprint = fingerprintFromInventory;
|
||||
}
|
||||
|
||||
texture = cachedTexture.Texture;
|
||||
return texture.TextureId != 0;
|
||||
}
|
||||
|
||||
private bool TryResolveHeldBagContents(BlockEntity blockEntity, [NotNullWhen(true)] out List<ItemStack>? contents)
|
||||
{
|
||||
contents = null;
|
||||
if (blockEntity is not BlockEntityGroundStorage groundStorage)
|
||||
return false;
|
||||
|
||||
foreach (ItemSlot? slot in groundStorage.Inventory)
|
||||
{
|
||||
ItemStack? bagStack = slot?.Itemstack;
|
||||
if (bagStack?.Collectible is not { } collectible)
|
||||
continue;
|
||||
|
||||
IHeldBag? heldBag = collectible.GetCollectibleInterface<IHeldBag>();
|
||||
if (heldBag == null)
|
||||
continue;
|
||||
|
||||
contents = [];
|
||||
ItemStack[] bagContents = heldBag.GetContents(bagStack, api.World);
|
||||
foreach (ItemStack contentStack in bagContents)
|
||||
{
|
||||
if (contentStack == null || contentStack.StackSize <= 0)
|
||||
continue;
|
||||
|
||||
contents.Add(contentStack);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool TryResolveInventory(PreviewTarget target, [NotNullWhen(true)] out InventoryBase? inventory)
|
||||
{
|
||||
inventory = null;
|
||||
|
||||
if (TryResolveBloomeryInventory(target.BlockEntity, out InventoryBase? bloomeryInventory))
|
||||
{
|
||||
inventory = bloomeryInventory;
|
||||
return true;
|
||||
}
|
||||
|
||||
BlockPos pos = target.BlockEntity.Pos;
|
||||
if (target.Block.GetInterface<IBlockEntityContainer>(api.World, pos) is { } container && container.Inventory is InventoryBase blockInventory)
|
||||
{
|
||||
inventory = blockInventory;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool TryResolveBloomeryInventory(BlockEntity blockEntity, [NotNullWhen(true)] out InventoryBase? inventory)
|
||||
{
|
||||
inventory = null;
|
||||
if (blockEntity is not BlockEntityBloomery)
|
||||
return false;
|
||||
|
||||
tempTree.Clear();
|
||||
blockEntity.ToTreeAttributes(tempTree);
|
||||
|
||||
// bloomery inventory has 3 slots: fuel, ore, output
|
||||
InventoryGeneric bloomeryInventory = new(3, "bloomery-preview", null, null);
|
||||
bloomeryInventory.FromTreeAttributes(tempTree);
|
||||
inventory = bloomeryInventory;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void DrawAtScreen(float x, float y, LoadedTexture texture)
|
||||
{
|
||||
if (texture.TextureId == 0)
|
||||
@@ -105,16 +201,29 @@ internal class CardRenderer(ICoreClientAPI api, Config config) : IDisposable
|
||||
}
|
||||
|
||||
private void BuildLayout(IInventory inventory, int maxColumns, out List<GroupedItemCell> cells, out int usedColumns, out int usedRows, out string fingerprint)
|
||||
{
|
||||
List<ItemStack> stacks = [];
|
||||
foreach (ItemSlot? slot in inventory)
|
||||
{
|
||||
if (slot?.Itemstack == null || slot.Empty)
|
||||
continue;
|
||||
|
||||
stacks.Add(slot.Itemstack);
|
||||
}
|
||||
|
||||
BuildLayoutFromStacks(stacks, maxColumns, out cells, out usedColumns, out usedRows, out fingerprint);
|
||||
}
|
||||
|
||||
private void BuildLayoutFromStacks(IEnumerable<ItemStack> stacks, int maxColumns, out List<GroupedItemCell> cells, out int usedColumns, out int usedRows, out string fingerprint)
|
||||
{
|
||||
List<GroupedItemCell> groupedItems = [];
|
||||
int naturalOrder = 0;
|
||||
|
||||
foreach (ItemSlot? slot in inventory)
|
||||
foreach (ItemStack stack in stacks)
|
||||
{
|
||||
if (slot == null || slot.Empty || slot.Itemstack == null)
|
||||
if (stack == null || stack.StackSize <= 0)
|
||||
continue;
|
||||
|
||||
ItemStack stack = slot.Itemstack;
|
||||
int existingIndex = FindMatchingItemIndex(groupedItems, stack);
|
||||
if (existingIndex >= 0)
|
||||
{
|
||||
|
||||
@@ -72,7 +72,7 @@
|
||||
"code": "WhitelistedContainers",
|
||||
"comment": "config-desc-WhitelistedContainers",
|
||||
"type": "string",
|
||||
"default": "barrel, crate, chest-*, trunk-*, storagevessel-*, stationarybasket-*, labeledchest-*",
|
||||
"default": "barrel, crate, chest-*, trunk-*, storagevessel-*, stationarybasket-*, labeledchest-*, labeledtrunk-*, groundstorage, beehive-*",
|
||||
"clientSide": true
|
||||
},
|
||||
{
|
||||
@@ -81,6 +81,13 @@
|
||||
"type": "string",
|
||||
"default": "",
|
||||
"clientSide": true
|
||||
},
|
||||
{
|
||||
"code": "GroundStorageOnlyContainers",
|
||||
"comment": "config-desc-GroundStorageOnlyContainers",
|
||||
"type": "boolean",
|
||||
"default": true,
|
||||
"clientSide": true
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
"config-desc-ColumnsUnderCursor": "Columns in the UnderCursor mode.",
|
||||
"config-desc-PreviewNearbyRadius": "Radius for \"Preview containers nearby\".",
|
||||
"config-desc-WhitelistedContainersOnly": "If true, only container codes in WhitelistedContainers are handled.",
|
||||
"config-desc-GroundStorageOnlyContainers": "If true, groundstorage previews only appear when the stored item has its own inventory (for example bags/backpacks).",
|
||||
"config-desc-WhitelistedContainers": "Allowed container codes, separated by comma, semicolon, or spaces. No effectif WhitelistedContainersOnly==false.",
|
||||
"config-desc-BlacklistedContainers": "Blocked container codes, separated by comma, semicolon, or spaces."
|
||||
}
|
||||
@@ -7,7 +7,7 @@
|
||||
"OrekiWoof"
|
||||
],
|
||||
"description": "see containers' contents without having to open them",
|
||||
"version": "1.0.0",
|
||||
"version": "1.1.0",
|
||||
"dependencies": {
|
||||
"game": "1.21.0"
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user