Implement Myles idea to just use templates instead of defining compatibility

This commit is contained in:
Matsv 2016-05-21 21:19:20 +02:00
parent 50a3e03649
commit cdb757eae5
8 changed files with 43 additions and 35 deletions

View File

@ -117,7 +117,7 @@ public class PacketWrapper {
}
} else {
Pair<Type, Object> read = readableObjects.poll();
if (read.getKey().equals(type) || (type.isCompatibleWith(read.getKey()) && type.getOutputClass().equals(read.getKey().getOutputClass()))) {
if (read.getKey().equals(type) || (type.getBaseClass().equals(read.getKey().getBaseClass()) && type.getOutputClass().equals(read.getKey().getOutputClass()))) {
return (T) read.getValue();
} else {
if (type == Type.NOTHING) {

View File

@ -83,16 +83,8 @@ public abstract class Type<T> implements ByteBufReader<T>, ByteBufWriter<T> {
this.compatibilities = new HashSet<>();
}
public void addCompatibility(Class<? extends Type> claz) {
compatibilities.add(claz);
}
public void addCompatibility(Type claz) {
addCompatibility(claz.getClass());
}
public boolean isCompatibleWith(Type claz) {
return compatibilities.contains(claz.getClass());
public Class<? extends Type> getBaseClass() {
return this.getClass();
}
@Override

View File

@ -0,0 +1,17 @@
package us.myles.ViaVersion.api.type.types.minecraft;
import us.myles.ViaVersion.api.minecraft.metadata.Metadata;
import us.myles.ViaVersion.api.type.Type;
import java.util.List;
public abstract class MetaListTypeTemplate extends Type<List<Metadata>> {
public MetaListTypeTemplate() {
super("MetaData List", List.class);
}
@Override
public Class<? extends Type> getBaseClass() {
return MetaListTypeTemplate.class;
}
}

View File

@ -0,0 +1,15 @@
package us.myles.ViaVersion.api.type.types.minecraft;
import us.myles.ViaVersion.api.minecraft.metadata.Metadata;
import us.myles.ViaVersion.api.type.Type;
public abstract class MetaTypeTemplate extends Type<Metadata> {
public MetaTypeTemplate() {
super("Metadata type", Metadata.class);
}
@Override
public Class<? extends Type> getBaseClass() {
return MetaTypeTemplate.class;
}
}

View File

@ -2,16 +2,13 @@ package us.myles.ViaVersion.protocols.protocol1_9to1_8.types;
import io.netty.buffer.ByteBuf;
import us.myles.ViaVersion.api.minecraft.metadata.Metadata;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.api.type.types.minecraft.MetaListTypeTemplate;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9TO1_8;
import java.util.ArrayList;
import java.util.List;
public class MetadataListType extends Type<List<Metadata>> {
public MetadataListType() {
super("Metadata List", List.class);
}
public class MetadataListType extends MetaListTypeTemplate {
@Override
public List<Metadata> read(ByteBuf buffer) throws Exception {

View File

@ -3,14 +3,10 @@ package us.myles.ViaVersion.protocols.protocol1_9to1_8.types;
import io.netty.buffer.ByteBuf;
import us.myles.ViaVersion.api.minecraft.metadata.Metadata;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.api.type.types.minecraft.MetaTypeTemplate;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.metadata.MetadataTypes;
public class MetadataType extends Type<Metadata> {
public MetadataType() {
super(Metadata.class);
}
public class MetadataType extends MetaTypeTemplate {
@Override
public Metadata read(ByteBuf buffer) throws Exception {

View File

@ -3,17 +3,13 @@ package us.myles.ViaVersion.protocols.protocolsnapshotto1_9_3;
import io.netty.buffer.ByteBuf;
import us.myles.ViaVersion.api.minecraft.metadata.Metadata;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9TO1_8;
import us.myles.ViaVersion.api.type.types.minecraft.MetaListTypeTemplate;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.metadata.NewType;
import java.util.ArrayList;
import java.util.List;
public class MetaListSnapshotType extends Type<List<Metadata>> {
public MetaListSnapshotType() {
super(List.class);
addCompatibility(Protocol1_9TO1_8.METADATA_LIST);
}
public class MetaListSnapshotType extends MetaListTypeTemplate {
@Override
public List<Metadata> read(ByteBuf buffer) throws Exception {

View File

@ -2,15 +2,10 @@ package us.myles.ViaVersion.protocols.protocolsnapshotto1_9_3;
import io.netty.buffer.ByteBuf;
import us.myles.ViaVersion.api.minecraft.metadata.Metadata;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9TO1_8;
import us.myles.ViaVersion.api.type.types.minecraft.MetaTypeTemplate;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.metadata.NewType;
public class MetaSnapshotType extends Type<Metadata> {
public MetaSnapshotType() {
super(Metadata.class);
addCompatibility(Protocol1_9TO1_8.METADATA);
}
public class MetaSnapshotType extends MetaTypeTemplate {
@Override
public Metadata read(ByteBuf buffer) throws Exception {