Questionable/GatheringPathRenderer/EditorCommands.cs

223 lines
7.5 KiB
C#
Raw Permalink Normal View History

2024-08-03 01:21:11 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using Dalamud.Game.ClientState.Objects;
using Dalamud.Game.ClientState.Objects.Enums;
using Dalamud.Game.ClientState.Objects.Types;
using Dalamud.Game.Command;
using Dalamud.Plugin.Services;
2024-11-19 14:57:15 +00:00
using Lumina.Excel.Sheets;
2024-08-03 01:21:11 +00:00
using Questionable.Model;
using Questionable.Model.Gathering;
using Questionable.Model.Questing;
2024-08-03 01:21:11 +00:00
namespace GatheringPathRenderer;
internal sealed class EditorCommands : IDisposable
{
private readonly RendererPlugin _plugin;
private readonly IDataManager _dataManager;
private readonly ICommandManager _commandManager;
private readonly ITargetManager _targetManager;
private readonly IClientState _clientState;
private readonly IChatGui _chatGui;
2024-08-03 19:33:52 +00:00
private readonly Configuration _configuration;
2024-08-03 01:21:11 +00:00
public EditorCommands(RendererPlugin plugin, IDataManager dataManager, ICommandManager commandManager,
2024-08-03 19:33:52 +00:00
ITargetManager targetManager, IClientState clientState, IChatGui chatGui, Configuration configuration)
2024-08-03 01:21:11 +00:00
{
_plugin = plugin;
_dataManager = dataManager;
_commandManager = commandManager;
_targetManager = targetManager;
_clientState = clientState;
_chatGui = chatGui;
2024-08-03 19:33:52 +00:00
_configuration = configuration;
2024-08-03 01:21:11 +00:00
_commandManager.AddHandler("/qg", new CommandInfo(ProcessCommand));
}
private void ProcessCommand(string command, string argument)
{
string[] parts = argument.Split(' ');
string subCommand = parts[0];
List<string> arguments = parts.Skip(1).ToList();
try
{
switch (subCommand)
{
case "add":
CreateOrAddLocationToGroup(arguments);
break;
}
}
catch (Exception e)
{
_chatGui.PrintError(e.ToString(), "qG");
}
}
private void CreateOrAddLocationToGroup(List<string> arguments)
{
var target = _targetManager.Target;
if (target == null || target.ObjectKind != ObjectKind.GatheringPoint)
throw new Exception("No valid target");
2024-11-19 14:57:15 +00:00
var gatheringPoint = _dataManager.GetExcelSheet<GatheringPoint>().GetRowOrDefault(target.DataId);
2024-08-03 01:21:11 +00:00
if (gatheringPoint == null)
throw new Exception("Invalid gathering point");
FileInfo targetFile;
GatheringRoot root;
var locationsInTerritory = _plugin.GetLocationsInTerritory(_clientState.TerritoryType).ToList();
2024-11-19 14:57:15 +00:00
var location = locationsInTerritory.SingleOrDefault(x => x.Id == gatheringPoint.Value.GatheringPointBase.RowId);
2024-08-03 01:21:11 +00:00
if (location != null)
{
targetFile = location.File;
root = location.Root;
// if this is an existing node, ignore it
var existingNode = root.Groups.SelectMany(x => x.Nodes.Where(y => y.DataId == target.DataId))
.Any(x => x.Locations.Any(y => Vector3.Distance(y.Position, target.Position) < 0.1f));
if (existingNode)
throw new Exception("Node already exists");
if (arguments.Contains("group"))
AddToNewGroup(root, target);
else
AddToExistingGroup(root, target);
}
else
{
2024-11-19 14:57:15 +00:00
(targetFile, root) = CreateNewFile(gatheringPoint.Value, target);
2024-08-03 01:21:11 +00:00
_chatGui.Print($"Creating new file under {targetFile.FullName}", "qG");
}
_plugin.Save(targetFile, root);
}
public void AddToNewGroup(GatheringRoot root, IGameObject target)
{
root.Groups.Add(new GatheringNodeGroup
{
Nodes =
[
new GatheringNode
{
DataId = target.DataId,
Locations =
[
new GatheringLocation
{
Position = target.Position,
}
]
}
]
});
_chatGui.Print("Added group.", "qG");
}
public void AddToExistingGroup(GatheringRoot root, IGameObject target)
{
// find the same data id
var node = root.Groups.SelectMany(x => x.Nodes)
.SingleOrDefault(x => x.DataId == target.DataId);
if (node != null)
{
node.Locations.Add(new GatheringLocation
{
Position = target.Position,
});
_chatGui.Print($"Added location to existing node {target.DataId}.", "qG");
}
else
{
// find the closest group
var closestGroup = root.Groups
.Select(group => new
{
Group = group,
Distance = group.Nodes.Min(x =>
x.Locations.Min(y =>
Vector3.Distance(_clientState.LocalPlayer!.Position, y.Position)))
})
.OrderBy(x => x.Distance)
.First();
closestGroup.Group.Nodes.Add(new GatheringNode
{
DataId = target.DataId,
Locations =
[
new GatheringLocation
{
Position = target.Position,
}
]
});
_chatGui.Print($"Added new node {target.DataId}.", "qG");
}
}
2024-08-03 15:26:49 +00:00
public (FileInfo targetFile, GatheringRoot root) CreateNewFile(GatheringPoint gatheringPoint, IGameObject target)
2024-08-03 01:21:11 +00:00
{
// determine target folder
DirectoryInfo? targetFolder = _plugin.GetLocationsInTerritory(_clientState.TerritoryType).FirstOrDefault()
?.File.Directory;
if (targetFolder == null)
{
2024-11-19 14:57:15 +00:00
var territoryInfo = _dataManager.GetExcelSheet<TerritoryType>().GetRow(_clientState.TerritoryType);
2024-08-03 01:21:11 +00:00
targetFolder = _plugin.PathsDirectory
2024-11-19 14:57:15 +00:00
.CreateSubdirectory(ExpansionData.ExpansionFolders[(EExpansionVersion)territoryInfo.ExVersion.RowId])
.CreateSubdirectory(territoryInfo.PlaceName.Value.Name.ToString());
2024-08-03 01:21:11 +00:00
}
FileInfo targetFile =
new FileInfo(
2024-08-03 15:26:49 +00:00
Path.Combine(targetFolder.FullName,
2024-11-19 14:57:15 +00:00
$"{gatheringPoint.GatheringPointBase.RowId}_{gatheringPoint.PlaceName.Value.Name}_{(_clientState.LocalPlayer!.ClassJob.RowId == 16 ? "MIN" : "BTN")}.json"));
2024-08-03 01:21:11 +00:00
var root = new GatheringRoot
{
2024-08-03 19:33:52 +00:00
Author = [_configuration.AuthorName],
Steps =
[
new QuestStep
{
TerritoryId = _clientState.TerritoryType,
InteractionType = EInteractionType.None,
}
],
2024-08-03 01:21:11 +00:00
Groups =
[
new GatheringNodeGroup
{
Nodes =
[
new GatheringNode
{
DataId = target.DataId,
Locations =
[
new GatheringLocation
{
Position = target.Position
}
]
}
]
}
]
};
return (targetFile, root);
}
public void Dispose()
{
_commandManager.RemoveHandler("/qg");
}
}