Skip to content

Commit

Permalink
Merge pull request #60 from retrooper/dev
Browse files Browse the repository at this point in the history
1.7.4 Release
  • Loading branch information
retrooper authored Nov 8, 2020
2 parents 29a4280 + 999e21b commit a1c591a
Show file tree
Hide file tree
Showing 24 changed files with 501 additions and 332 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.7.3</version>
<version>1.7.4</version>

<!-- You can build the project with this: "mvn clean compile assembly:single" -->
<build>
Expand Down
27 changes: 19 additions & 8 deletions src/main/java/io/github/retrooper/packetevents/PacketEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,23 @@
import org.bukkit.plugin.Plugin;

import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public final class PacketEvents implements Listener {
private static final PacketEventsAPI packetEventsAPI = new PacketEventsAPI();
private static final PacketEvents instance = new PacketEvents();
private static final ArrayList<Plugin> plugins = new ArrayList<>(1);
private static boolean loaded, initialized;
private static final PEVersion version = new PEVersion(1, 7, 3);
private static final PEVersion version = new PEVersion(1, 7, 4);

private static PacketEventsSettings settings = new PacketEventsSettings();
/** General executor service, basically for anything that the packet executor service doesn't do.
*/
public static ExecutorService generalExecutorService = Executors.newCachedThreadPool();
//Executor used for player injecting/ejecting and for packet processing/event calling
public static ExecutorService packetHandlingExecutorService = Executors.newSingleThreadExecutor();

/**
* This loads the PacketEvents API.
Expand Down Expand Up @@ -128,6 +135,14 @@ public static void init(final Plugin pl, PacketEventsSettings packetEventsSettin
load();
if (!initialized) {
settings = packetEventsSettings;
int packetHandlingThreadCount = settings.getPacketHandlingThreadCount();
//if the count is 1 or is invalid
if(packetHandlingThreadCount == 1 || packetHandlingThreadCount < 0) {
packetHandlingExecutorService = Executors.newSingleThreadExecutor();
}
else {
packetHandlingExecutorService = Executors.newFixedThreadPool(packetHandlingThreadCount);
}
plugins.add(pl);

//Register Bukkit listener
Expand All @@ -139,12 +154,7 @@ public static void init(final Plugin pl, PacketEventsSettings packetEventsSettin
}

if (settings.shouldCheckForUpdates()) {
Future<?> future = NettyPacketManager.executorService.submit(new Runnable() {
@Override
public void run() {
new UpdateChecker().handleUpdate();
}
});
PacketEvents.generalExecutorService.execute(() -> new UpdateChecker().handleUpdate());
}

if (getAPI().getServerUtils().isBungeeCordEnabled()) {
Expand All @@ -169,7 +179,8 @@ public static void stop() {
PacketEvents.getAPI().packetManager.tinyProtocol.unregisterChannelHandler();
}
getAPI().getEventManager().unregisterAllListeners();
NettyPacketManager.executorService.shutdownNow();
PacketEvents.generalExecutorService.shutdownNow();
PacketEvents.packetHandlingExecutorService.shutdownNow();
initialized = false;
}
}
Expand Down
38 changes: 28 additions & 10 deletions src/main/java/io/github/retrooper/packetevents/enums/Direction.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,33 @@
package io.github.retrooper.packetevents.enums;

public enum Direction {
DOWN, UP, NORTH, SOUTH, WEST, EAST, NORTH_EAST,
NORTH_WEST, SOUTH_EAST,
SOUTH_WEST, WEST_NORTH_WEST,
NORTH_NORTH_WEST, NORTH_NORTH_EAST,
EAST_NORTH_EAST, EAST_SOUTH_EAST,
SOUTH_SOUTH_EAST, SOUTH_SOUTH_WEST,
WEST_SOUTH_WEST, SELF, NULL;
/**
* -Y offset
*/
DOWN,

public static Direction get(final int i) {
return values()[i];
}
/**
* +Y offset
*/
UP,

/**
* -Z offset
*/
NORTH,

/**
* +Z offset
*/
SOUTH,

/**
* -X offset
*/
WEST,

/**
* +X offset
*/
EAST;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,26 @@
import io.github.retrooper.packetevents.event.PacketListener;
import io.github.retrooper.packetevents.event.annotation.PacketHandler;
import io.github.retrooper.packetevents.event.eventtypes.CancellableEvent;
import io.github.retrooper.packetevents.event.impl.*;
import io.github.retrooper.packetevents.event.priority.PacketEventPriority;
import io.github.retrooper.packetevents.utils.protocollib.ProtocolLibAPIListener;
import io.github.retrooper.packetevents.utils.protocollib.ProtocolLibUtils;

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.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

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

private final Map<PacketListener, List<Method>> staticRegisteredMethods = new HashMap<>();
public void callEvent(final PacketEvent e) {
for (final PacketListener listener : registeredMethods.keySet()) {
ConcurrentLinkedQueue<Method> methods = registeredMethods.get(listener);
//STATIC LISTENERS
for (final PacketListener listener : staticRegisteredMethods.keySet()) {
List<Method> methods = staticRegisteredMethods.get(listener);
final boolean[] isCancelled = {false};
final byte[] eventPriority = {PacketEventPriority.LOWEST};
for (Method method : methods) {
Expand All @@ -64,17 +69,13 @@ public void callEvent(final PacketEvent e) {
isCancelled[0] = ce.isCancelled();
}
}
if (e instanceof CancellableEvent) {
CancellableEvent cancellableEvent = (CancellableEvent) e;
cancellableEvent.setCancelled(isCancelled[0]);
}
}
}
}
}

public void registerListener(final PacketListener listener) {
final ConcurrentLinkedQueue<Method> methods = new ConcurrentLinkedQueue<>();
final List<Method> methods = new ArrayList<>();
for (final Method m : listener.getClass().getDeclaredMethods()) {
if (!m.isAccessible()) {
m.setAccessible(true);
Expand All @@ -90,7 +91,7 @@ public void registerListener(final PacketListener listener) {
&& PacketEvents.getSettings().shouldUseProtocolLibIfAvailable()) {
ProtocolLibAPIListener.registerProtocolLibListener(listener, methods);
} else {
registeredMethods.put(listener, methods);
staticRegisteredMethods.put(listener, methods);
}
}
}
Expand All @@ -102,7 +103,7 @@ public void registerListeners(final PacketListener... listeners) {
}

public void unregisterListener(final PacketListener e) {
registeredMethods.remove(e);
staticRegisteredMethods.remove(e);
}

public void unregisterListeners(final PacketListener... listeners) {
Expand All @@ -112,10 +113,10 @@ public void unregisterListeners(final PacketListener... listeners) {
}

public void unregisterAllListeners() {
registeredMethods.clear();
staticRegisteredMethods.clear();
}

public boolean isRegistered(final PacketListener listener) {
return registeredMethods.containsKey(listener);
return staticRegisteredMethods.containsKey(listener);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@
import io.github.retrooper.packetevents.event.PacketListener;
import io.github.retrooper.packetevents.event.annotation.PacketHandler;
import io.github.retrooper.packetevents.event.impl.PacketReceiveEvent;
import io.github.retrooper.packetevents.event.impl.PlayerEjectEvent;
import io.github.retrooper.packetevents.event.impl.PlayerInjectEvent;
import io.github.retrooper.packetevents.packettype.PacketType;
import io.github.retrooper.packetevents.packetwrappers.in.chat.WrappedPacketInChat;
import io.github.retrooper.packetevents.utils.player.ClientVersion;
import io.github.retrooper.packetevents.packetwrappers.out.helditemslot.WrappedPacketOutHeldItemSlot;
import io.github.retrooper.packetevents.utils.server.ServerVersion;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

public class MainExample extends JavaPlugin implements PacketListener {
Expand All @@ -43,10 +46,15 @@ public void onLoad() {

@Override
public void onEnable() {
PacketEvents.getSettings().injectAsync(true).ejectAsync(true)
.backupServerVersion(ServerVersion.v_1_7_10).
useProtocolLibIfAvailable(true).checkForUpdates(true).injectEarly(true);
PacketEvents.getSettings().injectAsync(true).ejectAsync(true).
backupServerVersion(ServerVersion.v_1_7_10).useProtocolLibIfAvailable(true).
checkForUpdates(true).injectEarly(true).packetHandlingThreadCount(1);
PacketEvents.getAPI().getEventManager().registerListener(this);
PacketEvents.init(this);
}

@Override
public void onDisable() {
PacketEvents.stop();
}
}
Loading

0 comments on commit a1c591a

Please sign in to comment.