Compare commits

..

6 Commits

10 changed files with 1372 additions and 1160 deletions

View File

@@ -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
}
]
}

View File

@@ -1,20 +1,22 @@
namespace ChestPreview.Configs;
public sealed class Config
{
public string Mode { get; set; } = PreviewModes.UNDER_CURSOR;
public bool HoldKey { get; set; } = true;
public int BillboardColumnsPerBlock { get; set; } = 4;
public int ColumnsUnderCursor { get; set; } = 10;
public int PreviewNearbyRadius { get; set; } = 8;
public bool WhitelistedContainersOnly { get; set; } = true;
public string WhitelistedContainers { get; set; } = "barrel, crate, chest-*, trunk-*, storagevessel-*, stationarybasket-*, labeledchest-*";
public string BlacklistedContainers { get; set; } = "";
namespace ChestPreview.Configs;
public sealed class Config
{
public string Mode { get; set; } = PreviewModes.UNDER_CURSOR;
public bool HoldKey { get; set; } = true;
public int BillboardColumnsPerBlock { get; set; } = 4;
public int ColumnsUnderCursor { get; set; } = 10;
public int PreviewNearbyRadius { get; set; } = 8;
public bool WhitelistedContainersOnly { get; set; } = true;
public string WhitelistedContainers { get; set; } = "barrel, crate, chest-*, trunk-*, storagevessel-*, stationarybasket-*, labeledchest-*, groundstorage";
public string BlacklistedContainers { get; set; } = "";
public bool GroundStorageOnlyContainers { get; set; } = true;
}

View File

@@ -1,284 +1,331 @@
using ChestPreview.Configs;
using ChestPreview.Core;
using ChestPreview.Models;
using ChestPreview.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text.RegularExpressions;
using Vintagestory.API.Client;
using Vintagestory.API.Common;
using Vintagestory.API.MathTools;
using Vintagestory.API.Util;
namespace ChestPreview;
internal partial class PreviewTargetProvider(ICoreClientAPI api, Config config) : IDisposable
{
private const float nearby_scan_interval_seconds = 0.1f;
private static readonly Regex list_separator_regex = ListSeparatorRegex();
private readonly ICoreClientAPI api = api;
private readonly Config config = config;
private readonly List<BlockEntity> nearbyContainerEntities = [];
private float nearbyScanAccumulator;
public void CollectTargets(float deltaTime, List<PreviewTarget> targets)
{
string mode = GetActiveMode();
if (mode == PreviewModes.NONE)
return;
if (mode == PreviewModes.ON_NEARBY_CONTAINERS)
{
CollectNearbyTargets(deltaTime, targets);
return;
}
if (!TryGetHoveredContainer(out Block hoveredBlock, out BlockEntity hoveredBlockEntity))
return;
if (!CanAccessContainer(hoveredBlockEntity))
return;
if (!IsContainerAllowed(hoveredBlock))
return;
targets.Add(new PreviewTarget(hoveredBlock, hoveredBlockEntity, mode, GetContainerAnchor(hoveredBlock, hoveredBlockEntity.Pos)));
}
public void Dispose()
{
nearbyContainerEntities.Clear();
}
private void CollectNearbyTargets(float deltaTime, List<PreviewTarget> targets)
{
nearbyScanAccumulator += deltaTime;
bool shouldRefresh = nearbyContainerEntities.Count == 0 || nearbyScanAccumulator >= nearby_scan_interval_seconds;
if (shouldRefresh)
{
RefreshNearbyContainers();
nearbyScanAccumulator = 0f;
}
foreach (BlockEntity blockEntity in nearbyContainerEntities)
{
Block block = api.World.BlockAccessor.GetBlock(blockEntity.Pos);
if (block.Id == 0)
continue;
if (!CanAccessContainer(blockEntity))
continue;
if (!IsContainerAllowed(block))
continue;
targets.Add(new PreviewTarget(block, blockEntity, PreviewModes.ON_NEARBY_CONTAINERS, GetContainerAnchor(block, blockEntity.Pos)));
}
}
private string GetActiveMode()
{
if (IsHotkeyHeld(ChestPreviewModSystem.PREVIEW_CONTAINERS_NEARBY_HOTKEY_CODE))
return PreviewModes.ON_NEARBY_CONTAINERS;
if (config.HoldKey && !IsHotkeyHeld(ChestPreviewModSystem.PREVIEW_CONTAINERS_HOTKEY_CODE))
return PreviewModes.NONE;
return PreviewModes.Normalize(config.Mode);
}
private bool IsHotkeyHeld(string hotkeyCode)
{
HotKey? hotKey = api.Input.GetHotKeyByCode(hotkeyCode);
if (hotKey?.CurrentMapping == null)
return false;
KeyCombination mapping = hotKey.CurrentMapping;
if (!IsKeyHeld(mapping.KeyCode))
return false;
if (mapping.SecondKeyCode.HasValue && !IsKeyHeld(mapping.SecondKeyCode.Value))
return false;
EntityControls? controls = api.World.Player?.Entity?.Controls;
if (mapping.Ctrl && controls?.CtrlKey != true)
return false;
if (mapping.Shift && controls?.ShiftKey != true)
return false;
return !mapping.Alt || IsAltHeld();
}
private bool IsKeyHeld(int keyCode)
{
bool[] keyStates = api.Input.KeyboardKeyStateRaw;
return keyCode >= 0 && keyCode < keyStates.Length && keyStates[keyCode];
}
private bool IsAltHeld()
{
return IsKeyHeld((int)GlKeys.AltLeft) || IsKeyHeld((int)GlKeys.AltRight);
}
private bool TryGetHoveredContainer(out Block block, out BlockEntity blockEntity)
{
block = null!;
blockEntity = null!;
BlockSelection? currentBlockSelection = api.World.Player?.CurrentBlockSelection;
if (currentBlockSelection == null)
return false;
BlockEntity? currentBlockEntity = api.World.BlockAccessor.GetBlockEntity(currentBlockSelection.Position);
if (currentBlockEntity is IBlockEntityContainer)
{
block = currentBlockSelection.Block;
blockEntity = currentBlockEntity;
return true;
}
if (!TryResolveMultiblockControllerContainer(currentBlockSelection, out Block controllerBlock, out BlockEntity controllerBlockEntity))
return false;
block = controllerBlock;
blockEntity = controllerBlockEntity;
return true;
}
private bool TryResolveMultiblockControllerContainer(BlockSelection blockSelection, out Block controllerBlock, out BlockEntity controllerBlockEntity)
{
controllerBlock = null!;
controllerBlockEntity = null!;
if (blockSelection.Block is not IMultiblockOffset multiblockOffset)
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;
controllerBlockEntity = blockEntity;
return true;
}
private void RefreshNearbyContainers()
{
nearbyContainerEntities.Clear();
EntityPlayer? playerEntity = api.World.Player?.Entity;
if (playerEntity?.CameraPos == null)
return;
float nearbyRadius = config.PreviewNearbyRadius;
float nearbyRadiusSquared = nearbyRadius * nearbyRadius;
Vec3d nearbyRadiusOffset = new(nearbyRadius, nearbyRadius, nearbyRadius);
BlockPos minPos = (playerEntity.CameraPos - nearbyRadiusOffset).AsBlockPos;
BlockPos maxPos = (playerEntity.CameraPos + nearbyRadiusOffset + new Vec3d(1d, 1d, 1d)).AsBlockPos;
api.World.BlockAccessor.WalkBlocks(minPos, maxPos, (block, x, y, z) =>
{
double horizontalDx = x + 0.5d - playerEntity.CameraPos.X;
double horizontalDz = z + 0.5d - playerEntity.CameraPos.Z;
if (horizontalDx * horizontalDx + horizontalDz * horizontalDz > nearbyRadiusSquared)
return;
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)
return;
if (block.Id == 0)
return;
nearbyContainerEntities.Add(blockEntity);
});
}
private Vec3d GetContainerAnchor(Block block, BlockPos blockPos)
{
GetContainerBounds(block, blockPos, out Vector3 min, out Vector3 max);
Vector3 anchor = blockPos.ToVector3() + new Vector3((min.X + max.X) * 0.5f, max.Y + 0.1f, (min.Z + max.Z) * 0.5f);
return anchor.ToVec3d();
}
private void GetContainerBounds(Block block, BlockPos blockPos, out Vector3 min, out Vector3 max)
{
Cuboidf[]? selectionBoxes = block.GetSelectionBoxes(api.World.BlockAccessor, blockPos);
min = new Vector3(0f, 0f, 0f);
max = new Vector3(1f, 1f, 1f);
if (selectionBoxes is not { Length: > 0 })
return;
foreach (Cuboidf selectionBox in selectionBoxes)
{
min = Vector3.Min(min, selectionBox.Start.ToVector3());
max = Vector3.Max(max, selectionBox.End.ToVector3());
}
}
private bool CanAccessContainer(BlockEntity blockEntity)
{
if (blockEntity is not IBlockEntityContainer container || container.Inventory is not InventoryBase inventory)
return false;
IPlayer? player = api.World.Player;
EntityPlayer? playerEntity = player?.Entity;
if (player == null || playerEntity == null)
return false;
return inventory.CanPlayerAccess(player, playerEntity.GetPos());
}
private bool IsContainerAllowed(Block block)
{
string? code = block.Code?.Path;
if (string.IsNullOrWhiteSpace(code))
return true;
HashSet<string> blacklist = ParseContainerCodes(config.BlacklistedContainers);
if (blacklist.Any(x => WildcardUtil.Match(x, code)))
return false;
if (!config.WhitelistedContainersOnly)
return true;
HashSet<string> whitelist = ParseContainerCodes(config.WhitelistedContainers);
return whitelist.Any(x => WildcardUtil.Match(x, code));
}
private static HashSet<string> ParseContainerCodes(string rawList)
{
if (string.IsNullOrWhiteSpace(rawList))
return [];
string[] entries = list_separator_regex.Split(rawList.Trim());
HashSet<string> result = new(StringComparer.OrdinalIgnoreCase);
foreach (string entry in entries)
{
if (!string.IsNullOrWhiteSpace(entry))
result.Add(entry);
}
return result;
}
[GeneratedRegex("[,;\\s]+", RegexOptions.Compiled)]
private static partial Regex ListSeparatorRegex();
using ChestPreview.Configs;
using ChestPreview.Core;
using ChestPreview.Models;
using ChestPreview.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text.RegularExpressions;
using Vintagestory.API.Client;
using Vintagestory.API.Common;
using Vintagestory.API.MathTools;
using Vintagestory.API.Util;
using Vintagestory.GameContent;
namespace ChestPreview;
internal partial class PreviewTargetProvider(ICoreClientAPI api, Config config) : IDisposable
{
private const float nearby_scan_interval_seconds = 0.1f;
private static readonly Regex list_separator_regex = ListSeparatorRegex();
private readonly ICoreClientAPI api = api;
private readonly Config config = config;
private readonly List<BlockEntity> nearbyContainerEntities = [];
private float nearbyScanAccumulator;
public void CollectTargets(float deltaTime, List<PreviewTarget> targets)
{
string mode = GetActiveMode();
if (mode == PreviewModes.NONE)
return;
if (mode == PreviewModes.ON_NEARBY_CONTAINERS)
{
CollectNearbyTargets(deltaTime, targets);
return;
}
if (!TryGetHoveredContainer(out Block hoveredBlock, out BlockEntity hoveredBlockEntity))
return;
if (!CanAccessContainer(hoveredBlock, hoveredBlockEntity))
return;
if (!IsContainerAllowed(hoveredBlock))
return;
targets.Add(new PreviewTarget(hoveredBlock, hoveredBlockEntity, mode, GetContainerAnchor(hoveredBlock, hoveredBlockEntity.Pos)));
}
public void Dispose()
{
nearbyContainerEntities.Clear();
}
private void CollectNearbyTargets(float deltaTime, List<PreviewTarget> targets)
{
nearbyScanAccumulator += deltaTime;
bool shouldRefresh = nearbyContainerEntities.Count == 0 || nearbyScanAccumulator >= nearby_scan_interval_seconds;
if (shouldRefresh)
{
RefreshNearbyContainers();
nearbyScanAccumulator = 0f;
}
foreach (BlockEntity blockEntity in nearbyContainerEntities)
{
Block block = api.World.BlockAccessor.GetBlock(blockEntity.Pos);
if (block.Id == 0)
continue;
if (!CanAccessContainer(block, blockEntity))
continue;
if (!IsContainerAllowed(block))
continue;
targets.Add(new PreviewTarget(block, blockEntity, PreviewModes.ON_NEARBY_CONTAINERS, GetContainerAnchor(block, blockEntity.Pos)));
}
}
private string GetActiveMode()
{
if (IsHotkeyHeld(ChestPreviewModSystem.PREVIEW_CONTAINERS_NEARBY_HOTKEY_CODE))
return PreviewModes.ON_NEARBY_CONTAINERS;
if (config.HoldKey && !IsHotkeyHeld(ChestPreviewModSystem.PREVIEW_CONTAINERS_HOTKEY_CODE))
return PreviewModes.NONE;
return PreviewModes.Normalize(config.Mode);
}
private bool IsHotkeyHeld(string hotkeyCode)
{
HotKey? hotKey = api.Input.GetHotKeyByCode(hotkeyCode);
if (hotKey?.CurrentMapping == null)
return false;
KeyCombination mapping = hotKey.CurrentMapping;
if (!IsKeyHeld(mapping.KeyCode))
return false;
if (mapping.SecondKeyCode.HasValue && !IsKeyHeld(mapping.SecondKeyCode.Value))
return false;
EntityControls? controls = api.World.Player?.Entity?.Controls;
if (mapping.Ctrl && controls?.CtrlKey != true)
return false;
if (mapping.Shift && controls?.ShiftKey != true)
return false;
return !mapping.Alt || IsAltHeld();
}
private bool IsKeyHeld(int keyCode)
{
bool[] keyStates = api.Input.KeyboardKeyStateRaw;
return keyCode >= 0 && keyCode < keyStates.Length && keyStates[keyCode];
}
private bool IsAltHeld()
{
return IsKeyHeld((int)GlKeys.AltLeft) || IsKeyHeld((int)GlKeys.AltRight);
}
private bool TryGetHoveredContainer(out Block block, out BlockEntity blockEntity)
{
block = null!;
blockEntity = null!;
BlockSelection? currentBlockSelection = api.World.Player?.CurrentBlockSelection;
if (currentBlockSelection == null)
return false;
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;
}
if (!TryResolveMultiblockControllerContainer(currentBlockSelection, out Block controllerBlock, out BlockEntity controllerBlockEntity))
return false;
block = controllerBlock;
blockEntity = controllerBlockEntity;
return true;
}
private bool TryResolveMultiblockControllerContainer(BlockSelection blockSelection, out Block controllerBlock, out BlockEntity controllerBlockEntity)
{
controllerBlock = null!;
controllerBlockEntity = null!;
if (blockSelection.Block is not IMultiblockOffset multiblockOffset)
return false;
BlockPos controllerPos = multiblockOffset.GetControlBlockPos(blockSelection.Position);
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;
}
private void RefreshNearbyContainers()
{
nearbyContainerEntities.Clear();
EntityPlayer? playerEntity = api.World.Player?.Entity;
if (playerEntity?.CameraPos == null)
return;
float nearbyRadius = config.PreviewNearbyRadius;
float nearbyRadiusSquared = nearbyRadius * nearbyRadius;
Vec3d nearbyRadiusOffset = new(nearbyRadius, nearbyRadius, nearbyRadius);
BlockPos minPos = (playerEntity.CameraPos - nearbyRadiusOffset).AsBlockPos;
BlockPos maxPos = (playerEntity.CameraPos + nearbyRadiusOffset + new Vec3d(1d, 1d, 1d)).AsBlockPos;
api.World.BlockAccessor.WalkBlocks(minPos, maxPos, (block, x, y, z) =>
{
double horizontalDx = x + 0.5d - playerEntity.CameraPos.X;
double horizontalDz = z + 0.5d - playerEntity.CameraPos.Z;
if (horizontalDx * horizontalDx + horizontalDz * horizontalDz > nearbyRadiusSquared)
return;
if (Math.Abs(y + 0.5d - playerEntity.CameraPos.Y) > nearbyRadius)
return;
if (block.Id == 0)
return;
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);
});
}
private Vec3d GetContainerAnchor(Block block, BlockPos blockPos)
{
GetContainerBounds(block, blockPos, out Vector3 min, out Vector3 max);
Vector3 anchor = blockPos.ToVector3() + new Vector3((min.X + max.X) * 0.5f, max.Y + 0.1f, (min.Z + max.Z) * 0.5f);
return anchor.ToVec3d();
}
private void GetContainerBounds(Block block, BlockPos blockPos, out Vector3 min, out Vector3 max)
{
Cuboidf[]? selectionBoxes = block.GetSelectionBoxes(api.World.BlockAccessor, blockPos);
min = new Vector3(0f, 0f, 0f);
max = new Vector3(1f, 1f, 1f);
if (selectionBoxes is not { Length: > 0 })
return;
foreach (Cuboidf selectionBox in selectionBoxes)
{
min = Vector3.Min(min, selectionBox.Start.ToVector3());
max = Vector3.Max(max, selectionBox.End.ToVector3());
}
}
private bool CanAccessContainer(Block block, BlockEntity blockEntity)
{
if (config.GroundStorageOnlyContainers && blockEntity is BlockEntityGroundStorage && !HasHeldBagInGroundStorage(blockEntity))
return false;
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 or BlockEntityForge)
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) || (blockEntity is BlockEntityBloomery or BlockEntityForge);
}
private static bool HasHeldBagInGroundStorage(BlockEntity? blockEntity)
{
if (blockEntity is not BlockEntityGroundStorage groundStorage)
return false;
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)
{
string? code = block.Code?.Path;
if (string.IsNullOrWhiteSpace(code))
return true;
HashSet<string> blacklist = ParseContainerCodes(config.BlacklistedContainers);
if (blacklist.Any(x => WildcardUtil.Match(x, code)))
return false;
if (!config.WhitelistedContainersOnly)
return true;
HashSet<string> whitelist = ParseContainerCodes(config.WhitelistedContainers);
return whitelist.Any(x => WildcardUtil.Match(x, code));
}
private static HashSet<string> ParseContainerCodes(string rawList)
{
if (string.IsNullOrWhiteSpace(rawList))
return [];
string[] entries = list_separator_regex.Split(rawList.Trim());
HashSet<string> result = new(StringComparer.OrdinalIgnoreCase);
foreach (string entry in entries)
{
if (!string.IsNullOrWhiteSpace(entry))
result.Add(entry);
}
return result;
}
[GeneratedRegex("[,;\\s]+", RegexOptions.Compiled)]
private static partial Regex ListSeparatorRegex();
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,329 +1,329 @@
using ChestPreview.Configs;
using ChestPreview.Models;
using ChestPreview.Utils;
using System;
using System.Collections.Generic;
using System.Numerics;
using Vintagestory.API.Client;
using Vintagestory.API.Common;
using Vintagestory.API.MathTools;
using Vintagestory.GameContent;
namespace ChestPreview.Rendering;
internal class WorldBillboardPresenter(ICoreClientAPI api)
{
private const int multiblock_scan_radius = 2;
private static readonly float front_face_offset = 0.01f;
private readonly ICoreClientAPI api = api;
private readonly List<BillboardTarget> frameBillboards = [];
public IReadOnlyList<BillboardTarget> FrameBillboards => frameBillboards;
public void PrepareFrame(List<PreviewTarget> previewTargets)
{
frameBillboards.Clear();
foreach (PreviewTarget previewTarget in previewTargets)
{
if (!CanPresentAsBillboard(previewTarget))
continue;
if (!TryCreateBillboardTarget(previewTarget, out BillboardTarget billboardTarget))
continue;
frameBillboards.Add(billboardTarget);
}
}
public void Clear()
{
frameBillboards.Clear();
}
private static bool CanPresentAsBillboard(PreviewTarget previewTarget)
{
return previewTarget.Mode is PreviewModes.ON_HOVERED_CONTAINER or PreviewModes.ON_NEARBY_CONTAINERS;
}
private bool TryCreateBillboardTarget(PreviewTarget previewTarget, out BillboardTarget billboardTarget)
{
return TryCreateFrontBillboardTarget(previewTarget, out billboardTarget);
}
private bool TryCreateFrontBillboardTarget(PreviewTarget previewTarget, out BillboardTarget billboardTarget)
{
billboardTarget = default;
Block block = previewTarget.Block;
BlockPos blockPos = previewTarget.BlockEntity.Pos;
GetContainerBounds(block, blockPos, out Vector3 min, out Vector3 max);
if (!TryResolveFacingNormal(previewTarget.Block, previewTarget.BlockEntity, out Vec3f forward, out Vec3f right, out Vec3f up))
return false;
Vector3 centerLocal = (min + max) * 0.5f;
Vector3 halfExtents = (max - min) * 0.5f;
Vector3 rightVector = right.ToVector3();
Vector3 upVector = up.ToVector3();
Vector3 forwardVector = forward.ToVector3();
float halfWidth = Vector3.Dot(Vector3.Abs(rightVector), halfExtents);
float halfHeight = Vector3.Dot(Vector3.Abs(upVector), halfExtents);
float halfDepth = ResolveStableHalfDepth(forwardVector, halfExtents);
float width = halfWidth * 2f;
float height = halfHeight * 2f;
Vector3 center = blockPos.ToVector3() + centerLocal + forwardVector * (halfDepth + front_face_offset);
width = Math.Max(0.05f, width);
height = Math.Max(0.05f, height);
billboardTarget = new BillboardTarget(previewTarget, center.ToVec3d(), right, up, forward, width, height, true);
return true;
}
private static float ResolveStableHalfDepth(Vector3 forward, Vector3 halfExtents)
{
if (MathF.Abs(forward.Y) < 0.5f)
return MathF.Abs(forward.X) >= MathF.Abs(forward.Z) ? halfExtents.X : halfExtents.Z;
return MathF.Abs(forward.Y) >= MathF.Abs(forward.Z) ? halfExtents.Y : halfExtents.Z;
}
private void GetContainerBounds(Block block, BlockPos blockPos, out Vector3 min, out Vector3 max)
{
min = new Vector3(0f, 0f, 0f);
max = new Vector3(1f, 1f, 1f);
ExpandBoundsFromSelectionBoxes(block, blockPos, blockPos, ref min, ref max);
ExpandBoundsFromLinkedMultiblockParts(blockPos, ref min, ref max);
}
private void ExpandBoundsFromLinkedMultiblockParts(BlockPos controllerPos, ref Vector3 min, ref Vector3 max)
{
int minScanX = controllerPos.X - multiblock_scan_radius;
int maxScanX = controllerPos.X + multiblock_scan_radius;
int minScanY = controllerPos.Y - multiblock_scan_radius;
int maxScanY = controllerPos.Y + multiblock_scan_radius;
int minScanZ = controllerPos.Z - multiblock_scan_radius;
int maxScanZ = controllerPos.Z + multiblock_scan_radius;
Vector3 minLocal = min;
Vector3 maxLocal = max;
api.World.BlockAccessor.WalkBlocks(new BlockPos(minScanX, minScanY, minScanZ), new BlockPos(maxScanX, maxScanY, maxScanZ), (partBlock, x, y, z) =>
{
if (x == controllerPos.X && y == controllerPos.Y && z == controllerPos.Z)
return;
if (partBlock.Id == 0 || partBlock is not IMultiblockOffset multiblockOffset)
return;
BlockPos partPos = new(x, y, z);
BlockPos linkedControllerPos = multiblockOffset.GetControlBlockPos(partPos);
if (!IsSameBlockPos(linkedControllerPos, controllerPos))
return;
ExpandBoundsFromSelectionBoxes(partBlock, partPos, controllerPos, ref minLocal, ref maxLocal);
});
min = minLocal;
max = maxLocal;
}
private void ExpandBoundsFromSelectionBoxes(Block block, BlockPos sourcePos, BlockPos originPos, ref Vector3 min, ref Vector3 max)
{
Cuboidf[]? selectionBoxes = block.GetSelectionBoxes(api.World.BlockAccessor, sourcePos);
Vector3 sourceOffset = sourcePos.ToVector3() - originPos.ToVector3();
if (selectionBoxes is not { Length: > 0 })
{
min = Vector3.Min(min, sourceOffset);
max = Vector3.Max(max, sourceOffset + Vector3.One);
return;
}
foreach (Cuboidf selectionBox in selectionBoxes)
{
min = Vector3.Min(min, sourceOffset + selectionBox.Start.ToVector3());
max = Vector3.Max(max, sourceOffset + selectionBox.End.ToVector3());
}
}
private static bool IsSameBlockPos(BlockPos a, BlockPos b)
{
return a.X == b.X && a.Y == b.Y && a.Z == b.Z;
}
private bool TryResolveFacingNormal(Block block, BlockEntity blockEntity, out Vec3f forward, out Vec3f right, out Vec3f up)
{
forward = new Vec3f(0f, 0f, 1f);
right = new Vec3f(1f, 0f, 0f);
up = new Vec3f(0f, 1f, 0f);
if (TryGetMeshAngle(blockEntity, out float meshAngleRadians))
{
// MeshAngle is around Y axis in radians.
forward = new Vec3f(MathF.Sin(meshAngleRadians), 0f, MathF.Cos(meshAngleRadians));
NormalizeFacing(ref forward);
BuildBasisFromForward(forward, out right, out up);
return true;
}
if (TryGetBlockSideFacing(block, out forward))
{
BuildBasisFromForward(forward, out right, out up);
return true;
}
if (!TryGetPlayerFacing(blockEntity.Pos, out forward))
return false;
BuildBasisFromForward(forward, out right, out up);
return true;
}
private bool TryGetPlayerFacing(BlockPos blockPos, out Vec3f facing)
{
facing = new Vec3f(0f, 0f, 1f);
EntityPlayer? playerEntity = api.World.Player?.Entity;
if (playerEntity?.CameraPos == null)
return false;
Vec3d cameraPos = playerEntity.CameraPos + playerEntity.LocalEyePos;
double dx = cameraPos.X - (blockPos.X + 0.5d);
double dz = cameraPos.Z - (blockPos.Z + 0.5d);
if (Math.Abs(dx) <= 0.0001d && Math.Abs(dz) <= 0.0001d)
return false;
double absDx = Math.Abs(dx);
double absDz = Math.Abs(dz);
double diagonalTolerance = Math.Max(absDx, absDz) * 0.65d;
bool isDiagonalView = Math.Abs(absDx - absDz) <= diagonalTolerance;
if (isDiagonalView)
{
Vec3f facingX = dx >= 0d ? new Vec3f(1f, 0f, 0f) : new Vec3f(-1f, 0f, 0f);
Vec3f facingZ = dz >= 0d ? new Vec3f(0f, 0f, 1f) : new Vec3f(0f, 0f, -1f);
int obstructionX = GetFacingObstructionScore(blockPos, facingX);
int obstructionZ = GetFacingObstructionScore(blockPos, facingZ);
if (obstructionX != obstructionZ)
{
facing = obstructionX < obstructionZ ? facingX : facingZ;
return true;
}
}
facing = absDx >= absDz
? (dx >= 0d ? new Vec3f(1f, 0f, 0f) : new Vec3f(-1f, 0f, 0f))
: (dz >= 0d ? new Vec3f(0f, 0f, 1f) : new Vec3f(0f, 0f, -1f));
return true;
}
private int GetFacingObstructionScore(BlockPos blockPos, Vec3f facing)
{
int offsetX = Math.Sign(facing.X);
int offsetZ = Math.Sign(facing.Z);
int score = 0;
for (int yOffset = 0; yOffset <= 1; yOffset++)
{
BlockPos checkPos = new(blockPos.X + offsetX, blockPos.Y + yOffset, blockPos.Z + offsetZ);
if (IsSolidBlock(checkPos))
score++;
}
return score;
}
private bool IsSolidBlock(BlockPos blockPos)
{
Block block = api.World.BlockAccessor.GetBlock(blockPos);
if (block.Id == 0)
return false;
Cuboidf[]? collisionBoxes = block.GetCollisionBoxes(api.World.BlockAccessor, blockPos);
return collisionBoxes is { Length: > 0 };
}
private static bool TryGetBlockSideFacing(Block block, out Vec3f facing)
{
facing = new Vec3f(0f, 0f, 1f);
string? side = block.Variant?["side"];
if (string.IsNullOrWhiteSpace(side))
return false;
switch (side.ToLowerInvariant())
{
case "north":
facing = new Vec3f(0f, 0f, -1f);
return true;
case "south":
facing = new Vec3f(0f, 0f, 1f);
return true;
case "east":
facing = new Vec3f(1f, 0f, 0f);
return true;
case "west":
facing = new Vec3f(-1f, 0f, 0f);
return true;
case "up":
facing = new Vec3f(0f, 1f, 0f);
return true;
case "down":
facing = new Vec3f(0f, -1f, 0f);
return true;
default:
return false;
}
}
private static void BuildBasisFromForward(Vec3f forward, out Vec3f right, out Vec3f up)
{
up = new Vec3f(0f, 1f, 0f);
if (Math.Abs(forward.Y) > 0.5f)
{
up = forward.Y > 0f ? new Vec3f(0f, 0f, -1f) : new Vec3f(0f, 0f, 1f);
right = new Vec3f(1f, 0f, 0f);
return;
}
right = new Vec3f(-forward.Z, 0f, forward.X);
}
private static void NormalizeFacing(ref Vec3f facing)
{
float length = MathF.Sqrt(facing.X * facing.X + facing.Y * facing.Y + facing.Z * facing.Z);
if (length <= 0.0001f)
{
facing = new Vec3f(0f, 0f, 1f);
return;
}
float invLength = 1f / length;
facing = new Vec3f(facing.X * invLength, facing.Y * invLength, facing.Z * invLength);
}
private static bool TryGetMeshAngle(BlockEntity blockEntity, out float radians)
{
if (blockEntity is BlockEntityGenericTypedContainer genericContainer)
{
radians = genericContainer.MeshAngle;
return true;
}
if (blockEntity is BlockEntityCrate crate)
{
radians = crate.MeshAngle;
return true;
}
radians = 0f;
return false;
}
}
using ChestPreview.Configs;
using ChestPreview.Models;
using ChestPreview.Utils;
using System;
using System.Collections.Generic;
using System.Numerics;
using Vintagestory.API.Client;
using Vintagestory.API.Common;
using Vintagestory.API.MathTools;
using Vintagestory.GameContent;
namespace ChestPreview.Rendering;
internal class WorldBillboardPresenter(ICoreClientAPI api)
{
private const int multiblock_scan_radius = 2;
private static readonly float front_face_offset = 0.05f;
private readonly ICoreClientAPI api = api;
private readonly List<BillboardTarget> frameBillboards = [];
public IReadOnlyList<BillboardTarget> FrameBillboards => frameBillboards;
public void PrepareFrame(List<PreviewTarget> previewTargets)
{
frameBillboards.Clear();
foreach (PreviewTarget previewTarget in previewTargets)
{
if (!CanPresentAsBillboard(previewTarget))
continue;
if (!TryCreateBillboardTarget(previewTarget, out BillboardTarget billboardTarget))
continue;
frameBillboards.Add(billboardTarget);
}
}
public void Clear()
{
frameBillboards.Clear();
}
private static bool CanPresentAsBillboard(PreviewTarget previewTarget)
{
return previewTarget.Mode is PreviewModes.ON_HOVERED_CONTAINER or PreviewModes.ON_NEARBY_CONTAINERS;
}
private bool TryCreateBillboardTarget(PreviewTarget previewTarget, out BillboardTarget billboardTarget)
{
return TryCreateFrontBillboardTarget(previewTarget, out billboardTarget);
}
private bool TryCreateFrontBillboardTarget(PreviewTarget previewTarget, out BillboardTarget billboardTarget)
{
billboardTarget = default;
Block block = previewTarget.Block;
BlockPos blockPos = previewTarget.BlockEntity.Pos;
GetContainerBounds(block, blockPos, out Vector3 min, out Vector3 max);
if (!TryResolveFacingNormal(previewTarget.Block, previewTarget.BlockEntity, out Vec3f forward, out Vec3f right, out Vec3f up))
return false;
Vector3 centerLocal = (min + max) * 0.5f;
Vector3 halfExtents = (max - min) * 0.5f;
Vector3 rightVector = right.ToVector3();
Vector3 upVector = up.ToVector3();
Vector3 forwardVector = forward.ToVector3();
float halfWidth = Vector3.Dot(Vector3.Abs(rightVector), halfExtents);
float halfHeight = Vector3.Dot(Vector3.Abs(upVector), halfExtents);
float halfDepth = ResolveStableHalfDepth(forwardVector, halfExtents);
float width = halfWidth * 2f;
float height = halfHeight * 2f;
Vector3 center = blockPos.ToVector3() + centerLocal + forwardVector * (halfDepth + front_face_offset);
width = Math.Max(0.05f, width);
height = Math.Max(0.05f, height);
billboardTarget = new BillboardTarget(previewTarget, center.ToVec3d(), right, up, forward, width, height, true);
return true;
}
private static float ResolveStableHalfDepth(Vector3 forward, Vector3 halfExtents)
{
if (MathF.Abs(forward.Y) < 0.5f)
return MathF.Abs(forward.X) >= MathF.Abs(forward.Z) ? halfExtents.X : halfExtents.Z;
return MathF.Abs(forward.Y) >= MathF.Abs(forward.Z) ? halfExtents.Y : halfExtents.Z;
}
private void GetContainerBounds(Block block, BlockPos blockPos, out Vector3 min, out Vector3 max)
{
min = new Vector3(0f, 0f, 0f);
max = new Vector3(1f, 1f, 1f);
ExpandBoundsFromSelectionBoxes(block, blockPos, blockPos, ref min, ref max);
ExpandBoundsFromLinkedMultiblockParts(blockPos, ref min, ref max);
}
private void ExpandBoundsFromLinkedMultiblockParts(BlockPos controllerPos, ref Vector3 min, ref Vector3 max)
{
int minScanX = controllerPos.X - multiblock_scan_radius;
int maxScanX = controllerPos.X + multiblock_scan_radius;
int minScanY = controllerPos.Y - multiblock_scan_radius;
int maxScanY = controllerPos.Y + multiblock_scan_radius;
int minScanZ = controllerPos.Z - multiblock_scan_radius;
int maxScanZ = controllerPos.Z + multiblock_scan_radius;
Vector3 minLocal = min;
Vector3 maxLocal = max;
api.World.BlockAccessor.WalkBlocks(new BlockPos(minScanX, minScanY, minScanZ), new BlockPos(maxScanX, maxScanY, maxScanZ), (partBlock, x, y, z) =>
{
if (x == controllerPos.X && y == controllerPos.Y && z == controllerPos.Z)
return;
if (partBlock.Id == 0 || partBlock is not IMultiblockOffset multiblockOffset)
return;
BlockPos partPos = new(x, y, z);
BlockPos linkedControllerPos = multiblockOffset.GetControlBlockPos(partPos);
if (!IsSameBlockPos(linkedControllerPos, controllerPos))
return;
ExpandBoundsFromSelectionBoxes(partBlock, partPos, controllerPos, ref minLocal, ref maxLocal);
});
min = minLocal;
max = maxLocal;
}
private void ExpandBoundsFromSelectionBoxes(Block block, BlockPos sourcePos, BlockPos originPos, ref Vector3 min, ref Vector3 max)
{
Cuboidf[]? selectionBoxes = block.GetSelectionBoxes(api.World.BlockAccessor, sourcePos);
Vector3 sourceOffset = sourcePos.ToVector3() - originPos.ToVector3();
if (selectionBoxes is not { Length: > 0 })
{
min = Vector3.Min(min, sourceOffset);
max = Vector3.Max(max, sourceOffset + Vector3.One);
return;
}
foreach (Cuboidf selectionBox in selectionBoxes)
{
min = Vector3.Min(min, sourceOffset + selectionBox.Start.ToVector3());
max = Vector3.Max(max, sourceOffset + selectionBox.End.ToVector3());
}
}
private static bool IsSameBlockPos(BlockPos a, BlockPos b)
{
return a.X == b.X && a.Y == b.Y && a.Z == b.Z;
}
private bool TryResolveFacingNormal(Block block, BlockEntity blockEntity, out Vec3f forward, out Vec3f right, out Vec3f up)
{
forward = new Vec3f(0f, 0f, 1f);
right = new Vec3f(1f, 0f, 0f);
up = new Vec3f(0f, 1f, 0f);
if (TryGetMeshAngle(blockEntity, out float meshAngleRadians))
{
// MeshAngle is around Y axis in radians.
forward = new Vec3f(MathF.Sin(meshAngleRadians), 0f, MathF.Cos(meshAngleRadians));
NormalizeFacing(ref forward);
BuildBasisFromForward(forward, out right, out up);
return true;
}
if (TryGetBlockSideFacing(block, out forward))
{
BuildBasisFromForward(forward, out right, out up);
return true;
}
if (!TryGetPlayerFacing(blockEntity.Pos, out forward))
return false;
BuildBasisFromForward(forward, out right, out up);
return true;
}
private bool TryGetPlayerFacing(BlockPos blockPos, out Vec3f facing)
{
facing = new Vec3f(0f, 0f, 1f);
EntityPlayer? playerEntity = api.World.Player?.Entity;
if (playerEntity?.CameraPos == null)
return false;
Vec3d cameraPos = playerEntity.CameraPos + playerEntity.LocalEyePos;
double dx = cameraPos.X - (blockPos.X + 0.5d);
double dz = cameraPos.Z - (blockPos.Z + 0.5d);
if (Math.Abs(dx) <= 0.0001d && Math.Abs(dz) <= 0.0001d)
return false;
double absDx = Math.Abs(dx);
double absDz = Math.Abs(dz);
double diagonalTolerance = Math.Max(absDx, absDz) * 0.65d;
bool isDiagonalView = Math.Abs(absDx - absDz) <= diagonalTolerance;
if (isDiagonalView)
{
Vec3f facingX = dx >= 0d ? new Vec3f(1f, 0f, 0f) : new Vec3f(-1f, 0f, 0f);
Vec3f facingZ = dz >= 0d ? new Vec3f(0f, 0f, 1f) : new Vec3f(0f, 0f, -1f);
int obstructionX = GetFacingObstructionScore(blockPos, facingX);
int obstructionZ = GetFacingObstructionScore(blockPos, facingZ);
if (obstructionX != obstructionZ)
{
facing = obstructionX < obstructionZ ? facingX : facingZ;
return true;
}
}
facing = absDx >= absDz
? (dx >= 0d ? new Vec3f(1f, 0f, 0f) : new Vec3f(-1f, 0f, 0f))
: (dz >= 0d ? new Vec3f(0f, 0f, 1f) : new Vec3f(0f, 0f, -1f));
return true;
}
private int GetFacingObstructionScore(BlockPos blockPos, Vec3f facing)
{
int offsetX = Math.Sign(facing.X);
int offsetZ = Math.Sign(facing.Z);
int score = 0;
for (int yOffset = 0; yOffset <= 1; yOffset++)
{
BlockPos checkPos = new(blockPos.X + offsetX, blockPos.Y + yOffset, blockPos.Z + offsetZ);
if (IsSolidBlock(checkPos))
score++;
}
return score;
}
private bool IsSolidBlock(BlockPos blockPos)
{
Block block = api.World.BlockAccessor.GetBlock(blockPos);
if (block.Id == 0)
return false;
Cuboidf[]? collisionBoxes = block.GetCollisionBoxes(api.World.BlockAccessor, blockPos);
return collisionBoxes is { Length: > 0 };
}
private static bool TryGetBlockSideFacing(Block block, out Vec3f facing)
{
facing = new Vec3f(0f, 0f, 1f);
string? side = block.Variant?["side"];
if (string.IsNullOrWhiteSpace(side))
return false;
switch (side.ToLowerInvariant())
{
case "north":
facing = new Vec3f(0f, 0f, 1f);
return true;
case "south":
facing = new Vec3f(0f, 0f, -1f);
return true;
case "east":
facing = new Vec3f(-1f, 0f, 0f);
return true;
case "west":
facing = new Vec3f(1f, 0f, 0f);
return true;
case "up":
facing = new Vec3f(0f, 1f, 0f);
return true;
case "down":
facing = new Vec3f(0f, -1f, 0f);
return true;
default:
return false;
}
}
private static void BuildBasisFromForward(Vec3f forward, out Vec3f right, out Vec3f up)
{
up = new Vec3f(0f, 1f, 0f);
if (Math.Abs(forward.Y) > 0.5f)
{
up = forward.Y > 0f ? new Vec3f(0f, 0f, -1f) : new Vec3f(0f, 0f, 1f);
right = new Vec3f(1f, 0f, 0f);
return;
}
right = new Vec3f(-forward.Z, 0f, forward.X);
}
private static void NormalizeFacing(ref Vec3f facing)
{
float length = MathF.Sqrt(facing.X * facing.X + facing.Y * facing.Y + facing.Z * facing.Z);
if (length <= 0.0001f)
{
facing = new Vec3f(0f, 0f, 1f);
return;
}
float invLength = 1f / length;
facing = new Vec3f(facing.X * invLength, facing.Y * invLength, facing.Z * invLength);
}
private static bool TryGetMeshAngle(BlockEntity blockEntity, out float radians)
{
if (blockEntity is BlockEntityGenericTypedContainer genericContainer)
{
radians = genericContainer.MeshAngle;
return true;
}
if (blockEntity is BlockEntityCrate crate)
{
radians = crate.MeshAngle;
return true;
}
radians = 0f;
return false;
}
}

View File

@@ -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
}
]
}

View File

@@ -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."
}

View File

@@ -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"
},

View File

@@ -4,6 +4,9 @@
<TargetFramework>net8.0</TargetFramework>
<RunWorkingDirectory>$(MSBuildProjectDirectory)</RunWorkingDirectory>
<Configurations>Debug;Release;Version22</Configurations>
<VS_CONFIGLIB Condition="'$(VS_CONFIGLIB)' == ''">$([System.Environment]::GetEnvironmentVariable('VS_CONFIGLIB'))</VS_CONFIGLIB>
<ConfigLibAvailable Condition="'$(VS_CONFIGLIB)' != '' and Exists('$(VS_CONFIGLIB)')">true</ConfigLibAvailable>
<DefineConstants Condition="'$(ConfigLibAvailable)' == 'true'">$(DefineConstants);CONFIGLIB</DefineConstants>
</PropertyGroup>
<ItemGroup>
@@ -17,4 +20,15 @@
<HintPath>$(VINTAGE_STORY)/VintagestoryAPI.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Reference Include="configlib" Condition="'$(ConfigLibAvailable)' == 'true'">
<HintPath>$(VS_CONFIGLIB)</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
<Target Name="WarnWithoutConfigLib" BeforeTargets="CoreCompile" Condition="'$(ConfigLibAvailable)' != 'true'">
<Warning Text="No VS_CONFIGLIB - will compile without supporting ConfigLib. Set VS_CONFIGLIB env var to a path that contains the configlib's dlls." />
</Target>
</Project>

0
ChestPreview/build.sh Normal file → Executable file
View File