Skip to content

Commit

Permalink
Added timestamp to Packet events and added a priority system to Event…
Browse files Browse the repository at this point in the history
…Method annotation.
  • Loading branch information
funkemunky committed Jan 11, 2019
1 parent 5620b51 commit 9dc88aa
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 112 deletions.
204 changes: 93 additions & 111 deletions API/.idea/workspace.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ public class PacketRecieveEvent extends Event implements Cancellable {
private Object packet;
private boolean cancelled;
private String type;
private long timeStamp;

public PacketRecieveEvent(Player player, Object packet, String type) {
this.player = player;
this.packet = packet;
this.type = type;

timeStamp = System.currentTimeMillis();
}

@Override
Expand Down
3 changes: 3 additions & 0 deletions API/src/cc/funkemunky/api/event/custom/PacketSendEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ public class PacketSendEvent extends Event implements Cancellable {
private Object packet;
private boolean cancelled;
private String type;
private long timeStamp;

public PacketSendEvent(Player player, Object packet, String type) {
this.player = player;
this.packet = packet;
this.type = type;

timeStamp = System.currentTimeMillis();
}

@Override
Expand Down
14 changes: 14 additions & 0 deletions API/src/cc/funkemunky/api/event/system/EnumPriority.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cc.funkemunky.api.event.system;

import lombok.Getter;

public enum EnumPriority {
LOWEST(0), LOW(1), NORMAL(2), HIGH(3), HIGHEST(4);

@Getter
private int priority;

EnumPriority(int priority) {
this.priority = priority;
}
}
7 changes: 6 additions & 1 deletion API/src/cc/funkemunky/api/event/system/EventManager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cc.funkemunky.api.event.system;

import cc.funkemunky.api.Atlas;
import com.google.common.collect.Lists;
import org.bukkit.block.Block;
import org.bukkit.scheduler.BukkitRunnable;

Expand All @@ -18,6 +19,10 @@ public static void register(Listener listener) {
if (method.isAnnotationPresent(EventMethod.class)) {
List<Method> methods = registered.getOrDefault(listener, new ArrayList<>());
methods.add(method);

methods.sort(Comparator.comparingInt(m -> m.getAnnotation(EventMethod.class).priority().getPriority()));
Collections.reverse(methods);

registered.put(listener, methods);
}
}
Expand All @@ -40,7 +45,7 @@ public static void callEvent(Event event) {
Atlas.getInstance().getThreadPool().execute(() -> call(event));
}

private synchronized static void call(Event event) {
private static void call(Event event) {
for (Listener listener : registered.keySet()) {
for (Method method : registered.get(listener)) {
if (method.getParameterTypes()[0] == event.getClass()) {
Expand Down
4 changes: 4 additions & 0 deletions API/src/cc/funkemunky/api/event/system/EventMethod.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cc.funkemunky.api.event.system;

import org.bukkit.event.EventPriority;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand All @@ -8,5 +10,7 @@
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = {ElementType.METHOD})
public @interface EventMethod {

EnumPriority priority() default EnumPriority.NORMAL;
}

0 comments on commit 9dc88aa

Please sign in to comment.