forked from jo3bingham/TibiaAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreatureStorage.cs
More file actions
70 lines (58 loc) · 1.93 KB
/
CreatureStorage.cs
File metadata and controls
70 lines (58 loc) · 1.93 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
namespace OXGaming.TibiaAPI.Creatures
{
public class CreatureStorage
{
private const int MaxCreaturesCount = 2600;
private readonly List<Creature> _creatures = new List<Creature>(MaxCreaturesCount);
public void Reset()
{
_creatures.Clear();
}
public Creature GetCreature(uint creatureId)
{
return _creatures.FirstOrDefault(c => c.Id == creatureId);
}
public Creature GetCreature(string name)
{
if (string.IsNullOrEmpty(name))
{
return null;
}
return _creatures.FirstOrDefault(c => c.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
}
public void RemoveCreature(uint creatureId)
{
var creature = GetCreature(creatureId);
if (creature == null)
{
throw new Exception($"[CreatureStorage.RemoveCreature] Creature not found: {creatureId}");
}
//if (creature == Player)
//{
// throw new Exception("[CreatureStorage.RemoveCreature] Can't remove the player.");
//}
_creatures.Remove(creature);
}
public Creature ReplaceCreature(Creature newCreature, uint removeCreatureId = 0)
{
if (removeCreatureId != 0)
{
RemoveCreature(removeCreatureId);
}
if (_creatures.Count >= MaxCreaturesCount)
{
throw new Exception($"[CreatureStorage.ReplaceCreature] No space left to add creature: {newCreature.Id}");
}
var creature = GetCreature(newCreature.Id);
if (creature != null)
{
return newCreature;
}
_creatures.Add(newCreature);
return newCreature;
}
}
}