public async Task InjectVillagerAsync(int index, string internalName) { if (!Globals.Bot.Config.DodoModeConfig.LimitedDodoRestoreOnlyMode) { await ReplyAsync($"{Context.User.Mention} - Villagers cannot be injected in order mode."); return; } if (!Globals.Bot.Config.AllowVillagerInjection) { await ReplyAsync($"{Context.User.Mention} - Villager injection is currently disabled."); return; } var bot = Globals.Bot; var nameSearched = internalName; if (!VillagerResources.IsVillagerDataKnown(internalName)) { internalName = GameInfo.Strings.VillagerMap.FirstOrDefault(z => string.Equals(z.Value, internalName, StringComparison.InvariantCultureIgnoreCase)).Key; } if (internalName == default) { await ReplyAsync($"{Context.User.Mention} - {nameSearched} is not a valid internal villager name."); return; } if (index > byte.MaxValue || index < 0) { await ReplyAsync($"{Context.User.Mention} - {index} is not a valid index"); return; } int slot = index; var replace = VillagerResources.GetVillager(internalName); var user = Context.User; var mention = Context.User.Mention; var request = new VillagerRequest(Context.User.Username, replace, (byte)index, GameInfo.Strings.GetVillager(internalName)) { OnFinish = success => { var reply = success ? $"Villager has been injected by the bot at Index {slot}. Please go talk to them!" : "Failed to inject villager. Please tell the bot owner to look at the logs!"; Task.Run(async() => await ReplyAsync($"{mention}: {reply}").ConfigureAwait(false)); } }; bot.VillagerInjections.Enqueue(request); var msg = $"{mention}: Villager inject request has been added to the queue and will be injected momentarily. I will reply to you once this has completed."; await ReplyAsync(msg).ConfigureAwait(false); }
private async Task InjectVillagers(int startIndex, string[] villagerNames) { if (!Globals.Bot.Config.DodoModeConfig.LimitedDodoRestoreOnlyMode) { await ReplyAsync($"{Context.User.Mention} - Villagers cannot be injected in order mode.").ConfigureAwait(false); return; } if (!Globals.Bot.Config.AllowVillagerInjection) { await ReplyAsync($"{Context.User.Mention} - Villager injection is currently disabled.").ConfigureAwait(false); return; } var bot = Globals.Bot; int index = startIndex; int count = villagerNames.Length; if (count < 1) { await ReplyAsync($"{Context.User.Mention} - No villager names in command").ConfigureAwait(false); return; } foreach (var nameLookup in villagerNames) { var internalName = nameLookup; var nameSearched = internalName; if (!VillagerResources.IsVillagerDataKnown(internalName)) { internalName = GameInfo.Strings.VillagerMap.FirstOrDefault(z => string.Equals(z.Value, internalName, StringComparison.InvariantCultureIgnoreCase)).Key; } if (internalName == default) { await ReplyAsync($"{Context.User.Mention} - {nameSearched} is not a valid internal villager name."); return; } if (index > byte.MaxValue || index < 0) { await ReplyAsync($"{Context.User.Mention} - {index} is not a valid index"); return; } int slot = index; var replace = VillagerResources.GetVillager(internalName); var user = Context.User; var mention = Context.User.Mention; var extraMsg = string.Empty; if (VillagerOrderParser.IsUnadoptable(internalName)) { extraMsg += " Please note that you will not be able to adopt this villager."; } var request = new VillagerRequest(Context.User.Username, replace, (byte)index, GameInfo.Strings.GetVillager(internalName)) { OnFinish = success => { var reply = success ? $"{nameSearched} has been injected by the bot at Index {slot}. Please go talk to them!{extraMsg}" : "Failed to inject villager. Please tell the bot owner to look at the logs!"; Task.Run(async() => await ReplyAsync($"{mention}: {reply}").ConfigureAwait(false)); } }; bot.VillagerInjections.Enqueue(request); index = (index + 1) % 10; } var addMsg = count > 1 ? $"Villager inject request for {count} villagers have" : "Villager inject request has"; var msg = $"{Context.User.Mention}: {addMsg} been added to the queue and will be injected momentarily. I will reply to you once this has completed."; await ReplyAsync(msg).ConfigureAwait(false); }
public async Task <bool> InjectVillager(VillagerRequest vr, CancellationToken token) { var vd = vr.Item; var index = vr.Index; if (VillagerHouses == null || VillagerShells == null || Connection == null || Bot == null) { return(false); } VillagerHouse?house = VillagerHouses.Find(x => x.NPC1 == (sbyte)index); if (house == null) { LogUtil.LogInfo($"House {index} requested but does not exist. Injecting to first available house", Bot.Config.IP); for (int i = 0; i < VillagerHouses.Count; ++i) { if (VillagerHouses[i].HouseStatus == 0) { house = VillagerHouses[i]; break; } } } if (house == null) { LogUtil.LogInfo($"No house found to inject to.", Bot.Config.IP); return(false); } var houseIndex = VillagerHouses.IndexOf(house); var villagerToInject = (byte[])vd.Villager.Clone(); var villagerAsVillager = new Villager2(villagerToInject); LogUtil.LogInfo($"Beginning injection for: {GameInfo.Strings.GetVillager(villagerAsVillager.InternalName)}", Bot.Config.IP); // update moving out state villagerAsVillager.MoveType = 0; villagerAsVillager.MovingOut = true; ushort[] flags = villagerAsVillager.GetEventFlagsSave(); flags[05] = 1; // flag 5 = MoveInCompletion flags[09] = 0; // flag 9 = AbandonedHouse flags[24] = 0; // flag 24 = ForceMoveOut villagerAsVillager.SetEventFlagsSave(flags); var houseToInject = (byte[])vd.House.Clone(); var houseAsHouse = new VillagerHouse(houseToInject) { HouseStatus = house.HouseStatus == 0 ? 2 : house.HouseStatus, NPC1 = house.NPC1 == -1 ? (sbyte)index : house.NPC1, NPC2 = house.NPC2 }; // inject villager await Connection.WriteBytesAsync(villagerAsVillager.Data, (uint)OffsetHelper.VillagerAddress + ((uint)index *Villager2.SIZE), token).ConfigureAwait(false); await Connection.WriteBytesAsync(villagerAsVillager.Data, (uint)OffsetHelper.VillagerAddress + ((uint)index *Villager2.SIZE) + (uint)OffsetHelper.BackupSaveDiff, token).ConfigureAwait(false); // inject house await Connection.WriteBytesAsync(houseAsHouse.Data, (uint)OffsetHelper.VillagerHouseAddress + ((uint)houseIndex *VillagerHouse.SIZE), token).ConfigureAwait(false); await Connection.WriteBytesAsync(houseAsHouse.Data, (uint)OffsetHelper.VillagerHouseAddress + ((uint)houseIndex *VillagerHouse.SIZE) + (uint)OffsetHelper.BackupSaveDiff, token).ConfigureAwait(false); LogUtil.LogInfo($"Villager injection complete.", Bot.Config.IP); vr.OnFinish?.Invoke(true); return(true); }