Files
ChestPreview/ChestPreview/ChestPreview/PreviewTargetProvider.cs
2026-03-15 23:53:23 +01:00

331 lines
11 KiB
C#

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();
}