Skip to content

Commit

Permalink
Release 1.5.8.2
Browse files Browse the repository at this point in the history
* Code cleanup
* PacketReceiveEvent#getNMSPacketClass() deprecated
* PacketReceiveEvent#getPacketName() deprecated
* PacketSendEvent#getNMSPacketClass() deprecated
* PacketSendEvent#getPacketName() deprecated
* PlayerUninjectEvent#PlayerUninjectEvent(Player player, boolean sync) deprecated, use PlayerUninjectEvent#PlayerUninjectEvent(Player player)
* WrappedPacketInBlockDig null pointers fixed for 1.7.10, just Direction field will be Direction.NULL, I will fix later
* Clientbound wrappers cleaned up
  • Loading branch information
retrooper committed Aug 19, 2020
1 parent 4ce0463 commit a1d48d3
Show file tree
Hide file tree
Showing 40 changed files with 165 additions and 272 deletions.
14 changes: 4 additions & 10 deletions src/main/java/io/github/retrooper/packetevents/PacketEvents.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package io.github.retrooper.packetevents;

import io.github.retrooper.packetevents.annotations.PacketHandler;
import io.github.retrooper.packetevents.api.PacketEventsAPI;
import io.github.retrooper.packetevents.enums.ClientVersion;
import io.github.retrooper.packetevents.enums.ServerVersion;
import io.github.retrooper.packetevents.event.PacketListener;
import io.github.retrooper.packetevents.event.impl.BukkitMoveEvent;
import io.github.retrooper.packetevents.event.impl.PacketReceiveEvent;
import io.github.retrooper.packetevents.packet.PacketType;
import io.github.retrooper.packetevents.packet.PacketTypeClasses;
import io.github.retrooper.packetevents.packetwrappers.in.settings.WrappedPacketInSettings;
import io.github.retrooper.packetevents.settings.PacketEventsSettings;
import io.github.retrooper.packetevents.utils.onlineplayers.OnlinePlayerUtilities;
import io.github.retrooper.packetevents.utils.versionlookup.VersionLookupUtils;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
Expand All @@ -22,14 +18,12 @@
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.Plugin;

import java.util.HashMap;

public final class PacketEvents implements PacketListener, Listener {
private static Plugin plugin;
private static final PacketEventsAPI packetEventsAPI = new PacketEventsAPI();
private static final PacketEvents instance = new PacketEvents();
private static boolean hasLoaded, hasStarted;
private static final PacketEventsSettings settings = new PacketEventsSettings();
private static Plugin plugin;
private static boolean hasLoaded, hasStarted;

/**
* Call this before start()
Expand Down Expand Up @@ -60,7 +54,7 @@ public static void start(final Plugin pl) {

Bukkit.getPluginManager().registerEvents(instance, plugin);

for (final Player p : OnlinePlayerUtilities.getOnlinePlayers()) {
for (final Player p : Bukkit.getOnlinePlayers()) {
getAPI().getPlayerUtils().injectPlayer(p);
}
hasStarted = true;
Expand All @@ -72,7 +66,7 @@ public static void start(final Plugin pl) {
*/
public static void stop() {
if (hasStarted) {
for (final Player p : OnlinePlayerUtilities.getOnlinePlayers()) {
for (final Player p : Bukkit.getOnlinePlayers()) {

getAPI().getPlayerUtils().uninjectPlayerNow(p);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.retrooper.packetevents.annotations;

import io.github.retrooper.packetevents.annotations.data.EventSynchronization;
import io.github.retrooper.packetevents.enums.EventSynchronization;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public enum ClientVersion {
v_1_15, v_1_15_1, v_1_15_2, v_1_16, v_1_16_PRE_1, v_1_16_PRE_2, v_1_16_PRE_3, v_1_16_PRE_4, v_1_16_PRE_5, v_1_16_PRE_6, v_1_16_PRE_7, v_1_16_PRE_8, v_1_16_RC_1, v_1_16_1, v_1_16_2, HIGHER_THAN_V_1_16_2, INVALID, ACCESS_FAILURE;

private static final Map<Integer, ClientVersion> VERSION_LOOKUP = new HashMap<Integer, ClientVersion>();

public static void prepareLookUp() {
VERSION_LOOKUP.put(-1, ACCESS_FAILURE);
VERSION_LOOKUP.put(1, LESS_THAN_V_1_7_10);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.retrooper.packetevents.annotations.data;
package io.github.retrooper.packetevents.enums;

public enum EventSynchronization {
FORCE_SYNC, FORCE_ASYNC, NORMAL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
import io.github.retrooper.packetevents.PacketEvents;
import org.bukkit.Bukkit;

import java.util.HashMap;

/**
* @author retrooper
*/
public enum ServerVersion {
v_1_7_10, v_1_8, v_1_8_3, v_1_8_8, v_1_9, v_1_9_4, v_1_10, v_1_11, v_1_12, v_1_13, v_1_13_2, v_1_14, v_1_15, v_1_16, v_1_16_1, v_1_16_2,
ERROR, EMPTY;

public static ServerVersion[] reversedValues;
private static final String nmsVersionSuffix = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];
public static ServerVersion[] reversedValues;
private static ServerVersion cachedVersion;

private static ServerVersion getVers() {
for (final ServerVersion val : reversedValues) {
String valName = val.name().substring(2).replace("_", ".");
Expand Down Expand Up @@ -133,10 +132,10 @@ public int toProtocolVersion() {
if (this.equals(ServerVersion.v_1_16)) {
return 735;
}
if(this.equals(ServerVersion.v_1_16_1)) {
if (this.equals(ServerVersion.v_1_16_1)) {
return 736;
}
if(this.equals(ServerVersion.v_1_16_2)) {
if (this.equals(ServerVersion.v_1_16_2)) {
return 737;
}
return -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ public Location getTo() {
return to;
}

public void setCancelled(final boolean val) {
this.cancelled = val;
}

public boolean isCancelled() {
return cancelled;
}

public void setCancelled(final boolean val) {
this.cancelled = val;
}

public void cancel() {
setCancelled(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ public Player getPlayer() {
}

/**
* Get the packet's name (NMS packet class simple name)
* Get the packet's name (NMS packet class simple name).
* Deprecated, it is recommended not to use this for performance reasons.
*/
@Deprecated
public String getPacketName() {
return this.packet.getClass().getSimpleName();
}
Expand All @@ -41,11 +43,13 @@ public Object getNMSPacket() {

/**
* Get the class of the NMS packet object
* Deprecated, as it is useless, rather use getNMSPacket().getClass()
*
* @return packet object class
*/
@Deprecated
public Class<?> getNMSPacketClass() {
return getNMSPacket().getClass();
return packet.getClass();
}

/**
Expand All @@ -54,11 +58,7 @@ public Class<?> getNMSPacketClass() {
* @return packet id
*/
public byte getPacketId() {
Byte id = PacketType.Client.packetIds.get(getNMSPacketClass());
if (id == null) {
return -1;
}
return id;
return PacketType.Client.packetIds.getOrDefault(packet.getClass(), (byte) -1);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public Player getPlayer() {

/**
* Get the packet's name (NMS packet class simple name)
* Deprecated, it is recommended not to use this for performance reasons.
*/
@Deprecated
public String getPacketName() {
return this.packet.getClass().getSimpleName();
}
Expand All @@ -34,12 +36,7 @@ public String getPacketName() {
* @return packet id
*/
public byte getPacketId() {
Byte id =
PacketType.Server.packetIds.get(getNMSPacketClass());
if (id == null) {
return -1;
}
return id;
return PacketType.Server.packetIds.getOrDefault(packet.getClass(), (byte) -1);
}

/**
Expand All @@ -52,12 +49,14 @@ public Object getNMSPacket() {
}

/**
* Get the class of the NMS packet object
* Get the class of the NMS packet object.
* Deprecated because it is useless, rather use getNMSPacket().getClass().
*
* @return packet object class
*/
@Deprecated
public Class<?> getNMSPacketClass() {
return getNMSPacket().getClass();
return packet.getClass();
}

public boolean isCancelled() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@

public final class PlayerUninjectEvent extends PacketEvent {
private final Player player;
private boolean cancelled;
private final boolean sync;
private boolean cancelled;

public PlayerUninjectEvent(final Player player, final boolean sync) {
@Deprecated
public PlayerUninjectEvent(final Player player, @Deprecated final boolean sync) {
this.player = player;
this.sync = sync;
}

public PlayerUninjectEvent(final Player player) {
this.player = player;
this.sync = true;
}

public void cancel() {
setCancelled(true);
}
Expand All @@ -31,7 +37,9 @@ public void setCancelled(final boolean val) {
* By default all uninjections are called aynchronously in this build.
*
* @return If the uninjection was done async or sync
* @deprecated because it is unneeded
*/
@Deprecated
public boolean isSync() {
return sync;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import io.github.retrooper.packetevents.PacketEvents;
import io.github.retrooper.packetevents.annotations.PacketHandler;
import io.github.retrooper.packetevents.annotations.data.EventSynchronization;
import io.github.retrooper.packetevents.event.PacketEvent;
import io.github.retrooper.packetevents.event.PacketListener;
import org.bukkit.Bukkit;
Expand All @@ -12,21 +11,10 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

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

public EventManager(final int threadCount) {
executorService = Executors.newFixedThreadPool(threadCount);
}

public EventManager() {
this(Runtime.getRuntime().availableProcessors());
}

public void callEvent(final PacketEvent e) {
for (final PacketListener listener : registeredMethods.keySet()) {
//Annotated methods
Expand All @@ -46,12 +34,17 @@ public void run() {
}
}
};
if (annotation.synchronization() == EventSynchronization.FORCE_ASYNC) {
executorService.execute(invokeMethod);
} else if (annotation.synchronization() == EventSynchronization.FORCE_SYNC) {
Bukkit.getScheduler().runTask(PacketEvents.getPlugin(), invokeMethod);
} else {
invokeMethod.run();

switch (annotation.synchronization()) {
case FORCE_ASYNC:
Bukkit.getScheduler().runTaskAsynchronously(PacketEvents.getPlugin(), invokeMethod);
break;
case FORCE_SYNC:
Bukkit.getScheduler().runTask(PacketEvents.getPlugin(), invokeMethod);
break;
default:
invokeMethod.run();
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
package io.github.retrooper.packetevents.example;

import io.github.retrooper.packetevents.PacketEvents;
import io.github.retrooper.packetevents.annotations.PacketHandler;
import io.github.retrooper.packetevents.enums.Direction;
import io.github.retrooper.packetevents.enums.minecraft.PlayerDigType;
import io.github.retrooper.packetevents.event.PacketListener;
import io.github.retrooper.packetevents.event.impl.PacketReceiveEvent;
import io.github.retrooper.packetevents.packet.PacketType;
import io.github.retrooper.packetevents.packetwrappers.in.armanimation.WrappedPacketInArmAnimation;
import io.github.retrooper.packetevents.packetwrappers.in.blockdig.WrappedPacketInBlockDig;
import io.github.retrooper.packetevents.packetwrappers.in.custompayload.WrappedPacketInCustomPayload;
import io.github.retrooper.packetevents.packetwrappers.in.helditemslot.WrappedPacketInHeldItemSlot;
import io.github.retrooper.packetevents.packetwrappers.in.settings.WrappedPacketInSettings;
import org.bukkit.plugin.java.JavaPlugin;

public class MainExample extends JavaPlugin implements PacketListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static void injectPlayer(final Player player) {

public static Future<?> uninjectPlayer(final Player player) {
try {
final PlayerUninjectEvent uninjectEvent = new PlayerUninjectEvent(player, false);
final PlayerUninjectEvent uninjectEvent = new PlayerUninjectEvent(player);
PacketEvents.getAPI().getEventManager().callEvent(uninjectEvent);
if (!uninjectEvent.isCancelled()) {
if (v1_7_nettyMode) {
Expand All @@ -51,7 +51,7 @@ public static Future<?> uninjectPlayer(final Player player) {

public static void uninjectPlayerNow(final Player player) {
try {
final PlayerUninjectEvent uninjectEvent = new PlayerUninjectEvent(player, true);
final PlayerUninjectEvent uninjectEvent = new PlayerUninjectEvent(player);
PacketEvents.getAPI().getEventManager().callEvent(uninjectEvent);
if (!uninjectEvent.isCancelled()) {
if (v1_7_nettyMode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

import io.github.retrooper.packetevents.PacketEvents;
import io.github.retrooper.packetevents.utils.NMSUtils;
import net.minecraft.util.io.netty.channel.Channel;
import net.minecraft.util.io.netty.channel.ChannelDuplexHandler;
import net.minecraft.util.io.netty.channel.ChannelHandlerContext;
import net.minecraft.util.io.netty.channel.ChannelPipeline;
import net.minecraft.util.io.netty.channel.ChannelPromise;
import net.minecraft.util.io.netty.channel.*;
import org.bukkit.entity.Player;

import java.util.concurrent.Future;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

import io.github.retrooper.packetevents.PacketEvents;
import io.github.retrooper.packetevents.utils.NMSUtils;
import io.netty.channel.Channel;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelPromise;
import io.netty.channel.*;
import org.bukkit.entity.Player;

import java.util.concurrent.Future;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,7 @@ public class PacketType {


public static class Client {
public static class Util {
public static boolean isInstanceOfFlying(final byte packetId) {
return packetId == FLYING
|| packetId == POSITION
|| packetId == POSITION_LOOK
|| packetId == LOOK;
}
}

public static final Map<Class<?>, Byte> packetIds = new HashMap<Class<?>, Byte>();

public static final byte TELEPORT_ACCEPT = 0,
TILE_NBT_QUERY = 1, DIFFICULTY_CHANGE = 2, CHAT = 3, CLIENT_COMMAND = 4,
SETTINGS = 5, TAB_COMPLETE = 6, TRANSACTION = 7, ENCHANT_ITEM = 8,
Expand Down Expand Up @@ -81,6 +71,15 @@ public static void init() {
packetIds.put(PacketTypeClasses.Client.USE_ITEM, USE_ITEM);
packetIds.put(PacketTypeClasses.Client.BLOCK_PLACE, BLOCK_PLACE);
}

public static class Util {
public static boolean isInstanceOfFlying(final byte packetId) {
return packetId == FLYING
|| packetId == POSITION
|| packetId == POSITION_LOOK
|| packetId == LOOK;
}
}
}

public static class Server {
Expand Down
Loading

0 comments on commit a1d48d3

Please sign in to comment.