Skip to content

Commit

Permalink
now thread safe event priority system,
Browse files Browse the repository at this point in the history
contains example code,
outbound custompayload wrapper is now loaded(not guaranteed to work)
  • Loading branch information
retrooper committed Sep 10, 2020
1 parent 8691304 commit 51a58f7
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 60 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.retrooper</groupId>
<artifactId>packetevents</artifactId>
<version>1.6.6.1</version>
<version>1.6.7</version>

<!-- You can build the project with this: "mvn clean compile assembly:single" -->
<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,49 +36,40 @@

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

public final class EventManager {
private final HashMap<PacketListener, List<Method>> registeredMethods = new HashMap<PacketListener, List<Method>>();
private final ConcurrentHashMap<PacketListener, ConcurrentLinkedQueue<Method>> registeredMethods = new ConcurrentHashMap<>();

public void callEvent(final PacketEvent e) {
for (final PacketListener listener : registeredMethods.keySet()) {
//Annotated methods
final List<Method> methods = registeredMethods.get(listener);
final List<Boolean> isCancellingList = new ArrayList<Boolean>(methods.size());

int highestPriorityMethodListenerIndex = 0;
for(int i = 0; i < methods.size(); i++) {
Method method = methods.get(i);
final Class<?> parameterType = method.getParameterTypes()[0];
ConcurrentLinkedQueue<Method> methods = registeredMethods.get(listener);
final boolean[] isCancelled = {false};
final byte[] eventPriority = {PacketEventPriority.LOWEST};
for (Method method : methods) {
Class<?> parameterType = method.getParameterTypes()[0];
if (parameterType.equals(PacketEvent.class)
|| parameterType.isInstance(e)) {
PacketHandler annotation = methods.get(highestPriorityMethodListenerIndex).getAnnotation(PacketHandler.class);

PacketHandler competitorAnnotation = methods.get(i).getAnnotation(PacketHandler.class);

if(competitorAnnotation.priority() >= annotation.priority()) {
highestPriorityMethodListenerIndex = i;
}

boolean isCurEventCancelled = false;
final Runnable invokeMethod = new Runnable() {
PacketHandler annotation = method.getAnnotation(PacketHandler.class);
Runnable invokeMethod = new Runnable() {
@Override
public void run() {
try {
method.invoke(listener, e);
if(e instanceof CancellableEvent) {
isCancellingList.add(((CancellableEvent) e).isCancelled());
}
} catch (IllegalAccessException | InvocationTargetException ex) {
ex.printStackTrace();
}
if (e instanceof CancellableEvent) {
CancellableEvent ce = (CancellableEvent) e;
if (annotation.priority() >= eventPriority[0]) {
eventPriority[0] = annotation.priority();
isCancelled[0] = ce.isCancelled();
}
}
}
};


switch (annotation.synchronization()) {
case FORCE_ASYNC:
Bukkit.getScheduler().runTaskAsynchronously(PacketEvents.getPlugin(), invokeMethod);
Expand All @@ -92,24 +83,26 @@ public void run() {
} catch (IllegalAccessException | InvocationTargetException ex) {
ex.printStackTrace();
}
isCancellingList.add(((CancellableEvent)e).isCancelled());
if (e instanceof CancellableEvent) {
CancellableEvent ce = (CancellableEvent) e;
if (annotation.priority() >= eventPriority[0]) {
eventPriority[0] = annotation.priority();
isCancelled[0] = ce.isCancelled();
}
}
break;
}

//Last iteration
if(i + 1 == methods.size()) {
if(e instanceof CancellableEvent) {
CancellableEvent cancellable = (CancellableEvent)e;
cancellable.setCancelled(isCancellingList.get(highestPriorityMethodListenerIndex));
}
if (e instanceof CancellableEvent) {
CancellableEvent cancellableEvent = (CancellableEvent) e;
cancellableEvent.setCancelled(isCancelled[0]);
}
}
}
}
}

public void registerListener(final PacketListener listener) {
final List<Method> methods = new ArrayList<Method>();
final ConcurrentLinkedQueue<Method> methods = new ConcurrentLinkedQueue<>();
for (final Method m : listener.getClass().getDeclaredMethods()) {
if (m.isAnnotationPresent(PacketHandler.class)
&& m.getParameterTypes().length == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@

import io.github.retrooper.packetevents.PacketEvents;
import io.github.retrooper.packetevents.annotations.PacketHandler;
import io.github.retrooper.packetevents.enums.PacketEventPriority;
import io.github.retrooper.packetevents.event.PacketListener;
import io.github.retrooper.packetevents.event.impl.PacketReceiveEvent;
import io.github.retrooper.packetevents.event.impl.PacketSendEvent;
import io.github.retrooper.packetevents.packet.PacketType;
import io.github.retrooper.packetevents.packetwrappers.in.chat.WrappedPacketInChat;
import io.github.retrooper.packetevents.packetwrappers.in.useentity.WrappedPacketInUseEntity;
import io.github.retrooper.packetevents.packetwrappers.out.custompayload.WrappedPacketOutCustomPayload;
import io.github.retrooper.packetevents.packetwrappers.out.entity.WrappedPacketOutEntity;
import org.bukkit.plugin.java.JavaPlugin;

public class MainExample extends JavaPlugin implements PacketListener {
Expand All @@ -54,17 +57,26 @@ public void onDisable() {
PacketEvents.stop();
}

@PacketHandler(priority = PacketEventPriority.MONITOR)
public void onReceive1(PacketReceiveEvent e) {
if(e.getPacketId() == PacketType.Client.USE_ENTITY) {
WrappedPacketInUseEntity ue = new WrappedPacketInUseEntity(e.getNMSPacket());
e.cancel();
}
}

@PacketHandler(priority = PacketEventPriority.NORMAL)
public void onReceive2(PacketReceiveEvent e) {
if(e.getPacketId() == PacketType.Client.USE_ENTITY) {
WrappedPacketInUseEntity ue = new WrappedPacketInUseEntity(e.getNMSPacket());
e.uncancel();
}
}

@PacketHandler
public void onSend(PacketSendEvent e) {
if (e.getPacketId() == PacketType.Server.CUSTOM_PAYLOAD) {
WrappedPacketOutCustomPayload cp = new WrappedPacketOutCustomPayload(e.getNMSPacket());
if(e.getPacketId() == PacketType.Server.CUSTOM_PAYLOAD) {

}
}
//creating wrappers is much easier and cleaner now with new functions.
//all wrappers cleaned up
// walk speed returning the fly speed FIXED,
// wrappedpacketoutchat.fromStringToJSON() debug removed,
// wrappedpacketoutkickdisconnect constructor now requires a json string,
// food saturation in wrappedpacketoutupdatehealth was equal to health value by accident
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import io.github.retrooper.packetevents.packetwrappers.out.abilities.WrappedPacketOutAbilities;
import io.github.retrooper.packetevents.packetwrappers.out.animation.WrappedPacketOutAnimation;
import io.github.retrooper.packetevents.packetwrappers.out.chat.WrappedPacketOutChat;
import io.github.retrooper.packetevents.packetwrappers.out.custompayload.WrappedPacketOutCustomPayload;
import io.github.retrooper.packetevents.packetwrappers.out.entity.WrappedPacketOutEntity;
import io.github.retrooper.packetevents.packetwrappers.out.entityvelocity.WrappedPacketOutEntityVelocity;
import io.github.retrooper.packetevents.packetwrappers.out.keepalive.WrappedPacketOutKeepAlive;
Expand Down Expand Up @@ -112,6 +113,7 @@ public static void loadAllWrappers() {
WrappedPacketOutPosition.load();
WrappedPacketOutTransaction.load();
WrappedPacketOutUpdateHealth.load();
WrappedPacketOutCustomPayload.load();
}

protected void setup() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@
import io.github.retrooper.packetevents.packetwrappers.WrappedPacket;
import io.github.retrooper.packetevents.reflectionutils.Reflection;
import io.github.retrooper.packetevents.utils.NMSUtils;
import io.netty.buffer.Unpooled;
import net.minecraft.server.v1_13_R2.MinecraftKey;
import net.minecraft.server.v1_16_R1.PacketDataSerializer;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -99,28 +96,26 @@ public static void load() {
}
}
}
PacketDataSerializer ds = new PacketDataSerializer(Unpooled.copiedBuffer(new byte[10]));

}

private String tag;
private byte[] data;

public WrappedPacketOutCustomPayload(String tag, byte[] bytes) {
public WrappedPacketOutCustomPayload(String tag, byte[] data) {
this.tag = tag;
this.data = data;
}


public WrappedPacketOutCustomPayload(Object packet) {
super(packet);
}

@Override
protected void setup() {
/* public WrappedPacketOutCustomPayload(Object packet) {
super(packet);
}
}
@Override
protected void setup() {
}
*/
public String getTag() {
return tag;
}
Expand Down Expand Up @@ -158,7 +153,8 @@ public Object asNMSPacket() {

try {
Object minecraftKey = minecraftKeyConstructor.newInstance(tag);
Object dataSerializer = packetDataSerializerConstructor.newInstance(byteBufObject);;
Object dataSerializer = packetDataSerializerConstructor.newInstance(byteBufObject);
;
return constructor.newInstance(minecraftKey, dataSerializer);
} catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;

public class ProtocolLibListener {

public static void registerProtocolLibListener(PacketListener listener, List<Method> methods) {
public static void registerProtocolLibListener(PacketListener listener, ConcurrentLinkedQueue<Method> methods) {
if (ProtocolLibUtils.isAvailable()) {
for (Method m : methods) {
if (m.getParameterTypes()[0].equals(PacketReceiveEvent.class)) {
Expand Down

0 comments on commit 51a58f7

Please sign in to comment.