using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace RebirthUtils.Harmony { internal class LootManagerPatches { [HarmonyPatch(typeof(LootManager))] [HarmonyPatch("LootContainerOpened")] public class LootContainerOpened { private static bool Prefix(LootManager __instance, ref TileEntityLootContainer _tileEntity, int _entityIdThatOpenedIt, FastTags _containerTags) { EntityPlayer entityPlayer = (EntityPlayer)GameManager.Instance.World.GetEntity(_entityIdThatOpenedIt); if (entityPlayer == null) { //Log.Out("LootManager-LootContainerOpened Player == null"); return false; } //Log.Out("LootManager-LootContainerOpened START, _tileEntity.lootListName: " + _tileEntity.lootListName); if (GameManager.Instance.World.IsEditor()) { //Log.Out("LootManager-LootContainerOpened 1"); return false; } if (_tileEntity.bTouched) { //Log.Out("LootManager-LootContainerOpened Touched"); return false; } _tileEntity.bTouched = true; _tileEntity.worldTimeTouched = GameManager.Instance.World.GetWorldTime(); LootContainer lootContainer = LootContainer.GetLootContainer(_tileEntity.lootListName); if (lootContainer == null) return false; string lootList = _tileEntity.lootListName; //Log.Out("LootManager-LootContainerOpened lootList: " + lootList); if (lootList.ToLower().Contains("randomloot")) { //Log.Out("LootManager-LootContainerOpened BEFORE lootList: " + lootList); string subString = lootList.Split('-').Last(); //Log.Out("LootManager-LootContainerOpened subString: " + subString); int maxRange = int.Parse(Regex.Match(subString, @"\d+").Value); //Log.Out("LootManager-LootContainerOpened maxRange: " + maxRange); float random = GameManager.Instance.World.GetGameRandom().RandomRange(1, maxRange + 1); //Log.Out("LootManager-LootContainerOpened random: " + random); lootList = lootList.Substring(0, lootList.IndexOf("-")) + random; //Log.Out("LootManager-LootContainerOpened AFTER lootList: " + lootList); lootContainer = LootContainer.GetLootContainer(lootList, true); if (lootContainer == null) { Log.Out("LootManager-LootContainerOpened COULD NOT FIND lootList: " + lootList); return false; } } bool flag = _tileEntity.IsEmpty(); _tileEntity.bTouched = true; _tileEntity.worldTimeTouched = GameManager.Instance.World.GetWorldTime(); if (!flag) { //Log.Out("LootManager-LootContainerOpened is EMPTY"); return false; } entityPlayer.MinEventContext.TileEntity = _tileEntity; entityPlayer.FireEvent(MinEventTypes.onSelfOpenLootContainer, true); float containerMod = 0f; float containerBonus = 0f; if (_tileEntity.EntityId == -1) { //Log.Out("LootManager-LootContainerOpened 6"); try { BlockLoot blockLoot = _tileEntity.blockValue.Block as BlockLoot; containerMod = blockLoot.LootStageMod; containerBonus = blockLoot.LootStageBonus; } catch (Exception ex) { //Log.Out("LootManager-LootContainerOpened Error while attempting to get BlockLoot"); return false; } } int num = lootContainer.useUnmodifiedLootstage ? entityPlayer.unModifiedGameStage : entityPlayer.GetHighestPartyLootStage(containerMod, containerBonus); //Log.Out("LootManager-LootContainerOpened Loot Stage: " + num); IList list = lootContainer.Spawn(__instance.Random, _tileEntity.items.Length, (float)num, 0f, entityPlayer, _containerTags, lootContainer.UniqueItems, lootContainer.IgnoreLootProb); //Log.Out("LootManager-LootContainerOpened list.Count: " + list.Count); for (int i = 0; i < list.Count; i++) { _tileEntity.items[i] = list[i].Clone(); } entityPlayer.FireEvent(MinEventTypes.onSelfLootContainer, true); //Log.Out("LootManager-LootContainerOpened END"); return false; } } } }