Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚀 Add event values to events in docs #5690

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/main/java/ch/njol/skript/doc/HTMLGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@
import ch.njol.skript.lang.function.JavaFunction;
import ch.njol.skript.lang.function.Parameter;
import ch.njol.skript.registrations.Classes;
import ch.njol.skript.registrations.EventValues;
import ch.njol.skript.util.Utils;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import org.apache.commons.lang.StringUtils;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
import org.skriptlang.skript.lang.entry.EntryData;
import org.skriptlang.skript.lang.entry.EntryValidator;
Expand All @@ -48,9 +51,13 @@
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

Expand All @@ -68,6 +75,7 @@ public class HTMLGenerator {
private final File template;
private final File output;
private final String skeleton;
private Map<Class<? extends Event>, List<EventValues.EventValueInfo<?, ?>>> eventValuesCache = new HashMap<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could probably use a MultiMap here

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should clear this cache after the templates have been generated, we don't need it sitting in here.


public HTMLGenerator(File templateDir, File outputDir) {
this.template = templateDir;
Expand Down Expand Up @@ -210,6 +218,9 @@ public int compare(@Nullable JavaFunction<?> o1, @Nullable JavaFunction<?> o2) {
*/
@SuppressWarnings("unchecked")
public void generate() {
// cache event values per event before generating events
eventValuesCache = EventValues.getPerEventEventValues();

for (File f : template.listFiles()) {
if (f.getName().matches("css|js|assets")) { // Copy CSS/JS/Assets folders
String slashName = "/" + f.getName();
Expand Down Expand Up @@ -470,6 +481,9 @@ private String generateAnnotated(String descTemp, SyntaxElementInfo<?> info, @Nu
desc = desc.replace("${element.examples-safe}", Joiner.on("\\n").join(getDefaultIfNullOrEmpty((examples != null ? examples.value() : null), "Missing examples."))
.replace("\\", "\\\\").replace("\"", "\\\"").replace("\t", " "));

// Event Values
desc = handleIf(desc, "${if event-values}", false);

// Documentation ID
DocumentationId docId = c.getAnnotation(DocumentationId.class);
String ID = docId != null ? (docId != null ? docId.value() : null) : info.c.getSimpleName();
Expand Down Expand Up @@ -603,6 +617,31 @@ private String generateEvent(String descTemp, SkriptEventInfo<?> info, @Nullable
String[] keywords = info.getKeywords();
desc = desc.replace("${element.keywords}", keywords == null ? "" : Joiner.on(", ").join(keywords));

// Event Values
Set<String> eventValuesSet = new TreeSet<>(); // sort and remove duplicates due to having multiple events
for (Class<? extends Event> event : info.events) {
for (Class<? extends Event> index : eventValuesCache.keySet()) {
if (!(event.isAssignableFrom(index) || index.isAssignableFrom(event))) // index.isAssignableFrom(event) has inaccuracy like TeleportCause in PlayerMoveEvent but also needed for superclass event values such as event-location
continue;
for (EventValues.EventValueInfo<?, ?> eventValueInfo : eventValuesCache.get(index)) {
if (eventValueInfo.isEventExcluded(event))
continue;
String prefix = "";
if (eventValueInfo.getTime() == EventValues.TIME_PAST)
prefix = "past ";
else if (eventValueInfo.getTime() == EventValues.TIME_FUTURE)
prefix = "future ";
String className = eventValueInfo.c.getSimpleName().toLowerCase(Locale.ENGLISH);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this use the ClassInfo name instead of replacing?

eventValuesSet.add(prefix + "event-" + (className.contains("[]") ? Utils.toEnglishPlural((className.replace("[]", ""))) : className));
}
}
}
List<String> eventValues = new ArrayList<>(0);
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
for (String value : eventValuesSet)
eventValues.add("<span class='event-value'>" + value + "</span>");
desc = handleIf(desc, "${if event-values}", eventValues.size() > 0);
desc = desc.replace("${element.event-values}", eventValues.size() == 0 ? "" : Joiner.on(", ").join(eventValues));

// Documentation ID
String ID = info.getDocumentationID() != null ? info.getDocumentationID() : info.getId();
// Fix duplicated IDs
Expand Down Expand Up @@ -707,6 +746,9 @@ private String generateClass(String descTemp, ClassInfo<?> info, @Nullable Strin
desc = desc.replace("${element.examples-safe}", Joiner.on("\\n").join(examples)
.replace("\\", "\\\\").replace("\"", "\\\"").replace("\t", " "));

// Event Values
desc = handleIf(desc, "${if event-values}", false);

// Documentation ID
String ID = info.getDocumentationID() != null ? info.getDocumentationID() : info.getCodeName();
// Fix duplicated IDs
Expand Down Expand Up @@ -813,6 +855,9 @@ private String generateFunction(String descTemp, JavaFunction<?> info) {
.replace("${element.examples-safe}", Joiner.on("\\n").join(examples)
.replace("\\", "\\\\").replace("\"", "\\\"").replace("\t", " "));

// Event Values
desc = handleIf(desc, "${if event-values}", false);

// Documentation ID
desc = desc.replace("${element.id}", info.getName());

Expand Down
89 changes: 73 additions & 16 deletions src/main/java/ch/njol/skript/registrations/EventValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@
*/
package ch.njol.skript.registrations;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import ch.njol.skript.Skript;
import ch.njol.skript.expressions.base.EventValueExpression;
import ch.njol.skript.util.Getter;
import com.google.common.collect.ImmutableList;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

import com.google.common.collect.ImmutableList;
import ch.njol.skript.Skript;
import org.skriptlang.skript.lang.converter.Converter;
import org.skriptlang.skript.lang.converter.Converters;
import ch.njol.skript.expressions.base.EventValueExpression;
import ch.njol.skript.util.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author Peter Güttinger
Expand All @@ -39,7 +40,7 @@ public class EventValues {

private EventValues() {}

private final static class EventValueInfo<E extends Event, T> {
public final static class EventValueInfo<E extends Event, T> {

public final Class<E> event;
public final Class<T> c;
Expand All @@ -48,7 +49,22 @@ private final static class EventValueInfo<E extends Event, T> {
public final Class<? extends E>[] excludes;
@Nullable
public final String excludeErrorMessage;

@Nullable
public final int time;

public EventValueInfo(Class<E> event, Class<T> c, Getter<T, E> getter, @Nullable String excludeErrorMessage, @Nullable Class<? extends E>[] excludes, int time) {
assert event != null;
assert c != null;
assert getter != null;
this.event = event;
this.c = c;
this.getter = getter;
this.excludes = excludes;
this.excludeErrorMessage = excludeErrorMessage;
this.time = time;
}

@Deprecated
public EventValueInfo(Class<E> event, Class<T> c, Getter<T, E> getter, @Nullable String excludeErrorMessage, @Nullable Class<? extends E>[] excludes) {
assert event != null;
assert c != null;
Expand All @@ -58,8 +74,9 @@ public EventValueInfo(Class<E> event, Class<T> c, Getter<T, E> getter, @Nullable
this.getter = getter;
this.excludes = excludes;
this.excludeErrorMessage = excludeErrorMessage;
this.time = 0;
}

/**
* Get the class that represents the Event.
* @return The class of the Event associated with this event value
Expand Down Expand Up @@ -89,13 +106,35 @@ public Class<? extends E>[] getExcludes() {
}

/**
* Get the error message used when encountering an exclude value.
* @return The error message to use when encountering an exclude
* Get the error message used when encountering an excluded event.
* @return The error message to use when encountering an excluded event
*/
@Nullable
public String getExcludeErrorMessage() {
return excludeErrorMessage;
}

/**
* Get the time state of this event value.
* @return Either {@link #TIME_PAST}, {@link #TIME_NOW} or {@link #TIME_FUTURE}
*/
public int getTime() {
return time;
}

/**
* Whether the provided event is one of the excluded events of this {@link EventValueInfo}
* @return Either {@link #TIME_PAST}, {@link #TIME_NOW} or {@link #TIME_FUTURE}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this return javadoc is wrong ;)

*/
public boolean isEventExcluded(Class<? extends Event> c) {
if (excludes != null)
for (Class<? extends Event> exclude : excludes) {
if (exclude.isAssignableFrom(c))
return true;
}
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

}

private final static List<EventValueInfo<?, ?>> defaultEventValues = new ArrayList<>(30);
Expand Down Expand Up @@ -169,11 +208,11 @@ public static <T, E extends Event> void registerEventValue(Class<E> e, Class<T>
for (int i = 0; i < eventValues.size(); i++) {
EventValueInfo<?, ?> info = eventValues.get(i);
if (info.event != e ? info.event.isAssignableFrom(e) : info.c.isAssignableFrom(c)) {
eventValues.add(i, new EventValueInfo<>(e, c, g, excludeErrorMessage, excludes));
eventValues.add(i, new EventValueInfo<>(e, c, g, excludeErrorMessage, excludes, time));
return;
}
}
eventValues.add(new EventValueInfo<>(e, c, g, excludeErrorMessage, excludes));
eventValues.add(new EventValueInfo<>(e, c, g, excludeErrorMessage, excludes, time));
}

/**
Expand Down Expand Up @@ -355,5 +394,23 @@ public T get(E e) {
public static boolean doesEventValueHaveTimeStates(Class<? extends Event> e, Class<?> c) {
return getEventValueGetter(e, c, -1, false) != null || getEventValueGetter(e, c, 1, false) != null;
}

/**
* @return All the event values for each registered event.
*/
public static Map<Class<? extends Event>, List<EventValueInfo<?, ?>>> getPerEventEventValues() {
Map<Class<? extends Event>, List<EventValues.EventValueInfo<?, ?>>> eventValuesCache = new HashMap<>();
for (int i = -1; i < 2; i++) {
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
for (EventValues.EventValueInfo<?, ?> eventValueInfo : EventValues.getEventValuesListForTime(i)) {
List<EventValueInfo<?, ?>> list = new ArrayList<>(0);
List<EventValueInfo<?, ?>> oldList = eventValuesCache.get(eventValueInfo.event);
if (oldList != null)
list.addAll(oldList);
list.add(eventValueInfo);
eventValuesCache.put(eventValueInfo.event, list);
}
}
return eventValuesCache;
}

}