context: using System;
  using System.Collections.Generic;
  using System.IO;
  using Microsoft.Xna.Framework;
  using Microsoft.Xna.Framework.Graphics;
  using Terraria;
  using Terraria.DataStructures;
  using Terraria.ID;
  using Terraria.ModLoader;
  using ExampleMod.NPCs.PuritySpirit;
  using Terraria.ModLoader.IO;
  using Terraria.GameInput;
  
  namespace ExampleMod
  {
  	public class ExamplePlayer : ModPlayer
  	{
  		private const int saveVersion = 0;
  		public int score = 0;
  		public bool eFlames = false;
  		public bool elementShield = false;
  		public int elementShields = 0;
  		private int elementShieldTimer = 0;
  		public int elementShieldPos = 0;
  		public int lockTime = 0;
  		public bool voidMonolith = false;
  		public int heroLives = 0;
  		public int reviveTime = 0;
  		public int constantDamage = 0;
  		public float percentDamage = 0f;
  		public float defenseEffect = -1f;
  		public bool badHeal = false;
  		public int healHurt = 0;
  		public bool nullified = false;
  		public int purityDebuffCooldown = 0;
  		public bool purityMinion = false;
  		public bool examplePet = false;
  		public bool exampleLightPet = false;
  		public bool exampleShield = false;
  
  		public bool ZoneExample = false;
  
  		public override void ResetEffects()
  		{
  			eFlames = false;
  			elementShield = false;
  			constantDamage = 0;
  			percentDamage = 0f;
  			defenseEffect = -1f;
  			badHeal = false;
  			healHurt = 0;
  			nullified = false;
  			purityMinion = false;
  			examplePet = false;
  			exampleLightPet = false;
  			exampleShield = false;
  		}
  
  		public override void UpdateDead()
  		{
  			eFlames = false;
  			badHeal = false;
  		}
  
  		public override TagCompound Save()
  		{
  			return new TagCompound {
  				{"score", score}
  			};
  			//note that C# 6.0 supports indexer initialisers
  			//return new TagCompound {
  			//	["score"] = score
  			//};
  		}
  
  		public override void Load(TagCompound tag)
  		{
  			score = tag.GetInt("score");
  		}
  
  		public override void LoadLegacy(BinaryReader reader)
  		{
  			int loadVersion = reader.ReadInt32();
  			score = reader.ReadInt32();
  		}
  
  		public override void SetupStartInventory(IList<Item> items)
  		{
  			Item item = new Item();
  			item.SetDefaults(mod.ItemType("ExampleItem"));
  			item.stack = 5;
  			items.Add(item);
  		}
  
  		public override void UpdateBiomes()
  		{
  			ZoneExample = (ExampleWorld.exampleTiles > 50);
  		}
  
  		public override bool CustomBiomesMatch(Player other)
  		{
  			ExamplePlayer modOther = other.GetModPlayer<ExamplePlayer>(mod);
  			return ZoneExample == modOther.ZoneExample;
  		}
  
  		public override void CopyCustomBiomesTo(Player other)
  		{
  			ExamplePlayer modOther = other.GetModPlayer<ExamplePlayer>(mod);
  			modOther.ZoneExample = ZoneExample;
  		}
  
  		public override void SendCustomBiomes(BinaryWriter writer)
  		{
  			BitsByte flags = new BitsByte();
  			flags[0] = ZoneExample;
  			writer.Write(flags);
  		}
  
  		public override void ReceiveCustomBiomes(BinaryReader reader)
  		{
  			BitsByte flags = reader.ReadByte();
  			ZoneExample = flags[0];
  		}
  
  		public override void UpdateBiomeVisuals()
  		{
  			bool usePurity = NPC.AnyNPCs(mod.NPCType("PuritySpirit"));
  			player.ManageSpecialBiomeVisuals("ExampleMod:PuritySpirit", usePurity);
  			bool useVoidMonolith = voidMonolith && !usePurity && !NPC.AnyNPCs(NPCID.MoonLordCore);
  			player.ManageSpecialBiomeVisuals("ExampleMod:MonolithVoid", useVoidMonolith, player.Center);
  		}
  
  		public override Texture2D GetMapBackgroundImage()
  		{
  			if (ZoneExample)
  			{
  				return mod.GetTexture("ExampleBiomeMapBackground");
  			}
  			return null;
  		}
  
  		public override void UpdateBadLifeRegen()
  		{
  			if (eFlames)
  			{
  				if (player.lifeRegen > 0)
  				{
  					player.lifeRegen = 0;
  				}
  				player.lifeRegenTime = 0;
  				player.lifeRegen -= 16;
  			}
  			if (healHurt > 0)
  			{
  				if (player.lifeRegen > 0)
  				{
  					player.lifeRegen = 0;
  				}
  				player.lifeRegenTime = 0;
  				player.lifeRegen -= 120 * healHurt;
  			}
  		}
  
  		public override void ProcessTriggers(TriggersSet triggersSet)
  		{
  			if (ExampleMod.RandomBuffHotKey.JustPressed)
  			{
  				int buff = Main.rand.Next(BuffID.Count);
  				player.AddBuff(buff, 600);
  			}
  		}
  
  		public override void PreUpdateBuffs()
  		{
  			if (heroLives > 0)
  			{
  				bool flag = false;
  				for (int k = 0; k < 200; k++)
  				{
  					NPC npc = Main.npc[k];
  					if (npc.active && npc.type == mod.NPCType("PuritySpirit"))
  					{
  						flag = true;
  						PuritySpiritTeleport(npc);
  						break;
  					}
  				}
  				if (!flag)
  				{
  					heroLives = 0;
  				}
  				if (heroLives == 1)
  				{
  					player.AddBuff(mod.BuffType<Buffs.HeroOne>(), 2); // Consider using this alternate method call for maintainable code.
  				}
  				else if (heroLives == 2)
  				{
  					player.AddBuff(mod.BuffType("HeroTwo"), 2);
  				}
  				else if (heroLives == 3)
  				{
  					player.AddBuff(mod.BuffType("HeroThree"), 3);
  				}
  			}
  			if (purityDebuffCooldown > 0)
  			{
  				purityDebuffCooldown--;
  			}
  		}
  
  		private void PuritySpiritTeleport(NPC npc)
  		{
  			int halfWidth = PuritySpirit.arenaWidth / 2;
  			int halfHeight = PuritySpirit.arenaHeight / 2;
  			Vector2 newPosition = player.position;
  			if (player.position.X <= npc.Center.X - halfWidth)
  			{
  				newPosition.X = npc.Center.X + halfWidth - player.width - 1;
  				while (Collision.SolidCollision(newPosition, player.width, player.height))
  				{
  					newPosition.X -= 16f;
  				}
  			}
  			else if (player.position.X + player.width >= npc.Center.X + halfWidth)
  			{
  				newPosition.X = npc.Center.X - halfWidth + 1;
  				while (Collision.SolidCollision(newPosition, player.width, player.height))
  				{
  					newPosition.X += 16f;
  				}
  			}
  			else if (player.position.Y <= npc.Center.Y - halfHeight)
  			{
  				newPosition.Y = npc.Center.Y + halfHeight - player.height - 1;
  				while (Collision.SolidCollision(newPosition, player.width, player.height))
  				{
  					newPosition.Y -= 16f;
  				}
  			}
  			else if (player.position.Y + player.height >= npc.Center.Y + halfHeight)
  			{
  				newPosition.Y = npc.Center.Y - halfHeight + 1;
  				while (Collision.SolidCollision(newPosition, player.width, player.height))
  				{
  					newPosition.Y += 16f;
  				}
  			}
  			if (newPosition != player.position)
  			{
  				player.Teleport(newPosition, 1, 0);
  				NetMessage.SendData(65, -1, -1, null, 0, player.whoAmI, newPosition.X, newPosition.Y, 1, 0, 0);
  				PuritySpiritDebuff();
  			}
  		}
  
  		public void PuritySpiritDebuff()
  		{
  			bool flag = true;
  			if (Main.rand.Next(2) == 0)
  			{
  				flag = false;
  				for (int k = 0; k < 2; k++)
  				{
  					int buffType;
  					int buffTime;
  					switch (Main.rand.Next(5))
  					{
  						case 0:
  							buffType = BuffID.Darkness;
  							buffTime = 1800;
  							break;
  						case 1:
  							buffType = BuffID.Cursed;
  							buffTime = 900;
  							break;
  						case 2:
  							buffType = BuffID.Confused;
  							buffTime = 1800;
  							break;
  						case 3:
  							buffType = BuffID.Slow;
  							buffTime = 1800;
  							break;
  						default:
  							buffType = BuffID.Silenced;
  							buffTime = 900;
  							break;
  					}
  					if (!player.buffImmune[buffType])
  					{
  						player.AddBuff(buffType, buffTime);
  						return;
  					}
  				}
  			}
  			if (flag || Main.expertMode || Main.rand.Next(2) == 0)
  			{
  				player.AddBuff(mod.BuffType("Undead"), 1800, false);
  			}
  			for (int k = 0; k < 25; k++)
  			{
  				Dust.NewDust(player.position, player.width, player.height, mod.DustType("Negative"), 0f, -1f, 0, default(Color), 2f);
  			}
  		}
  
  		public override void PostUpdateBuffs()
  		{
  			if (nullified)
  			{
  				Nullify();
  			}
  		}
  
  		public override void PostUpdateEquips()
  		{
  			if (nullified)
  			{
  				Nullify();
  			}
  			if (elementShield)
  			{
  				if (elementShields > 0)
  				{
  					elementShieldTimer--;
  					if (elementShieldTimer < 0)
  					{
  						elementShields--;
  						elementShieldTimer = 600;
  					}
  				}
  			}
  			else
  			{
  				elementShields = 0;
  				elementShieldTimer = 0;
  			}
  			elementShieldPos++;
  			elementShieldPos %= 300;
  		}
  
  		public override void PostUpdateMiscEffects()
  		{
  			if (lockTime > 0)
  			{
  				lockTime--;
  			}
  			if (reviveTime > 0)
  			{
  				reviveTime--;
  			}
  		}
  
  		public override void FrameEffects()
  		{
  			if (nullified)
  			{
  				Nullify();
  			}
  		}
  
  		private void Nullify()
  		{
  			player.ResetEffects();
  			player.head = -1;
  			player.body = -1;
  			player.legs = -1;
  			player.handon = -1;
  			player.handoff = -1;
  			player.back = -1;
  			player.front = -1;
  			player.shoe = -1;
  			player.waist = -1;
  			player.shield = -1;
  			player.neck = -1;
  			player.face = -1;
  			player.balloon = -1;
  			nullified = true;
  		}
  
  		public override bool PreHurt(bool pvp, bool quiet, ref int damage, ref int hitDirection, ref bool crit,
  			ref bool customDamage, ref bool playSound, ref bool genGore, ref PlayerDeathReason damageSource)
  		{
  			if (constantDamage > 0 || percentDamage > 0f)
  			{
  				int damageFromPercent = (int)(player.statLifeMax2 * percentDamage);
  				damage = Math.Max(constantDamage, damageFromPercent);
  				customDamage = true;
  			}
  			else if (defenseEffect >= 0f)
  			{
  				if (Main.expertMode)
  				{
  					defenseEffect *= 1.5f;
  				}
  				damage -= (int)(player.statDefense * defenseEffect);
  				if (damage < 0)
  				{
  					damage = 1;
  				}
  				customDamage = true;
  			}
  			constantDamage = 0;
  			percentDamage = 0f;
  			defenseEffect = -1f;
  			return base.PreHurt(pvp, quiet, ref damage, ref hitDirection, ref crit, ref customDamage, ref playSound, ref genGore, ref damageSource);
  		}
  
  		public override void Hurt(bool pvp, bool quiet, double damage, int hitDirection, bool crit)
  		{
  			if (elementShield && damage > 1.0)
  			{
  				if (elementShields < 6)
  				{
  					int k;
  					bool flag = false;
  
gt: 					for (k = 3; k < 8 + player.extraAccessorySlots; k++)