Ensure modification count is checked properly

This commit is contained in:
Dan Mulloy 2016-07-26 20:08:35 -04:00
parent 1e69a116ba
commit e1715e26b3
1 changed files with 13 additions and 8 deletions

View File

@ -1,5 +1,7 @@
package com.comphenix.protocol.injector.packet;
import static com.google.common.base.Preconditions.checkNotNull;
import java.lang.reflect.Field;
import com.comphenix.protocol.reflect.FieldUtils;
@ -10,17 +12,20 @@ import com.comphenix.protocol.reflect.FieldUtils;
*/
public class MapContainer {
// For detecting changes
private Field modCountField;
private final Field modCountField;
private int lastModCount;
// The object along with whether or not this is the initial run
private Object source;
private final Object source;
private boolean changed;
public MapContainer(Object source) {
this.source = source;
this.changed = true;
this.modCountField = FieldUtils.getField(source.getClass(), "modCount", true);
this.changed = false;
Field modCountField = FieldUtils.getField(source.getClass(), "modCount", true);
this.modCountField = checkNotNull(modCountField, "Failed to obtain modCount field");
this.lastModCount = getModificationCount();
}
/**
@ -55,13 +60,13 @@ public class MapContainer {
/**
* Retrieve the current modification count.
* @return The current count, or something different than lastModCount if not accessible.
* @return The current count
*/
private int getModificationCount() {
try {
return modCountField != null ? modCountField.getInt(source) : lastModCount + 1;
} catch (Exception e) {
throw new RuntimeException("Unable to retrieve modCount.", e);
return modCountField.getInt(source);
} catch (Exception ex) {
throw new RuntimeException("Unable to retrieve modCount.", ex);
}
}
}