daemitus / SomethingNeedDoing

91 stars 64 forks source link

target problem #5

Closed LittleNightmare closed 2 years ago

LittleNightmare commented 3 years ago

sometimes, when I switching between /target Eirikur and /target Moyce, there is a System.NullReferenceException. Once crash the game. I rewrite /target function like to the code of SimpleTweks below (just work, not very well). it working now. However, maybe just another language problem because I use it on cn server.

        private void ProcessTargetCommand(string step)
        {
            var match = TARGET_COMMAND.Match(step);
            if (!match.Success)
                throw new InvalidMacroOperationException("Syntax error");

            var actorName = match.Groups["name"].Value.Trim(new char[] { ' ', '"', '\'' }).ToLowerInvariant();
            if (actorName.StartsWith("<") && actorName.EndsWith(">"))
            {
                plugin.ChatManager.SendChatBoxMessage(step);
                return;
            }
            Dalamud.Game.ClientState.Actors.Types.Actor closestMatch = null;
            // var closestDistance = float.MaxValue;
            // var player = plugin.Interface.ClientState.LocalPlayer;
            try
            {
                foreach (var actor in plugin.Interface.ClientState.Actors)
                {
                    if (actor == null) continue;
                    if (actor.Name.ToLowerInvariant().Contains(actorName))
                    {
                        //var distance = Vector3.Distance(player.Position, actor.Position);
                        if (closestMatch == null)
                        {
                            closestMatch = actor;
                            //closestDistance = distance;
                            break;
                        }

                    }

                }
            }
            catch (InvalidOperationException)
            {
                throw new InvalidMacroOperationException($"Unknown actor");
            }

            /*
            try
            {
                npc = plugin.Interface.ClientState.Actors.Where(actor => actor.Name.ToLower() == actorName).First();
            }
            catch (InvalidOperationException)
            {
                throw new InvalidMacroOperationException($"Unknown actor");
            }
            */
            if (closestMatch != null)
            {
                plugin.Interface.ClientState.Targets.SetCurrentTarget(closestMatch);
            }

        }
daemitus commented 2 years ago

i think? this has been fixed.