-
Notifications
You must be signed in to change notification settings - Fork 1
/
PlaceholderLib.cs
152 lines (127 loc) · 4.26 KB
/
PlaceholderLib.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
151
152
using BBRAPIModules;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text.RegularExpressions;
using static System.Net.Mime.MediaTypeNames;
namespace BattleBitAPI.Features
{
[Module("A library for placeholders. Supports multiple elements and formats rich text.", "1.3.0")]
public class PlaceholderLib : BattleBitModule
{
private readonly Regex re = new Regex(@"\{([^\}]+)\}", RegexOptions.Compiled);
public string Text { get; set; }
public Dictionary<string, object> Parameters;
public PlaceholderLib Create() => new PlaceholderLib();
public PlaceholderLib Create(string text) => new PlaceholderLib(text);
public PlaceholderLib Create(string text, Dictionary<string, object> parameters) => new PlaceholderLib(text, parameters);
public PlaceholderLib()
{
Text = "";
Parameters = new();
}
public PlaceholderLib(string text)
{
Text = text;
Parameters = new Dictionary<string, object>();
}
public PlaceholderLib(string text, Dictionary<string, object> parameters)
{
Text = text;
Parameters = parameters;
}
public PlaceholderLib AddParam(string key, object value, bool translateHex = true)
{
if (key == null || value == null)
{
return this;
}
if (translateHex)
value = GetHexTranslate(value.ToString()!);
Parameters.Add(key, value);
return this;
}
public PlaceholderLib AddParam(string key, object value)
{
if (key == null || value == null)
{
return this;
}
value = GetHexTranslate(value.ToString()!);
Parameters.Add(key, value);
return this;
}
public string GetHexTranslate(string str)
{
return re.Replace(str, delegate (Match match)
{
if (match.Groups[1].Value.StartsWith("#"))
return $"<color={match.Groups[1].Value}>";
else if (match.Groups[1].Value.Equals("/"))
return "</color>";
return "{" + match.Groups[1].Value + "}";
});
}
public string GetSurroundedValue(string str)
{
List<string> split = str.Split(" ").ToList();
string newString = "";
bool limited = str.StartsWith("!");
if (limited)
{
str = str.Substring(1);
if (Parameters.ContainsKey(str))
return Parameters[str].ToString()!;
else
return "{!" + str + "}";
}
foreach (string value in split)
{
string newValue = GetValueOf(value);
newString += newValue;
}
return newString;
}
public string GetValueOf(string str)
{
string[] equalsSplit = str.Split("=");
if (str.StartsWith("#"))
return $"<color={str}>";
else if (str.Equals("/"))
return "</color>";
else if (str.StartsWith("/"))
return "<" + str + ">";
else if (Parameters.ContainsKey(str))
return GetHexTranslate(Parameters[str].ToString()!);
else if (equalsSplit.Length > 1)
{
return "<" + str + ">";
}
switch (str)
{
case "b":
case "i":
case "lowercase":
case "uppercase":
case "smallcaps":
case "noparse":
case "nobr":
case "sup":
case "sub":
return "<" + str + ">";
default:
return "{" + str + "}";
}
}
public string Build()
{
return re.Replace(Text, delegate (Match match)
{
return GetSurroundedValue(match.Groups[1].Value);
});
}
[Obsolete]
public string Run() => Build();
}
}