Skip to content

Commit 588f736

Browse files
committed
Fix missing TileEntity write methods
Fixes #249
1 parent 8283021 commit 588f736

File tree

3 files changed

+55
-25
lines changed

3 files changed

+55
-25
lines changed

modules/API/src/main/java/com/comphenix/protocol/wrappers/nbt/TileEntityAccessor.java

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class TileEntityAccessor<T extends BlockState> {
4646
private boolean writeDetected;
4747
private boolean readDetected;
4848

49-
private TileEntityAccessor() {
49+
TileEntityAccessor() {
5050
// Do nothing
5151
}
5252

@@ -59,25 +59,28 @@ private TileEntityAccessor(FieldAccessor tileEntityField, T state) {
5959
if (tileEntityField != null) {
6060
this.tileEntityField = tileEntityField;
6161
Class<?> type = tileEntityField.getField().getType();
62+
findMethods(type, state);
63+
}
64+
}
6265

63-
// Possible read/write methods
66+
void findMethods(Class<?> type, T state) {
67+
// Possible read/write methods
68+
try {
69+
findMethodsUsingASM();
70+
} catch (IOException ex1) {
6471
try {
65-
findMethodsUsingASM();
66-
} catch (IOException ex1) {
67-
try {
68-
// Much slower though
69-
findMethodUsingCGLib(state);
70-
} catch (Exception ex2) {
71-
throw new RuntimeException("Cannot find read/write methods in " + type, ex2);
72-
}
72+
// Much slower though
73+
findMethodUsingCGLib(state);
74+
} catch (Exception ex2) {
75+
throw new RuntimeException("Cannot find read/write methods in " + type, ex2);
7376
}
74-
75-
// Ensure we found them
76-
if (readCompound == null)
77-
throw new RuntimeException("Unable to find read method in " + type);
78-
if (writeCompound == null)
79-
throw new RuntimeException("Unable to find write method in " + type);
8077
}
78+
79+
// Ensure we found them
80+
if (readCompound == null)
81+
throw new RuntimeException("Unable to find read method in " + type);
82+
if (writeCompound == null)
83+
throw new RuntimeException("Unable to find write method in " + type);
8184
}
8285

8386
/**
@@ -90,25 +93,25 @@ private void findMethodsUsingASM() throws IOException {
9093
final ClassReader reader = new ClassReader(tileEntityClass.getCanonicalName());
9194

9295
final String tagCompoundName = getJarName(MinecraftReflection.getNBTCompoundClass());
93-
final String expectedDesc = "(L" + tagCompoundName + ";)V";
96+
final String expectedDesc = "(L" + tagCompoundName + ";)";
9497

9598
reader.accept(new EmptyClassVisitor() {
9699
@Override
97100
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
98101
final String methodName = name;
99102

100103
// Detect read/write calls to NBTTagCompound
101-
if (expectedDesc.equals(desc)) {
104+
if (desc.startsWith(expectedDesc)) {
102105
return new EmptyMethodVisitor() {
103106
private int readMethods;
104107
private int writeMethods;
105108

106109
@Override
107110
public void visitMethodInsn(int opcode, String owner, String name, String desc) {
108111
// This must be a virtual call on NBTTagCompound that accepts a String
109-
if (opcode == Opcodes.INVOKEVIRTUAL &&
110-
tagCompoundName.equals(owner) &&
111-
desc.startsWith("(Ljava/lang/String")) {
112+
if (opcode == Opcodes.INVOKEVIRTUAL
113+
&& tagCompoundName.equals(owner)
114+
&& desc.startsWith("(Ljava/lang/String")) {
112115

113116
// Is this a write call?
114117
if (desc.endsWith(")V")) {
@@ -126,10 +129,12 @@ public void visitEnd() {
126129
} else if (writeMethods > readMethods) {
127130
writeCompound = Accessors.getMethodAccessor(tileEntityClass, methodName, nbtCompoundClass);
128131
}
132+
129133
super.visitEnd();
130134
}
131135
};
132136
}
137+
133138
return null;
134139
}
135140
}, 0);

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
public class BukkitInitialization {
2626
private static boolean initialized;
27+
private static boolean packaged;
2728

2829
/**
2930
* Initialize Bukkit and ProtocolLib such that we can perfrom unit testing.
@@ -34,6 +35,8 @@ public static void initializeItemMeta() throws IllegalAccessException {
3435
// Denote that we're done
3536
initialized = true;
3637

38+
initializePackage();
39+
3740
DispenserRegistry.c(); // Basically registers everything
3841

3942
// Mock the server object
@@ -50,16 +53,19 @@ public static void initializeItemMeta() throws IllegalAccessException {
5053
// Inject this fake server
5154
Bukkit.setServer(mockedServer);
5255

53-
initializePackage();
56+
5457
}
5558
}
5659

5760
/**
5861
* Ensure that package names are correctly set up.
5962
*/
6063
public static void initializePackage() {
61-
// Initialize reflection
62-
MinecraftReflection.setMinecraftPackage(Constants.NMS, Constants.OBC);
63-
MinecraftVersion.setCurrentVersion(MinecraftVersion.COMBAT_UPDATE);
64+
if (!packaged) {
65+
packaged = true;
66+
67+
MinecraftReflection.setMinecraftPackage(Constants.NMS, Constants.OBC);
68+
MinecraftVersion.setCurrentVersion(MinecraftVersion.FROSTBURN_UPDATE);
69+
}
6470
}
6571
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/**
2+
* ProtocolLib - Bukkit server library that allows access to the Minecraft protocol.
3+
* Copyright (C) 2016 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+
*/
117
package com.comphenix.protocol;
218

319
import static org.junit.Assert.assertEquals;
@@ -16,6 +32,9 @@
1632

1733
import net.minecraft.server.v1_10_R1.PacketLoginInStart;
1834

35+
/**
36+
* @author dmulloy2
37+
*/
1938
public class PacketTypeTest {
2039

2140
@BeforeClass

0 commit comments

Comments
 (0)