Skip to content

Commit 4330bae

Browse files
committed
Add some debug info for #208
1 parent 1c36c41 commit 4330bae

File tree

5 files changed

+165
-133
lines changed

5 files changed

+165
-133
lines changed
Lines changed: 108 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,108 @@
1-
/**
2-
* ProtocolLib - Bukkit server library that allows access to the Minecraft protocol.
3-
* Copyright (C) 2015 dmulloy2
4-
*
5-
* This program is free software; you can redistribute it and/or modify it under the terms of the
6-
* GNU General Public License as published by the Free Software Foundation; either version 2 of
7-
* the License, or (at your option) any later version.
8-
*
9-
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10-
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11-
* See the GNU General Public License for more details.
12-
*
13-
* You should have received a copy of the GNU General Public License along with this program;
14-
* if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
15-
* 02111-1307 USA
16-
*/
17-
package com.comphenix.protocol.injector.netty;
18-
19-
import java.util.Map;
20-
import java.util.Map.Entry;
21-
22-
import com.comphenix.protocol.PacketType;
23-
import com.comphenix.protocol.PacketType.Protocol;
24-
import com.comphenix.protocol.PacketType.Sender;
25-
import com.comphenix.protocol.injector.netty.ProtocolRegistry;
26-
import com.comphenix.protocol.injector.packet.MapContainer;
27-
import com.comphenix.protocol.reflect.StructureModifier;
28-
import com.google.common.collect.Maps;
29-
30-
/**
31-
* @author dmulloy2
32-
*/
33-
34-
public class NettyProtocolRegistry extends ProtocolRegistry {
35-
36-
@Override
37-
protected synchronized void initialize() {
38-
Object[] protocols = enumProtocol.getEnumConstants();
39-
40-
// ID to Packet class maps
41-
Map<Object, Map<Integer, Class<?>>> serverMaps = Maps.newLinkedHashMap();
42-
Map<Object, Map<Integer, Class<?>>> clientMaps = Maps.newLinkedHashMap();
43-
44-
Register result = new Register();
45-
StructureModifier<Object> modifier = null;
46-
47-
// Iterate through the protocols
48-
for (Object protocol : protocols) {
49-
if (modifier == null)
50-
modifier = new StructureModifier<Object>(protocol.getClass().getSuperclass(), false);
51-
StructureModifier<Map<Object, Map<Integer, Class<?>>>> maps = modifier.withTarget(protocol).withType(Map.class);
52-
for (Entry<Object, Map<Integer, Class<?>>> entry : maps.read(0).entrySet()) {
53-
String direction = entry.getKey().toString();
54-
if (direction.contains("CLIENTBOUND")) { // Sent by Server
55-
serverMaps.put(protocol, entry.getValue());
56-
} else if (direction.contains("SERVERBOUND")) { // Sent by Client
57-
clientMaps.put(protocol, entry.getValue());
58-
}
59-
}
60-
}
61-
62-
// Maps we have to occationally check have changed
63-
for (Map<Integer, Class<?>> map : serverMaps.values()) {
64-
result.containers.add(new MapContainer(map));
65-
}
66-
67-
for (Map<Integer, Class<?>> map : clientMaps.values()) {
68-
result.containers.add(new MapContainer(map));
69-
}
70-
71-
for (int i = 0; i < protocols.length; i++) {
72-
Object protocol = protocols[i];
73-
Enum<?> enumProtocol = (Enum<?>) protocol;
74-
Protocol equivalent = Protocol.fromVanilla(enumProtocol);
75-
76-
// Associate known types
77-
if (serverMaps.containsKey(protocol))
78-
associatePackets(result, serverMaps.get(protocol), equivalent, Sender.SERVER);
79-
if (clientMaps.containsKey(protocol))
80-
associatePackets(result, clientMaps.get(protocol), equivalent, Sender.CLIENT);
81-
}
82-
83-
// Exchange (thread safe, as we have only one writer)
84-
this.register = result;
85-
}
86-
87-
@Override
88-
protected void associatePackets(Register register, Map<Integer, Class<?>> lookup, Protocol protocol, Sender sender) {
89-
for (Entry<Integer, Class<?>> entry : lookup.entrySet()) {
90-
PacketType type = PacketType.fromCurrent(protocol, sender, entry.getKey(), entry.getValue());
91-
92-
try {
93-
register.typeToClass.put(type, entry.getValue());
94-
95-
if (sender == Sender.SERVER)
96-
register.serverPackets.add(type);
97-
if (sender == Sender.CLIENT)
98-
register.clientPackets.add(type);
99-
} catch (IllegalArgumentException ex) {
100-
// Sometimes this happens with fake packets, just ignore it
101-
}
102-
}
103-
}
104-
}
1+
/**
2+
* ProtocolLib - Bukkit server library that allows access to the Minecraft protocol.
3+
* Copyright (C) 2015 dmulloy2
4+
*
5+
* This program is free software; you can redistribute it and/or modify it under the terms of the
6+
* GNU General Public License as published by the Free Software Foundation; either version 2 of
7+
* the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10+
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11+
* See the GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License along with this program;
14+
* if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
15+
* 02111-1307 USA
16+
*/
17+
package com.comphenix.protocol.injector.netty;
18+
19+
import java.util.Map;
20+
import java.util.Map.Entry;
21+
22+
import com.comphenix.protocol.PacketType;
23+
import com.comphenix.protocol.PacketType.Protocol;
24+
import com.comphenix.protocol.PacketType.Sender;
25+
import com.comphenix.protocol.injector.netty.ProtocolRegistry;
26+
import com.comphenix.protocol.injector.packet.MapContainer;
27+
import com.comphenix.protocol.reflect.StructureModifier;
28+
import com.google.common.collect.Maps;
29+
30+
/**
31+
* @author dmulloy2
32+
*/
33+
34+
public class NettyProtocolRegistry extends ProtocolRegistry {
35+
36+
public NettyProtocolRegistry() {
37+
super();
38+
}
39+
40+
@Override
41+
protected synchronized void initialize() {
42+
Object[] protocols = enumProtocol.getEnumConstants();
43+
44+
// ID to Packet class maps
45+
Map<Object, Map<Integer, Class<?>>> serverMaps = Maps.newLinkedHashMap();
46+
Map<Object, Map<Integer, Class<?>>> clientMaps = Maps.newLinkedHashMap();
47+
48+
Register result = new Register();
49+
StructureModifier<Object> modifier = null;
50+
51+
// Iterate through the protocols
52+
for (Object protocol : protocols) {
53+
if (modifier == null)
54+
modifier = new StructureModifier<Object>(protocol.getClass().getSuperclass(), false);
55+
StructureModifier<Map<Object, Map<Integer, Class<?>>>> maps = modifier.withTarget(protocol).withType(Map.class);
56+
for (Entry<Object, Map<Integer, Class<?>>> entry : maps.read(0).entrySet()) {
57+
String direction = entry.getKey().toString();
58+
if (direction.contains("CLIENTBOUND")) { // Sent by Server
59+
serverMaps.put(protocol, entry.getValue());
60+
} else if (direction.contains("SERVERBOUND")) { // Sent by Client
61+
clientMaps.put(protocol, entry.getValue());
62+
}
63+
}
64+
}
65+
66+
// Maps we have to occasionally check have changed
67+
for (Map<Integer, Class<?>> map : serverMaps.values()) {
68+
result.containers.add(new MapContainer(map));
69+
}
70+
71+
for (Map<Integer, Class<?>> map : clientMaps.values()) {
72+
result.containers.add(new MapContainer(map));
73+
}
74+
75+
for (int i = 0; i < protocols.length; i++) {
76+
Object protocol = protocols[i];
77+
Enum<?> enumProtocol = (Enum<?>) protocol;
78+
Protocol equivalent = Protocol.fromVanilla(enumProtocol);
79+
80+
// Associate known types
81+
if (serverMaps.containsKey(protocol))
82+
associatePackets(result, serverMaps.get(protocol), equivalent, Sender.SERVER);
83+
if (clientMaps.containsKey(protocol))
84+
associatePackets(result, clientMaps.get(protocol), equivalent, Sender.CLIENT);
85+
}
86+
87+
// Exchange (thread safe, as we have only one writer)
88+
this.register = result;
89+
}
90+
91+
@Override
92+
protected void associatePackets(Register register, Map<Integer, Class<?>> lookup, Protocol protocol, Sender sender) {
93+
for (Entry<Integer, Class<?>> entry : lookup.entrySet()) {
94+
PacketType type = PacketType.fromCurrent(protocol, sender, entry.getKey(), entry.getValue());
95+
96+
try {
97+
register.typeToClass.put(type, entry.getValue());
98+
99+
if (sender == Sender.SERVER)
100+
register.serverPackets.add(type);
101+
if (sender == Sender.CLIENT)
102+
register.clientPackets.add(type);
103+
} catch (IllegalArgumentException ex) {
104+
// Sometimes this happens with fake packets, just ignore it
105+
}
106+
}
107+
}
108+
}

modules/API/src/main/java/com/comphenix/protocol/reflect/FuzzyReflection.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import java.util.Set;
3030
import java.util.regex.Pattern;
3131

32+
import org.apache.commons.lang.Validate;
33+
3234
import com.comphenix.protocol.reflect.accessors.Accessors;
3335
import com.comphenix.protocol.reflect.fuzzy.AbstractFuzzyMatcher;
3436
import com.comphenix.protocol.reflect.fuzzy.FuzzyMethodContract;
@@ -577,6 +579,8 @@ public List<Constructor<?>> getConstructorList(AbstractFuzzyMatcher<MethodInfo>
577579
* @return Every field.
578580
*/
579581
public Set<Field> getFields() {
582+
Validate.notNull(source, "source cannot be null!");
583+
580584
// We will only consider private fields in the declared class
581585
if (forceAccess)
582586
return setUnion(source.getDeclaredFields(), source.getFields());

modules/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/ChannelInjector.java

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -582,26 +582,35 @@ protected void finishRead(ChannelHandlerContext ctx, Object msg) {
582582
* @param packet - the packet.
583583
*/
584584
protected void handleLogin(Class<?> packetClass, Object packet) {
585-
Class<?> loginClass = PACKET_LOGIN_CLIENT;
586-
FieldAccessor loginClient = LOGIN_GAME_PROFILE;
585+
try {
586+
Class<?> loginClass = PACKET_LOGIN_CLIENT;
587+
FieldAccessor loginClient = LOGIN_GAME_PROFILE;
587588

588-
// Initialize packet class and login
589-
if (loginClass == null) {
590-
loginClass = PacketType.Login.Client.START.getPacketClass();
591-
PACKET_LOGIN_CLIENT = loginClass;
592-
}
593-
if (loginClient == null) {
594-
loginClient = Accessors.getFieldAccessor(PACKET_LOGIN_CLIENT, MinecraftReflection.getGameProfileClass(), true);
595-
LOGIN_GAME_PROFILE = loginClient;
596-
}
589+
// Initialize packet class and login
590+
if (loginClass == null) {
591+
loginClass = PacketType.Login.Client.START.getPacketClass();
592+
PACKET_LOGIN_CLIENT = loginClass;
593+
}
594+
if (loginClient == null) {
595+
loginClient = Accessors.getFieldAccessor(PACKET_LOGIN_CLIENT, MinecraftReflection.getGameProfileClass(), true);
596+
LOGIN_GAME_PROFILE = loginClient;
597+
}
597598

598-
// See if we are dealing with the login packet
599-
if (loginClass.equals(packetClass)) {
600-
// GameProfile profile = (GameProfile) loginClient.get(packet);
601-
WrappedGameProfile profile = WrappedGameProfile.fromHandle(loginClient.get(packet));
599+
// See if we are dealing with the login packet
600+
if (loginClass.equals(packetClass)) {
601+
// GameProfile profile = (GameProfile) loginClient.get(packet);
602+
WrappedGameProfile profile = WrappedGameProfile.fromHandle(loginClient.get(packet));
602603

603-
// Save the channel injector
604-
factory.cacheInjector(profile.getName(), this);
604+
// Save the channel injector
605+
factory.cacheInjector(profile.getName(), this);
606+
}
607+
} catch (NullPointerException ex) {
608+
System.err.println(String.format("[ProtocolLib] Encountered NPE in handleLogin(%s, %s)", packetClass, packet));
609+
System.err.println("PACKET_LOGIN_CLIENT = " + PACKET_LOGIN_CLIENT);
610+
System.err.println("LOGIN_GAME_PROFILE = " + LOGIN_GAME_PROFILE);
611+
System.err.println("GameProfile class = " + MinecraftReflection.getGameProfileClass());
612+
System.err.println("Provide this information in a new or existing issue");
613+
throw ex;
605614
}
606615
}
607616

modules/ProtocolLib/src/test/java/com/comphenix/protocol/PacketTypeTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import com.comphenix.protocol.injector.netty.NettyProtocolRegistry;
1515
import com.comphenix.protocol.injector.netty.ProtocolRegistry;
1616

17+
import net.minecraft.server.v1_9_R2.PacketLoginInStart;
18+
1719
public class PacketTypeTest {
1820

1921
@BeforeClass
@@ -26,6 +28,12 @@ public void testFindCurrent() {
2628
assertEquals(PacketType.Play.Client.STEER_VEHICLE, PacketType.findCurrent(Protocol.PLAY, Sender.CLIENT, "SteerVehicle"));
2729
}
2830

31+
@Test
32+
public void testLoginStart() {
33+
// This packet is critical for handleLoin
34+
assertEquals(PacketLoginInStart.class, PacketType.Login.Client.START.getPacketClass());
35+
}
36+
2937
@Test
3038
public void ensureAllExist() {
3139
boolean missing = false;

modules/ProtocolLib/src/test/java/com/comphenix/protocol/utility/MinecraftReflectionTest.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,6 @@
44
import static org.mockito.Mockito.mock;
55
import static org.mockito.Mockito.times;
66
import static org.mockito.Mockito.verify;
7-
import net.minecraft.server.v1_9_R2.ChatComponentText;
8-
import net.minecraft.server.v1_9_R2.ChunkCoordIntPair;
9-
import net.minecraft.server.v1_9_R2.DataWatcher;
10-
import net.minecraft.server.v1_9_R2.IBlockData;
11-
import net.minecraft.server.v1_9_R2.IChatBaseComponent;
12-
import net.minecraft.server.v1_9_R2.IChatBaseComponent.ChatSerializer;
13-
import net.minecraft.server.v1_9_R2.NBTCompressedStreamTools;
14-
import net.minecraft.server.v1_9_R2.PacketPlayOutUpdateAttributes.AttributeSnapshot;
15-
import net.minecraft.server.v1_9_R2.PlayerConnection;
16-
import net.minecraft.server.v1_9_R2.ServerPing;
17-
import net.minecraft.server.v1_9_R2.ServerPing.ServerData;
18-
import net.minecraft.server.v1_9_R2.ServerPing.ServerPingPlayerSample;
197

208
import org.bukkit.Material;
219
import org.bukkit.block.Block;
@@ -28,6 +16,20 @@
2816
import org.powermock.core.classloader.annotations.PowerMockIgnore;
2917

3018
import com.comphenix.protocol.BukkitInitialization;
19+
import com.mojang.authlib.GameProfile;
20+
21+
import net.minecraft.server.v1_9_R2.ChatComponentText;
22+
import net.minecraft.server.v1_9_R2.ChunkCoordIntPair;
23+
import net.minecraft.server.v1_9_R2.DataWatcher;
24+
import net.minecraft.server.v1_9_R2.IBlockData;
25+
import net.minecraft.server.v1_9_R2.IChatBaseComponent;
26+
import net.minecraft.server.v1_9_R2.IChatBaseComponent.ChatSerializer;
27+
import net.minecraft.server.v1_9_R2.NBTCompressedStreamTools;
28+
import net.minecraft.server.v1_9_R2.PacketPlayOutUpdateAttributes.AttributeSnapshot;
29+
import net.minecraft.server.v1_9_R2.PlayerConnection;
30+
import net.minecraft.server.v1_9_R2.ServerPing;
31+
import net.minecraft.server.v1_9_R2.ServerPing.ServerData;
32+
import net.minecraft.server.v1_9_R2.ServerPing.ServerPingPlayerSample;
3133

3234
@RunWith(org.powermock.modules.junit4.PowerMockRunner.class)
3335
@PowerMockIgnore({ "org.apache.log4j.*", "org.apache.logging.*", "org.bukkit.craftbukkit.libs.jline.*" })
@@ -136,4 +138,9 @@ public void testItemStacks() {
136138
Object nmsStack = MinecraftReflection.getMinecraftItemStack(stack);
137139
assertEquals(stack, MinecraftReflection.getBukkitItemStack(nmsStack));
138140
}
141+
142+
@Test
143+
public void testGameProfile() {
144+
assertEquals(GameProfile.class, MinecraftReflection.getGameProfileClass());
145+
}
139146
}

0 commit comments

Comments
 (0)