Skip to content

Commit 000b29c

Browse files
Fix Docs JSON Output (SkriptLang#8339)
1 parent d0b30c3 commit 000b29c

10 files changed

Lines changed: 63 additions & 22 deletions

File tree

src/main/java/ch/njol/skript/conditions/CondLeashWillDrop.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
allow the leash to drop
2222
""")
2323
@Keywords("lead")
24-
@Events("Unleash")
24+
@Events("Leash / Unleash")
2525
@Since("2.10")
2626
public class CondLeashWillDrop extends Condition {
2727

src/main/java/ch/njol/skript/doc/DocumentationIdProvider.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
import ch.njol.skript.lang.Effect;
77
import ch.njol.skript.lang.Expression;
88
import ch.njol.skript.lang.Section;
9-
import ch.njol.skript.lang.SkriptEventInfo;
109
import ch.njol.skript.lang.SyntaxElementInfo;
1110
import ch.njol.skript.lang.function.Function;
1211
import ch.njol.skript.lang.function.Functions;
1312
import ch.njol.skript.registrations.Classes;
13+
import org.skriptlang.skript.bukkit.registration.BukkitSyntaxInfos;
1414
import org.skriptlang.skript.lang.properties.Property;
1515
import org.skriptlang.skript.lang.properties.PropertyRegistry;
1616
import org.skriptlang.skript.lang.structure.Structure;
@@ -138,20 +138,20 @@ public static String getId(ClassInfo<?> classInfo) {
138138
* @param eventInfo the event to get the ID of
139139
* @return the ID of the event
140140
*/
141-
private static String getEventId(SkriptEventInfo<?> eventInfo) {
142-
return Objects.requireNonNullElse(eventInfo.getDocumentationID(), eventInfo.getId());
141+
private static String getEventId(BukkitSyntaxInfos.Event<?> eventInfo) {
142+
return Objects.requireNonNullElse(eventInfo.documentationId(), eventInfo.id());
143143
}
144144

145145
/**
146146
* Gets the documentation ID of an event
147147
* @param eventInfo the event to get the ID of
148148
* @return the ID of the event
149149
*/
150-
public static String getId(SkriptEventInfo<?> eventInfo) {
150+
public static String getId(BukkitSyntaxInfos.Event<?> eventInfo) {
151151
String eventId = getEventId(eventInfo);
152-
int collisionCount = calculateCollisionCount(Skript.getEvents().iterator(),
152+
int collisionCount = calculateCollisionCount(Skript.instance().syntaxRegistry().syntaxes(BukkitSyntaxInfos.Event.KEY).iterator(),
153153
otherEventInfo -> eventId.equals(getEventId(otherEventInfo)),
154-
otherEventInfo -> Arrays.equals(otherEventInfo.getPatterns(), eventInfo.getPatterns()));
154+
otherEventInfo -> Arrays.equals(otherEventInfo.patterns().toArray(), eventInfo.patterns().toArray()));
155155
return addCollisionSuffix(eventId, collisionCount);
156156
}
157157

src/main/java/ch/njol/skript/doc/JSONGenerator.java

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ private static JsonArray convertToJsonArray(String @Nullable ... strings) {
122122
* @param syntaxInfo the syntax info element to generate the documentation object of
123123
* @return the JsonObject representing the documentation of the provided syntax element
124124
*/
125-
private static JsonObject generatedAnnotatedElement(SyntaxInfo<?> syntaxInfo) {
125+
private JsonObject generatedAnnotatedElement(SyntaxInfo<?> syntaxInfo) {
126126
Class<?> syntaxClass = syntaxInfo.type();
127127
Name name = syntaxClass.getAnnotation(Name.class);
128128
if (name == null || syntaxClass.getAnnotation(NoDoc.class) != null)
@@ -189,7 +189,7 @@ private static JsonObject generatedAnnotatedElement(SyntaxInfo<?> syntaxInfo) {
189189
* @param events The events annotation.
190190
* @return A json array with the formatted events value, or null if there is no annotation.
191191
*/
192-
private static @Nullable JsonArray getAnnotatedEvents(Events events) {
192+
private @Nullable JsonArray getAnnotatedEvents(Events events) {
193193
if (events == null || events.value() == null) {
194194
return null;
195195
}
@@ -199,8 +199,43 @@ private static JsonObject generatedAnnotatedElement(SyntaxInfo<?> syntaxInfo) {
199199
for (String event : events.value()) {
200200
JsonObject object = new JsonObject();
201201

202-
object.addProperty("id", event);
203-
object.addProperty("name", event);
202+
// determine candidate infos
203+
List<BukkitSyntaxInfos.Event<?>> candidates = new ArrayList<>();
204+
for (BukkitSyntaxInfos.Event<?> info : source.syntaxRegistry().syntaxes(BukkitSyntaxInfos.Event.KEY)) {
205+
String infoName = info.name().toLowerCase(Locale.ENGLISH);
206+
if (infoName.startsWith("on ")) {
207+
infoName = infoName.substring(3);
208+
}
209+
if (infoName.equals(event.toLowerCase(Locale.ENGLISH)) || info.id().equals(event)) {
210+
candidates.add(info);
211+
} else if (event.equals(info.documentationId())) { // should be unique, this is an exact match
212+
candidates.clear();
213+
candidates.add(info);
214+
break;
215+
}
216+
}
217+
218+
// determine id, name
219+
String id;
220+
String name;
221+
if (candidates.isEmpty()) {
222+
throw new IllegalArgumentException("No matching info found for event annotation: " + event);
223+
} else if (candidates.size() == 1) {
224+
var info = candidates.getFirst();
225+
id = info.documentationId();
226+
if (id == null) {
227+
id = info.id();
228+
}
229+
name = info.name();
230+
} else {
231+
// TODO other options?
232+
throw new IllegalArgumentException("Multiple matching info found for event annotation: " + event +
233+
"\nDifferentiate by specifying a documentationId on the relevant event infos.");
234+
}
235+
236+
// add properties
237+
object.addProperty("id", id);
238+
object.addProperty("name", name);
204239

205240
array.add(object);
206241
}
@@ -216,10 +251,16 @@ private static JsonObject generatedAnnotatedElement(SyntaxInfo<?> syntaxInfo) {
216251
*/
217252
private static @NotNull JsonObject getExpressionReturnTypes(DefaultSyntaxInfos.Expression<?, ?> expression) {
218253
ClassInfo<?> exact = Classes.getSuperClassInfo(expression.returnType());
254+
String name = Objects.requireNonNullElse(exact.getDocName(), exact.getName().getSingular());
255+
if (name.equals(ClassInfo.NO_DOC)) { // undocumented type is not helpful
256+
// hopefully the supertype has something better...
257+
exact = Classes.getSuperClassInfo(expression.returnType().getSuperclass());
258+
name = Objects.requireNonNullElse(exact.getDocName(), exact.getName().getSingular());
259+
}
219260

220261
JsonObject object = new JsonObject();
221262
object.addProperty("id", exact.getCodeName());
222-
object.addProperty("name", exact.getName().getSingular());
263+
object.addProperty("name", name);
223264
return object;
224265
}
225266

@@ -338,7 +379,7 @@ private static boolean isCancellable(BukkitSyntaxInfos.Event<?> info) {
338379
* @param infos the structures to generate documentation for
339380
* @return a JsonArray containing the documentation JsonObjects for each structure
340381
*/
341-
private static <T extends SyntaxInfo<? extends Structure>> JsonArray generateStructureElementArray(Collection<T> infos) {
382+
private <T extends SyntaxInfo<? extends Structure>> JsonArray generateStructureElementArray(Collection<T> infos) {
342383
JsonArray syntaxArray = new JsonArray();
343384
infos.forEach(info -> {
344385
if (info instanceof BukkitSyntaxInfos.Event<?> eventInfo) {
@@ -358,7 +399,7 @@ private static <T extends SyntaxInfo<? extends Structure>> JsonArray generateStr
358399
* @param infos the syntax elements to generate documentation for
359400
* @return a JsonArray containing the documentation JsonObjects for each syntax element
360401
*/
361-
private static <T extends SyntaxInfo<? extends SyntaxElement>> JsonArray generateSyntaxElementArray(Collection<T> infos) {
402+
private <T extends SyntaxInfo<? extends SyntaxElement>> JsonArray generateSyntaxElementArray(Collection<T> infos) {
362403
JsonArray syntaxArray = new JsonArray();
363404
infos.forEach(info -> {
364405
JsonObject syntaxJsonObject = generatedAnnotatedElement(info);

src/main/java/ch/njol/skript/effects/EffCancelDrops.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
""")
4040
@Since("2.4, 2.12 (harvest event)")
4141
@RequiredPlugins("1.12.2 or newer (cancelling item drops of blocks)")
42-
@Events({"death", "break / mine", "block drop", "harvest"})
42+
@Events({"death", "break / mine", "block drop", "harvest block"})
4343
public class EffCancelDrops extends Effect implements EventRestrictedSyntax {
4444

4545
static {

src/main/java/ch/njol/skript/effects/EffDropLeash.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
allow the leash to drop
2222
""")
2323
@Keywords("lead")
24-
@Events("Unleash")
24+
@Events("Leash / Unleash")
2525
@Since("2.10")
2626
public class EffDropLeash extends Effect {
2727

src/main/java/ch/njol/skript/expressions/ExprAttacker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
damage victim by 1 heart
4040
""")
4141
@Since("1.3")
42-
@Events({"damage", "death", "destroy"})
42+
@Events({"damage", "death", "vehicle destroy"})
4343
public class ExprAttacker extends SimpleExpression<Entity> implements EventRestrictedSyntax {
4444

4545
static {

src/main/java/ch/njol/skript/expressions/ExprExperience.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
add 70 to dropped experience
4747
""")
4848
@Since("2.1, 2.5.3 (block break event), 2.7 (experience change event), 2.10 (breeding, fishing)")
49-
@Events({"experience spawn", "break / mine", "experience change", "entity breeding"})
49+
@Events({"experience spawn", "break / mine", "experience change", "entity breed"})
5050
public class ExprExperience extends SimpleExpression<Experience> {
5151

5252
static {
@@ -60,7 +60,7 @@ public boolean init(Expression<?>[] expressions, int matchedPattern,
6060
if (!getParser().isCurrentEvent(ExperienceSpawnEvent.class, BlockBreakEvent.class,
6161
PlayerExpChangeEvent.class, EntityBreedEvent.class, PlayerFishEvent.class)) {
6262
Skript.error("The 'experience' expression can only be used in experience spawn, " +
63-
"block break, player experience change, entity breeding or fishing events");
63+
"block break, player experience change, entity breed or fishing events");
6464
return false;
6565
}
6666

src/main/java/ch/njol/skript/expressions/ExprExplosionBlockYield.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
on explode:
2828
set the explosion's block yield to 10%
2929
""")
30-
@Events("explosion")
30+
@Events("explode")
3131
@Since("2.5")
3232
public class ExprExplosionBlockYield extends SimpleExpression<Number> {
3333

src/main/java/ch/njol/skript/expressions/ExprUnleashReason.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
if the unleash reason is distance:
2020
broadcast "The leash was snapped in half."
2121
""")
22-
@Events("Unleash")
22+
@Events("Leash / Unleash")
2323
@Since("2.10")
2424
public class ExprUnleashReason extends EventValueExpression<UnleashReason> {
2525

src/main/java/org/skriptlang/skript/bukkit/furnace/elements/ExprFurnaceEventItems.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
broadcast smelting item
4444
clear smelting item
4545
""")
46-
@Events({"smelt", "fuel burn", "smelting start", "furnace extract"})
46+
@Events({"smelt", "fuel burn", "start smelt", "furnace item extract"})
4747
@Since("2.10")
4848
public class ExprFurnaceEventItems extends PropertyExpression<Block, ItemStack> {
4949

0 commit comments

Comments
 (0)