public override void Execute(CommandEntry entry) { if (entry.Arguments.Count < 1) { ShowUsage(entry); } else { string count = entry.GetArgument(0); if (count == "\0CALLBACK") { if (entry.BlockOwner.Command.Name == "repeat" || entry.BlockOwner.Block == null || entry.BlockOwner.Block.Count == 0 || entry.BlockOwner.Block[entry.BlockOwner.Block.Count - 1] != entry) { RepeatCommandData data = (RepeatCommandData)entry.BlockOwner.Data; data.Index++; if (data.Index > data.Total) { entry.Good("Repeating ending, reached target."); } else { entry.Good("Repeating at index <{color.emphasis}>" + data.Index + "/" + data.Total + "<{color.base}>..."); entry.Queue.SetVariable("repeat_index", new TextTag(data.Index.ToString())); entry.Queue.SetVariable("repeat_total", new TextTag(data.Total.ToString())); entry.Queue.AddCommandsNow(entry.BlockOwner.Block); } } else { entry.Bad("Repeat CALLBACK invalid: not a real callback!"); } } else if (count.ToLower() == "stop") { bool hasnext = false; for (int i = 0; i < entry.Queue.CommandList.Length; i++) { if (entry.Queue.GetCommand(i).Command is RepeatCommand && entry.Queue.GetCommand(i).Arguments[0] == "\0CALLBACK") { hasnext = true; break; } } if (hasnext) { entry.Good("Stopping repeat loop."); while (entry.Queue.CommandList.Length > 0) { if (entry.Queue.GetCommand(0).Command is RepeatCommand && entry.Queue.GetCommand(0).Arguments[0] == "\0CALLBACK") { entry.Queue.RemoveCommand(0); break; } entry.Queue.RemoveCommand(0); } } else { entry.Bad("Cannot stop repeat: not in one!"); } } else if (count.ToLower() == "next") { bool hasnext = false; for (int i = 0; i < entry.Queue.CommandList.Length; i++) { if (entry.Queue.GetCommand(i).Command is RepeatCommand && entry.Queue.GetCommand(i).Arguments[0] == "\0CALLBACK") { hasnext = true; break; } } if (hasnext) { entry.Good("Skipping to next repeat entry..."); while (entry.Queue.CommandList.Length > 0) { if (entry.Queue.GetCommand(0).Command is RepeatCommand && entry.Queue.GetCommand(0).Arguments[0] == "\0CALLBACK") { break; } entry.Queue.RemoveCommand(0); } } else { entry.Bad("Cannot stop repeat: not in one!"); } } else { int target = StringToInt(count); if (target <= 0) { entry.Good("Not repeating."); return; } RepeatCommandData data = new RepeatCommandData(); data.Total = target; data.Index = 1; entry.Data = data; if (entry.Block != null) { entry.Good("Repeating <{color.emphasis}>" + target + "<{color.base}> times..."); CommandEntry callback = new CommandEntry("repeat \0CALLBACK", null, entry, this, new List<string> { "\0CALLBACK" }, "repeat", 0); entry.Block.Add(callback); entry.Queue.SetVariable("repeat_index", new TextTag("1")); entry.Queue.SetVariable("repeat_total", new TextTag(target.ToString())); entry.Queue.AddCommandsNow(entry.Block); } else { entry.Bad("Repeat invalid: No block follows!"); } } } }
public override AbstractCommandEntryData Duplicate() { RepeatCommandData toret = new RepeatCommandData(); toret.Index = Index; toret.Total = Total; return toret; }