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

Event Context #7523

Draft
wants to merge 1 commit into
base: dev/feature
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 3 additions & 10 deletions src/main/java/ch/njol/skript/expressions/ExprEventExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,17 @@
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.localization.Noun;
import ch.njol.skript.registrations.Classes;
import ch.njol.skript.util.Utils;
import ch.njol.util.Kleenean;
import ch.njol.util.NonNullPair;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

/**
* Provided for convenience: one can write 'event-world' instead of only 'world' to distinguish between the event-world and the loop-world.
*
* @author Peter Güttinger
*/
@NoDoc
public class ExprEventExpression extends WrapperExpression<Object> {

static {
Skript.registerExpression(ExprEventExpression.class, Object.class, ExpressionType.PROPERTY, "[the] event-%*classinfo%");// property so that it is parsed after most other expressions
Skript.registerExpression(ExprEventExpression.class, Object.class, ExpressionType.PROPERTY, "[the] [:original] event-%*classinfo%");// property so that it is parsed after most other expressions
}

@Override
Expand All @@ -37,7 +29,8 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
Class<?> c = classInfo.getC();

boolean plural = Utils.getEnglishPlural(parser.expr).getSecond();
EventValueExpression<?> eventValue = new EventValueExpression<>(plural ? CollectionUtils.arrayType(c) : c);
boolean original = parser.hasTag("original");
EventValueExpression<?> eventValue = new EventValueExpression<>(original, plural ? CollectionUtils.arrayType(c) : c);
setExpr(eventValue);
return eventValue.init();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@
import ch.njol.skript.log.SkriptLogger;
import ch.njol.skript.registrations.Classes;
import ch.njol.skript.registrations.EventValues;
import ch.njol.skript.registrations.EventValues.EventContext;
import ch.njol.skript.registrations.EventValues.EventValueContext;
import ch.njol.skript.util.Utils;
import ch.njol.util.Kleenean;
import org.skriptlang.skript.registration.SyntaxInfo;
import org.skriptlang.skript.registration.SyntaxRegistry;
import org.skriptlang.skript.util.Priority;
import org.bukkit.event.Event;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.lang.converter.Converter;
import org.skriptlang.skript.registration.SyntaxInfo;
import org.skriptlang.skript.registration.SyntaxRegistry;
import org.skriptlang.skript.util.Priority;

import java.lang.reflect.Array;
import java.util.HashMap;
Expand Down Expand Up @@ -75,9 +77,9 @@ public class EventValueExpression<T> extends SimpleExpression<T> implements Defa
@ApiStatus.Experimental
public static <E extends EventValueExpression<T>, T> SyntaxInfo.Expression<E, T> register(SyntaxRegistry registry, Class<E> expressionClass, Class<T> returnType, String pattern) {
SyntaxInfo.Expression<E, T> info = SyntaxInfo.Expression.builder(expressionClass, returnType)
.priority(DEFAULT_PRIORITY)
.addPattern("[the] " + pattern)
.build();
.priority(DEFAULT_PRIORITY)
.addPattern("[the] " + pattern)
.build();
registry.register(SyntaxRegistry.EXPRESSION, info);
return info;
}
Expand All @@ -103,11 +105,16 @@ public static <T> void register(Class<? extends EventValueExpression<T>> express
private Changer<? super T> changer;
private final boolean single;
private final boolean exact;
private final boolean original;

public EventValueExpression(Class<? extends T> type) {
this(type, null);
}

public EventValueExpression(boolean original, Class<? extends T> type) {
this(original, type, null);
}

/**
* Construct an event value expression.
*
Expand All @@ -122,11 +129,20 @@ public EventValueExpression(Class<? extends T> type, @Nullable Changer<? super T
this(type, changer, false);
}

public EventValueExpression(boolean original, Class<? extends T> type, @Nullable Changer<? super T> changer) {
this(original, type, changer, false);
}

public EventValueExpression(Class<? extends T> type, @Nullable Changer<? super T> changer, boolean exact) {
this(false, type, changer, exact);
}

public EventValueExpression(boolean original, Class<? extends T> type, @Nullable Changer<? super T> changer, boolean exact) {
assert type != null;
this.type = type;
this.exact = exact;
this.changer = changer;
this.original = original;
single = !type.isArray();
componentType = single ? type : type.getComponentType();
}
Expand Down Expand Up @@ -156,7 +172,7 @@ public boolean init() {
if (EventValues.hasMultipleConverters(event, type, getTime()) == Kleenean.TRUE) {
Noun typeName = Classes.getExactClassInfo(componentType).getName();
log.printError("There are multiple " + typeName.toString(true) + " in " + Utils.a(getParser().getCurrentEventName()) + " event. " +
"You must define which " + typeName + " to use.");
"You must define which " + typeName + " to use.");
return false;
}
Converter<?, ? extends T> converter;
Expand All @@ -182,9 +198,8 @@ public boolean init() {
}

@Override
@Nullable
@SuppressWarnings("unchecked")
protected T[] get(Event event) {
protected T @Nullable [] get(Event event) {
T value = getValue(event);
if (value == null)
return (T[]) Array.newInstance(componentType, 0);
Expand Down Expand Up @@ -219,17 +234,29 @@ private <E extends Event> T getValue(E event) {
return null;
}

private <E extends Event, V> T getValue(EventContext<E, V> eventContext) {
//noinspection unchecked
EventValueContext<E, V> valueContext = eventContext.getEventValueContext((Class<V>) type);
if (valueContext == null || !valueContext.isSingleConverter())
return null;
if (original)
//noinspection unchecked
return (T) valueContext.getOriginalValue();
//noinspection unchecked
return (T) valueContext.getCurrentValue();

}

@Override
@Nullable
@SuppressWarnings("unchecked")
public Class<?>[] acceptChange(ChangeMode mode) {
public Class<?> @Nullable [] acceptChange(ChangeMode mode) {
if (changer == null)
//noinspection unchecked
changer = (Changer<? super T>) Classes.getSuperClassInfo(componentType).getChanger();
return changer == null ? null : changer.acceptChange(mode);
}

@Override
public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
if (changer == null)
throw new SkriptAPIException("The changer cannot be null");
ChangerUtils.change(changer, getArray(event), delta, mode);
Expand Down
Loading
Loading