public override void AI()
 {
     //npc.active = false;
     SafeToUnlock = true;
     for (int k = 0; k < 200; k++)
     {
         if (Main.npc[k].active && !Main.npc[k].friendly && Main.npc[k].lifeMax > 5)
         {
             if (Main.npc[k].Distance(npc.Center) < 300f)
             {
                 SafeToUnlock = false;
             }
         }
     }
     for (int k = 0; k < 200; k++)
     {
         Player player = Main.player[k];
         if (player.active && !player.dead)
         {
             Rectangle itemRect = player.GetModPlayer <KeyPlayer>().itemRectangle;
             itemRect.Inflate(itemRect.Width / 2, 0);
             if (!Unlocked && SafeToUnlock && player.altFunctionUse != 2 && player.itemAnimation > 0 && player.HeldItem.GetGlobalItem <KeyItem>().IsKeybrand&& itemRect.Intersects(npc.getRect()) && Collision.CanHit(player.GetModPlayer <KeyPlayer>().itemRectangle.Center.ToVector2(), 0, 0, npc.Center, 0, 0))
             {
                 Unlocked      = true;
                 npc.netUpdate = true;
             }
         }
     }
     if (Unlocked)
     {
         npc.localAI[0]++;
     }
     if (npc.localAI[0] == 1)
     {
         Main.PlaySound(mod.GetLegacySoundSlot(SoundType.Custom, "Sounds/ChestOpen").WithVolume(0.5f), npc.Center);
     }
     else if (npc.localAI[0] == 10)
     {
         npc.rarity = 0;
         #region Munny
         if (NPC.downedMoonlord)
         {
             MaxMunny = 500;
         }
         else if (NPC.downedPlantBoss)
         {
             MaxMunny = 300;
         }
         else if (Main.hardMode)
         {
             MaxMunny = 150;
         }
         else
         {
             MaxMunny = 75;
         }
         int DropAmount = 1;
         if (KeyUtils.RandPercent(.05f))
         {
             DropAmount = Main.rand.Next(MaxMunny / 2, MaxMunny + 1);
         }
         else if (KeyUtils.RandPercent(.25f))
         {
             DropAmount = Main.rand.Next(MaxMunny / 4, MaxMunny / 2 + 1);
         }
         else if (KeyUtils.RandPercent(.5f))
         {
             DropAmount = Main.rand.Next(MaxMunny / 10, MaxMunny / 4 + 1);
         }
         else
         {
             DropAmount = Main.rand.Next(1, MaxMunny / 10 + 1);
         }
         if (KeyUtils.RandPercent(.1f))
         {
             if (KeyUtils.RandPercent(.05f))
             {
                 DropAmount += Main.rand.Next(MaxMunny / 2, MaxMunny + 1);
             }
             else if (KeyUtils.RandPercent(.25f))
             {
                 DropAmount += Main.rand.Next(MaxMunny / 4, MaxMunny / 2 + 1);
             }
             else if (KeyUtils.RandPercent(.5f))
             {
                 DropAmount += Main.rand.Next(MaxMunny / 10, MaxMunny / 4 + 1);
             }
             else
             {
                 DropAmount += Main.rand.Next(1, MaxMunny / 10 + 1);
             }
         }
         if (DropAmount > (int)((float)MaxMunny * 1.5f))
         {
             DropAmount = (int)((float)MaxMunny * 1.5f);
         }
         KeyUtils.NewSyncedItem(npc.getRect(), ItemType <Items.Currency.Munny>(), DropAmount);
         #endregion
         #region Synthesis Materials
         if (KeyUtils.RandPercent(NPC.downedMoonlord ? .1f : .01f))
         {
             if (KeyUtils.RandPercent(.01f))
             {
                 KeyUtils.NewSyncedItem(npc.getRect(), ItemType <Items.Synthesis.Other.Zanithite>());
             }
             else
             {
                 KeyUtils.NewSyncedItem(npc.getRect(), ItemType <Items.Synthesis.Other.Zenithite>());
             }
         }
         #endregion
         #region Other Materials
         if (NPC.downedPlantBoss || (NPC.downedMechBoss1 && NPC.downedMechBoss2 && NPC.downedMechBoss3))
         {
             if (KeyUtils.RandPercent(.15f))
             {
                 KeyUtils.NewSyncedItem(npc.getRect(), ItemType <Items.Materials.BrokenHeroKeybrand>());
             }
             if (KeyUtils.RandPercent(.3f))
             {
                 KeyUtils.NewSyncedItem(npc.getRect(), ItemType <Items.Materials.WarriorFragment>(), Main.rand.Next(3, 8));
             }
             if (KeyUtils.RandPercent(.3f))
             {
                 KeyUtils.NewSyncedItem(npc.getRect(), ItemType <Items.Materials.GuardianFragment>(), Main.rand.Next(3, 8));
             }
             if (KeyUtils.RandPercent(.3f))
             {
                 KeyUtils.NewSyncedItem(npc.getRect(), ItemType <Items.Materials.MysticFragment>(), Main.rand.Next(3, 8));
             }
         }
         else if (KeyUtils.RandPercent(.15f))
         {
             KeyUtils.NewSyncedItem(npc.getRect(), ItemType <Items.Materials.KeybrandMold>());
         }
         #endregion
         npc.netUpdate = true;
     }
     else if (npc.localAI[0] > 30)
     {
         npc.alpha += 5;
     }
 }