Skip to content

Commit

Permalink
1.7.3 release
Browse files Browse the repository at this point in the history
* We no longer eject players when they exit
* Fixed error when you leave the server
* No reload support with inject early setting enabled
  • Loading branch information
retrooper committed Nov 3, 2020
1 parent 4a3e89d commit 6126081
Show file tree
Hide file tree
Showing 15 changed files with 166 additions and 175 deletions.
9 changes: 8 additions & 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.2</version>
<version>1.7.3</version>

<!-- You can build the project with this: "mvn clean compile assembly:single" -->
<build>
Expand Down Expand Up @@ -95,6 +95,13 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.github.spigot</groupId>
<artifactId>1.9</artifactId>
<version>1.9</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.github.spigot</groupId>
<artifactId>1.8.8</artifactId>
Expand Down
35 changes: 10 additions & 25 deletions src/main/java/io/github/retrooper/packetevents/PacketEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,14 @@
import org.bukkit.plugin.Plugin;

import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.Future;

public final class PacketEvents implements Listener {
private static final String nettyHandlerIdentifier = generateRandomNettyIdentifier();

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, 2);
private static final PEVersion version = new PEVersion(1, 7, 3);

private static PacketEventsSettings settings = new PacketEventsSettings();

Expand Down Expand Up @@ -164,10 +161,13 @@ public void run() {
*/
public static void stop() {
if (initialized) {
for (final Player p : Bukkit.getOnlinePlayers()) {
getAPI().getPlayerUtils().ejectPlayer(p);
for (Player player : Bukkit.getOnlinePlayers()) {
PacketEvents.getAPI().packetManager.ejectPlayer(player);
}

if (PacketEvents.getAPI().packetManager.tinyProtocol != null) {
PacketEvents.getAPI().packetManager.tinyProtocol.unregisterChannelHandler();
}
getAPI().getEventManager().unregisterAllListeners();
NettyPacketManager.executorService.shutdownNow();
initialized = false;
Expand All @@ -190,10 +190,6 @@ public static ArrayList<Plugin> getPlugins() {
return plugins;
}

public static String getHandlerName(final String name) {
return "pe-" + nettyHandlerIdentifier + "-" + name;
}

/**
* Get the PacketEvents settings.
*
Expand All @@ -212,7 +208,7 @@ public void onLogin(PlayerLoginEvent e) {
if (PacketEvents.getSettings().shouldInjectEarly()) {
assert getAPI().packetManager.tinyProtocol != null;
try {
if (getAPI().packetManager.tinyProtocol.canInject(e.getPlayer())) {
if (getAPI().packetManager.canInject(e.getPlayer())) {
getAPI().packetManager.injectPlayer(e.getPlayer());
}
} catch (Exception ex) {
Expand All @@ -236,26 +232,15 @@ public void onJoin(final PlayerJoinEvent e) {
}
}
if (!PacketEvents.getSettings().shouldInjectEarly()) {
PacketEvents.getAPI().packetManager.injectPlayer(e.getPlayer());
if (getAPI().packetManager.canInject(e.getPlayer())) {
PacketEvents.getAPI().packetManager.injectPlayer(e.getPlayer());
}
}
}


@EventHandler
public void onQuit(final PlayerQuitEvent e) {
PacketEvents.getAPI().getPlayerUtils().ejectPlayer(e.getPlayer());
PacketEvents.getAPI().getPlayerUtils().clientVersionsMap.remove(e.getPlayer().getUniqueId());
}

private static String generateRandomNettyIdentifier() {
String alphabet = "abcdefghijklmnopqrstuvwxyz";
StringBuilder sb = new StringBuilder();
Random random = new Random();
while (sb.length() < 13) {
int index = (int) (random.nextFloat() * alphabet.length());
sb.append(alphabet.charAt(index));
}
return sb.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
package io.github.retrooper.packetevents.event.manager;

import io.github.retrooper.packetevents.PacketEvents;
import io.github.retrooper.packetevents.event.annotation.PacketHandler;
import io.github.retrooper.packetevents.event.eventtypes.CancellableEvent;
import io.github.retrooper.packetevents.event.PacketEvent;
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.priority.PacketEventPriority;
import io.github.retrooper.packetevents.utils.protocollib.ProtocolLibListener;
import io.github.retrooper.packetevents.utils.protocollib.ProtocolLibAPIListener;
import io.github.retrooper.packetevents.utils.protocollib.ProtocolLibUtils;

import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -76,7 +76,7 @@ public void callEvent(final PacketEvent e) {
public void registerListener(final PacketListener listener) {
final ConcurrentLinkedQueue<Method> methods = new ConcurrentLinkedQueue<>();
for (final Method m : listener.getClass().getDeclaredMethods()) {
if(!m.isAccessible()) {
if (!m.isAccessible()) {
m.setAccessible(true);
}
if (m.isAnnotationPresent(PacketHandler.class)
Expand All @@ -88,7 +88,7 @@ public void registerListener(final PacketListener listener) {
if (!methods.isEmpty()) {
if (ProtocolLibUtils.isAvailable()
&& PacketEvents.getSettings().shouldUseProtocolLibIfAvailable()) {
ProtocolLibListener.registerProtocolLibListener(listener, methods);
ProtocolLibAPIListener.registerProtocolLibListener(listener, methods);
} else {
registeredMethods.put(listener, methods);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.github.retrooper.packetevents.event.impl.PacketReceiveEvent;
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.utils.server.ServerVersion;
import org.bukkit.plugin.java.JavaPlugin;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,18 @@

import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;

public class PacketManager {
private static AtomicInteger handlerID = new AtomicInteger(0);
private int handlerNumber = handlerID.getAndIncrement();
private final Plugin plugin;
private final boolean tinyProtocolMode;
public final TinyProtocol tinyProtocol;
public final NettyPacketManager nettyProtocol;
private final HashMap<UUID, Long> keepAliveMap = new HashMap<>();
public final ConcurrentLinkedQueue<Object> queuedEjectionChannels = new ConcurrentLinkedQueue<>();

public PacketManager(Plugin plugin, boolean tinyProtocolMode) {
this.plugin = plugin;
Expand All @@ -67,6 +72,10 @@ public void injectPlayer(Player player) {
}
}

public void schedulePlayerEjection(Player player) {
queuedEjectionChannels.add(NMSUtils.getChannel(player));
}

public void ejectPlayer(Player player) {
if (PacketEvents.getSettings().shouldEjectAsync()) {
ejectPlayerAsync(player);
Expand Down Expand Up @@ -104,7 +113,6 @@ public void ejectPlayerSync(Player player) {
PacketEvents.getAPI().getEventManager().callEvent(ejectEvent);
if (!ejectEvent.isCancelled()) {
keepAliveMap.remove(player.getUniqueId());
NMSUtils.channelCache.remove(player.getUniqueId());
if (tinyProtocolMode) {
tinyProtocol.ejectPlayer(player);
} else {
Expand All @@ -118,7 +126,6 @@ public void ejectPlayerAsync(Player player) {
PacketEvents.getAPI().getEventManager().callEvent(ejectEvent);
if (!ejectEvent.isCancelled()) {
keepAliveMap.remove(player.getUniqueId());
NMSUtils.channelCache.remove(player.getUniqueId());
if (tinyProtocolMode) {
tinyProtocol.ejectPlayerAsync(player);
} else {
Expand All @@ -127,6 +134,33 @@ public void ejectPlayerAsync(Player player) {
}
}

public void ejectChannel(Object channel) {
if(!PacketEvents.getSettings().shouldEjectAsync()) {
ejectChannelSync(channel);
}
else {
ejectChannelAsync(channel);
}
}

public void ejectChannelSync(Object channel) {
if(tinyProtocolMode) {
tinyProtocol.ejectChannelSync(channel);
}
else {
nettyProtocol.ejectChannelSync(channel);
}
}

public void ejectChannelAsync(Object channel) {
if(tinyProtocolMode) {
tinyProtocol.ejectChannelAsync(channel);
}
else {
nettyProtocol.ejectChannelAsync(channel);
}
}

public void sendPacket(Object channel, Object packet) {
if (tinyProtocolMode) {
tinyProtocol.sendPacket(channel, packet);
Expand All @@ -135,6 +169,18 @@ public void sendPacket(Object channel, Object packet) {
}
}

public boolean canInject(Player player) {
Object channel = NMSUtils.getChannel(player);
if(channel == null) {
return true;
}
return !queuedEjectionChannels.contains(channel);
}

public String getNettyHandlerName() {
return "pe-" + plugin.getName() + "-" + handlerNumber;
}

public Object read(Player player, Object channel, Object packet) {
switch (ClassUtil.getClassSimpleName(packet.getClass())) {
case "PacketHandshakingInSetProtocol":
Expand Down Expand Up @@ -265,4 +311,5 @@ private void interceptLogin(PacketLoginEvent event) {
private void interceptStatus(PacketStatusEvent event) {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

package io.github.retrooper.packetevents.packetmanager.netty;

import io.github.retrooper.packetevents.PacketEvents;
import io.github.retrooper.packetevents.utils.nms.NMSUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
Expand Down Expand Up @@ -68,10 +67,10 @@ public NettyPacketManager(Plugin plugin) {
*/
public void injectPlayer(final Player player) {
if (v1_7_nettyMode) {
NettyPacketManager_7 npm_7 = (NettyPacketManager_7)npm;
NettyPacketManager_7 npm_7 = (NettyPacketManager_7) npm;
npm_7.injectPlayer(player);
} else {
NettyPacketManager_8 npm_8 = (NettyPacketManager_8)npm;
NettyPacketManager_8 npm_8 = (NettyPacketManager_8) npm;
npm_8.injectPlayer(player);
}
}
Expand All @@ -86,10 +85,10 @@ public void injectPlayerAsync(final Player player) {
Object channel = NMSUtils.getChannel(player);
executorService.execute(() -> {
if (v1_7_nettyMode) {
NettyPacketManager_7 npm_7 = (NettyPacketManager_7)npm;
NettyPacketManager_7 npm_7 = (NettyPacketManager_7) npm;
npm_7.injectPlayer(player);
} else {
NettyPacketManager_8 npm_8 = (NettyPacketManager_8)npm;
NettyPacketManager_8 npm_8 = (NettyPacketManager_8) npm;
npm_8.injectPlayer(player);
}
});
Expand All @@ -102,10 +101,10 @@ public void injectPlayerAsync(final Player player) {
*/
public void ejectPlayer(final Player player) {
if (v1_7_nettyMode) {
NettyPacketManager_7 npm_7 = (NettyPacketManager_7)npm;
NettyPacketManager_7 npm_7 = (NettyPacketManager_7) npm;
npm_7.ejectPlayer(player);
} else {
NettyPacketManager_8 npm_8 = (NettyPacketManager_8)npm;
NettyPacketManager_8 npm_8 = (NettyPacketManager_8) npm;
npm_8.ejectPlayer(player);
}
}
Expand All @@ -118,21 +117,43 @@ public void ejectPlayer(final Player player) {
public void ejectPlayerAsync(final Player player) {
executorService.execute(() -> {
if (v1_7_nettyMode) {
NettyPacketManager_7 npm_7 = (NettyPacketManager_7)npm;
NettyPacketManager_7 npm_7 = (NettyPacketManager_7) npm;
npm_7.ejectPlayer(player);
} else {
NettyPacketManager_8 npm_8 = (NettyPacketManager_8)npm;
NettyPacketManager_8 npm_8 = (NettyPacketManager_8) npm;
npm_8.ejectPlayer(player);
}
});
}

public void ejectChannelSync(Object channel) {
if (v1_7_nettyMode) {
NettyPacketManager_7 npm_7 = (NettyPacketManager_7) npm;
npm_7.ejectChannel(channel);
} else {
NettyPacketManager_8 npm_8 = (NettyPacketManager_8) npm;
npm_8.ejectChannel(channel);
}
}

public void ejectChannelAsync(Object channel) {
executorService.execute(() -> {
if (v1_7_nettyMode) {
NettyPacketManager_7 npm_7 = (NettyPacketManager_7) npm;
npm_7.ejectChannel(channel);
} else {
NettyPacketManager_8 npm_8 = (NettyPacketManager_8) npm;
npm_8.ejectChannel(channel);
}
});
}

public void sendPacket(Object channel, Object packet) {
if (v1_7_nettyMode) {
NettyPacketManager_7 npm_7 = (NettyPacketManager_7)npm;
NettyPacketManager_7 npm_7 = (NettyPacketManager_7) npm;
npm_7.sendPacket(channel, packet);
} else {
NettyPacketManager_8 npm_8 = (NettyPacketManager_8)npm;
NettyPacketManager_8 npm_8 = (NettyPacketManager_8) npm;
npm_8.sendPacket(channel, packet);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,20 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
}
};
final ChannelPipeline pipeline = ((Channel) NMSUtils.getChannel(player)).pipeline();
pipeline.addBefore("packet_handler", PacketEvents.getHandlerName(player.getName()), channelDuplexHandler);
pipeline.addBefore("packet_handler", PacketEvents.getAPI().packetManager.getNettyHandlerName(), channelDuplexHandler);
}

/**
* Eject a player using 1.7.10 netty import location.
* @param player Target Player
*/
public void ejectPlayer(final Player player) {
Channel channel = (Channel) NMSUtils.getChannelNoCache(player);
public void ejectPlayer(Player player) {
Channel channel = (Channel)NMSUtils.getChannel(player);
assert channel != null;
channel.pipeline().remove(PacketEvents.getHandlerName(player.getName()));
ejectChannel(channel);
}

public void ejectChannel(Object ch) {
Channel channel = (Channel)ch;
channel.pipeline().remove(PacketEvents.getAPI().packetManager.getNettyHandlerName());
}

public void sendPacket(Object rawChannel, Object packet) {
Channel channel = (Channel)rawChannel;
channel.pipeline().writeAndFlush(packet);
Expand Down
Loading

0 comments on commit 6126081

Please sign in to comment.