Skip to content

Commit 72914a4

Browse files
committed
Yea I forgot most of the files with last commit, here's the actual thing
1 parent 12ad04a commit 72914a4

File tree

8 files changed

+119
-29
lines changed

8 files changed

+119
-29
lines changed

src/main/java/com/btk5h/skriptmirror/skript/custom/event/BukkitCustomEvent.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import ch.njol.skript.classes.ClassInfo;
44
import ch.njol.skript.registrations.Classes;
5+
import org.bukkit.Bukkit;
56
import org.bukkit.event.Event;
67
import org.bukkit.event.HandlerList;
78

@@ -23,6 +24,11 @@ public class BukkitCustomEvent extends Event {
2324
private final Map<String, Object> dataMap;
2425

2526
public BukkitCustomEvent(String name) {
27+
this(name, !Bukkit.isPrimaryThread());
28+
}
29+
30+
public BukkitCustomEvent(String name, boolean isAsync) {
31+
super(isAsync);
2632
this.name = name;
2733
this.eventValueMap = new HashMap<>();
2834
this.dataMap = new HashMap<>();

src/main/java/com/btk5h/skriptmirror/skript/custom/event/CustomEvent.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313

1414
/**
1515
* This is the Skript event for all custom classes.
16-
* {@link #getLastCustomEvent()} returns the last instance of this class, which is used internally for determining
16+
* {@link #getLastWhich()} ()} returns the last EventSyntaxInfo of the last custom event, which is used internally for determining
1717
* which event-values can be used in parse-time.
1818
*/
1919
public class CustomEvent extends SkriptEvent {
2020

21-
private static CustomEvent lastCustomEvent;
21+
// private static CustomEvent lastCustomEvent;
22+
private static EventSyntaxInfo lastWhich;
2223

2324
private EventSyntaxInfo which;
2425
private Expression<?>[] exprs;
@@ -42,7 +43,8 @@ public boolean init(Literal<?>[] args, int matchedPattern, SkriptParser.ParseRes
4243
return false;
4344
}
4445

45-
lastCustomEvent = this;
46+
// lastCustomEvent = this;
47+
lastWhich = which;
4648

4749
Trigger parseHandler = CustomEventSection.parserHandlers.get(which);
4850

@@ -83,8 +85,12 @@ public String toString(@Nullable Event e, boolean debug) {
8385
return which.getPattern();
8486
}
8587

86-
public static CustomEvent getLastCustomEvent() {
87-
return lastCustomEvent;
88+
public static EventSyntaxInfo getLastWhich() {
89+
return lastWhich;
90+
}
91+
92+
public static void setLastWhich(EventSyntaxInfo which) {
93+
lastWhich = which;
8894
}
8995

9096
public EventSyntaxInfo getWhich() {

src/main/java/com/btk5h/skriptmirror/skript/custom/event/CustomEventSection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import com.btk5h.skriptmirror.skript.custom.CustomSyntaxSection;
1212
import com.btk5h.skriptmirror.skript.custom.SyntaxParseEvent;
1313
import com.btk5h.skriptmirror.util.SkriptUtil;
14-
import org.bukkit.Bukkit;
1514

1615
import java.io.File;
1716
import java.lang.reflect.Field;
@@ -135,6 +134,7 @@ protected boolean init(Literal<?>[] args, int matchedPattern, SkriptParser.Parse
135134

136135
if (key.equalsIgnoreCase("check")) {
137136
ScriptLoader.setCurrentEvent("custom event trigger", EventTriggerEvent.class);
137+
CustomEvent.setLastWhich(whichInfo.get(0));
138138
List<TriggerItem> items = SkriptUtil.getItemsFromNode(sectionNode);
139139
whichInfo.forEach(which ->
140140
eventHandlers.put(which,

src/main/java/com/btk5h/skriptmirror/skript/custom/event/CustomEventUtils.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@
1111
public class CustomEventUtils {
1212

1313
/**
14-
* @param customEvent The CustomEvent which is used
14+
* @param which The EventSyntaxInfo that belongs to the used custom event
1515
* @param classInfo The used ClassInfo
1616
* @return whether the given CustomEvent supports the given ClassInfo as an event-value.
1717
*/
18-
public static boolean hasEventValue(CustomEvent customEvent, ClassInfo<?> classInfo) {
19-
EventSyntaxInfo which = customEvent.getWhich();
20-
18+
public static boolean hasEventValue(EventSyntaxInfo which, ClassInfo<?> classInfo) {
2119
List<ClassInfo<?>> eventValueClassInfoList = CustomEventSection.eventValueTypes.get(which);
2220
if (eventValueClassInfoList == null)
2321
return false;
@@ -40,11 +38,11 @@ public static String getName(ClassInfo<?> classInfo) {
4038
}
4139

4240
/**
43-
* @param customEvent The CustomEvent which name is returned
44-
* @return The defined name (identifier) of the given CustomEvent.
41+
* @param which The EventSyntaxInfo that belongs to the used custom event
42+
* @return The defined name (identifier) of the custom event from the given EventSyntaxInfo
4543
*/
46-
public static String getName(CustomEvent customEvent) {
47-
return CustomEventSection.nameValues.get(customEvent.getWhich());
44+
public static String getName(EventSyntaxInfo which) {
45+
return CustomEventSection.nameValues.get(which);
4846
}
4947

5048
}

src/main/java/com/btk5h/skriptmirror/skript/custom/event/EffCallCustomEvent.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
public class EffCallCustomEvent extends Effect {
1717

1818
static {
19-
Skript.registerEffect(EffCallCustomEvent.class, "call custom event %string% [(with|using) [[event-]values] %-objects%] [(with|using) data %-objects%]");
19+
Skript.registerEffect(EffCallCustomEvent.class, "call custom event %string% [(with|using) [[event-]values] %-objects%] [[and] [(with|using)] data %-objects%]");
2020
}
2121

2222
private Expression<String> customEventName;
@@ -53,8 +53,6 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
5353

5454
@Override
5555
protected void execute(Event e) {
56-
// TODO check for async custom event call
57-
5856
BukkitCustomEvent bukkitCustomEvent = new BukkitCustomEvent(this.customEventName.getSingle(e));
5957

6058
if (eventValueVarList != null)

src/main/java/com/btk5h/skriptmirror/skript/custom/event/ExprCustomEventValue.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ public ExprCustomEventValue() {
3131

3232
@Override
3333
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final SkriptParser.ParseResult parseResult) {
34-
if (!ScriptLoader.isCurrentEvent(BukkitCustomEvent.class))
34+
if (!ScriptLoader.isCurrentEvent(BukkitCustomEvent.class, EventTriggerEvent.class))
3535
return false;
36-
CustomEvent customEvent = CustomEvent.getLastCustomEvent();
37-
if (customEvent == null)
36+
EventSyntaxInfo which = CustomEvent.getLastWhich();
37+
if (which == null)
3838
return false;
3939

4040
String stringClass = parseResult.regexes.get(0).group();
@@ -43,9 +43,9 @@ public boolean init(final Expression<?>[] exprs, final int matchedPattern, final
4343
return false;
4444
}
4545

46-
if (!CustomEventUtils.hasEventValue(customEvent, classInfo)) {
46+
if (!CustomEventUtils.hasEventValue(which, classInfo)) {
4747
Skript.error("There is no " + CustomEventUtils.getName(classInfo) + " in the custom event " +
48-
CustomEventUtils.getName(customEvent));
48+
CustomEventUtils.getName(which));
4949
return false;
5050
}
5151

@@ -55,9 +55,14 @@ public boolean init(final Expression<?>[] exprs, final int matchedPattern, final
5555
@SuppressWarnings("unchecked")
5656
@Override
5757
public T[] get(Event event) {
58-
if (!(event instanceof BukkitCustomEvent))
58+
if (!(event instanceof BukkitCustomEvent || event instanceof EventTriggerEvent))
5959
return null;
60-
BukkitCustomEvent bukkitCustomEvent = (BukkitCustomEvent) event;
60+
BukkitCustomEvent bukkitCustomEvent;
61+
if (event instanceof BukkitCustomEvent) {
62+
bukkitCustomEvent = (BukkitCustomEvent) event;
63+
} else {
64+
bukkitCustomEvent = (BukkitCustomEvent) ((EventTriggerEvent) event).getEvent();
65+
}
6166

6267
T[] tArray = (T[]) Array.newInstance(classInfo.getC(), 1);
6368
tArray[0] = (T) bukkitCustomEvent.getEventValue(classInfo);
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.btk5h.skriptmirror.skript.custom.event;
2+
3+
import ch.njol.skript.ScriptLoader;
4+
import ch.njol.skript.Skript;
5+
import ch.njol.skript.lang.Expression;
6+
import ch.njol.skript.lang.ExpressionType;
7+
import ch.njol.skript.lang.SkriptParser;
8+
import ch.njol.skript.lang.util.SimpleExpression;
9+
import ch.njol.util.Kleenean;
10+
import com.btk5h.skriptmirror.util.SkriptUtil;
11+
import org.bukkit.event.Event;
12+
import org.eclipse.jdt.annotation.Nullable;
13+
14+
import java.util.ArrayList;
15+
16+
public class ExprEventData extends SimpleExpression<Object> {
17+
18+
static {
19+
Skript.registerExpression(ExprEventData.class, Object.class, ExpressionType.COMBINED, "[extra] [event[-]] data %strings%");
20+
}
21+
22+
private Expression<String> dataIndex;
23+
24+
@Override
25+
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
26+
if (!ScriptLoader.isCurrentEvent(BukkitCustomEvent.class, EventTriggerEvent.class)) {
27+
Skript.error("This expression can only be used in a custom event");
28+
return false;
29+
}
30+
dataIndex = SkriptUtil.defendExpression(exprs[0]);
31+
return true;
32+
}
33+
34+
@Nullable
35+
@Override
36+
protected Object[] get(Event e) {
37+
BukkitCustomEvent bukkitCustomEvent;
38+
if (e instanceof BukkitCustomEvent) {
39+
bukkitCustomEvent = (BukkitCustomEvent) e;
40+
} else {
41+
bukkitCustomEvent = (BukkitCustomEvent) ((EventTriggerEvent) e).getEvent();
42+
}
43+
44+
if (dataIndex.isSingle()) {
45+
Object data = bukkitCustomEvent.getData(dataIndex.getSingle(e));
46+
return new Object[] {data};
47+
} else {
48+
ArrayList<Object> arrayList = new ArrayList<>();
49+
for (String index : dataIndex.getArray(e)) {
50+
arrayList.add(bukkitCustomEvent.getData(index));
51+
}
52+
return arrayList.toArray();
53+
}
54+
}
55+
56+
@Override
57+
public boolean isSingle() {
58+
return dataIndex.isSingle();
59+
}
60+
61+
@Override
62+
public Class<?> getReturnType() {
63+
return Object.class;
64+
}
65+
66+
@Override
67+
public String toString(@Nullable Event e, boolean debug) {
68+
return "event data";
69+
}
70+
71+
}

src/main/java/com/btk5h/skriptmirror/skript/custom/event/ExprReplacedEventValue.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@ public ExprReplacedEventValue(EventValueExpression<T> original) {
2828
@Override
2929
@Nullable
3030
protected T[] get(final Event e) {
31-
if (e instanceof BukkitCustomEvent) {
32-
BukkitCustomEvent bukkitCustomEvent = (BukkitCustomEvent) e;
31+
if (e instanceof BukkitCustomEvent || e instanceof EventTriggerEvent) {
32+
BukkitCustomEvent bukkitCustomEvent;
33+
if (e instanceof BukkitCustomEvent) {
34+
bukkitCustomEvent = (BukkitCustomEvent) e;
35+
} else {
36+
bukkitCustomEvent = (BukkitCustomEvent) ((EventTriggerEvent) e).getEvent();
37+
}
38+
3339
Class<?> valueClass = original.getReturnType();
3440

3541
T[] tArray = (T[]) Array.newInstance(valueClass, 1);
@@ -43,10 +49,10 @@ protected T[] get(final Event e) {
4349
@SuppressWarnings("null")
4450
@Override
4551
public boolean init() {
46-
if (ScriptLoader.isCurrentEvent(BukkitCustomEvent.class)) {
47-
CustomEvent customEvent = CustomEvent.getLastCustomEvent();
52+
if (ScriptLoader.isCurrentEvent(BukkitCustomEvent.class, EventTriggerEvent.class)) {
53+
EventSyntaxInfo which = CustomEvent.getLastWhich();
4854
ClassInfo<?> classInfo = Classes.getSuperClassInfo(getReturnType());
49-
return CustomEventUtils.hasEventValue(customEvent, classInfo);
55+
return CustomEventUtils.hasEventValue(which, classInfo);
5056
} else {
5157
return original.init();
5258
}

0 commit comments

Comments
 (0)