Skip to content

Commit a7d068a

Browse files
committed
implement documents plus a bit more cleanup
1 parent 56a4dad commit a7d068a

20 files changed

Lines changed: 659 additions & 296 deletions

src/main/java/ch/njol/skript/classes/ClassInfo.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.bukkit.event.Event;
1313
import org.jetbrains.annotations.NotNull;
1414
import org.jetbrains.annotations.Nullable;
15+
import org.jetbrains.annotations.Unmodifiable;
1516
import org.skriptlang.skript.lang.properties.Property;
1617
import org.skriptlang.skript.lang.properties.Property.PropertyInfo;
1718
import org.skriptlang.skript.lang.properties.PropertyHandler;
@@ -477,22 +478,50 @@ public String toString(final @Nullable Event event, final boolean debug) {
477478
return getName().getSingular();
478479
}
479480

480-
481481
private final Map<Property<?>, PropertyInfo<?>> propertyInfos = new HashMap<>();
482+
private final Map<Property<?>, String> propertyDescriptions = new HashMap<>();
482483

483-
public <Handler extends PropertyHandler<T>> ClassInfo<T> property(Property<? super Handler> property, @NotNull Handler handler) {
484+
/**
485+
* Registers this class as having the given property, using the given property handler.
486+
* @param property The property this class should have
487+
* @param description A short description of the property for documentation
488+
* @param handler The handler for this property
489+
* @return This ClassInfo object
490+
* @param <Handler> The type of the property handler
491+
* @throws IllegalStateException If this property is already registered for this class
492+
*/
493+
public <Handler extends PropertyHandler<T>> ClassInfo<T> property(Property<? super Handler> property, String description, @NotNull Handler handler) {
484494
if (propertyInfos.containsKey(property)) {
485495
throw new IllegalStateException("Property " + property.name() + " is already registered for the " + c.getName() + " type.");
486496
}
487497
propertyInfos.put(property, new PropertyInfo<>(property, handler));
488498
Classes.CLASS_INFOS_BY_PROPERTY.computeIfAbsent(property, k -> new ArrayList<>()).add(this);
499+
propertyDescriptions.put(property, description);
489500
return this;
490501
}
491502

503+
/**
504+
* Checks whether this class already has the given property registered.
505+
* @param property The property to check
506+
* @return True if this class has the property, false otherwise
507+
*/
492508
public boolean hasProperty(Property<?> property) {
493509
return propertyInfos.containsKey(property);
494510
}
495511

512+
/**
513+
* @return An unmodifiable collection of all the properties this class has.
514+
*/
515+
public @Unmodifiable Collection<Property<?>> getAllProperties() {
516+
return Collections.unmodifiableCollection(propertyInfos.keySet());
517+
}
518+
519+
/**
520+
* Gets the property info for the given property, or null if this class does not have the property.
521+
* @param property The property to get the info for
522+
* @return The property info, or null if this class does not have the property
523+
* @param <Handler> The type of the property handler
524+
*/
496525
public <Handler extends PropertyHandler<?>> @Nullable PropertyInfo<Handler> getPropertyInfo(Property<Handler> property) {
497526
if (!propertyInfos.containsKey(property)) {
498527
return null;
@@ -501,5 +530,14 @@ public boolean hasProperty(Property<?> property) {
501530
return (PropertyInfo<Handler>) propertyInfos.get(property);
502531
}
503532

533+
/**
534+
* Gets the description for the given property, or null if this class does not have the property.
535+
* Meant to be used for documentation.
536+
* @param property The property to get the description for
537+
* @return The description, or null if this class does not have the property
538+
*/
539+
public String getPropertyDescription(Property<?> property) {
540+
return propertyDescriptions.get(property);
541+
}
504542

505543
}

src/main/java/ch/njol/skript/classes/data/BukkitClasses.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,9 @@ public boolean mustSyncDeserialization() {
413413
return true;
414414
}
415415
})
416-
.property(Property.NAME, ExpressionPropertyHandler.of(World::getName, String.class))
416+
.property(Property.NAME,
417+
"A world's name, as text. Cannot be changed.",
418+
ExpressionPropertyHandler.of(World::getName, String.class))
417419
);
418420

419421
Classes.registerClass(new InventoryClassInfo());
@@ -485,7 +487,11 @@ public String toVariableNameString(final CommandSender s) {
485487
return s.getName();
486488
}
487489
})
488-
.property(Property.NAME, ExpressionPropertyHandler.of(CommandSender::getName, String.class)));
490+
.property(Property.NAME,
491+
"A command sender's name, as text. Cannot be changed.",
492+
ExpressionPropertyHandler.of(CommandSender::getName, String.class)));
493+
494+
Classes.registerClass(new NameableClassInfo());
489495

490496
Classes.registerClass(new ClassInfo<>(InventoryHolder.class, "inventoryholder")
491497
.name(ClassInfo.NO_DOC)
@@ -999,7 +1005,9 @@ public String[] getPatterns() {
9991005
.requiredPlugins("Minecraft 1.13 or newer")
10001006
.supplier(GameRule.values())
10011007
.parser(gameRuleParser)
1002-
.property(Property.NAME, ExpressionPropertyHandler.of(GameRule::getName, String.class))
1008+
.property(Property.NAME,
1009+
"A gamerule's name, as text. Cannot be changed.",
1010+
ExpressionPropertyHandler.of(GameRule::getName, String.class))
10031011
);
10041012

10051013
Classes.registerClass(new ClassInfo<>(EnchantmentOffer.class, "enchantmentoffer")

src/main/java/ch/njol/skript/classes/data/JavaClasses.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,9 @@ public boolean mustSyncDeserialization() {
304304
return false;
305305
}
306306
})
307-
.property(Property.CONTAINS, new ContainsHandler<String, String>() {
307+
.property(Property.CONTAINS,
308+
"Strings can contain other strings.",
309+
new ContainsHandler<String, String>() {
308310
@Override
309311
public boolean contains(String container, String element) {
310312
return StringUtils.contains(container, element, SkriptConfig.caseSensitive.value());

0 commit comments

Comments
 (0)