Skip to content

Commit cc362a1

Browse files
committed
Ensure the modification count is checked correctly
Fixes #202
1 parent 1ca7973 commit cc362a1

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

modules/API/src/main/java/com/comphenix/protocol/injector/netty/NettyProtocolRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public NettyProtocolRegistry() {
4040

4141
@Override
4242
protected synchronized void initialize() {
43-
ProtocolLogger.debug("NettyProtocolRegistry#initialize()"); // Debug for issue #202
43+
ProtocolLogger.debug("Initializing the Netty protocol registry"); // Debug for issue #202
4444

4545
Object[] protocols = enumProtocol.getEnumConstants();
4646

modules/API/src/main/java/com/comphenix/protocol/injector/netty/ProtocolRegistry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* Represents a way of accessing the new netty Protocol enum.
2020
* @author Kristian
2121
*/
22-
// TODO: Handle modifications to the BiMap
22+
2323
public abstract class ProtocolRegistry {
2424
/**
2525
* Represents a register we are currently building.
@@ -97,7 +97,7 @@ public Set<PacketType> getServerPackets() {
9797
* This operation may block the calling thread.
9898
*/
9999
public synchronized void synchronize() {
100-
// See if the register is outdated
100+
// Check if the packet registry has changed
101101
if (register.isOutdated()) {
102102
initialize();
103103
}

modules/API/src/main/java/com/comphenix/protocol/injector/packet/MapContainer.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,28 @@
33
import java.lang.reflect.Field;
44

55
import com.comphenix.protocol.reflect.FieldUtils;
6+
import static com.google.common.base.Preconditions.checkNotNull;
67

78
/**
89
* Represents a class that can detect if a map has changed.
910
* @author Kristian
1011
*/
1112
public class MapContainer {
1213
// For detecting changes
13-
private Field modCountField;
14+
private final Field modCountField;
1415
private int lastModCount;
1516

1617
// The object along with whether or not this is the initial run
17-
private Object source;
18+
private final Object source;
1819
private boolean changed;
1920

2021
public MapContainer(Object source) {
2122
this.source = source;
22-
this.changed = true;
23-
this.modCountField = FieldUtils.getField(source.getClass(), "modCount", true);
23+
this.changed = false;
24+
25+
Field modCountField = FieldUtils.getField(source.getClass(), "modCount", true);
26+
this.modCountField = checkNotNull(modCountField, "Could not obtain modCount field");
27+
this.lastModCount = getModificationCount();
2428
}
2529

2630
/**
@@ -55,13 +59,13 @@ protected void checkChanged() {
5559

5660
/**
5761
* Retrieve the current modification count.
58-
* @return The current count, or something different than lastModCount if not accessible.
62+
* @return The current count
5963
*/
6064
private int getModificationCount() {
6165
try {
62-
return modCountField != null ? modCountField.getInt(source) : lastModCount + 1;
63-
} catch (Exception e) {
64-
throw new RuntimeException("Unable to retrieve modCount.", e);
66+
return modCountField.getInt(source);
67+
} catch (ReflectiveOperationException ex) {
68+
throw new RuntimeException("Unable to retrieve modCount.", ex);
6569
}
6670
}
6771
}

0 commit comments

Comments
 (0)