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

EventValues - add an option to use changers #7512

Open
wants to merge 7 commits into
base: dev/feature
Choose a base branch
from
Open
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
14 changes: 13 additions & 1 deletion src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.potion.PotionType;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.lang.converter.Converter;
import ch.njol.skript.registrations.EventConverter;

import java.util.ArrayList;
import java.util.HashSet;
Expand Down Expand Up @@ -298,7 +300,17 @@ public PotionEffectType convert(AreaEffectCloudApplyEvent event) {
EventValues.registerEventValue(EntityPickupItemEvent.class, Item.class, EntityPickupItemEvent::getItem);
EventValues.registerEventValue(EntityPickupItemEvent.class, ItemType.class, event -> new ItemType(event.getItem().getItemStack()));
// PlayerItemConsumeEvent
EventValues.registerEventValue(PlayerItemConsumeEvent.class, ItemStack.class, PlayerItemConsumeEvent::getItem);
EventValues.registerEventValue(PlayerItemConsumeEvent.class, ItemStack.class, new EventConverter<>() {
@Override
public void set(PlayerItemConsumeEvent event, @Nullable ItemStack itemStack) {
event.setItem(itemStack);
}

@Override
public ItemStack convert(PlayerItemConsumeEvent from) {
return from.getItem();
}
});
// PlayerItemBreakEvent
EventValues.registerEventValue(PlayerItemBreakEvent.class, ItemStack.class, PlayerItemBreakEvent::getBrokenItem);
// PlayerInteractEntityEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.parser.ParserInstance;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.localization.Noun;
import ch.njol.skript.log.ParseLogHandler;
Expand All @@ -18,6 +19,8 @@
import ch.njol.skript.registrations.EventValues;
import ch.njol.skript.util.Utils;
import ch.njol.util.Kleenean;
import ch.njol.util.coll.CollectionUtils;
import ch.njol.skript.registrations.EventConverter;
import org.skriptlang.skript.registration.SyntaxInfo;
import org.skriptlang.skript.registration.SyntaxRegistry;
import org.skriptlang.skript.util.Priority;
Expand Down Expand Up @@ -95,6 +98,7 @@ public static <T> void register(Class<? extends EventValueExpression<T>> express
}

private final Map<Class<? extends Event>, Converter<?, ? extends T>> converters = new HashMap<>();
private final Map<Class<? extends Event>, EventConverter<Event, T>> eventConverters = new HashMap<>();

private final Class<?> componentType;
private final Class<? extends T> type;
Expand All @@ -103,6 +107,7 @@ public static <T> void register(Class<? extends EventValueExpression<T>> express
private Changer<? super T> changer;
private final boolean single;
private final boolean exact;
private boolean isDelayed;

public EventValueExpression(Class<? extends T> type) {
this(type, null);
Expand Down Expand Up @@ -140,10 +145,12 @@ public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean is

@Override
public boolean init() {
ParserInstance parser = getParser();
isDelayed = parser.getHasDelayBefore().isTrue();
ParseLogHandler log = SkriptLogger.startParseLogHandler();
try {
boolean hasValue = false;
Class<? extends Event>[] events = getParser().getCurrentEvents();
Class<? extends Event>[] events = parser.getCurrentEvents();
if (events == null) {
assert false;
return false;
Expand All @@ -155,7 +162,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. " +
log.printError("There are multiple " + typeName.toString(true) + " in " + Utils.a(parser.getCurrentEventName()) + " event. " +
"You must define which " + typeName + " to use.");
return false;
}
Expand All @@ -168,10 +175,13 @@ public boolean init() {
if (converter != null) {
converters.put(event, converter);
hasValue = true;
if (converter instanceof EventConverter eventConverter) {
eventConverters.put(event, eventConverter);
}
}
}
if (!hasValue) {
log.printError("There's no " + Classes.getSuperClassInfo(componentType).getName().toString(!single) + " in " + Utils.a(getParser().getCurrentEventName()) + " event");
log.printError("There's no " + Classes.getSuperClassInfo(componentType).getName().toString(!single) + " in " + Utils.a(parser.getCurrentEventName()) + " event");
return false;
}
log.printLog();
Expand Down Expand Up @@ -223,16 +233,34 @@ private <E extends Event> T getValue(E event) {
@Nullable
@SuppressWarnings("unchecked")
public Class<?>[] acceptChange(ChangeMode mode) {
if (mode == ChangeMode.SET && !eventConverters.isEmpty()) {
if (isDelayed) {
Skript.error("Event values cannot be changed after the event has already passed.");
return null;
}
return CollectionUtils.array(type);
}
if (changer == null)
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) {
if (changer == null)
throw new SkriptAPIException("The changer cannot be null");
ChangerUtils.change(changer, getArray(event), delta, mode);
if (mode == ChangeMode.SET) {
EventConverter<Event, T> converter = eventConverters.get(event.getClass());
if (converter != null) {
if (!type.isArray() && delta != null) {
converter.set(event, (T)delta[0]);
} else {
converter.set(event, (T)delta);
}
return;
}
}
if (changer != null) {
ChangerUtils.change(changer, getArray(event), delta, mode);
}
}

@Override
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/ch/njol/skript/registrations/EventConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ch.njol.skript.registrations;

import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.lang.converter.Converter;

/**
* Used as a converter in EventValue registration to allow for setting event-values
*
* @param <E> Event class to change value
* @param <T> Type of value to change
*/
public interface EventConverter<E extends Event, T> extends Converter<E, T> {

/**
* Set the value of something in an event
*
* @param event Event to have value changed
* @param value Value to change in event
*/
void set(E event, @Nullable T value);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.skriptlang.skript.test.tests.syntaxes.expressions;

import ch.njol.skript.test.runner.SkriptJUnitTest;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;

public class EventValueExpressionTest extends SkriptJUnitTest {

static {
setShutdownDelay(1);
}

private Player player;

@Before
public void before() {
player = EasyMock.niceMock(Player.class);
EasyMock.expect(player.getName()).andReturn("MrHungryPants");
EasyMock.replay(player);
}

@Test
public void test() {
Bukkit.getPluginManager().callEvent(new PlayerItemConsumeEvent(player, new ItemStack(Material.APPLE), EquipmentSlot.HAND));
}

}
17 changes: 17 additions & 0 deletions src/test/skript/junit/EventValueExpression.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
test "EventValueExpression setters" when running JUnit:
set {_tests::1} to "correct starting item"
set {_tests::2} to "correct changed item"

ensure junit test "org.skriptlang.skript.test.tests.syntaxes.expressions.EventValueExpressionTest" completes {_tests::*}

on consume:
set {_test} to "org.skriptlang.skript.test.tests.syntaxes.expressions.EventValueExpressionTest"
junit test is {_test}

if event-item is an apple:
complete objective "correct starting item" for {_test}

set event-item to 1 of stick

if event-item is a stick:
complete objective "correct changed item" for {_test}
Loading