-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEndlessItem.cs
150 lines (139 loc) · 6.55 KB
/
EndlessItem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using System.Linq;
using static Terraria.ModLoader.ModContent;
using Terraria.ModLoader.Config;
namespace TrueEndless
{
class EndlessItem : GlobalItem
{
private readonly EndlessConfigServer cfg = GetInstance<EndlessConfigServer>();
public override bool InstancePerEntity => true;
public override bool CloneNewInstances => true;
public bool wasConsumable = false;
public int properStack = 0;
// returns true if the player has infinity enabled
private bool HasInfinity() => HasInfinity(Main.player[Main.myPlayer]);
private bool HasInfinity(Player player) => player.GetModPlayer<EndlessPlayer>().HasInfinity;
// is ammo that can be endless
public bool IsEndlessAmmo(Item item) =>
(item.stack >= item.maxStack || item.stack >= cfg.AmmoStack) &&
item.maxStack > 1 &&
!item.notAmmo &&
item.ammo != 0 &&
!IsntEndlessSpecific(item);
// is a consumable item that can be endless
public bool IsEndlessConsumable(Item item) =>
(item.stack >= item.maxStack || item.stack >= cfg.ConsumableStack) &&
item.maxStack > 1 &&
(item.consumable || wasConsumable) &&
item.createTile == -1 &&
item.createWall == -1 &&
!IsntEndlessSpecific(item);
// is a potion that can be endless
public bool IsEndlessPotion(Item item) =>
(item.stack >= item.maxStack || item.stack >= cfg.PotionStack) &&
item.maxStack > 1 &&
(item.buffType != 0 || item.healLife > 0 || item.healMana > 0) &&
item.damage <= 0 &&
!IsntEndlessSpecific(item);
// returns true if the item is in a list of item definitions
public bool IsInItemDefinition(List<ItemDefinition> l, Item item)
{
return l.Contains(new ItemDefinition(item.type));
}
// returns true and an associated int if the item is a key in the dictionary
public (bool, int) IsInItemDefinition(Dictionary<ItemDefinition, int> d, Item item)
{
foreach (KeyValuePair<ItemDefinition, int> kv in d) // loops through the key-value pairs of the dictionary
{
if (kv.Key.Type == item.type) { return (true, kv.Value); }
}
return (false, -1);
}
// returns true if the item is part of the dictionary defined by the mod config, and its stack size is greater than or equal to the associated int
public bool IsEndlessSpecific(Item item)
{
(bool, int) idl = IsInItemDefinition(cfg.OverrideEnable, item);
return (item.stack >= item.maxStack || item.stack >= idl.Item2) && idl.Item1;
}
// returns true if the item is part of the list defined by the mod config
public bool IsntEndlessSpecific(Item item)
{
return IsInItemDefinition(cfg.OverrideDisable, item);
}
/*public bool GrantsSameBuff(Item item)
{
foreach (ItemDefinition potion in cfg.BuffDisable)
{
// I want to get the buffType from an ItemDefinition
if (item.buffType == GetModItem(potion.Type).buffType) { return true; }
}
return false;
}*/
// returns true if the item is endless ammo, consumable, potion, or specifically defined, and isnt excluded
public bool IsEndless(Item item)
{
return (IsEndlessAmmo(item) || IsEndlessConsumable(item) || IsEndlessPotion(item) || IsEndlessSpecific(item)) && !IsntEndlessSpecific(item);
}
// runs for every item in the inventory every tick. Importantly, its run *after* a used itemstack is decreased
public override bool PreDrawInInventory(Item item, SpriteBatch spriteBatch, Vector2 position, Rectangle frame, Color drawColor, Color itemColor, Vector2 origin, float scale)
{
// resets the stack size back to what it was if reduced in certain cases
if (properStack != 0)
{
item.stack = properStack;
properStack = 0;
}
// makes the sprite rainbowy
if ((Main.gameMenu || HasInfinity()) && GetInstance<EndlessConfigClient>().RainbowSprite && (IsEndless(item)))
{
Texture2D texture = Main.itemTexture[item.type];
spriteBatch.Draw(texture, position, null, Main.DiscoColor, 0, origin, scale, SpriteEffects.None, 0f); // Main.DiscoColor is my favourite variable :)
return false;
}
return true;
}
private bool IsEndlessTooltip(TooltipLine tooltip) => tooltip.mod == mod.Name && tooltip.Name == "EndlessNotice";
// adds the endless tooltip
public override void ModifyTooltips(Item item, List<TooltipLine> tooltips)
{
tooltips.RemoveAll(IsEndlessTooltip); // removes the endless tooltip
if (HasInfinity() && IsEndless(item))
{
string text = "Endless";
if (GetInstance<EndlessConfigClient>().RainbowTooltip) // changes the colour to be rainbowy if enabled
{
text = "[c/" + Main.DiscoColor.Hex3() + ":" + text + "]";
}
tooltips.Add(new TooltipLine(mod, "EndlessNotice", text));
}
}
public override void UpdateInventory(Item item, Player player)
{
// does stuff with applying buffs over in EndlessPlayer.cs
player.GetModPlayer<EndlessPlayer>().EndlessPotion(item);
}
// one of the cases where the stack size reset in PreDrawInInventory is used
public override void GetHealLife(Item item, Player player, bool quickHeal, ref int healValue)
{
if (HasInfinity(player) && IsEndless(item))
{
properStack = item.stack;
item.stack++;
}
}
public override void OnConsumeItem(Item item, Player player)
{
if (HasInfinity(player) && IsEndless(item))
{
properStack = item.stack;
item.stack++; // prevents item from being deleted if it has a maxStack of 1
}
}
}
}