Skip to content

Commit

Permalink
LoginInEncryptionBegin & LoginOutEncryptionBegin wrapper added,
Browse files Browse the repository at this point in the history
LoginStart & LoginStatusPing wrapper added,
Bug fixed with PostPlayerInjectEvent not being called,
Custom GameProfile class coded,
Main example class readded
  • Loading branch information
purplexdev committed Jun 17, 2020
1 parent ceb3581 commit 7f94b25
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/main/java/io/github/retrooper/packetevents/PacketEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
import io.github.retrooper.packetevents.event.impl.ServerTickEvent;
import io.github.retrooper.packetevents.event.manager.EventManager;
import io.github.retrooper.packetevents.handler.TinyProtocolHandler;
import io.github.retrooper.packetevents.mojang.GameProfile;
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.packetwrappers.login.*;
import io.github.retrooper.packetevents.utils.NMSUtils;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
Expand All @@ -25,16 +24,17 @@
import org.bukkit.scheduler.BukkitTask;

import io.github.retrooper.packetevents.annotations.Nullable;

import java.lang.reflect.InvocationTargetException;
import java.security.PublicKey;
import java.util.HashMap;

public final class PacketEvents implements PacketListener, Listener {


/*
* Login wrappers TODO
* PacketLoginInStart
* PacketLoginInEncryptionBegin
* Wrappers TODO:
* Spawn entity
*/

private static boolean hasRegistered;
Expand Down Expand Up @@ -223,6 +223,7 @@ public void onInject(final PlayerInjectEvent e) {

/**
* Called after the PlayerJoinEvent ONLY if the player has been injected!
*
* @param e
*/
@PacketHandler
Expand All @@ -240,6 +241,7 @@ public void onJoin(PlayerJoinEvent e) {

/**
* Version independant player injection
*
* @param player
*/
public static void injectPlayer(final Player player) {
Expand All @@ -248,6 +250,7 @@ public static void injectPlayer(final Player player) {

/**
* Version independant player injection
*
* @param player
*/
public static void uninjectPlayer(final Player player) {
Expand All @@ -256,6 +259,7 @@ public static void uninjectPlayer(final Player player) {

/**
* Returns whether we have injected the player
*
* @param player
* @return hasInjected
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.github.retrooper.packetevents.example;

import io.github.retrooper.packetevents.PacketEvents;
import org.bukkit.plugin.java.JavaPlugin;

public class MainExample extends JavaPlugin {

@Override
public void onEnable() {
PacketEvents.start(this);
}

@Override
public void onDisable() {
PacketEvents.stop();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package io.github.retrooper.packetevents.mojang;

import com.mojang.authlib.properties.PropertyMap;
import org.bukkit.craftbukkit.libs.org.apache.commons.lang3.StringUtils;
import org.bukkit.craftbukkit.libs.org.apache.commons.lang3.builder.ToStringBuilder;

import java.util.UUID;

public class GameProfile {
private final UUID id;
private final String name;
private final PropertyMap properties = new PropertyMap();
private boolean legacy;

public GameProfile(UUID id, String name) {
if (id == null && StringUtils.isBlank(name)) {
throw new IllegalArgumentException("Name and ID cannot both be blank");
} else {
this.id = id;
this.name = name;
}
}

public UUID getId() {
return this.id;
}

public String getName() {
return this.name;
}

public PropertyMap getProperties() {
return this.properties;
}

public boolean isComplete() {
return this.id != null && StringUtils.isNotBlank(this.getName());
}

public boolean equalsMojangGameProfile(com.mojang.authlib.GameProfile profile) {
if (profile != null) {
if (profile.getId() != null && getId() != null && profile.getId() == getId()
&& profile.getName() != null && getName() != null && profile.getName().equals(getName())) {
return true;
}
}
return false;
}

public boolean equals(Object o) {
if (this == o) {
return true;
} else if (o != null && this.getClass() == o.getClass()) {
GameProfile that = (GameProfile) o;
if (this.id != null) {
if (!this.id.equals(that.id)) {
return false;
}
} else if (that.id != null) {
return false;
}

if (this.name != null) {
if (this.name.equals(that.name)) {
return true;
}
} else if (that.name == null) {
return true;
}

return false;
} else {
return false;
}
}

public int hashCode() {
int result = this.id != null ? this.id.hashCode() : 0;
result = 31 * result + (this.name != null ? this.name.hashCode() : 0);
return result;
}

public String toString() {
return (new ToStringBuilder(this)).append("id", this.id).append("name", this.name).append("properties", this.properties).append("legacy", this.legacy).toString();
}

public boolean isLegacy() {
return this.legacy;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public static class Login {
public static final String PING = "PacketStatusInPing";
public static final String START = "PacketStatusInStart";
public static final String SUCCESS = "PacketLoginOutSuccess";
public static final String ENCRYPTION_BEGIN_IN = "PacketLoginInEncryptionBegin";
public static final String ENCRYPTION_BEGIN_OUT = "PacketLoginOutEncryptionBegin";

public static final String[] LOGIN_PACKETS = new String[]{"PacketHandshakingInSetProtocol", "PacketLoginInStart", "PacketLoginInEncryptionBegin"};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.github.retrooper.packetevents.packetwrappers.login;

import io.github.retrooper.packetevents.packetwrappers.api.WrappedPacket;
import io.github.retrooper.packetevents.tinyprotocol.Reflection;
import io.github.retrooper.packetevents.utils.NMSUtils;

public final class WrappedPacketLoginInEncryptionBegin extends WrappedPacket {
private byte[] publicKey, verifyToken;
public WrappedPacketLoginInEncryptionBegin(final Object packet) {
super(packet);
}

@Override
protected void setup() {
this.publicKey = fields[0].get(packet);
this.verifyToken = fields[1].get(packet);
}

public byte[] getPublicKey() {
return publicKey;
}

public byte[] getVerifyToken() {
return verifyToken;
}

private static Class<?> packetClass;

private static final Reflection.FieldAccessor<byte[]>[] fields = new Reflection.FieldAccessor[2];

static {
try {
packetClass = NMSUtils.getNMSClass("PacketLoginInEncryptionBegin");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

fields[0] = Reflection.getField(packetClass, byte[].class, 0);
fields[1] = Reflection.getField(packetClass, byte[].class, 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.github.retrooper.packetevents.packetwrappers.login;

import io.github.retrooper.packetevents.packetwrappers.api.WrappedPacket;
import io.github.retrooper.packetevents.tinyprotocol.Reflection;
import io.github.retrooper.packetevents.utils.NMSUtils;

import java.security.PublicKey;


public final class WrappedPacketLoginOutEncryptionBegin extends WrappedPacket {
private String encodedString;
private PublicKey encodedKey;
private byte[] byteArray;

public WrappedPacketLoginOutEncryptionBegin(final Object packet) {
super(packet);
}

@Override
protected void setup() {
//string - a (encodedstring)
//public key - b (encoded)
//byte array - c(byte array)
this.encodedString = fields[0].get(packet).toString();
this.encodedKey = (PublicKey) fields[1].get(packet);
this.byteArray = (byte[]) fields[2].get(packet);
}

public String getEncodedString() {
return encodedString;
}

public PublicKey getEncodedKey() {
return encodedKey;
}

public byte[] getByteArray() {
return byteArray;
}

private static Class<?> packetClass;

private static final Reflection.FieldAccessor<?>[] fields = new Reflection.FieldAccessor[3];

static {
try {
packetClass = NMSUtils.getNMSClass("PacketLoginOutEncryptionBegin");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

fields[0] = Reflection.getField(packetClass, String.class, 0);
fields[1] = Reflection.getField(packetClass, PublicKey.class, 1);
fields[2] = Reflection.getField(packetClass, byte[].class, 2);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.github.retrooper.packetevents.packetwrappers.login;

import io.github.retrooper.packetevents.mojang.GameProfile;
import io.github.retrooper.packetevents.packetwrappers.api.WrappedPacket;
import io.github.retrooper.packetevents.tinyprotocol.Reflection;
import io.github.retrooper.packetevents.utils.NMSUtils;

public final class WrappedPacketLoginStart extends WrappedPacket {
private GameProfile gameProfile;
public WrappedPacketLoginStart(final Object packet) {
super(packet);
}

@Override
protected void setup() {
com.mojang.authlib.GameProfile gp = gameProfileAccessor.get(packet);
this.gameProfile = new GameProfile(gp.getId(), gp.getName());
}

public GameProfile getGameProfile() {
return gameProfile;
}

private static Class<?> packetClass;

private static final Reflection.FieldAccessor<com.mojang.authlib.GameProfile> gameProfileAccessor;

static {
try {
packetClass = NMSUtils.getNMSClass("PacketLoginInStart");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
gameProfileAccessor = Reflection.getField(packetClass, com.mojang.authlib.GameProfile.class, 0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.github.retrooper.packetevents.packetwrappers.login;

import io.github.retrooper.packetevents.packetwrappers.api.WrappedPacket;
import io.github.retrooper.packetevents.tinyprotocol.Reflection;
import io.github.retrooper.packetevents.utils.NMSUtils;

public final class WrappedPacketLoginStatusPing extends WrappedPacket {
private long number;
public WrappedPacketLoginStatusPing(final Object packet) {
super(packet);
}

@Override
protected void setup() {
this.number = numberAccessor.get(packet);
}

public long getNumber() {
return number;
}

private static Class<?> packetClass;

private static final Reflection.FieldAccessor<Long> numberAccessor;

static {
try {
packetClass = NMSUtils.getNMSClass("PacketStatusInPing");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

numberAccessor = Reflection.getField(packetClass, long.class, 0);
}

}

0 comments on commit 7f94b25

Please sign in to comment.