Skip to content

Commit

Permalink
Fixing entityaction, transaction, and entityteleport wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
funkemunky committed Dec 2, 2021
1 parent e7bc843 commit b6c12e5
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import cc.funkemunky.api.reflections.types.WrappedMethod;
import cc.funkemunky.api.tinyprotocol.api.ProtocolVersion;
import cc.funkemunky.api.tinyprotocol.api.packets.AbstractTinyProtocol;
import cc.funkemunky.api.tinyprotocol.packet.login.WrappedHandshakingInSetProtocol;
import cc.funkemunky.api.tinyprotocol.packet.types.enums.WrappedEnumProtocol;
import cc.funkemunky.api.tinyprotocol.reflection.Reflection;
import cc.funkemunky.api.utils.RunUtils;
import com.google.common.collect.Lists;
Expand Down Expand Up @@ -462,6 +464,7 @@ public int getProtocolVersion(Player player) {
* @param player - the injected player.
*/
public void uninjectPlayer(Player player) {
channelLookup.remove(player.getName());
uninjectChannel(getChannel(player));
}

Expand Down Expand Up @@ -540,11 +543,11 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
GameProfile profile = getGameProfile.get(msg);
channelLookup.put(profile.getName(), channel);
} else if (PACKET_SET_PROTOCOL.getParent().isInstance(msg)) {
int protocol = ((Enum)protocolType.get(msg)).ordinal();
System.out.println("PacketPlayInSetProtocol: " + protocol);
if (protocol == 3) {
int id = protocolId.get(msg);
protocolLookup.put(channel, id);
WrappedHandshakingInSetProtocol protocol = new WrappedHandshakingInSetProtocol(msg);
System.out.println("PacketPlayInSetProtocol: " + protocol.enumProtocol + ";" + protocol.a);
if (protocol.enumProtocol == WrappedEnumProtocol.LOGIN) {
System.out.println("Enum protocol matched");
protocolLookup.put(channel, protocol.a);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public enum EnumPlayerDigType {
public static WrappedClass classDigType;

public static EnumPlayerDigType fromVanilla(Enum obj) {
return valueOf(obj.name());
return EnumPlayerDigType.values()[obj.ordinal()];
}

public <T> T toVanilla() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void process(Player player, ProtocolVersion version) {
if (ProtocolVersion.getGameVersion().isBelow(ProtocolVersion.V1_8)) {
action = EnumPlayerAction.values()[Math.min(8, fetch(fieldAction1_7) - 1)];
} else {
action = EnumPlayerAction.values()[fetch(fieldAction1_8).ordinal()];
action = EnumPlayerAction.values()[((Enum)fetch(fieldAction1_8)).ordinal()];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public void process(Player player, ProtocolVersion version) {
accept = transaction.isAccepted();
} else {
idRaw = fetch(fieldId);
id = (short) ((id >> 16) & 0xFF);
action = (short) (id & 0xFFFF);
id = (short) ((idRaw >> 16) & 0xFF);
action = (short) (idRaw & 0xFFFF);
accept = (idRaw & (1 << 30)) != 0;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
import cc.funkemunky.api.tinyprotocol.packet.types.WrappedPacketDataSerializer;
import cc.funkemunky.api.utils.MathHelper;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.buffer.Unpooled;
import org.bukkit.entity.Player;

public class WrappedOutEntityTeleportPacket extends NMSObject {

private static WrappedField fieldEntityId, fieldX, fieldY, fieldZ, fieldYaw, fieldPitch, fieldOnGround;
private static WrappedClass classEntityTeleport = Reflections.getNMSClass(Packet.Server.ENTITY_TELEPORT);
private static WrappedConstructor emptyConstructor = classEntityTeleport.getConstructor();
private static WrappedConstructor emptyConstructor;

public int entityId;
public double x, y, z;
Expand All @@ -42,8 +44,8 @@ public WrappedOutEntityTeleportPacket(int entityId, double x, double y, double z
setObject(emptyConstructor.newInstance());
updateObject();
} else {
ByteBuf buf = Unpooled.buffer();
buf.writeInt(entityId);
WrappedPacketDataSerializer buf = new WrappedPacketDataSerializer(Unpooled.buffer());
buf.d(entityId);
buf.writeDouble(x);
buf.writeDouble(y);
buf.writeDouble(z);
Expand All @@ -52,8 +54,7 @@ public WrappedOutEntityTeleportPacket(int entityId, double x, double y, double z
buf.writeBoolean(onGround);

setObject(classEntityTeleport.getConstructor(WrappedPacketDataSerializer.vanillaClass.getParent())
.newInstance((Object) WrappedPacketDataSerializer.vanillaClass.getConstructor(ByteBuf.class)
.newInstance(buf)));
.newInstance(buf.getObject()));
}
}

Expand Down Expand Up @@ -107,6 +108,10 @@ public void updateObject() {
fieldX = fetchField(classEntityTeleport, double.class, 0);
fieldY = fetchField(classEntityTeleport, double.class, 1);
fieldZ = fetchField(classEntityTeleport, double.class, 2);

if(ProtocolVersion.getGameVersion().isBelow(ProtocolVersion.v1_17)) {
emptyConstructor = classEntityTeleport.getConstructor();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public void process(Player player, ProtocolVersion version) {
} else {
idRaw = fetch(fieldId);

id = (short) ((id >> 16) & 0xFF);
action = (short) (id & 0xFFFF);
id = (short) ((idRaw >> 16) & 0xFF);
action = (short) (idRaw & 0xFFFF);
accept = (idRaw & (1 << 30)) != 0;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public WrappedPacketDataSerializer(Object object) {
else data = new byte[0];
}

public WrappedPacketDataSerializer(ByteBuf buf) {
setObject(byteConst.newInstance(buf));
}

@Override
public void updateObject() {
//Empty method
Expand All @@ -40,4 +44,24 @@ public WrappedPacketDataSerializer(byte[] data) {
this.data = data;
setObject(pds);
}

public void d(int dint) {
vanillaClass.getMethod("d", int.class).invoke(getObject(), dint);
}

public void writeInt(int integer) {
vanillaClass.getMethod("writeInt", int.class).invoke(getObject(), integer);
}

public void writeDouble(double doubleFloat) {
vanillaClass.getMethod("writeDouble", double.class).invoke(getObject(), doubleFloat);
}

public void writeFloat(float floating) {
vanillaClass.getMethod("writeFloat", float.class).invoke(getObject(), floating);
}

public void writeBoolean(boolean bool) {
vanillaClass.getMethod("writeBoolean", boolean.class).invoke(getObject(), bool);
}
}

0 comments on commit b6c12e5

Please sign in to comment.