Add a getMinecraftVersion() method to the ProtocolManager.

This can be useful if you want to implement different read/write 
methods for each version of Minecraft.
This commit is contained in:
Kristian S. Stangeland 2013-07-19 21:22:34 +02:00
parent 458a92c34a
commit 0b56df20d5
5 changed files with 46 additions and 16 deletions

View File

@ -32,6 +32,7 @@ import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketListener; import com.comphenix.protocol.events.PacketListener;
import com.comphenix.protocol.injector.PacketConstructor; import com.comphenix.protocol.injector.PacketConstructor;
import com.comphenix.protocol.reflect.FieldAccessException; import com.comphenix.protocol.reflect.FieldAccessException;
import com.comphenix.protocol.utility.MinecraftVersion;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
/** /**
@ -169,6 +170,12 @@ public interface ProtocolManager extends PacketStream {
*/ */
public Set<Integer> getReceivingFilters(); public Set<Integer> getReceivingFilters();
/**
* Retrieve the current Minecraft version.
* @return The current version.
*/
public MinecraftVersion getMinecraftVersion();
/** /**
* Determines whether or not this protocol mananger has been disabled. * Determines whether or not this protocol mananger has been disabled.
* @return TRUE if it has, FALSE otherwise. * @return TRUE if it has, FALSE otherwise.

View File

@ -24,6 +24,7 @@ import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketListener; import com.comphenix.protocol.events.PacketListener;
import com.comphenix.protocol.injector.PacketFilterManager.PlayerInjectHooks; import com.comphenix.protocol.injector.PacketFilterManager.PlayerInjectHooks;
import com.comphenix.protocol.reflect.FieldAccessException; import com.comphenix.protocol.reflect.FieldAccessException;
import com.comphenix.protocol.utility.MinecraftVersion;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
@ -123,10 +124,14 @@ public class DelayedPacketManager implements ProtocolManager, InternalManager {
private PluginManager queuedManager; private PluginManager queuedManager;
private Plugin queuedPlugin; private Plugin queuedPlugin;
public DelayedPacketManager(@Nonnull ErrorReporter reporter) { private MinecraftVersion version;
public DelayedPacketManager(@Nonnull ErrorReporter reporter, @Nonnull MinecraftVersion version) {
Preconditions.checkNotNull(reporter, "reporter cannot be NULL."); Preconditions.checkNotNull(reporter, "reporter cannot be NULL.");
Preconditions.checkNotNull(version, "version cannot be NULL.");
this.reporter = reporter; this.reporter = reporter;
this.version = version;
} }
/** /**
@ -137,6 +142,14 @@ public class DelayedPacketManager implements ProtocolManager, InternalManager {
return delegate; return delegate;
} }
@Override
public MinecraftVersion getMinecraftVersion() {
if (delegate != null)
return delegate.getMinecraftVersion();
else
return version;
}
/** /**
* Update the delegate to the underlying manager. * Update the delegate to the underlying manager.
* <p> * <p>

View File

@ -190,7 +190,7 @@ public class PacketFilterBuilder {
// If the server hasn't loaded yet - wait // If the server hasn't loaded yet - wait
if (InjectedServerConnection.getServerConnection(reporter, server) == null) { if (InjectedServerConnection.getServerConnection(reporter, server) == null) {
// We need to delay this until we know if Netty is enabled // We need to delay this until we know if Netty is enabled
final DelayedPacketManager delayed = new DelayedPacketManager(reporter); final DelayedPacketManager delayed = new DelayedPacketManager(reporter, mcVersion);
// They must reference each other // They must reference each other
delayed.setAsynchronousManager(asyncManager); delayed.setAsynchronousManager(asyncManager);

View File

@ -66,6 +66,7 @@ import com.comphenix.protocol.injector.spigot.SpigotPacketInjector;
import com.comphenix.protocol.reflect.FieldAccessException; import com.comphenix.protocol.reflect.FieldAccessException;
import com.comphenix.protocol.reflect.FuzzyReflection; import com.comphenix.protocol.reflect.FuzzyReflection;
import com.comphenix.protocol.utility.MinecraftReflection; import com.comphenix.protocol.utility.MinecraftReflection;
import com.comphenix.protocol.utility.MinecraftVersion;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
@ -179,6 +180,9 @@ public final class PacketFilterManager implements ProtocolManager, ListenerInvok
// Plugin verifier // Plugin verifier
private PluginVerifier pluginVerifier; private PluginVerifier pluginVerifier;
// The current Minecraft version
private MinecraftVersion minecraftVersion;
/** /**
* Only create instances of this class if protocol lib is disabled. * Only create instances of this class if protocol lib is disabled.
*/ */
@ -210,9 +214,10 @@ public final class PacketFilterManager implements ProtocolManager, ListenerInvok
// The plugin verifier // The plugin verifier
this.pluginVerifier = new PluginVerifier(builder.getLibrary()); this.pluginVerifier = new PluginVerifier(builder.getLibrary());
this.minecraftVersion = builder.getMinecraftVersion();
// The write packet interceptor // The write packet interceptor
interceptWritePacket = new InterceptWritePacket(classLoader, reporter); this.interceptWritePacket = new InterceptWritePacket(classLoader, reporter);
// Use the correct injection type // Use the correct injection type
if (builder.isNettyEnabled()) { if (builder.isNettyEnabled()) {
@ -267,6 +272,11 @@ public final class PacketFilterManager implements ProtocolManager, ListenerInvok
return new PacketFilterBuilder(); return new PacketFilterBuilder();
} }
@Override
public MinecraftVersion getMinecraftVersion() {
return minecraftVersion;
}
@Override @Override
public AsynchronousManager getAsynchronousManager() { public AsynchronousManager getAsynchronousManager() {
return asyncFilterManager; return asyncFilterManager;

View File

@ -141,10 +141,10 @@ public class MinecraftVersion implements Comparable<MinecraftVersion> {
* @return A normal version string. * @return A normal version string.
*/ */
public String getVersion() { public String getVersion() {
if (development == null) if (getDevelopmentStage() == null)
return String.format("%s.%s.%s", major, minor, build); return String.format("%s.%s.%s", getMajor(), getMinor(), getBuild());
else else
return String.format("%s.%s.%s-%s", major, minor, build, development); return String.format("%s.%s.%s-%s", getMajor(), getMinor(), getBuild(), getDevelopmentStage());
} }
@Override @Override
@ -153,11 +153,11 @@ public class MinecraftVersion implements Comparable<MinecraftVersion> {
return 1; return 1;
return ComparisonChain.start(). return ComparisonChain.start().
compare(major, o.major). compare(getMajor(), o.getMajor()).
compare(minor, o.minor). compare(getMinor(), o.getMinor()).
compare(build, o.build). compare(getBuild(), o.getBuild()).
// No development String means it's a release // No development String means it's a release
compare(development, o.development, Ordering.natural().nullsLast()). compare(getDevelopmentStage(), o.getDevelopmentStage(), Ordering.natural().nullsLast()).
result(); result();
} }
@ -171,10 +171,10 @@ public class MinecraftVersion implements Comparable<MinecraftVersion> {
if (obj instanceof MinecraftVersion) { if (obj instanceof MinecraftVersion) {
MinecraftVersion other = (MinecraftVersion) obj; MinecraftVersion other = (MinecraftVersion) obj;
return major == other.major && return getMajor() == other.getMajor() &&
minor == other.minor && getMinor() == other.getMinor() &&
build == other.build && getBuild() == other.getBuild() &&
Objects.equal(development, other.development); Objects.equal(getDevelopmentStage(), other.getDevelopmentStage());
} }
return false; return false;
@ -182,7 +182,7 @@ public class MinecraftVersion implements Comparable<MinecraftVersion> {
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hashCode(major, minor, build); return Objects.hashCode(getMajor(), getMinor(), getBuild());
} }
@Override @Override