Skip to content

Commit e1715e2

Browse files
committed
Ensure modification count is checked properly
1 parent 1e69a11 commit e1715e2

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

ProtocolLib/src/main/java/com/comphenix/protocol/injector/packet/MapContainer.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.comphenix.protocol.injector.packet;
22

3+
import static com.google.common.base.Preconditions.checkNotNull;
4+
35
import java.lang.reflect.Field;
46

57
import com.comphenix.protocol.reflect.FieldUtils;
@@ -10,17 +12,20 @@
1012
*/
1113
public class MapContainer {
1214
// For detecting changes
13-
private Field modCountField;
15+
private final Field modCountField;
1416
private int lastModCount;
1517

1618
// The object along with whether or not this is the initial run
17-
private Object source;
19+
private final Object source;
1820
private boolean changed;
1921

2022
public MapContainer(Object source) {
2123
this.source = source;
22-
this.changed = true;
23-
this.modCountField = FieldUtils.getField(source.getClass(), "modCount", true);
24+
this.changed = false;
25+
26+
Field modCountField = FieldUtils.getField(source.getClass(), "modCount", true);
27+
this.modCountField = checkNotNull(modCountField, "Failed to obtain modCount field");
28+
this.lastModCount = getModificationCount();
2429
}
2530

2631
/**
@@ -55,13 +60,13 @@ protected void checkChanged() {
5560

5661
/**
5762
* Retrieve the current modification count.
58-
* @return The current count, or something different than lastModCount if not accessible.
63+
* @return The current count
5964
*/
6065
private int getModificationCount() {
6166
try {
62-
return modCountField != null ? modCountField.getInt(source) : lastModCount + 1;
63-
} catch (Exception e) {
64-
throw new RuntimeException("Unable to retrieve modCount.", e);
67+
return modCountField.getInt(source);
68+
} catch (Exception ex) {
69+
throw new RuntimeException("Unable to retrieve modCount.", ex);
6570
}
6671
}
6772
}

0 commit comments

Comments
 (0)