Ensure the modification count is checked correctly

Fixes #202
This commit is contained in:
Dan Mulloy 2016-05-21 17:44:25 -04:00
parent 1ca7973b77
commit cc362a1b7f
3 changed files with 15 additions and 11 deletions

View File

@ -40,7 +40,7 @@ public class NettyProtocolRegistry extends ProtocolRegistry {
@Override @Override
protected synchronized void initialize() { protected synchronized void initialize() {
ProtocolLogger.debug("NettyProtocolRegistry#initialize()"); // Debug for issue #202 ProtocolLogger.debug("Initializing the Netty protocol registry"); // Debug for issue #202
Object[] protocols = enumProtocol.getEnumConstants(); Object[] protocols = enumProtocol.getEnumConstants();

View File

@ -19,7 +19,7 @@ import com.google.common.collect.Sets;
* Represents a way of accessing the new netty Protocol enum. * Represents a way of accessing the new netty Protocol enum.
* @author Kristian * @author Kristian
*/ */
// TODO: Handle modifications to the BiMap
public abstract class ProtocolRegistry { public abstract class ProtocolRegistry {
/** /**
* Represents a register we are currently building. * Represents a register we are currently building.
@ -97,7 +97,7 @@ public abstract class ProtocolRegistry {
* This operation may block the calling thread. * This operation may block the calling thread.
*/ */
public synchronized void synchronize() { public synchronized void synchronize() {
// See if the register is outdated // Check if the packet registry has changed
if (register.isOutdated()) { if (register.isOutdated()) {
initialize(); initialize();
} }

View File

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