Decompiled source of DropTriplo v1.0.0

DropTriplo.dll

Decompiled 5 hours ago
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace DropTriplo;

[BepInPlugin("yuri.DropTriplo", "Drop Triplo", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	private class Marca
	{
		public Vector3 pos;

		public float quando;
	}

	private class Alvo
	{
		public Character bicho;

		public float quando;
	}

	[HarmonyPatch(typeof(Character), "Damage")]
	private static class AoGolpe
	{
		private static void Postfix(Character __instance, HitData hit)
		{
			if (!((Object)(object)Inst == (Object)null) && Inst.Ativo() && hit != null && !((Object)(object)__instance == (Object)null))
			{
				Character attacker = hit.GetAttacker();
				if (!((Object)(object)attacker == (Object)null) && !((Object)(object)attacker != (Object)(object)Player.m_localPlayer))
				{
					Inst.MarcarGolpe(__instance);
				}
			}
		}
	}

	[HarmonyPatch(typeof(Character), "OnDeath")]
	private static class AoMorrer
	{
		private static void Postfix(Character __instance)
		{
			if (!((Object)(object)Inst == (Object)null) && Inst.Ativo())
			{
				Inst.MarcarMorte(__instance);
			}
		}
	}

	[HarmonyPatch(typeof(MineRock), "Damage")]
	private static class AoMinerar
	{
		private static void Postfix(MineRock __instance, HitData hit)
		{
			MarcarPedra((Component)(object)__instance, hit);
		}
	}

	[HarmonyPatch(typeof(MineRock5), "Damage")]
	private static class AoMinerar5
	{
		private static void Postfix(MineRock5 __instance, HitData hit)
		{
			MarcarPedra((Component)(object)__instance, hit);
		}
	}

	[HarmonyPatch(typeof(ItemDrop), "OnPlayerDrop")]
	private static class AoLargar
	{
		private static void Postfix(ItemDrop __instance)
		{
			if (!((Object)(object)Inst == (Object)null))
			{
				Inst.MarcarLargado(__instance);
			}
		}
	}

	[HarmonyPatch(typeof(Humanoid), "Pickup")]
	private static class AoPegar
	{
		private static void Postfix(Humanoid __instance, GameObject go, bool __result)
		{
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
			if (!__result || (Object)(object)Inst == (Object)null || !Inst.Ativo() || (Object)(object)__instance != (Object)(object)Player.m_localPlayer || (Object)(object)go == (Object)null)
			{
				return;
			}
			ItemDrop component = go.GetComponent<ItemDrop>();
			if ((Object)(object)component == (Object)null || component.m_itemData == null || Inst.FoiLargado(component) || Idade(component) > 30.0)
			{
				return;
			}
			string text = "";
			if ((Object)(object)component.m_itemData.m_dropPrefab != (Object)null)
			{
				text = ((Object)component.m_itemData.m_dropPrefab).name;
			}
			Vector3 position = go.transform.position;
			bool flag = Inst.VeioDeMonstro(position);
			bool flag2 = Inst.VeioDeMina(position, text);
			if (!flag && !flag2)
			{
				return;
			}
			int num = component.m_itemData.m_stack;
			if (num < 1)
			{
				num = 1;
			}
			int num2 = num * (Inst.Multiplicador() - 1);
			if (num2 < 1)
			{
				return;
			}
			GameObject val = component.m_itemData.m_dropPrefab;
			if ((Object)(object)val == (Object)null)
			{
				val = ObjectDB.instance.GetItemPrefab(text);
			}
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			Player localPlayer = Player.m_localPlayer;
			Inventory inventory = ((Humanoid)localPlayer).GetInventory();
			if (inventory == null)
			{
				return;
			}
			int num3 = 0;
			for (int i = 0; i < num2; i++)
			{
				if (!inventory.AddItem(val, 1))
				{
					break;
				}
				num3++;
			}
			if (num3 == 0)
			{
				((Character)localPlayer).Message((MessageType)2, "Bolsa cheia. O extra nao coube.", 0, (Sprite)null, true);
			}
		}
	}

	public static Plugin Inst;

	private ConfigEntry<bool> Ligado;

	private ConfigEntry<int> Vezes;

	private List<Marca> mortes = new List<Marca>();

	private List<Marca> minas = new List<Marca>();

	private List<Alvo> alvos = new List<Alvo>();

	private List<ItemDrop> largados = new List<ItemDrop>();

	private float proximaLimpeza;

	private void Awake()
	{
		//IL_004d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0053: Expected O, but got Unknown
		Inst = this;
		Ligado = ((BaseUnityPlugin)this).Config.Bind<bool>("Geral", "Ligado", true, "Triplica o que voce pega de monstro e de minerio. O servidor nao precisa do mod.");
		Vezes = ((BaseUnityPlugin)this).Config.Bind<int>("Geral", "Vezes", 3, "Quantas vezes o item entra. 3 significa o drop normal mais o dobro.");
		Harmony val = new Harmony("yuri.DropTriplo");
		val.PatchAll(typeof(Plugin).Assembly);
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Drop Triplo 1.0.0");
	}

	private void Update()
	{
		if (Time.time < proximaLimpeza)
		{
			return;
		}
		proximaLimpeza = Time.time + 2f;
		Limpar(mortes, 30f);
		Limpar(minas, 20f);
		for (int num = alvos.Count - 1; num >= 0; num--)
		{
			if ((Object)(object)alvos[num].bicho == (Object)null || Time.time - alvos[num].quando > 90f)
			{
				alvos.RemoveAt(num);
			}
		}
		for (int num2 = largados.Count - 1; num2 >= 0; num2--)
		{
			if ((Object)(object)largados[num2] == (Object)null)
			{
				largados.RemoveAt(num2);
			}
		}
	}

	private static void Limpar(List<Marca> lista, float vida)
	{
		float time = Time.time;
		for (int num = lista.Count - 1; num >= 0; num--)
		{
			if (time - lista[num].quando > vida)
			{
				lista.RemoveAt(num);
			}
		}
	}

	public int Multiplicador()
	{
		int num = Vezes.Value;
		if (num < 1)
		{
			num = 1;
		}
		if (num > 5)
		{
			num = 5;
		}
		return num;
	}

	public bool Ativo()
	{
		if (Ligado.Value)
		{
			return Multiplicador() > 1;
		}
		return false;
	}

	public void MarcarGolpe(Character bicho)
	{
		if ((Object)(object)bicho == (Object)null || bicho.IsPlayer())
		{
			return;
		}
		for (int i = 0; i < alvos.Count; i++)
		{
			if ((Object)(object)alvos[i].bicho == (Object)(object)bicho)
			{
				alvos[i].quando = Time.time;
				return;
			}
		}
		if (alvos.Count > 24)
		{
			alvos.RemoveAt(0);
		}
		Alvo alvo = new Alvo();
		alvo.bicho = bicho;
		alvo.quando = Time.time;
		alvos.Add(alvo);
	}

	public void MarcarMorte(Character bicho)
	{
		//IL_005b: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)bicho == (Object)null)
		{
			return;
		}
		bool flag = false;
		for (int num = alvos.Count - 1; num >= 0; num--)
		{
			if ((Object)(object)alvos[num].bicho == (Object)(object)bicho)
			{
				flag = true;
				alvos.RemoveAt(num);
			}
		}
		if (flag)
		{
			Guardar(mortes, ((Component)bicho).transform.position, 16);
		}
	}

	public void MarcarMina(Vector3 pos)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		Guardar(minas, pos, 16);
	}

	public void MarcarLargado(ItemDrop drop)
	{
		if (!((Object)(object)drop == (Object)null) && !largados.Contains(drop))
		{
			largados.Add(drop);
		}
	}

	private static void Guardar(List<Marca> lista, Vector3 pos, int max)
	{
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		if (lista.Count >= max)
		{
			lista.RemoveAt(0);
		}
		Marca marca = new Marca();
		marca.pos = pos;
		marca.quando = Time.time;
		lista.Add(marca);
	}

	public bool VeioDeMonstro(Vector3 pos)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		return Perto(mortes, pos, 14f, 30f);
	}

	public bool VeioDeMina(Vector3 pos, string prefab)
	{
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		if (!EhMinerio(prefab))
		{
			return false;
		}
		return Perto(minas, pos, 8f, 20f);
	}

	public bool FoiLargado(ItemDrop drop)
	{
		if ((Object)(object)drop != (Object)null)
		{
			return largados.Contains(drop);
		}
		return false;
	}

	private static bool Perto(List<Marca> lista, Vector3 pos, float raio, float vida)
	{
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_002b: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		float time = Time.time;
		float num = raio * raio;
		for (int i = 0; i < lista.Count; i++)
		{
			if (!(time - lista[i].quando > vida))
			{
				Vector3 val = lista[i].pos - pos;
				if (((Vector3)(ref val)).sqrMagnitude <= num)
				{
					return true;
				}
			}
		}
		return false;
	}

	private static double Idade(ItemDrop drop)
	{
		MethodInfo method = typeof(ItemDrop).GetMethod("GetTimeSinceSpawned", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		if (method == null)
		{
			return 0.0;
		}
		object obj = method.Invoke(drop, null);
		if (obj is double)
		{
			return (double)obj;
		}
		if (obj is float)
		{
			return (float)obj;
		}
		return 0.0;
	}

	private static bool EhMinerio(string nome)
	{
		if (string.IsNullOrEmpty(nome))
		{
			return false;
		}
		string[] array = new string[17]
		{
			"Stone", "Flint", "Coal", "CopperOre", "TinOre", "IronScrap", "SilverOre", "Obsidian", "Chitin", "BlackMarble",
			"CopperScrap", "FlametalOre", "Sulfur", "ProustitePowder", "Grausten", "BlackwoodAsh", "Ashstone"
		};
		for (int i = 0; i < array.Length; i++)
		{
			if (nome == array[i])
			{
				return true;
			}
		}
		return false;
	}

	private static void MarcarPedra(Component pedra, HitData hit)
	{
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)Inst == (Object)null) && Inst.Ativo() && !((Object)(object)pedra == (Object)null) && hit != null)
		{
			Character attacker = hit.GetAttacker();
			if (!((Object)(object)attacker == (Object)null) && !((Object)(object)attacker != (Object)(object)Player.m_localPlayer))
			{
				Inst.MarcarMina(pedra.transform.position);
			}
		}
	}
}