-
Notifications
You must be signed in to change notification settings - Fork 6
/
ModTag.java
254 lines (221 loc) · 8.83 KB
/
ModTag.java
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package com.denizenscript.clientizen.objects;
import com.denizenscript.clientizen.util.Utilities;
import com.denizenscript.denizencore.events.ScriptEvent;
import com.denizenscript.denizencore.objects.Fetchable;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.core.MapTag;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.ObjectTagProcessor;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.fabricmc.loader.api.metadata.ModMetadata;
import java.util.Optional;
public class ModTag implements ObjectTag {
// <--[ObjectType]
// @name ModTag
// @prefix mod
// @base ElementTag
// @ExampleTagBase mod[clientizen]
// @ExampleValues <mod[clientizen]>
// @ExampleForReturns
// - narrate "The mod is %VALUE%!"
// @format
// The identity format for mods is the mod id.
// For example, 'mod@clientizen' or 'mod@theprinter'.
//
// @description
// A ModTag represents a currently loaded mod.
//
// This can be either a mod that's been downloaded and installed, a built-in mod, or a mod within another mod (a library mod, for example)
//
// @Matchable
// ModTag matchers:
// "mod" plaintext: always matches.
// Mod id: matches if the mod id matches the input, using advanced matchers.
// -->
@Fetchable("mod")
public static ModTag valueOf(String input, TagContext context) {
if (input.startsWith("mod@")) {
input = input.substring("mod@".length());
}
Optional<ModTag> modTag = FabricLoader.getInstance().getModContainer(CoreUtilities.toLowerCase(input)).map(ModTag::new);
if (modTag.isEmpty()) {
Utilities.echoErrorByContext(context, "valueOf ModTag returning null: '" + input + "' isn't a valid mod id.");
return null;
}
return modTag.get();
}
public static boolean matches(String string) {
if (string.startsWith("mod@")) {
return true;
}
return FabricLoader.getInstance().isModLoaded(CoreUtilities.toLowerCase(string));
}
public final ModContainer modContainer;
public ModTag(ModContainer modContainer) {
this.modContainer = modContainer;
}
public ModMetadata getMetadata() {
return modContainer.getMetadata();
}
public static void register() {
// <--[tag]
// @attribute <ModTag.id>
// @returns ElementTag
// @description
// Returns a mod's id.
// -->
tagProcessor.registerStaticTag(ElementTag.class, "id", (attribute, object) -> {
return new ElementTag(object.getMetadata().getId(), true);
});
// <--[tag]
// @attribute <ModTag.display_name>
// @returns ElementTag
// @description
// Returns a mod's display name.
// -->
tagProcessor.registerStaticTag(ElementTag.class, "display_name", (attribute, object) -> {
return new ElementTag(object.getMetadata().getName(), true);
});
// <--[tag]
// @attribute <ModTag.description>
// @returns ElementTag
// @description
// Returns a mod's description.
// -->
tagProcessor.registerStaticTag(ElementTag.class, "description", (attribute, object) -> {
return new ElementTag(object.getMetadata().getDescription(), true);
});
// <--[tag]
// @attribute <ModTag.version>
// @returns ElementTag
// @description
// Returns a mod's version.
// -->
tagProcessor.registerStaticTag(ElementTag.class, "version", (attribute, object) -> {
return new ElementTag(object.getMetadata().getVersion().getFriendlyString(), true);
});
// <--[tag]
// @attribute <ModTag.authors>
// @returns MapTag
// @description
// Returns a mod's authors, as a map of author name to their contact information.
// The contact information is a map of contact platforms to identification on that platform.
// Note that mods can provide anything here, although most mods will obviously provide valid info.
// -->
tagProcessor.registerStaticTag(MapTag.class, "authors", (attribute, object) -> {
return Utilities.personsToMap(object.getMetadata().getAuthors());
});
// <--[tag]
// @attribute <ModTag.contributors>
// @returns MapTag
// @description
// Returns a mod's contributors, as a map of contributor name to their contact information.
// The contact information is a map of contact platforms to identification on that platform.
// Note that mods can provide anything here, although most mods will obviously provide valid info.
// -->
tagProcessor.registerStaticTag(MapTag.class, "contributors", (attribute, object) -> {
return Utilities.personsToMap(object.getMetadata().getContributors());
});
// <--[tag]
// @attribute <ModTag.contact_info>
// @returns MapTag
// @description
// Returns a mod's contact information, as a map of contact platforms to identification on that platform.
// Some common examples are: "repo", "website", "issues", etc.
// Note that mods can provide anything here, although most mods will obviously provide valid info.
// -->
tagProcessor.registerStaticTag(MapTag.class, "contact_info", (attribute, object) -> {
return Utilities.contactInfoToMap(object.getMetadata().getContact());
});
// <--[tag]
// @attribute <ModTag.licenses>
// @returns ListTag
// @description
// Returns a list of a mod's licenses.
// -->
tagProcessor.registerStaticTag(ListTag.class, "licenses", (attribute, object) -> {
return new ListTag(object.getMetadata().getLicense(), true);
});
// <--[tag]
// @attribute <ModTag.type>
// @returns ElementTag
// @description
// Returns a mod's type, either:
// 'fabric' - a regular Fabric mod, either directly installed or included by another mod.
// 'builtin' - a built-in mod, generally used for internal mods included by Fabric itself.
// -->
tagProcessor.registerStaticTag(ElementTag.class, "type", (attribute, object) -> {
return new ElementTag(object.getMetadata().getType(), true);
});
// <--[tag]
// @attribute <ModTag.containing_mod>
// @returns ModTag
// @description
// Returns the mod that contains this mod, if any (for things like library mods included by other mods).
// -->
tagProcessor.registerStaticTag(ModTag.class, "containing_mod", (attribute, object) -> {
return object.modContainer.getContainingMod().map(ModTag::new).orElse(null);
});
// <--[tag]
// @attribute <ModTag.contained_mods>
// @returns ListTag(ModTag)
// @description
// Returns a list of mods contained by this mod (for mods that include libraries, for example).
// -->
tagProcessor.registerStaticTag(ListTag.class, "contained_mods", (attribute, object) -> {
return new ListTag(object.modContainer.getContainedMods(), ModTag::new);
});
}
public static final ObjectTagProcessor<ModTag> tagProcessor = new ObjectTagProcessor<>();
@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
return tagProcessor.getObjectAttribute(this, attribute);
}
@Override
public String identify() {
return "mod@" + getMetadata().getId();
}
@Override
public String debuggable() {
return "<LG>mod@<Y>" + getMetadata().getId();
}
@Override
public String identifySimple() {
return identify();
}
@Override
public String toString() {
return identify();
}
@Override
public ModContainer getJavaObject() {
return modContainer;
}
@Override
public boolean advancedMatches(String matcher) {
String matcherLower = CoreUtilities.toLowerCase(matcher);
if (matcherLower.equals("mod")) {
return true;
}
return ScriptEvent.runGenericCheck(matcherLower, getMetadata().getId());
}
@Override
public boolean isUnique() {
return true;
}
String prefix = "Mod";
@Override
public String getPrefix() {
return prefix;
}
@Override
public ObjectTag setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
}