Browse Source

Added event stub to scripting example

kindlich 4 years ago
parent
commit
4915d51d3f
No known key found for this signature in database

+ 6
- 0
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/Main.java View File

@@ -18,6 +18,9 @@ import org.openzen.zenscript.parser.BracketExpressionParser;
18 18
 import org.openzen.zenscript.parser.EscapableBracketParser;
19 19
 import org.openzen.zenscript.parser.PrefixedBracketParser;
20 20
 import org.openzen.zenscript.parser.SimpleBracketParser;
21
+import org.openzen.zenscript.scriptingexample.events.EventManager;
22
+import org.openzen.zenscript.scriptingexample.events.IEvent;
23
+import org.openzen.zenscript.scriptingexample.events.impl.CTStringedEvent;
21 24
 
22 25
 public class Main {
23 26
 	public static void main(String[] args) throws CompileException, ParseException, IOException, NoSuchMethodException {
@@ -30,6 +33,9 @@ public class Main {
30 33
 		example.addClass(TestGenericInterface.class);
31 34
 		example.addClass(TestClass.class);
32 35
 		example.addClass(TestInterface.class);
36
+		example.addClass(IEvent.class);
37
+		example.addClass(EventManager.class);
38
+		example.addClass(CTStringedEvent.class);
33 39
 		scriptingEngine.registerNativeProvided(example);
34 40
 
35 41
 		File inputDirectory = new File("scripts");

+ 24
- 0
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/events/EventManager.java View File

@@ -0,0 +1,24 @@
1
+package org.openzen.zenscript.scriptingexample.events;
2
+
3
+import org.openzen.zencode.java.ZenCodeType;
4
+import org.openzen.zenscript.scriptingexample.events.impl.CTStringedEvent;
5
+import org.openzen.zenscript.scriptingexample.events.impl.StringedEvent;
6
+
7
+@ZenCodeType.Name("example.org.openzen.scripting_example.events.EventManager")
8
+public class EventManager {
9
+    @ZenCodeType.Method
10
+    public static void register(IEvent<?, ?> event) {
11
+    //public static <EVE extends IEvent<EVE, VA>, VA extends SomeMCEvent> void register(IEvent<EVE, VA> event) {
12
+        System.out.println("HIT!!!");
13
+
14
+        if(event instanceof CTStringedEvent) {
15
+            ((CTStringedEvent)event).getHandler().accept(new CTStringedEvent(null));
16
+        }
17
+    }
18
+
19
+    //@ZenCodeType.Method
20
+    //public static void register(CTStringedEvent event) {
21
+    //    System.out.println("HIT!!!");
22
+    //    event.getConsumer().accept(new StringedEvent("Abcdef"));
23
+    //}
24
+}

+ 41
- 0
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/events/IEvent.java View File

@@ -0,0 +1,41 @@
1
+package org.openzen.zenscript.scriptingexample.events;
2
+
3
+import org.openzen.zencode.java.ZenCodeType;
4
+
5
+import java.util.function.Consumer;
6
+
7
+@ZenCodeType.Name("example.org.openzen.scripting_example.events.IEvent")
8
+public abstract class IEvent<E extends IEvent<E, V>, V extends SomeMCEvent> {
9
+
10
+    private V internal;
11
+
12
+    private Consumer<E> handler;
13
+
14
+    public IEvent(V internal) {
15
+        this.internal = internal;
16
+    }
17
+
18
+    @ZenCodeType.Constructor
19
+    public IEvent(Consumer<E> handler) {
20
+        this.handler = handler;
21
+    }
22
+
23
+    public void setInternal(V internal) {
24
+        this.internal = internal;
25
+    }
26
+
27
+    public abstract Consumer<V> getConsumer();
28
+
29
+
30
+    public V getInternal() {
31
+        return internal;
32
+    }
33
+
34
+    public Consumer<E> getHandler() {
35
+        return handler;
36
+    }
37
+
38
+    public void setHandler(Consumer<E> handler) {
39
+        this.handler = handler;
40
+    }
41
+}

+ 4
- 0
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/events/SomeMCEvent.java View File

@@ -0,0 +1,4 @@
1
+package org.openzen.zenscript.scriptingexample.events;
2
+
3
+public interface SomeMCEvent {
4
+}

+ 25
- 0
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/events/impl/CTStringedEvent.java View File

@@ -0,0 +1,25 @@
1
+package org.openzen.zenscript.scriptingexample.events.impl;
2
+
3
+import org.openzen.zencode.java.ZenCodeType;
4
+import org.openzen.zenscript.scriptingexample.events.IEvent;
5
+
6
+import java.util.function.Consumer;
7
+
8
+@ZenCodeType.Name("example.org.openzen.scripting_example.events.CTStringedEvent")
9
+public class CTStringedEvent extends IEvent<CTStringedEvent, StringedEvent> {
10
+
11
+    @ZenCodeType.Getter("blub")
12
+    public String getBlub() {
13
+        return "ASDF";
14
+    }
15
+
16
+    @ZenCodeType.Constructor
17
+    public CTStringedEvent(Consumer<CTStringedEvent> handler) {
18
+        super(handler);
19
+    }
20
+
21
+    @Override
22
+    public Consumer<StringedEvent> getConsumer() {
23
+        return stringedEvent -> getHandler().accept(new CTStringedEvent(null));
24
+    }
25
+}

+ 12
- 0
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/events/impl/StringedEvent.java View File

@@ -0,0 +1,12 @@
1
+package org.openzen.zenscript.scriptingexample.events.impl;
2
+
3
+import org.openzen.zenscript.scriptingexample.events.SomeMCEvent;
4
+
5
+public class StringedEvent implements SomeMCEvent {
6
+    private final String text;
7
+
8
+    public StringedEvent(String text) {
9
+
10
+        this.text = text;
11
+    }
12
+}

Loading…
Cancel
Save