Even More Modifiers  1.0.0.0
A mod for rolling various bonus stats on items
ModifierEffect.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Linq;
5 using System.Reflection;
6 using Loot.Api.Attributes;
7 using Loot.Api.Content;
8 using Loot.Api.Delegators;
9 using Microsoft.Xna.Framework.Graphics;
10 using Terraria;
11 using Terraria.ModLoader;
12 using Terraria.ModLoader.IO;
13 
14 namespace Loot.Api.Core
15 {
21  public abstract class ModifierEffect : ModPlayer, ILoadableContent, ILoadableContentSetter, ICloneable
22  {
23  private new Mod mod => Mod;
24  public Mod Mod { get; internal set; }
25 
26  Mod ILoadableContentSetter.Mod
27  {
28  set => Mod = value;
29  }
30 
31  public uint Type { get; internal set; }
32 
33  uint ILoadableContentSetter.Type
34  {
35  set => Type = value;
36  }
37 
38  public new string Name => GetType().Name;
39 
40  public ModifierDelegatorPlayer DelegatorPlayer { get; internal set; }
41 
42  public new Player player => DelegatorPlayer.player;
43 
48  public bool IsBeingDelegated { get; internal set; }
49 
53  public virtual void OnInitialize()
54  {
55  }
56 
61  public virtual void ResetEffects()
62  {
63  }
64 
70  public virtual void AttachDelegations(ModifierDelegatorPlayer delegatorPlayer)
71  {
72  }
73 
74  internal void _DetachDelegations(ModifierDelegatorPlayer delegatorPlayer)
75  {
76  // We need to reset effects first, as they will no longer run
77  // after the effect is detached
78 
79  // Regular reset
80  ResetEffects();
81 
82  // This part is actually kind of unneeded anymore
83  // But im keeping it in in case someone, for some reason,
84  // wants to split their ResetEffects in multiple methods
85  // without each other calling them.
86  // Performance is no issue since automatic attach and detach
87  // is handled by ModifierCachePlayer
88 
89  // Look for ResetEffects and manually invoke it
90  var resetEffects = GetType()
91  .GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)
92  .Where(x => x.GetCustomAttributes().OfType<AutoDelegation>().Any())
93  .ToDictionary(x => x, y => y.GetCustomAttribute<AutoDelegation>());
94 
95  foreach (var kvp in resetEffects.Where(x => x.Value.DelegationTypes.Contains("OnResetEffects")))
96  {
97  try
98  {
99  kvp.Key.Invoke(this, new object[] { delegatorPlayer.player });
100  }
101  catch (Exception e)
102  {
103  Loot.Logger.Error("Error in ModifierEffect._DetachDelegations", e);
104  }
105  }
106 
107  // Now detach
108  DetachDelegations(delegatorPlayer);
109  }
110 
114  public virtual void DetachDelegations(ModifierDelegatorPlayer delegatorPlayer)
115  {
116  }
117 
121  public virtual void Clone(ref ModifierEffect clone)
122  {
123  }
124 
125  public object Clone()
126  {
127  ModifierEffect clone = (ModifierEffect)MemberwiseClone();
128  clone.Mod = Mod;
129  clone.Type = Type;
130  Clone(ref clone);
131  return clone;
132  }
133 
134  public sealed override bool Autoload(ref string name)
135  => false;
136  public override bool CloneNewInstances
137  => false;
138  public sealed override void Load(TagCompound tag)
139  {
140  }
141  public sealed override void LoadLegacy(BinaryReader reader)
142  {
143  }
144  public sealed override void PreSaveCustomData()
145  {
146  }
147  public sealed override void SetupStartInventory(IList<Item> items, bool mediumcoreDeath)
148  {
149  }
150  [Obsolete("SetupStartInventory now has an overload with a mediumcoreDeath bool argument, please use that.")]
151  public sealed override void SetupStartInventory(IList<Item> items)
152  {
153  }
154  public override void PreSavePlayer()
155  {
156  }
157  public override void PostSavePlayer()
158  {
159  }
160  public override void UpdateBiomes()
161  {
162  }
163  public override void UpdateBiomeVisuals()
164  {
165  }
166  public sealed override bool CustomBiomesMatch(Player other)
167  {
168  return base.CustomBiomesMatch(other);
169  }
170  public sealed override void CopyCustomBiomesTo(Player other)
171  {
172  }
173  public sealed override void SendCustomBiomes(BinaryWriter writer)
174  {
175  }
176  public sealed override void ReceiveCustomBiomes(BinaryReader reader)
177  {
178  }
179  public sealed override void clientClone(ModPlayer clientClone)
180  {
181  }
182  public sealed override Texture2D GetMapBackgroundImage()
183  {
184  return base.GetMapBackgroundImage();
185  }
186  public sealed override void PlayerConnect(Player player)
187  {
188  }
189  public sealed override void PlayerDisconnect(Player player)
190  {
191  }
192  public sealed override void OnEnterWorld(Player player)
193  {
194  }
195  public sealed override bool ShiftClickSlot(Item[] inventory, int context, int slot)
196  {
197  return base.ShiftClickSlot(inventory, context, slot);
198  }
199  public sealed override void PostSellItem(NPC vendor, Item[] shopInventory, Item item)
200  {
201  }
202  public sealed override bool CanSellItem(NPC vendor, Item[] shopInventory, Item item)
203  {
204  return base.CanSellItem(vendor, shopInventory, item);
205  }
206  public sealed override void PostBuyItem(NPC vendor, Item[] shopInventory, Item item)
207  {
208  }
209  public sealed override bool CanBuyItem(NPC vendor, Item[] shopInventory, Item item)
210  {
211  return base.CanBuyItem(vendor, shopInventory, item);
212  }
213  }
214 }
sealed override void PostBuyItem(NPC vendor, Item[] shopInventory, Item item)
sealed override bool CustomBiomesMatch(Player other)
sealed override void ReceiveCustomBiomes(BinaryReader reader)
sealed override void Load(TagCompound tag)
sealed override void SendCustomBiomes(BinaryWriter writer)
This attribute may be used to skip usage of AttachDelegations and DetachDelegations Which is a cumber...
sealed override Texture2D GetMapBackgroundImage()
sealed override void PreSaveCustomData()
A ModifierEffect signifies the effect of a modifier on a player It should house the implementation...
sealed override void PlayerConnect(Player player)
virtual void AttachDelegations(ModifierDelegatorPlayer delegatorPlayer)
Allows modders to perform various delegations when this modifier is detected to become active This me...
sealed override void OnEnterWorld(Player player)
sealed override void CopyCustomBiomesTo(Player other)
override void UpdateBiomeVisuals()
virtual void DetachDelegations(ModifierDelegatorPlayer delegatorPlayer)
Allows modders to undo their performed delegations in AttachDelegations
sealed override void SetupStartInventory(IList< Item > items)
sealed override bool CanSellItem(NPC vendor, Item[] shopInventory, Item item)
virtual void ResetEffects()
Automatically called when the ModPlayer does its ResetEffects Also automatically called when the dele...
sealed override bool ShiftClickSlot(Item[] inventory, int context, int slot)
sealed override void PostSellItem(NPC vendor, Item[] shopInventory, Item item)
sealed override void LoadLegacy(BinaryReader reader)
sealed override void PlayerDisconnect(Player player)
virtual void OnInitialize()
Called when the ModPlayer initializes the effect
Defines a piece of loadable content The interface is used in classes where the deriving class already...
sealed override void SetupStartInventory(IList< Item > items, bool mediumcoreDeath)
virtual void Clone(ref ModifierEffect clone)
A modder can provide custom cloning of effects in this hook
Holds player-entity data and handles it
sealed override void clientClone(ModPlayer clientClone)
sealed override bool CanBuyItem(NPC vendor, Item[] shopInventory, Item item)