Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
66f2c371ec | ||
|
|
c5bcf50d66 | ||
|
|
86625694fc | ||
|
|
dcaae8a15a | ||
|
|
1996399cde | ||
|
|
62d3fdd276 | ||
|
|
7740cf6eae | ||
|
|
5bbaa5d7c2 | ||
|
|
70cb1ad0fb | ||
|
|
fcfc404120 | ||
|
|
2f855874a6 | ||
|
|
26d5542864 | ||
|
|
ee5e0ef3a7 | ||
|
|
51e5cf5c55 | ||
|
|
3c09b8c3a7 | ||
|
|
6e25876230 | ||
|
|
c5a3e57974 | ||
|
|
508fb5f1e2 | ||
|
|
e9b6f5ab0f | ||
|
|
efb79cc7d2 | ||
|
|
901084f1f6 | ||
|
|
acb4ca7619 |
No files matched your search
@@ -1,11 +1,15 @@
|
||||
# packetevents by retrooper
|
||||
|
||||
An asynchronous spigot packetListener coded by retrooper.
|
||||
A lightweight spigot packetlistener & packetsender coded by retrooper.
|
||||
|
||||
Credit to TinyProtocol, PacketEvents uses a modified version of [TinyProtocol](http://github.com/dmulloy2/ProtocolLib/blob/master/TinyProtocol/src/main/java/com/comphenix/tinyprotocol/TinyProtocol.java) by DeprecatedLuke to support 1.7.10 & 1.8.
|
||||
PacketEvents does some extra modifications to support the rest of the versions!
|
||||
All I do is make the wrapper classes and useful utilities that support 1.7.10 -> 1.15.2!
|
||||
This packetlistener is supported on spigot servers(Spigot, PaperSpigot, ...).
|
||||
## Credit:
|
||||
|
||||
PacketEvents uses a modified version of [TinyProtocol](http://github.com/dmulloy2/ProtocolLib/blob/master/TinyProtocol/src/main/java/com/comphenix/tinyprotocol/TinyProtocol.java) by DeprecatedLuke to support 1.7.10 & 1.8.
|
||||
I modified DeprecatedLuek's version of TinyProtocol and added support for 1.9+ versions.
|
||||
|
||||
PacketEvents also took Atlas' Packet class.(all the packet names)
|
||||
|
||||
This packetlistener is **only** supported on spigot servers and works on these spigot forks:(PaperSpigot, Tuinity)
|
||||
|
||||
* No dependencies
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>io.github.retrooper</groupId>
|
||||
<artifactId>packetevents</artifactId>
|
||||
<version>1.2.8</version>
|
||||
<version>1.2.9.2</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>packetevents</name>
|
||||
<properties>
|
||||
|
||||
@@ -1,50 +1,66 @@
|
||||
package io.github.retrooper.packetevents;
|
||||
|
||||
import io.github.retrooper.packetevents.enums.ClientVersion;
|
||||
import io.github.retrooper.packetevents.enums.ServerVersion;
|
||||
import io.github.retrooper.packetevents.event.PacketHandler;
|
||||
import io.github.retrooper.packetevents.event.PacketListener;
|
||||
import io.github.retrooper.packetevents.event.impl.PacketLoginEvent;
|
||||
import io.github.retrooper.packetevents.event.impl.PlayerInjectEvent;
|
||||
import io.github.retrooper.packetevents.event.impl.ServerTickEvent;
|
||||
import io.github.retrooper.packetevents.event.manager.EventManager;
|
||||
import io.github.retrooper.packetevents.injector.PacketHandler;
|
||||
import io.github.retrooper.packetevents.handler.TinyProtocolHandler;
|
||||
import io.github.retrooper.packetevents.packet.Packet;
|
||||
import io.github.retrooper.packetevents.packetwrappers.Sendable;
|
||||
import io.github.retrooper.packetevents.packetwrappers.login.WrappedPacketLoginHandshake;
|
||||
import io.github.retrooper.packetevents.utils.NMSUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
public class PacketEvents{
|
||||
import java.util.HashMap;
|
||||
|
||||
public class PacketEvents implements PacketListener{
|
||||
|
||||
private static boolean hasRegistered = false;
|
||||
|
||||
private static final ServerVersion version = ServerVersion.getVersion();
|
||||
private static PacketEvents instance;
|
||||
public static JavaPlugin plugin;
|
||||
private static PacketHandler packetInjector;
|
||||
private static EventManager eventManager = new EventManager();
|
||||
|
||||
private static int currentTick;
|
||||
|
||||
private static BukkitTask serverTickTask;
|
||||
|
||||
private static final HashMap<Object, ClientVersion> clientVersionLookup = new HashMap<Object, ClientVersion>();
|
||||
|
||||
public static EventManager getEventManager() {
|
||||
return eventManager;
|
||||
}
|
||||
|
||||
|
||||
public static void setup(final JavaPlugin plugin, final boolean serverTickEventEnabled) {
|
||||
PacketEvents.plugin = plugin;
|
||||
packetInjector = new PacketHandler(plugin);
|
||||
packetInjector.initTinyProtocol();
|
||||
public static void start(final JavaPlugin plugin, final boolean serverTickEventEnabled) {
|
||||
if (!hasRegistered) {
|
||||
getEventManager().registerListener(getInstance());
|
||||
hasRegistered = true;
|
||||
}
|
||||
final TinyProtocolHandler packetHandler = new TinyProtocolHandler(plugin);
|
||||
packetHandler.initTinyProtocol();
|
||||
|
||||
if (serverTickEventEnabled) {
|
||||
final Runnable runnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
getEventManager().callEvent(new ServerTickEvent(currentTick++, PacketEvents.currentTimeMS()));
|
||||
getEventManager().callEvent(new ServerTickEvent(currentTick++, PacketEvents.currentCalculatedMS()));
|
||||
}
|
||||
};
|
||||
serverTickTask = Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, runnable, 0L, 1L);
|
||||
}
|
||||
}
|
||||
|
||||
public static void cleanup() {
|
||||
public static void stop() {
|
||||
if (serverTickTask != null) {
|
||||
serverTickTask.cancel();
|
||||
}
|
||||
@@ -69,16 +85,8 @@ public class PacketEvents{
|
||||
double[] tpsArray = new double[0];
|
||||
try {
|
||||
tpsArray = NMSUtils.recentTPS();
|
||||
} catch (IllegalAccessException e) {
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
final int size = tpsArray.length;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (tpsArray[i] > 20.0) {
|
||||
tpsArray[i] = 20.0;
|
||||
}
|
||||
}
|
||||
return tpsArray;
|
||||
}
|
||||
@@ -91,10 +99,64 @@ public class PacketEvents{
|
||||
return NMSUtils.getPlayerPing(player);
|
||||
}
|
||||
|
||||
public static long currentTimeMS() {
|
||||
return System.currentTimeMillis();
|
||||
/**
|
||||
* This function returns nanoTime / 1 million,
|
||||
*
|
||||
* Why use this instead of System.currentTimeMillis()?
|
||||
*
|
||||
* System.currentTimeMillis() isn't supported on all machines,
|
||||
* This function is also more accurate, although it is slower on
|
||||
* Windows machines, it is faster than System.currentTimeMillis() on other Operating Systems!
|
||||
* @return
|
||||
*/
|
||||
public static long currentCalculatedMS() {
|
||||
return System.nanoTime() / 1000000;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the player's clients' version.
|
||||
*
|
||||
* Do not call this method in the PlayerInjectEvent or before the player is injected.
|
||||
* The EntityPlayer object is null at that time, resulting in the version lookup to fail.
|
||||
* @param player
|
||||
* @return
|
||||
*/
|
||||
@Nullable
|
||||
public static ClientVersion getClientVersion(final Player player) {
|
||||
final Object channel = TinyProtocolHandler.getPlayerChannel(player);
|
||||
return clientVersionLookup.get(channel);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static ClientVersion getClientVersion(final Object channel) {
|
||||
return clientVersionLookup.get(channel);
|
||||
}
|
||||
|
||||
@PacketHandler
|
||||
public void onLogin(final PacketLoginEvent e) {
|
||||
if (e.getPacketName().equals(Packet.Login.HANDSHAKE)) {
|
||||
final WrappedPacketLoginHandshake handshake = new WrappedPacketLoginHandshake(e.getPacket());
|
||||
final ClientVersion clientVersion = ClientVersion.fromProtocolVersion(handshake.getProtocolVersion());
|
||||
clientVersionLookup.put(e.getNettyChannel(), clientVersion);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Do not check the client version in or before the PlayerInjectEvent,
|
||||
* it will cause ERRORSS
|
||||
* @param e
|
||||
*/
|
||||
@PacketHandler
|
||||
public void onInject(final PlayerInjectEvent e) {
|
||||
final String username = e.getPlayer().getName();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a wrapped sendable packet to a player
|
||||
* @param player
|
||||
* @param sendable
|
||||
*/
|
||||
public static void sendPacket(final Player player, final Sendable sendable) {
|
||||
NMSUtils.sendSendableWrapper(player, sendable);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package io.github.retrooper.packetevents.enums;
|
||||
|
||||
public enum ClientVersion {
|
||||
LESS_THAN_V_1_7_10, v_1_7_10, v_1_8, v_1_9, v_1_9_1, v_1_9_2, v_1_9_3, v_1_10, v_1_11, v_1_11_1, v_1_12, v_1_12_1, v_1_12_2,
|
||||
v_1_13, v_1_13_1, v_1_13_2, v_1_14, v_1_14_1, v_1_14_2, v_1_14_3, v_1_14_4, v_1_15, v_1_15_1, v_1_15_2, HIGHER_THAN_V_1_15_2, INVALID;
|
||||
|
||||
public static ClientVersion fromProtocolVersion(final int protocolVersion) {
|
||||
int remainder = -1;
|
||||
switch (protocolVersion) {
|
||||
case 1:
|
||||
case 2:
|
||||
case 3:
|
||||
case 4:
|
||||
return LESS_THAN_V_1_7_10;
|
||||
case 47://ranging from 1.8->1.8.9
|
||||
return v_1_8;
|
||||
case 107:
|
||||
return v_1_9;
|
||||
case 108:
|
||||
return v_1_9_1;
|
||||
case 109:
|
||||
return v_1_9_2;
|
||||
case 110:
|
||||
return v_1_9_3; //can be 1.9.3 or 1.9.4
|
||||
case 210:
|
||||
return v_1_10; //could be 1.10,1.10.1, 1.10.2
|
||||
case 315:
|
||||
return v_1_11;
|
||||
case 316: //could be 11.1 or 11.2
|
||||
return v_1_11_1;
|
||||
case 335:
|
||||
return v_1_12;
|
||||
case 338:
|
||||
return v_1_12_1;
|
||||
case 340:
|
||||
return v_1_12_2;
|
||||
case 393:
|
||||
return v_1_13;
|
||||
case 401:
|
||||
return v_1_13_1;
|
||||
case 404:
|
||||
return v_1_13_2;
|
||||
case 477:
|
||||
return v_1_14;
|
||||
case 480:
|
||||
return v_1_14_1;
|
||||
case 485:
|
||||
return v_1_14_2;
|
||||
case 490:
|
||||
return v_1_14_3;
|
||||
case 498:
|
||||
return v_1_14_4;
|
||||
case 573:
|
||||
return v_1_15;
|
||||
case 575:
|
||||
return v_1_15_1;
|
||||
case 578:
|
||||
return v_1_15_2;
|
||||
default:
|
||||
remainder = protocolVersion;
|
||||
}
|
||||
if(remainder > 578) {
|
||||
return HIGHER_THAN_V_1_15_2;
|
||||
}
|
||||
return INVALID;
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ import io.github.retrooper.packetevents.enums.ServerVersion;
|
||||
* recommended to avoid non thread safe bukkit operations
|
||||
*/
|
||||
public class PacketEvent {
|
||||
protected long timestamp = PacketEvents.currentTimeMS();
|
||||
protected long timestamp = PacketEvents.currentCalculatedMS();
|
||||
protected static ServerVersion version = PacketEvents.getServerVersion();
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,18 +3,24 @@ package io.github.retrooper.packetevents.event.impl;
|
||||
import io.github.retrooper.packetevents.event.PacketEvent;
|
||||
|
||||
public class PacketLoginEvent extends PacketEvent {
|
||||
private final Object channel;
|
||||
private final String name;
|
||||
private final Object packet;
|
||||
public PacketLoginEvent(final String packetName, final Object packet) {
|
||||
public PacketLoginEvent(final Object channel, final String packetName, final Object packet) {
|
||||
this.channel =channel;
|
||||
this.name = packetName;
|
||||
this.packet = packet;
|
||||
}
|
||||
|
||||
public String getPacketName() {
|
||||
public final Object getNettyChannel() {
|
||||
return channel;
|
||||
}
|
||||
|
||||
public final String getPacketName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Object getPacket() {
|
||||
public final Object getPacket() {
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
@@ -1,21 +1,17 @@
|
||||
package io.github.retrooper.packetevents.event.impl;
|
||||
|
||||
import io.github.retrooper.packetevents.PacketEvents;
|
||||
import io.github.retrooper.packetevents.event.PacketEvent;
|
||||
|
||||
//unfinished
|
||||
public class ServerReloadEvent extends PacketEvent {
|
||||
private final long currentTime;
|
||||
private boolean cancelled;
|
||||
|
||||
public ServerReloadEvent(final long currentTime){
|
||||
this.currentTime = currentTime;
|
||||
public void setCancelled(final boolean val) {
|
||||
this.cancelled = val;
|
||||
}
|
||||
|
||||
public ServerReloadEvent() {
|
||||
this.currentTime = PacketEvents.currentTimeMS();
|
||||
public boolean isCancelled() {
|
||||
return cancelled;
|
||||
}
|
||||
|
||||
public final long getCurrentTime() {
|
||||
return currentTime;
|
||||
}
|
||||
}
|
||||
@@ -50,4 +50,7 @@ public class EventManager {
|
||||
registeredMethods.clear();
|
||||
}
|
||||
|
||||
public boolean hasRegistered(final PacketListener listener) {
|
||||
return registeredMethods.containsKey(listener);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package io.github.retrooper.packetevents.example;
|
||||
|
||||
import io.github.retrooper.packetevents.PacketEvents;
|
||||
import io.github.retrooper.packetevents.enums.ServerVersion;
|
||||
import io.github.retrooper.packetevents.packet.Packet;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class MainExample extends JavaPlugin {
|
||||
@@ -16,7 +18,7 @@ public class MainExample extends JavaPlugin {
|
||||
* The first argument is an instance to your Main Class,
|
||||
* The second argument is a boolean whether the servertickevent should ever get called
|
||||
*/
|
||||
PacketEvents.setup(this, serverTickEventEnabled);
|
||||
PacketEvents.start(this, serverTickEventEnabled);
|
||||
|
||||
|
||||
/**
|
||||
@@ -30,7 +32,7 @@ public class MainExample extends JavaPlugin {
|
||||
/**
|
||||
* Stops server tick event task if enabled and unregister all listeners
|
||||
*/
|
||||
PacketEvents.cleanup();
|
||||
PacketEvents.stop();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.github.retrooper.packetevents.example;
|
||||
|
||||
import io.github.retrooper.packetevents.PacketEvents;
|
||||
import io.github.retrooper.packetevents.enums.*;
|
||||
import io.github.retrooper.packetevents.event.PacketEvent;
|
||||
import io.github.retrooper.packetevents.event.PacketHandler;
|
||||
@@ -14,6 +15,7 @@ import io.github.retrooper.packetevents.packetwrappers.in.entityaction.WrappedPa
|
||||
import io.github.retrooper.packetevents.packetwrappers.in.flying.WrappedPacketInFlying;
|
||||
import io.github.retrooper.packetevents.packetwrappers.in.keepalive.WrappedPacketInKeepAlive;
|
||||
import io.github.retrooper.packetevents.packetwrappers.in.useentity.WrappedPacketInUseEntity;
|
||||
import io.github.retrooper.packetevents.packetwrappers.login.WrappedPacketLoginHandshake;
|
||||
import io.github.retrooper.packetevents.packetwrappers.out.chat.WrappedPacketOutChat;
|
||||
import io.github.retrooper.packetevents.packetwrappers.out.entityvelocity.WrappedPacketOutEntityVelocity;
|
||||
import io.github.retrooper.packetevents.utils.vector.Vector3i;
|
||||
@@ -26,8 +28,6 @@ import org.bukkit.inventory.ItemStack;
|
||||
* Implementing PacketListener to listen to packets and packetevents' custom events,
|
||||
*/
|
||||
public class RegisteredListener implements PacketListener {
|
||||
|
||||
|
||||
/**
|
||||
* Listen to PacketReceiveEvent
|
||||
*
|
||||
@@ -37,6 +37,15 @@ public class RegisteredListener implements PacketListener {
|
||||
public void onPacketReceive(PacketReceiveEvent e) {
|
||||
final Player p = e.getPlayer();
|
||||
final long timestamp = e.getTimestamp();
|
||||
|
||||
//The block place name field is not set at compilation time, so we cannot use it in the switch statement
|
||||
//On 1.7->1.8.8 servers it is "PacketPlayInBlockPlace" and on 1.9 servers it is "PacketPlayInUseItem"
|
||||
if(e.getPacketName().equals(Packet.Client.BLOCK_PLACE)) {
|
||||
final WrappedPacketInBlockPlace blockPlace = new WrappedPacketInBlockPlace(e.getPlayer(), e.getPacket());
|
||||
final Vector3i blockPosition = blockPlace.getBlockPosition();
|
||||
final ItemStack stack = blockPlace.getItemStack();
|
||||
e.getPlayer().sendMessage("pos: " + blockPosition.toString() + ", type: " + stack.getType());
|
||||
}
|
||||
switch (e.getPacketName()) {
|
||||
case Packet.Client.USE_ENTITY:
|
||||
final WrappedPacketInUseEntity useEntity = new WrappedPacketInUseEntity(e.getPacket());
|
||||
@@ -52,7 +61,7 @@ public class RegisteredListener implements PacketListener {
|
||||
final boolean canInstantlyBuild = abilities.canInstantlyBuild();
|
||||
final float flySpeed = abilities.getFlySpeed();
|
||||
final float walkSpeed = abilities.getWalkSpeed();
|
||||
// e.getPlayer().sendMessage("walkspeed: " + walkSpeed + ", flyspeed: " + flySpeed + ", canFly: " + canFly);
|
||||
e.getPlayer().sendMessage("walkspeed: " + walkSpeed + ", flyspeed: " + flySpeed + ", canFly: " + canFly);
|
||||
break;
|
||||
case Packet.Client.BLOCK_DIG:
|
||||
//Custom PlayerDigType enum
|
||||
@@ -69,6 +78,7 @@ public class RegisteredListener implements PacketListener {
|
||||
final WrappedPacketInFlying.WrappedPacketInPosition position =
|
||||
new WrappedPacketInFlying.WrappedPacketInPosition(e.getPacket());
|
||||
final boolean isPos = position.isPosition(); //true
|
||||
p.sendMessage("gaaang pos");
|
||||
break;
|
||||
case Packet.Client.POSITION_LOOK:
|
||||
final WrappedPacketInFlying.WrappedPacketInPosition_Look position_look =
|
||||
@@ -78,20 +88,6 @@ public class RegisteredListener implements PacketListener {
|
||||
case Packet.Client.CHAT:
|
||||
final WrappedPacketInChat chat = new WrappedPacketInChat(e.getPacket());
|
||||
final String message = chat.getMessage();
|
||||
if(message.contains("jump")) {
|
||||
//teleport them up in the air
|
||||
// PacketEvents.sendPacket(e.getPlayer(), new WrappedPacketOutEntityVelocity(e.getPlayer(), 0, 3, 0));
|
||||
}
|
||||
else if(message.contains("swing arm")) {
|
||||
//Swing the player's arm
|
||||
// PacketEvents.sendPacket(e.getPlayer(), new WrappedPacketOutAnimation(e.getPlayer(), EntityAnimationType.SWING_MAIN_ARM));
|
||||
}
|
||||
break;
|
||||
case Packet.Client.BLOCK_PLACE:
|
||||
final WrappedPacketInBlockPlace blockPlace = new WrappedPacketInBlockPlace(e.getPlayer(), e.getPacket());
|
||||
final Vector3i blockPosition = blockPlace.getBlockPosition();
|
||||
final Hand h = blockPlace.getHand();
|
||||
final ItemStack stack = blockPlace.getItemStack();
|
||||
break;
|
||||
case Packet.Client.KEEP_ALIVE:
|
||||
final WrappedPacketInKeepAlive keepAlive = new WrappedPacketInKeepAlive(e.getPacket());
|
||||
@@ -111,6 +107,10 @@ public class RegisteredListener implements PacketListener {
|
||||
* I recommend you use the player inject event
|
||||
* to register your players instead of any other bukkit event,
|
||||
* to avoid any issues.
|
||||
*
|
||||
* DO NOT CHECK YOUR CLIENT VERSIONS HERE,
|
||||
* USE PLAYER JOIN EVENT TO CHECK A CLIENT'S VERSION!
|
||||
*
|
||||
* @param event
|
||||
*/
|
||||
@PacketHandler
|
||||
@@ -121,14 +121,15 @@ public class RegisteredListener implements PacketListener {
|
||||
/**
|
||||
* I recommend you use the player uninject event,
|
||||
* to unregister your players
|
||||
*
|
||||
* <p>
|
||||
* You can also call this event to "whitelist" a player,
|
||||
* our API won't even notice the player exists, so you won't
|
||||
* be able to listen to any packets from the player
|
||||
* @param e
|
||||
*
|
||||
* @param event
|
||||
*/
|
||||
@PacketHandler
|
||||
public void onUninject(PlayerUninjectEvent e) {
|
||||
public void onUninject(PlayerUninjectEvent event) {
|
||||
|
||||
}
|
||||
|
||||
@@ -144,15 +145,12 @@ public class RegisteredListener implements PacketListener {
|
||||
final Player targetPlayer = p.getPlayer();
|
||||
final Object rawNMSPacket = p.getPacket();
|
||||
final long timestamp = p.getTimestamp();
|
||||
if(p.getPacketName().equals(Packet.Server.ENTITY_VELOCITY)) {
|
||||
if (p.getPacketName().equals(Packet.Server.ENTITY_VELOCITY)) {
|
||||
final WrappedPacketOutEntityVelocity vel = new WrappedPacketOutEntityVelocity(p.getPacket());
|
||||
final double velocityX = vel.getVelocityX();
|
||||
final double velocityY = vel.getVelocityY();
|
||||
final double velocityZ = vel.getVelocityZ();
|
||||
|
||||
|
||||
}
|
||||
else if(p.getPacketName().equals(Packet.Server.CHAT)) {
|
||||
} else if (p.getPacketName().equals(Packet.Server.CHAT)) {
|
||||
final WrappedPacketOutChat chat = new WrappedPacketOutChat(p.getPacket());
|
||||
}
|
||||
} else if (e instanceof CustomMoveEvent) {
|
||||
@@ -163,7 +161,16 @@ public class RegisteredListener implements PacketListener {
|
||||
} else if (e instanceof ServerTickEvent) {
|
||||
final ServerTickEvent event = (ServerTickEvent) e;
|
||||
final int currentTick = event.getCurrentTick();
|
||||
// System.out.println(PacketEvents.getCurrentServerTPS() + " is tps");
|
||||
final double tps = PacketEvents.getCurrentServerTPS();
|
||||
}
|
||||
else if(e instanceof PacketLoginEvent) {
|
||||
final PacketLoginEvent event = (PacketLoginEvent)e;
|
||||
if(event.getPacketName().equals(Packet.Login.HANDSHAKE)) {
|
||||
final WrappedPacketLoginHandshake handshake = new WrappedPacketLoginHandshake(event.getPacket());
|
||||
final int protocolVersion = handshake.getProtocolVersion();
|
||||
final String hostname = handshake.getHostname();
|
||||
final int port = handshake.getPort();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package io.github.retrooper.packetevents.handler;
|
||||
|
||||
|
||||
import io.github.retrooper.packetevents.PacketEvents;
|
||||
import io.github.retrooper.packetevents.enums.ServerVersion;
|
||||
import io.github.retrooper.packetevents.tinyprotocol.Reflection;
|
||||
import io.github.retrooper.packetevents.tinyprotocol.Reflection.FieldAccessor;
|
||||
import io.github.retrooper.packetevents.utils.NMSUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
public class TinyProtocolHandler {
|
||||
private static final ServerVersion version = PacketEvents.getServerVersion();
|
||||
private final Plugin plugin;
|
||||
|
||||
public TinyProtocolHandler(final Plugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
public void initTinyProtocol() {
|
||||
if(version.isLowerThan(ServerVersion.v_1_8)) {
|
||||
new TinyProtocolHandler_1_7(getPlugin()).initTinyProtocol();
|
||||
}
|
||||
else {
|
||||
new TinyProtocolHandler_1_8(getPlugin()).initTinyProtocol();
|
||||
}
|
||||
}
|
||||
|
||||
public static Object getPlayerChannel(final Player player) {
|
||||
final Object entityPlayer = NMSUtils.getEntityPlayer(player);
|
||||
final Object playerConnection = playerConnectionAccessor.get(entityPlayer);
|
||||
final Object networkManager = networkManagerAccessor.get(playerConnection);
|
||||
return nettyChannelAccessor.get(networkManager);
|
||||
}
|
||||
|
||||
public final Plugin getPlugin() {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
private static Class<?> networkManagerClass;
|
||||
|
||||
private static final FieldAccessor<?> playerConnectionAccessor, networkManagerAccessor, nettyChannelAccessor;
|
||||
|
||||
private static Class<?> channelClass;
|
||||
|
||||
static {
|
||||
try {
|
||||
networkManagerClass = NMSUtils.getNMSClass("NetworkManager");
|
||||
channelClass = NMSUtils.getNettyClass("channel.Channel");
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
playerConnectionAccessor = Reflection.getField(NMSUtils.entityPlayerClass, NMSUtils.playerConnectionClass, 0);
|
||||
networkManagerAccessor = Reflection.getField(NMSUtils.playerConnectionClass, networkManagerClass, 0);
|
||||
nettyChannelAccessor = Reflection.getField(networkManagerClass, channelClass, 0);
|
||||
}
|
||||
}
|
||||
+11
-3
@@ -1,17 +1,19 @@
|
||||
package io.github.retrooper.packetevents.injector;
|
||||
package io.github.retrooper.packetevents.handler;
|
||||
|
||||
import io.github.retrooper.packetevents.PacketEvents;
|
||||
import io.github.retrooper.packetevents.event.impl.PacketLoginEvent;
|
||||
import io.github.retrooper.packetevents.event.impl.PacketReceiveEvent;
|
||||
import io.github.retrooper.packetevents.event.impl.PacketSendEvent;
|
||||
import io.github.retrooper.packetevents.packet.Packet;
|
||||
import io.github.retrooper.packetevents.tinyprotocol.TinyProtocol7;
|
||||
import net.minecraft.util.io.netty.channel.Channel;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
public class PacketHandler_1_7 {
|
||||
public class TinyProtocolHandler_1_7 {
|
||||
private final Plugin plugin;
|
||||
|
||||
public PacketHandler_1_7(final Plugin plugin) {
|
||||
public TinyProtocolHandler_1_7(final Plugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@@ -31,6 +33,12 @@ public class PacketHandler_1_7 {
|
||||
@Override
|
||||
public Object onPacketInAsync(Player sender, Channel channel, Object packet) {
|
||||
final String packetName = packet.getClass().getSimpleName();
|
||||
for(final String loginPacket : Packet.Login.LOGIN_PACKETS) {
|
||||
if(packetName.equals(loginPacket)) {
|
||||
PacketEvents.getEventManager().callEvent(new PacketLoginEvent(channel, packetName, packet));
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
final PacketReceiveEvent packetReceiveEvent = new PacketReceiveEvent(sender, packetName, packet);
|
||||
PacketEvents.getEventManager().callEvent(packetReceiveEvent);
|
||||
if (!packetReceiveEvent.isCancelled()) {
|
||||
+23
-7
@@ -1,22 +1,32 @@
|
||||
package io.github.retrooper.packetevents.injector;
|
||||
package io.github.retrooper.packetevents.handler;
|
||||
|
||||
import io.github.retrooper.packetevents.PacketEvents;
|
||||
import io.github.retrooper.packetevents.enums.ServerVersion;
|
||||
import io.github.retrooper.packetevents.event.impl.PacketLoginEvent;
|
||||
import io.github.retrooper.packetevents.event.impl.PacketReceiveEvent;
|
||||
import io.github.retrooper.packetevents.event.impl.PacketSendEvent;
|
||||
import io.github.retrooper.packetevents.packet.Packet;
|
||||
import io.github.retrooper.packetevents.tinyprotocol.TinyProtocol8;
|
||||
import io.netty.channel.Channel;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
public class PacketHandler_1_8 {
|
||||
public class TinyProtocolHandler_1_8 {
|
||||
private static final ServerVersion version = PacketEvents.getServerVersion();
|
||||
private final Plugin plugin;
|
||||
|
||||
public PacketHandler_1_8(final Plugin plugin) {
|
||||
public TinyProtocolHandler_1_8(final Plugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Login wrappers TODO
|
||||
* PacketLoginInStart
|
||||
* PacketLoginInEncryptionBegin
|
||||
*/
|
||||
|
||||
|
||||
public void initTinyProtocol() {
|
||||
new TinyProtocol8(getPlugin()) {
|
||||
@Override
|
||||
@@ -33,6 +43,12 @@ public class PacketHandler_1_8 {
|
||||
@Override
|
||||
public Object onPacketInAsync(Player sender, Channel channel, Object packet) {
|
||||
final String packetName = packet.getClass().getSimpleName();
|
||||
for (final String loginPacket : Packet.Login.LOGIN_PACKETS) {
|
||||
if (packetName.equals(loginPacket)) {
|
||||
PacketEvents.getEventManager().callEvent(new PacketLoginEvent(channel, packetName, packet));
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
final PacketReceiveEvent packetReceiveEvent = new PacketReceiveEvent(sender, packetName, packet);
|
||||
PacketEvents.getEventManager().callEvent(packetReceiveEvent);
|
||||
if (!packetReceiveEvent.isCancelled()) {
|
||||
@@ -46,19 +62,19 @@ public class PacketHandler_1_8 {
|
||||
|
||||
public static String getNetworkManagersFieldName() {
|
||||
//1.8
|
||||
if(version.equals(ServerVersion.v_1_8)) {
|
||||
if (version.equals(ServerVersion.v_1_8)) {
|
||||
return "g";
|
||||
}
|
||||
//1.8.3->1.12
|
||||
else if(version.isLowerThan(ServerVersion.v_1_13)) {
|
||||
else if (version.isLowerThan(ServerVersion.v_1_13)) {
|
||||
return "h";
|
||||
}
|
||||
//1.13->1.14
|
||||
else if(version.isLowerThan(ServerVersion.v_1_15)) {
|
||||
else if (version.isLowerThan(ServerVersion.v_1_15)) {
|
||||
return "g";
|
||||
}
|
||||
//1.15
|
||||
else {
|
||||
else {
|
||||
return "listeningChannels";
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package io.github.retrooper.packetevents.injector;
|
||||
|
||||
|
||||
import io.github.retrooper.packetevents.PacketEvents;
|
||||
import io.github.retrooper.packetevents.enums.ServerVersion;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
public class PacketHandler {
|
||||
private static final ServerVersion version = PacketEvents.getServerVersion();
|
||||
private final Plugin plugin;
|
||||
|
||||
public PacketHandler(final Plugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
public void initTinyProtocol() {
|
||||
if(version.isLowerThan(ServerVersion.v_1_8)) {
|
||||
new PacketHandler_1_7(getPlugin()).initTinyProtocol();
|
||||
}
|
||||
else {
|
||||
new PacketHandler_1_8(getPlugin()).initTinyProtocol();
|
||||
}
|
||||
}
|
||||
|
||||
public final Plugin getPlugin() {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
package io.github.retrooper.packetevents.packet;
|
||||
|
||||
|
||||
import io.github.retrooper.packetevents.PacketEvents;
|
||||
import io.github.retrooper.packetevents.enums.ServerVersion;
|
||||
|
||||
public abstract class Packet {
|
||||
public static class Client {
|
||||
private static final String c = "PacketPlayIn";
|
||||
@@ -23,7 +26,10 @@ public abstract class Packet {
|
||||
public static final String STEER_VEHICLE = c + "SteerVehicle";
|
||||
public static final String CUSTOM_PAYLOAD = c + "CustomPayload";
|
||||
public static final String ARM_ANIMATION = c + "ArmAnimation";
|
||||
public static final String BLOCK_PLACE = c + "BlockPlace";
|
||||
/**
|
||||
* The packet name of the block place depending on your server version, "PacketPlayInBlockPlace" or "PacketPlayInUseItem"
|
||||
*/
|
||||
public static final String BLOCK_PLACE = c + ((ServerVersion.getVersion().isHigherThan(ServerVersion.v_1_8_8)) ? "UseItem" : "BlockPlace");;
|
||||
public static final String ABILITIES = c + "Abilities";
|
||||
public static final String HELD_ITEM_SLOT = c + "HeldItemSlot";
|
||||
public static final String CLOSE_WINDOW = c + "CloseWindow";
|
||||
@@ -68,6 +74,8 @@ public abstract class Packet {
|
||||
public static final String PING = "PacketStatusInPing";
|
||||
public static final String START = "PacketStatusInStart";
|
||||
public static final String SUCCESS = "PacketLoginOutSuccess";
|
||||
|
||||
public static final String[] LOGIN_PACKETS = new String[] {"PacketHandshakingInSetProtocol","PacketLoginInStart", "PacketLoginInEncryptionBegin"};
|
||||
}
|
||||
|
||||
public static boolean isInstanceOf(String fatherPacket, String childPacket) {
|
||||
|
||||
+1
-14
@@ -4,14 +4,9 @@ import io.github.retrooper.packetevents.enums.ServerVersion;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class WrappedPacket {
|
||||
private final Player player;
|
||||
protected final Player player;
|
||||
protected Object packet;
|
||||
protected static ServerVersion version = ServerVersion.getVersion();
|
||||
private static final String err = "Your Version is unsupported, " +
|
||||
"please contact explored " +
|
||||
"through his discord server (http://discord.gg/2uZY5A4) " +
|
||||
"and tell him what version your server is running on! " +
|
||||
"Make sure you are using spigot!";
|
||||
|
||||
public WrappedPacket(final Object packet) {
|
||||
this.player = null;
|
||||
@@ -39,12 +34,4 @@ public class WrappedPacket {
|
||||
|
||||
}
|
||||
|
||||
protected void throwUnsupportedVersion() {
|
||||
throw new IllegalStateException(err);
|
||||
}
|
||||
|
||||
public final Player getPlayer() {
|
||||
return player;
|
||||
}
|
||||
|
||||
}
|
||||
+8
-14
@@ -4,7 +4,7 @@ import io.github.retrooper.packetevents.enums.Direction;
|
||||
import io.github.retrooper.packetevents.enums.PlayerDigType;
|
||||
import io.github.retrooper.packetevents.enums.ServerVersion;
|
||||
import io.github.retrooper.packetevents.packetwrappers.api.WrappedPacket;
|
||||
import io.github.retrooper.packetevents.utils.BaseBlockUtils;
|
||||
import io.github.retrooper.packetevents.tinyprotocol.Reflection;
|
||||
import io.github.retrooper.packetevents.utils.NMSUtils;
|
||||
import io.github.retrooper.packetevents.utils.vector.Vector3i;
|
||||
|
||||
@@ -41,9 +41,9 @@ public class WrappedPacketInBlockDig extends WrappedPacket {
|
||||
final Object enumDirectionObj = fields[1].get(packet);
|
||||
final Object digTypeObj = fields[2].get(packet);
|
||||
|
||||
final int x = blockPosXYZ[0].getInt(blockPosObj);
|
||||
final int y = blockPosXYZ[1].getInt(blockPosObj);
|
||||
final int z = blockPosXYZ[2].getInt(blockPosObj);
|
||||
final int x = blockPosXYZ[0].get(blockPosObj);
|
||||
final int y = blockPosXYZ[1].get(blockPosObj);
|
||||
final int z = blockPosXYZ[2].get(blockPosObj);
|
||||
|
||||
this.blockPosition = new Vector3i(x, y, z);
|
||||
|
||||
@@ -79,7 +79,7 @@ public class WrappedPacketInBlockDig extends WrappedPacket {
|
||||
|
||||
private static Class<?> blockPositionClass;
|
||||
|
||||
private static Field[] blockPosXYZ = new Field[3];
|
||||
private static Reflection.FieldAccessor<Integer>[] blockPosXYZ = new Reflection.FieldAccessor[3];
|
||||
|
||||
//1.7
|
||||
private static Field face;
|
||||
@@ -107,9 +107,9 @@ public class WrappedPacketInBlockDig extends WrappedPacket {
|
||||
fields[2] = blockDigClass.getDeclaredField("c");
|
||||
|
||||
if (version != ServerVersion.v_1_7_10) {
|
||||
blockPosXYZ[0] = blockPositionClass.getSuperclass().getDeclaredField(BaseBlockUtils.getPosXFieldName());
|
||||
blockPosXYZ[1] = blockPositionClass.getSuperclass().getDeclaredField(BaseBlockUtils.getPosXFieldName());
|
||||
blockPosXYZ[2] = blockPositionClass.getSuperclass().getDeclaredField(BaseBlockUtils.getPosZFieldName());
|
||||
blockPosXYZ[0] = Reflection.getField(blockPositionClass.getSuperclass(), int.class, 0);
|
||||
blockPosXYZ[1] = Reflection.getField(blockPositionClass.getSuperclass(), int.class, 1);
|
||||
blockPosXYZ[2] = Reflection.getField(blockPositionClass.getSuperclass(), int.class, 2);
|
||||
} else {
|
||||
face = blockDigClass.getDeclaredField("face");
|
||||
e = blockDigClass.getDeclaredField("e");
|
||||
@@ -124,12 +124,6 @@ public class WrappedPacketInBlockDig extends WrappedPacket {
|
||||
}
|
||||
}
|
||||
|
||||
for(Field f : blockPosXYZ) {
|
||||
if(f != null) {
|
||||
f.setAccessible(true);
|
||||
}
|
||||
}
|
||||
|
||||
if(e != null) {
|
||||
e.setAccessible(true);
|
||||
}
|
||||
|
||||
+7
-16
@@ -1,6 +1,5 @@
|
||||
package io.github.retrooper.packetevents.packetwrappers.in.blockplace;
|
||||
|
||||
import io.github.retrooper.packetevents.enums.Hand;
|
||||
import io.github.retrooper.packetevents.enums.ServerVersion;
|
||||
import io.github.retrooper.packetevents.packetwrappers.api.WrappedPacket;
|
||||
import io.github.retrooper.packetevents.utils.vector.Vector3i;
|
||||
@@ -11,7 +10,6 @@ import org.bukkit.inventory.ItemStack;
|
||||
|
||||
public class WrappedPacketInBlockPlace extends WrappedPacket {
|
||||
private Vector3i blockPosition;
|
||||
private Hand hand;
|
||||
private ItemStack itemStack;
|
||||
|
||||
public WrappedPacketInBlockPlace(final Player player, final Object packet) {
|
||||
@@ -23,47 +21,40 @@ public class WrappedPacketInBlockPlace extends WrappedPacket {
|
||||
//1.7.10
|
||||
Vector3i position = null;
|
||||
ItemStack itemStack = null;
|
||||
Hand hand = null;
|
||||
try {
|
||||
if (version.isHigherThan(ServerVersion.v_1_8_8)) {
|
||||
final WrappedPacketInBlockPlace_1_9 updatedBlockPlace = new WrappedPacketInBlockPlace_1_9(getPlayer(), packet);
|
||||
final Block block = updatedBlockPlace.getBlock();
|
||||
position = new Vector3i(block.getX(), block.getY(), block.getZ());
|
||||
hand = updatedBlockPlace.getHand();
|
||||
itemStack = new ItemStack(block.getType());
|
||||
// init direction
|
||||
} else {
|
||||
final WrappedPacketInBlockPlace_Legacy legacyBlockPlace = new WrappedPacketInBlockPlace_Legacy(packet);
|
||||
final WrappedPacketInBlockPlace_1_8 legacyBlockPlace = new WrappedPacketInBlockPlace_1_8(packet);
|
||||
position = legacyBlockPlace.getBlockPosition();
|
||||
hand = legacyBlockPlace.getHand();
|
||||
itemStack = legacyBlockPlace.getItemStack();
|
||||
//init direction
|
||||
} }
|
||||
catch(IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
this.blockPosition = position;
|
||||
this.itemStack = itemStack;
|
||||
this.hand = hand;
|
||||
}
|
||||
public Player getPlayer() {
|
||||
return this.player;
|
||||
}
|
||||
|
||||
public Vector3i getBlockPosition() {
|
||||
return blockPosition;
|
||||
}
|
||||
|
||||
public Hand getHand() {
|
||||
return hand;
|
||||
}
|
||||
|
||||
public ItemStack getItemStack() {
|
||||
return itemStack;
|
||||
}
|
||||
|
||||
static {
|
||||
if (version.isHigherThan(ServerVersion.v_1_8_8)) {
|
||||
WrappedPacketInBlockPlace_1_9.setupStaticVars();
|
||||
} else {
|
||||
WrappedPacketInBlockPlace_Legacy.setupStaticVars();
|
||||
WrappedPacketInBlockPlace_1_9.initStatic();
|
||||
} else if(version.isLowerThan(ServerVersion.v_1_9) && version.isHigherThan(ServerVersion.v_1_7_10)){
|
||||
WrappedPacketInBlockPlace_1_8.initStatic();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package io.github.retrooper.packetevents.packetwrappers.in.blockplace;
|
||||
|
||||
import io.github.retrooper.packetevents.packetwrappers.api.WrappedPacket;
|
||||
import io.github.retrooper.packetevents.utils.vector.Vector3i;
|
||||
import net.minecraft.server.v1_7_R4.PacketPlayInBlockPlace;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
class WrappedPacketInBlockPlace_1_7_10 extends WrappedPacket {
|
||||
private Vector3i blockPosition;
|
||||
private ItemStack itemStack;
|
||||
private int blockFace;
|
||||
WrappedPacketInBlockPlace_1_7_10(final Player player, final Object packet) {
|
||||
super(player, packet);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void setup() {
|
||||
final PacketPlayInBlockPlace blockPlace = (PacketPlayInBlockPlace)packet;
|
||||
|
||||
this.blockPosition = new Vector3i(blockPlace.c(), blockPlace.d(), blockPlace.e());
|
||||
|
||||
this.blockFace = blockPlace.d();
|
||||
|
||||
net.minecraft.server.v1_7_R4.ItemStack stack = blockPlace.getItemStack();
|
||||
this.itemStack = org.bukkit.craftbukkit.v1_7_R4.inventory.CraftItemStack.asBukkitCopy(stack);
|
||||
}
|
||||
|
||||
public Vector3i getBlockPosition() {
|
||||
return blockPosition;
|
||||
}
|
||||
|
||||
public ItemStack getItemStack() {
|
||||
return itemStack;
|
||||
}
|
||||
|
||||
public int getBlockFace() {
|
||||
return blockFace;
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package io.github.retrooper.packetevents.packetwrappers.in.blockplace;
|
||||
|
||||
import io.github.retrooper.packetevents.packetwrappers.api.WrappedPacket;
|
||||
import io.github.retrooper.packetevents.tinyprotocol.Reflection;
|
||||
import io.github.retrooper.packetevents.utils.NMSUtils;
|
||||
import io.github.retrooper.packetevents.utils.vector.Vector3i;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
class WrappedPacketInBlockPlace_1_8 extends WrappedPacket {
|
||||
private Vector3i blockPosition;
|
||||
private ItemStack itemStack;
|
||||
WrappedPacketInBlockPlace_1_8(final Object packet) {
|
||||
super(packet);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setup() {
|
||||
Object nmsBlockPos = blockPositionAccessor.get(packet);
|
||||
|
||||
int x = (int) blockPositionXYZInvoker[0].invoke(nmsBlockPos);
|
||||
int y = (int) blockPositionXYZInvoker[1].invoke(nmsBlockPos);
|
||||
int z = (int) blockPositionXYZInvoker[2].invoke(nmsBlockPos);
|
||||
|
||||
this.blockPosition = new Vector3i(x, y, z);
|
||||
|
||||
this.itemStack = NMSUtils.toBukkitItemStack(itemStackFieldAccessor.get(packet));
|
||||
}
|
||||
|
||||
public Vector3i getBlockPosition() {
|
||||
return blockPosition;
|
||||
}
|
||||
|
||||
public ItemStack getItemStack() {
|
||||
return itemStack;
|
||||
}
|
||||
|
||||
private static Class<?> blockPlaceClass, blockPositionClass;
|
||||
|
||||
private static Reflection.FieldAccessor<?> blockPositionAccessor, itemStackFieldAccessor;
|
||||
private static final Reflection.MethodInvoker[] blockPositionXYZInvoker = new Reflection.MethodInvoker[3];
|
||||
|
||||
protected static void initStatic(){
|
||||
try {
|
||||
blockPlaceClass = NMSUtils.getNMSClass("PacketPlayInBlockPlace");
|
||||
|
||||
blockPositionClass = NMSUtils.getNMSClass("BlockPosition");
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
itemStackFieldAccessor = Reflection.getField(blockPlaceClass, NMSUtils.nmsItemStackClass, 0);
|
||||
|
||||
blockPositionAccessor = Reflection.getField(blockPlaceClass, blockPositionClass, 1);
|
||||
|
||||
blockPositionXYZInvoker[0] = Reflection.getMethod(blockPositionClass.getSuperclass(), "getX");
|
||||
blockPositionXYZInvoker[1] = Reflection.getMethod(blockPositionClass.getSuperclass(), "getY");
|
||||
blockPositionXYZInvoker[2] = Reflection.getMethod(blockPositionClass.getSuperclass(), "getZ");
|
||||
}
|
||||
}
|
||||
+6
-31
@@ -1,17 +1,13 @@
|
||||
package io.github.retrooper.packetevents.packetwrappers.in.blockplace;
|
||||
|
||||
import io.github.retrooper.packetevents.enums.Hand;
|
||||
|
||||
import io.github.retrooper.packetevents.packetwrappers.api.WrappedPacket;
|
||||
import io.github.retrooper.packetevents.utils.BlockFinder;
|
||||
import io.github.retrooper.packetevents.utils.NMSUtils;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
class WrappedPacketInBlockPlace_1_9 extends WrappedPacket {
|
||||
private Block block;
|
||||
private Hand hand;
|
||||
|
||||
public WrappedPacketInBlockPlace_1_9(final Player player, final Object packet) {
|
||||
super(player, packet);
|
||||
@@ -19,19 +15,12 @@ class WrappedPacketInBlockPlace_1_9 extends WrappedPacket {
|
||||
|
||||
@Override
|
||||
protected void setup() {
|
||||
try {
|
||||
this.block = BlockFinder.getBlocksInDirection(getPlayer(), 10);
|
||||
this.block = player.getTargetBlock(null, 3);
|
||||
|
||||
final Object enumHandObj = enumHandField.get(packet);
|
||||
|
||||
this.hand = Hand.valueOf(enumHandObj.toString());
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public Hand getHand() {
|
||||
return hand;
|
||||
public Player getPlayer() {
|
||||
return this.player;
|
||||
}
|
||||
|
||||
public Block getBlock() {
|
||||
@@ -39,26 +28,12 @@ class WrappedPacketInBlockPlace_1_9 extends WrappedPacket {
|
||||
}
|
||||
|
||||
private static Class<?> blockPlaceClass_1_9;
|
||||
|
||||
private static Field enumHandField;
|
||||
|
||||
|
||||
protected static void setupStaticVars() {
|
||||
protected static void initStatic() {
|
||||
try {
|
||||
blockPlaceClass_1_9 = NMSUtils.getNMSClass("PacketPlayInBlockPlace");
|
||||
blockPlaceClass_1_9 = NMSUtils.getNMSClass("PacketPlayInUseItem");
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
enumHandField = blockPlaceClass_1_9.getDeclaredField("a");
|
||||
} catch (NoSuchFieldException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if (!enumHandField.isAccessible()) {
|
||||
enumHandField.setAccessible(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
-153
@@ -1,153 +0,0 @@
|
||||
package io.github.retrooper.packetevents.packetwrappers.in.blockplace;
|
||||
|
||||
import io.github.retrooper.packetevents.enums.Hand;
|
||||
import io.github.retrooper.packetevents.enums.ServerVersion;
|
||||
import io.github.retrooper.packetevents.packetwrappers.api.WrappedPacket;
|
||||
import io.github.retrooper.packetevents.utils.BaseBlockUtils;
|
||||
import io.github.retrooper.packetevents.utils.NMSUtils;
|
||||
import io.github.retrooper.packetevents.utils.vector.Vector3i;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
class WrappedPacketInBlockPlace_Legacy extends WrappedPacket {
|
||||
private Vector3i blockPosition;
|
||||
private ItemStack itemStack;
|
||||
private Hand hand;
|
||||
|
||||
WrappedPacketInBlockPlace_Legacy(final Object packet) {
|
||||
super(packet);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setup() {
|
||||
try {
|
||||
final Hand hand = Hand.MAIN_HAND;
|
||||
final ItemStack itemStack;
|
||||
final int x, y, z;
|
||||
if (version.isHigherThan(ServerVersion.v_1_7_10)) {
|
||||
final Object nmsBlockPosObj = fields[0].get(packet);
|
||||
|
||||
x = blockPosXYZ[0].getInt(nmsBlockPosObj);
|
||||
y = blockPosXYZ[1].getInt(nmsBlockPosObj);
|
||||
z = blockPosXYZ[2].getInt(nmsBlockPosObj);
|
||||
|
||||
Object nmsItemStackObj = fields[1].get(packet);
|
||||
|
||||
Object craftItemStack = asBukkitCopyMethod.invoke(null, nmsItemStackObj);
|
||||
|
||||
itemStack = (ItemStack) craftItemStack;
|
||||
}
|
||||
//1.7.10
|
||||
else {
|
||||
x = fields_1_7[0].getInt(packet);
|
||||
y = fields_1_7[1].getInt(packet);
|
||||
z = fields_1_7[2].getInt(packet);
|
||||
|
||||
Object nmsItemStackObj = fields_1_7[3].get(packet);
|
||||
|
||||
Object craftItemStack = asBukkitCopyMethod.invoke(null, nmsItemStackObj);
|
||||
|
||||
itemStack = (ItemStack) craftItemStack;
|
||||
}
|
||||
|
||||
this.hand = hand;
|
||||
this.itemStack = itemStack;
|
||||
this.blockPosition = new Vector3i(x, y, z);
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public Hand getHand() {
|
||||
return hand;
|
||||
}
|
||||
|
||||
public ItemStack getItemStack() {
|
||||
return itemStack;
|
||||
}
|
||||
|
||||
public Vector3i getBlockPosition() {
|
||||
return blockPosition;
|
||||
}
|
||||
|
||||
|
||||
private static Class<?> blockPlaceClass;
|
||||
private static Class<?> itemStackClass;
|
||||
private static Class<?> craftItemStackClass;
|
||||
|
||||
private static Class<?> blockPositionClass;
|
||||
|
||||
private static Field[] fields = new Field[2];
|
||||
|
||||
private static Field[] fields_1_7 = new Field[4];
|
||||
|
||||
private static Field[] blockPosXYZ = new Field[3];
|
||||
|
||||
private static Method asBukkitCopyMethod;
|
||||
|
||||
|
||||
protected static void setupStaticVars() {
|
||||
try {
|
||||
blockPlaceClass = NMSUtils.getNMSClass("PacketPlayInBlockPlace");
|
||||
itemStackClass = NMSUtils.getNMSClass("ItemStack");
|
||||
craftItemStackClass = NMSUtils.getOBCClass("inventory.CraftItemStack");
|
||||
if (version.isHigherThan(ServerVersion.v_1_7_10)) {
|
||||
blockPositionClass = NMSUtils.getNMSClass("BlockPosition");
|
||||
}
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
asBukkitCopyMethod = craftItemStackClass.getMethod("asBukkitCopy", itemStackClass);
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
if (version.isLowerThan(ServerVersion.v_1_8)) {
|
||||
//x
|
||||
fields_1_7[0] = blockPlaceClass.getDeclaredField("a");
|
||||
//y
|
||||
fields_1_7[1] = blockPlaceClass.getDeclaredField("b");
|
||||
//z
|
||||
fields_1_7[2] = blockPlaceClass.getDeclaredField("c");
|
||||
//itemstack
|
||||
fields_1_7[3] = blockPlaceClass.getDeclaredField("e");
|
||||
} else {
|
||||
//block pos
|
||||
fields[0] = blockPlaceClass.getDeclaredField("b");
|
||||
//itemstack
|
||||
fields[1] = blockPlaceClass.getDeclaredField("d");
|
||||
|
||||
|
||||
blockPosXYZ[0] = blockPositionClass.getSuperclass().getDeclaredField(BaseBlockUtils.getPosXFieldName());
|
||||
blockPosXYZ[1] = blockPositionClass.getSuperclass().getDeclaredField(BaseBlockUtils.getPosYFieldName());
|
||||
blockPosXYZ[2] = blockPositionClass.getSuperclass().getDeclaredField(BaseBlockUtils.getPosZFieldName());
|
||||
}
|
||||
} catch (NoSuchFieldException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
for (Field f : fields) {
|
||||
if (f != null) {
|
||||
f.setAccessible(true);
|
||||
}
|
||||
}
|
||||
|
||||
for (Field f : fields_1_7) {
|
||||
if (f != null) {
|
||||
f.setAccessible(true);
|
||||
}
|
||||
}
|
||||
|
||||
for (Field f : blockPosXYZ) {
|
||||
if (f != null) {
|
||||
f.setAccessible(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+52
-1
@@ -1,4 +1,55 @@
|
||||
package io.github.retrooper.packetevents.packetwrappers.login;
|
||||
|
||||
public class WrappedPacketLoginHandshake {
|
||||
import io.github.retrooper.packetevents.packetwrappers.api.WrappedPacket;
|
||||
import io.github.retrooper.packetevents.tinyprotocol.Reflection;
|
||||
import io.github.retrooper.packetevents.tinyprotocol.Reflection.FieldAccessor;
|
||||
import io.github.retrooper.packetevents.utils.NMSUtils;
|
||||
|
||||
|
||||
public class WrappedPacketLoginHandshake extends WrappedPacket {
|
||||
private int protocolVersion, port;
|
||||
private String hostname;
|
||||
|
||||
public WrappedPacketLoginHandshake(final Object packet) {
|
||||
super(packet);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setup() {
|
||||
final Object protocolVersionObj = fields[0].get(packet);
|
||||
final Object hostNameObj = fields[1].get(packet);
|
||||
final Object portObj = fields[2].get(packet);
|
||||
|
||||
this.protocolVersion = (int)protocolVersionObj;
|
||||
this.hostname = hostNameObj.toString();
|
||||
this.port = (int)portObj;
|
||||
}
|
||||
|
||||
public int getProtocolVersion() {
|
||||
return protocolVersion;
|
||||
}
|
||||
|
||||
public String getHostname() {
|
||||
return hostname;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
private static Class<?> loginHandshakeClass;
|
||||
|
||||
private static FieldAccessor<?>[] fields = new FieldAccessor[3];
|
||||
|
||||
static {
|
||||
try {
|
||||
loginHandshakeClass = NMSUtils.getNMSClass("PacketHandshakingInSetProtocol");
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
fields[0] = Reflection.getField(loginHandshakeClass, int.class, 0);
|
||||
fields[1] = Reflection.getField(loginHandshakeClass, String.class, 0);
|
||||
fields[2] = Reflection.getField(loginHandshakeClass, int.class, 1);
|
||||
}
|
||||
}
|
||||
+49
-18
@@ -6,6 +6,7 @@ import io.github.retrooper.packetevents.packetwrappers.api.WrappedPacket;
|
||||
import io.github.retrooper.packetevents.utils.NMSUtils;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
@@ -18,13 +19,22 @@ public class WrappedPacketOutChat extends WrappedPacket implements Sendable {
|
||||
|
||||
public WrappedPacketOutChat(final String message) {
|
||||
super(null);
|
||||
this.message = message;
|
||||
this.message = "{\"text\": \"" + message + "\"}";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setup() {
|
||||
//all fields are IChatBaseComponents, and named 'a'
|
||||
net.minecraft.server.v1_8_R3.PacketPlayOutChat c;
|
||||
|
||||
try {
|
||||
Object iChatBaseObj = iChatBaseField.get(packet);
|
||||
|
||||
Object contentString = getStringOfIChatBase.invoke(iChatBaseObj);
|
||||
|
||||
this.message = contentString.toString();
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -32,18 +42,13 @@ public class WrappedPacketOutChat extends WrappedPacket implements Sendable {
|
||||
Object iChatBaseComponentObj = null;
|
||||
try {
|
||||
iChatBaseComponentObj = serialize.invoke(null, this.message);
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
return chatClassConstructor.newInstance(iChatBaseComponentObj);
|
||||
} catch (InstantiationException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
} catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -54,22 +59,31 @@ public class WrappedPacketOutChat extends WrappedPacket implements Sendable {
|
||||
return this.message;
|
||||
}
|
||||
|
||||
private static Class<?> chatClass;
|
||||
|
||||
private static Constructor<?> chatClassConstructor;
|
||||
|
||||
private static Class<?> iChatBaseComponentClass;
|
||||
private static Class<?> chatClass, iChatBaseComponentClass, chatSerializerClass;
|
||||
|
||||
private static Class<?> chatSerializer;
|
||||
private static Field iChatBaseField;
|
||||
|
||||
private static Method serialize;
|
||||
private static Method serialize, getStringOfIChatBase;
|
||||
|
||||
static {
|
||||
try {
|
||||
chatClass = NMSUtils.getNMSClass("PacketPlayOutChat");
|
||||
final String subClass = version.isHigherThan(ServerVersion.v_1_8) ? "IChatBaseComponent$" : "";
|
||||
iChatBaseComponentClass = NMSUtils.getNMSClass("IChatBaseComponent");
|
||||
chatSerializer = NMSUtils.getNMSClass(subClass + "ChatSerializer");
|
||||
|
||||
//In 1.8.3+ the ChatSerializer class is declared in the IChatBaseComponent class, so we have to handle tahat
|
||||
if(version.isHigherThan(ServerVersion.v_1_8)) {
|
||||
for(final Class<?> cls : iChatBaseComponentClass.getDeclaredClasses()) {
|
||||
if(cls.getSimpleName().equals("ChatSerializer")) {
|
||||
chatSerializerClass = cls;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
chatSerializerClass = NMSUtils.getNMSClass("ChatSerializer");
|
||||
}
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -81,10 +95,27 @@ public class WrappedPacketOutChat extends WrappedPacket implements Sendable {
|
||||
}
|
||||
|
||||
try {
|
||||
serialize = chatSerializer.getMethod("a", String.class);
|
||||
serialize = chatSerializerClass.getMethod("a", String.class);
|
||||
getStringOfIChatBase = iChatBaseComponentClass.getMethod(getTextMethodName());
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
iChatBaseField = chatClass.getDeclaredField("a");
|
||||
} catch (NoSuchFieldException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
iChatBaseField.setAccessible(true);
|
||||
|
||||
}
|
||||
|
||||
private static String getTextMethodName() {
|
||||
//1.7.10
|
||||
if (version.isLowerThan(ServerVersion.v_1_8)) {
|
||||
return "e"; //or c
|
||||
} else {
|
||||
return "getText";
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-3
@@ -4,7 +4,6 @@ import io.github.retrooper.packetevents.enums.ServerVersion;
|
||||
import io.github.retrooper.packetevents.packetwrappers.Sendable;
|
||||
import io.github.retrooper.packetevents.packetwrappers.api.WrappedPacket;
|
||||
import io.github.retrooper.packetevents.utils.NMSUtils;
|
||||
import net.minecraft.server.v1_15_R1.Vec3D;
|
||||
import org.bukkit.entity.Entity;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
@@ -32,6 +31,7 @@ public class WrappedPacketOutEntityVelocity extends WrappedPacket implements Sen
|
||||
@Override
|
||||
protected void setup() {
|
||||
try {
|
||||
int i = 0;
|
||||
final int entityId = fields[0].getInt(packet);
|
||||
|
||||
final int x = fields[1].getInt(packet);
|
||||
@@ -100,8 +100,6 @@ public class WrappedPacketOutEntityVelocity extends WrappedPacket implements Sen
|
||||
* 1.14+ use int, Vec3D style
|
||||
*/
|
||||
static {
|
||||
Vec3D vec = new Vec3D(0, 0, 0);
|
||||
|
||||
try {
|
||||
|
||||
velocityClass = NMSUtils.getNMSClass("PacketPlayOutEntityVelocity");
|
||||
|
||||
@@ -72,14 +72,10 @@ public abstract class TinyProtocol7 {
|
||||
|
||||
// Packets we have to intercept
|
||||
private static final Class<?> PACKET_LOGIN_IN_START = Reflection.getMinecraftClass("PacketLoginInStart");
|
||||
private static final Class<?> PACKET_HANDSHAKING_IN_SET_PROTOCOL = Reflection.getMinecraftClass("PacketHandshakingInSetProtocol");
|
||||
private static final FieldAccessor<GameProfile> getGameProfile = Reflection.getField(PACKET_LOGIN_IN_START, GameProfile.class, 0);
|
||||
private static final FieldAccessor<Integer> protocolId = Reflection.getField(PACKET_HANDSHAKING_IN_SET_PROTOCOL, int.class, 0);
|
||||
private static final FieldAccessor<Enum> protocolType = Reflection.getField(PACKET_HANDSHAKING_IN_SET_PROTOCOL, Enum.class, 0);
|
||||
|
||||
// Speedup channel/protocol lookup
|
||||
private Map<String, Channel> channelLookup = new MapMaker().weakValues().makeMap();
|
||||
private Map<Channel, Integer> protocolLookup = new MapMaker().weakKeys().makeMap();
|
||||
private final Map<String, Channel> channelLookup = new MapMaker().weakValues().makeMap();
|
||||
private Listener listener;
|
||||
|
||||
// Channels that have already been removed
|
||||
@@ -421,20 +417,6 @@ public abstract class TinyProtocol7 {
|
||||
return channel;
|
||||
}
|
||||
|
||||
public int getProtocolVersion(Player player) {
|
||||
Channel channel = channelLookup.get(player.getName());
|
||||
|
||||
// Lookup channel again
|
||||
if (channel == null) {
|
||||
Object connection = getConnection.get(getPlayerHandle.invoke(player));
|
||||
Object manager = getManager.get(connection);
|
||||
|
||||
channelLookup.put(player.getName(), channel = getChannel.get(manager));
|
||||
}
|
||||
|
||||
return protocolLookup.get(channel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Uninject a specific player.
|
||||
*
|
||||
@@ -550,10 +532,6 @@ public abstract class TinyProtocol7 {
|
||||
if (PACKET_LOGIN_IN_START.isInstance(packet)) {
|
||||
GameProfile profile = getGameProfile.get(packet);
|
||||
channelLookup.put(profile.getName(), channel);
|
||||
} else if (PACKET_HANDSHAKING_IN_SET_PROTOCOL.isInstance(packet)) {
|
||||
if (protocolType.get(packet).name().equalsIgnoreCase("LOGIN")) {
|
||||
protocolLookup.put(channel, protocolId.get(packet));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,9 +24,8 @@ import com.mojang.authlib.GameProfile;
|
||||
import io.github.retrooper.packetevents.PacketEvents;
|
||||
import io.github.retrooper.packetevents.event.impl.PlayerInjectEvent;
|
||||
import io.github.retrooper.packetevents.event.impl.PlayerUninjectEvent;
|
||||
import io.github.retrooper.packetevents.injector.PacketHandler_1_8;
|
||||
import io.github.retrooper.packetevents.handler.TinyProtocolHandler_1_8;
|
||||
import io.github.retrooper.packetevents.tinyprotocol.Reflection.FieldAccessor;
|
||||
import io.github.retrooper.packetevents.tinyprotocol.Reflection.MethodInvoker;
|
||||
import io.netty.channel.*;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -58,7 +57,7 @@ public abstract class TinyProtocol8 {
|
||||
private static final AtomicInteger ID = new AtomicInteger(0);
|
||||
|
||||
// Used in order to lookup a channel
|
||||
private static final MethodInvoker getPlayerHandle = Reflection.getMethod("{obc}.entity.CraftPlayer", "getHandle");
|
||||
private static final Reflection.MethodInvoker getPlayerHandle = Reflection.getMethod("{obc}.entity.CraftPlayer", "getHandle");
|
||||
private static final FieldAccessor<Object> getConnection = Reflection.getField("{nms}.EntityPlayer", "playerConnection", Object.class);
|
||||
private static final FieldAccessor<Object> getManager = Reflection.getField("{nms}.PlayerConnection", "networkManager", Object.class);
|
||||
private static final FieldAccessor<Channel> getChannel = Reflection.getField("{nms}.NetworkManager", Channel.class, 0);
|
||||
@@ -68,28 +67,23 @@ public abstract class TinyProtocol8 {
|
||||
private static final Class<Object> serverConnectionClass = Reflection.getUntypedClass("{nms}.ServerConnection");
|
||||
private static final FieldAccessor<Object> getMinecraftServer = Reflection.getField("{obc}.CraftServer", minecraftServerClass, 0);
|
||||
private static final FieldAccessor<Object> getServerConnection = Reflection.getField(minecraftServerClass, serverConnectionClass, 0);
|
||||
private static final MethodInvoker getNetworkMarkers = Reflection.getTypedMethod(serverConnectionClass, null, List.class, serverConnectionClass);
|
||||
|
||||
// Packets we have to intercept
|
||||
private static final Class<?> PACKET_LOGIN_IN_START = Reflection.getMinecraftClass("PacketLoginInStart");
|
||||
private static final Class<?> PACKET_HANDSHAKING_IN_SET_PROTOCOL = Reflection.getMinecraftClass("PacketHandshakingInSetProtocol");
|
||||
private static final FieldAccessor<GameProfile> getGameProfile = Reflection.getField(PACKET_LOGIN_IN_START, GameProfile.class, 0);
|
||||
private static final FieldAccessor<Integer> protocolId = Reflection.getField(PACKET_HANDSHAKING_IN_SET_PROTOCOL, int.class, 0);
|
||||
private static final FieldAccessor<Enum> protocolType = Reflection.getField(PACKET_HANDSHAKING_IN_SET_PROTOCOL, Enum.class, 0);
|
||||
|
||||
// Speedup channel/protocol lookup
|
||||
private Map<String, Channel> channelLookup = new MapMaker().weakValues().makeMap();
|
||||
private Map<Channel, Integer> protocolLookup = new MapMaker().weakKeys().makeMap();
|
||||
private final Map<String, Channel> channelLookup = new MapMaker().weakValues().makeMap();
|
||||
private Listener listener;
|
||||
|
||||
// Channels that have already been removed
|
||||
private Set<Channel> uninjectedChannels = Collections.newSetFromMap(new MapMaker().weakKeys().<Channel, Boolean>makeMap());
|
||||
private final Set<Channel> uninjectedChannels = Collections.newSetFromMap(new MapMaker().weakKeys().<Channel, Boolean>makeMap());
|
||||
|
||||
// List of network markers
|
||||
private List<Object> networkManagers;
|
||||
|
||||
// Injected channel handlers
|
||||
private List<Channel> serverChannels = Lists.newArrayList();
|
||||
private final List<Channel> serverChannels = Lists.newArrayList();
|
||||
private ChannelInboundHandlerAdapter serverChannelHandler;
|
||||
private ChannelInitializer<Channel> beginInitProtocol;
|
||||
private ChannelInitializer<Channel> endInitProtocol;
|
||||
@@ -221,7 +215,7 @@ public abstract class TinyProtocol8 {
|
||||
boolean looking = true;
|
||||
|
||||
// We need to synchronize against this list
|
||||
final Field networkManagersField = serverConnection.getClass().getDeclaredField(PacketHandler_1_8.getNetworkManagersFieldName());
|
||||
final Field networkManagersField = serverConnection.getClass().getDeclaredField(TinyProtocolHandler_1_8.getNetworkManagersFieldName());
|
||||
networkManagersField.setAccessible(true);
|
||||
networkManagers = (List<Object>) networkManagersField.get(serverConnection);
|
||||
createServerChannelHandler();
|
||||
@@ -284,7 +278,7 @@ public abstract class TinyProtocol8 {
|
||||
* @return The packet to send instead, or NULL to cancel the transmission.
|
||||
*/
|
||||
public Object onPacketOutAsync(Player receiver, Channel channel, Object packet) {
|
||||
return packet;
|
||||
return packet;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -420,20 +414,6 @@ public abstract class TinyProtocol8 {
|
||||
return channel;
|
||||
}
|
||||
|
||||
public int getProtocolVersion(Player player) {
|
||||
Channel channel = channelLookup.get(player.getName());
|
||||
|
||||
// Lookup channel again
|
||||
if (channel == null) {
|
||||
Object connection = getConnection.get(getPlayerHandle.invoke(player));
|
||||
Object manager = getManager.get(connection);
|
||||
|
||||
channelLookup.put(player.getName(), channel = getChannel.get(manager));
|
||||
}
|
||||
|
||||
return protocolLookup.get(channel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Uninject a specific player.
|
||||
*
|
||||
@@ -549,10 +529,6 @@ public abstract class TinyProtocol8 {
|
||||
if (PACKET_LOGIN_IN_START.isInstance(packet)) {
|
||||
GameProfile profile = getGameProfile.get(packet);
|
||||
channelLookup.put(profile.getName(), channel);
|
||||
} else if (PACKET_HANDSHAKING_IN_SET_PROTOCOL.isInstance(packet)) {
|
||||
if (protocolType.get(packet).name().equalsIgnoreCase("LOGIN")) {
|
||||
protocolLookup.put(channel, protocolId.get(packet));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
package io.github.retrooper.packetevents.utils;
|
||||
|
||||
import io.github.retrooper.packetevents.PacketEvents;
|
||||
import io.github.retrooper.packetevents.enums.ServerVersion;
|
||||
|
||||
public class BaseBlockUtils {
|
||||
private static final ServerVersion version = PacketEvents.getServerVersion();
|
||||
|
||||
public static String getPosXFieldName() {
|
||||
return "a";
|
||||
}
|
||||
|
||||
public static String getPosYFieldName() {
|
||||
if (version == ServerVersion.v_1_8) {
|
||||
return "b";
|
||||
} else if (version == ServerVersion.v_1_8_3
|
||||
|| version == ServerVersion.v_1_8_8
|
||||
|| version == ServerVersion.v_1_9) {
|
||||
return "c";
|
||||
}
|
||||
//1.9.4 and higher
|
||||
return "b";
|
||||
}
|
||||
|
||||
public static String getPosZFieldName() {
|
||||
if (version == ServerVersion.v_1_8) {
|
||||
return "c";
|
||||
} else if (version == ServerVersion.v_1_8_3 || version == ServerVersion.v_1_8_8 || version == ServerVersion.v_1_9) {
|
||||
return "d";
|
||||
}
|
||||
return "c";
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package io.github.retrooper.packetevents.utils;
|
||||
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.util.BlockIterator;
|
||||
|
||||
public class BlockFinder {
|
||||
public static Block getBlocksInDirection(final Player player, final int blockDistance) {
|
||||
final BlockIterator it = new BlockIterator(player, blockDistance);
|
||||
Block cur = null;
|
||||
while(it.hasNext()) {
|
||||
cur = it.next();
|
||||
}
|
||||
return cur;
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import io.github.retrooper.packetevents.utils.nms_entityfinder.EntityFinderUtils
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.lang.reflect.Field;
|
||||
@@ -16,25 +17,16 @@ public class NMSUtils {
|
||||
private static final ServerVersion version = ServerVersion.getVersion();
|
||||
|
||||
|
||||
private static final String nettyPrefix = version == ServerVersion.v_1_7_10 ? "net.minecraft.util.io.netty" : "io.netty";
|
||||
public static final String nettyPrefix = version.isLowerThan(ServerVersion.v_1_8) ? "net.minecraft.util.io.netty" : "io.netty";
|
||||
private static final String nmsDir = ServerVersion.getNMSDirectory();
|
||||
private static final String obcDir = ServerVersion.getOBCDirectory();
|
||||
|
||||
public static Class<?> minecraftServerClass, craftWorldsClass,
|
||||
packetClass, entityPlayerClass, playerConnectionClass, craftPlayerClass;
|
||||
private static Class<?> serverConnectionClass, craftEntityClass;
|
||||
packetClass, entityPlayerClass, playerConnectionClass, craftPlayerClass, serverConnectionClass, craftEntityClass, craftItemStack, nmsItemStackClass;
|
||||
|
||||
private static Method getServerMethod;
|
||||
private static Method getCraftWorldHandleMethod;
|
||||
private static Method entityMethod;
|
||||
public static Method getServerConnection;
|
||||
public static Method getCraftPlayerHandle;
|
||||
public static Method getCraftEntityHandle;
|
||||
private static Method sendPacketMethod;
|
||||
private static Method getServerMethod, getCraftWorldHandleMethod, getServerConnection, getCraftPlayerHandle, getCraftEntityHandle, sendPacketMethod, asBukkitCopy;
|
||||
|
||||
private static Field recentTPSField;
|
||||
private static Field entityPlayerPingField;
|
||||
private static Field playerConnectionField;
|
||||
private static Field recentTPSField, entityPlayerPingField, playerConnectionField;
|
||||
|
||||
static {
|
||||
try {
|
||||
@@ -46,6 +38,8 @@ public class NMSUtils {
|
||||
playerConnectionClass = getNMSClass("PlayerConnection");
|
||||
serverConnectionClass = getNMSClass("ServerConnection");
|
||||
craftEntityClass = getOBCClass("entity.CraftEntity");
|
||||
craftItemStack = getOBCClass("inventory.CraftItemStack");
|
||||
nmsItemStackClass = getNMSClass("ItemStack");
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -58,6 +52,7 @@ public class NMSUtils {
|
||||
getCraftEntityHandle = craftEntityClass.getMethod("getHandle");
|
||||
sendPacketMethod = playerConnectionClass.getMethod("sendPacket", packetClass);
|
||||
getServerConnection = minecraftServerClass.getMethod("getServerConnection");
|
||||
asBukkitCopy = craftItemStack.getMethod("asBukkitCopy", nmsItemStackClass);
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -118,35 +113,29 @@ public class NMSUtils {
|
||||
final Object craftEntity = craftEntityClass.cast(entity);
|
||||
try {
|
||||
return getCraftEntityHandle.invoke(craftEntity);
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Object getCraftPlayer(final Player player) {
|
||||
return craftPlayerClass.cast(player);
|
||||
}
|
||||
|
||||
public static Object getEntityPlayer(final Player player) {
|
||||
final Object craftPlayer = getCraftPlayer(player);
|
||||
Object entityPlayer = null;
|
||||
try {
|
||||
return getCraftPlayerHandle.invoke(craftPlayerClass.cast(player));
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
return getCraftPlayerHandle.invoke(craftPlayer);
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static int getPlayerPing(final Player player) {
|
||||
final Object craftPlayer = craftPlayerClass.cast(player);
|
||||
Object entityPlayer = null;
|
||||
try {
|
||||
entityPlayer = getCraftPlayerHandle.invoke(craftPlayer);
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Object entityPlayer = getEntityPlayer(player);
|
||||
try {
|
||||
return entityPlayerPingField.getInt(entityPlayer);
|
||||
} catch (IllegalAccessException e) {
|
||||
@@ -155,6 +144,17 @@ public class NMSUtils {
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static ItemStack toBukkitItemStack(final Object nmsItemstack) {
|
||||
try {
|
||||
return (ItemStack) asBukkitCopy.invoke(null, nmsItemstack);
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean is_1_8() {
|
||||
return version.isHigherThan(ServerVersion.v_1_7_10)
|
||||
&& version.isLowerThan(ServerVersion.v_1_9);
|
||||
@@ -165,15 +165,7 @@ public class NMSUtils {
|
||||
}
|
||||
|
||||
public static void sendNMSPacket(final Player player, final Object nmsPacket) {
|
||||
Object entityPlayer = null;
|
||||
try {
|
||||
entityPlayer = getCraftPlayerHandle.invoke(craftPlayerClass.cast(player));
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Object entityPlayer = getEntityPlayer(player);
|
||||
Object playerConnection = null;
|
||||
try {
|
||||
playerConnection = playerConnectionField.get(entityPlayer);
|
||||
@@ -183,9 +175,7 @@ public class NMSUtils {
|
||||
|
||||
try {
|
||||
sendPacketMethod.invoke(playerConnection, nmsPacket);
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
package io.github.retrooper.packetevents.utils.vector;
|
||||
|
||||
public class Vector2d {
|
||||
public double x, y;
|
||||
|
||||
public Vector2d(final double x, final double y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "X: " + x + ", Y: " + y;
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package io.github.retrooper.packetevents.utils.vector;
|
||||
|
||||
public class Vector2f {
|
||||
public float x, y;
|
||||
public Vector2f(final float x, final float y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "X: " + x + ", Y: " + y;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package io.github.retrooper.packetevents.utils.vector;
|
||||
|
||||
public class Vector2i {
|
||||
public int x, y;
|
||||
|
||||
public Vector2i(final int x, final int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "X: " + x + ", Y: " + y;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package io.github.retrooper.packetevents.utils.vector;
|
||||
|
||||
public class Vector3d {
|
||||
public double x, y, z;
|
||||
public Vector3d(final double x, final double y, final double z) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "X: " + x + ", Y: " + y + ", Z: " + z;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package io.github.retrooper.packetevents.utils.vector;
|
||||
|
||||
public class Vector3f {
|
||||
public float x, y, z;
|
||||
|
||||
public Vector3f(final float x, final float y, final float z) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "X: " + x + ", Y: " + y + ", Z: " + z;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user