93 lines
2.6 KiB
C#
93 lines
2.6 KiB
C#
using System.Collections.Generic;
|
|
using Vintagestory.API.Client;
|
|
using Vintagestory.API.Common;
|
|
using Vintagestory.API.MathTools;
|
|
|
|
namespace ClaimLinkVisualizer;
|
|
|
|
internal static class ClaimLinkVisualizerClient
|
|
{
|
|
private const int HighlightSlotId = 8341;
|
|
private static ICoreClientAPI capi = null!;
|
|
private static bool visible;
|
|
|
|
internal static void Start(ICoreClientAPI api)
|
|
{
|
|
capi = api;
|
|
|
|
api.Network.RegisterChannel(ClaimLinkVisualizerModSystem.ChannelName)
|
|
.RegisterMessageType<ClaimLinkBufferRequest>()
|
|
.RegisterMessageType<ClaimLinkBufferResponse>()
|
|
.SetMessageHandler<ClaimLinkBufferResponse>(OnBufferResponse);
|
|
|
|
api.Input.RegisterHotKey(
|
|
"claimlinkvisualizertoggle",
|
|
"Toggle ClaimLink buffer visualization",
|
|
GlKeys.R,
|
|
HotkeyType.CharacterControls,
|
|
ctrlPressed: true
|
|
);
|
|
api.Input.SetHotKeyHandler("claimlinkvisualizertoggle", OnToggle);
|
|
}
|
|
|
|
private static bool OnToggle(KeyCombination comb)
|
|
{
|
|
visible = !visible;
|
|
if (visible)
|
|
capi.Network.GetChannel(ClaimLinkVisualizerModSystem.ChannelName)
|
|
.SendPacket(new ClaimLinkBufferRequest());
|
|
else
|
|
Clear();
|
|
return true;
|
|
}
|
|
|
|
private static void OnBufferResponse(ClaimLinkBufferResponse response)
|
|
{
|
|
if (!visible)
|
|
return;
|
|
|
|
List<BlockPos> positions = new();
|
|
List<int> colors = new();
|
|
|
|
foreach (ClaimLinkBufferEntryDto entry in response.Entries)
|
|
{
|
|
int color = HashToColor(entry.GroupId);
|
|
foreach (BufferCuboidDto cuboid in entry.Cuboids)
|
|
{
|
|
positions.Add(new BlockPos(cuboid.MinX, cuboid.MinY, cuboid.MinZ));
|
|
positions.Add(new BlockPos(cuboid.MaxX, cuboid.MaxY, cuboid.MaxZ));
|
|
colors.Add(color);
|
|
}
|
|
}
|
|
|
|
capi.World.HighlightBlocks(
|
|
capi.World.Player,
|
|
HighlightSlotId,
|
|
positions,
|
|
colors,
|
|
EnumHighlightBlocksMode.Absolute,
|
|
EnumHighlightShape.Cubes
|
|
);
|
|
}
|
|
|
|
private static void Clear() =>
|
|
capi.World.HighlightBlocks(
|
|
capi.World.Player,
|
|
HighlightSlotId,
|
|
new List<BlockPos>(),
|
|
new List<int>()
|
|
);
|
|
|
|
private static int HashToColor(int groupId)
|
|
{
|
|
unchecked
|
|
{
|
|
uint h = (uint)groupId * 2654435761u;
|
|
int r = (byte)(h >> 16);
|
|
int g = (byte)(h >> 8);
|
|
int b = (byte)h;
|
|
return ColorUtil.ColorFromRgba(r, g, b, 255);
|
|
}
|
|
}
|
|
}
|