From 34c009e4e5186d4ed657375fcda2f0cb0e57d067 Mon Sep 17 00:00:00 2001 From: kashike Date: Sun, 12 May 2019 00:59:46 -0700 Subject: [PATCH] Reduce duplication in various classes --- .../types/minecraft/AbstractMetaListType.java | 39 +++++++++++++++++++ .../types/minecraft/ModernMetaListType.java | 12 ++++++ .../type/types/minecraft/ModernMetaType.java | 29 ++++++++++++++ .../type/types/version/Metadata1_12Type.java | 27 +++---------- .../type/types/version/Metadata1_13Type.java | 27 +++---------- .../types/version/Metadata1_13_2Type.java | 27 +++---------- .../type/types/version/Metadata1_14Type.java | 29 +++----------- .../type/types/version/Metadata1_9Type.java | 28 +++---------- .../types/version/MetadataList1_12Type.java | 30 +++----------- .../types/version/MetadataList1_13Type.java | 30 +++----------- .../types/version/MetadataList1_13_2Type.java | 30 +++----------- .../types/version/MetadataList1_14Type.java | 30 +++----------- .../types/version/MetadataList1_8Type.java | 28 ++++--------- .../types/version/MetadataList1_9Type.java | 31 +++------------ 14 files changed, 138 insertions(+), 259 deletions(-) create mode 100644 common/src/main/java/us/myles/ViaVersion/api/type/types/minecraft/AbstractMetaListType.java create mode 100644 common/src/main/java/us/myles/ViaVersion/api/type/types/minecraft/ModernMetaListType.java create mode 100644 common/src/main/java/us/myles/ViaVersion/api/type/types/minecraft/ModernMetaType.java diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/minecraft/AbstractMetaListType.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/minecraft/AbstractMetaListType.java new file mode 100644 index 000000000..deb2afc86 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/api/type/types/minecraft/AbstractMetaListType.java @@ -0,0 +1,39 @@ +package us.myles.ViaVersion.api.type.types.minecraft; + +import io.netty.buffer.ByteBuf; +import us.myles.ViaVersion.api.minecraft.metadata.Metadata; +import us.myles.ViaVersion.api.type.Type; + +import java.util.ArrayList; +import java.util.List; + +public abstract class AbstractMetaListType extends MetaListTypeTemplate { + protected abstract Type getType(); + + @Override + public List read(final ByteBuf buffer) throws Exception { + final Type type = this.getType(); + final List list = new ArrayList<>(); + Metadata meta; + do { + meta = type.read(buffer); + if (meta != null) { + list.add(meta); + } + } while (meta != null); + return list; + } + + @Override + public void write(final ByteBuf buffer, final List object) throws Exception { + final Type type = this.getType(); + + for (final Metadata metadata : object) { + type.write(buffer, metadata); + } + + this.writeEnd(type, buffer); + } + + protected abstract void writeEnd(final Type type, final ByteBuf buffer) throws Exception; +} diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/minecraft/ModernMetaListType.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/minecraft/ModernMetaListType.java new file mode 100644 index 000000000..cd8c0b17b --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/api/type/types/minecraft/ModernMetaListType.java @@ -0,0 +1,12 @@ +package us.myles.ViaVersion.api.type.types.minecraft; + +import io.netty.buffer.ByteBuf; +import us.myles.ViaVersion.api.minecraft.metadata.Metadata; +import us.myles.ViaVersion.api.type.Type; + +public abstract class ModernMetaListType extends AbstractMetaListType { + @Override + protected void writeEnd(final Type type, final ByteBuf buffer) throws Exception { + type.write(buffer, null); + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/minecraft/ModernMetaType.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/minecraft/ModernMetaType.java new file mode 100644 index 000000000..7d08e4a6f --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/api/type/types/minecraft/ModernMetaType.java @@ -0,0 +1,29 @@ +package us.myles.ViaVersion.api.type.types.minecraft; + +import io.netty.buffer.ByteBuf; +import us.myles.ViaVersion.api.minecraft.metadata.MetaType; +import us.myles.ViaVersion.api.minecraft.metadata.Metadata; + +public abstract class ModernMetaType extends MetaTypeTemplate { + @Override + public Metadata read(final ByteBuf buffer) throws Exception { + final short index = buffer.readUnsignedByte(); + if (index == 0xff) return null; // End of metadata + final MetaType type = this.getType(buffer.readByte()); + return new Metadata(index, type, type.getType().read(buffer)); + } + + protected abstract MetaType getType(final int index); + + @Override + public void write(final ByteBuf buffer, final Metadata object) throws Exception { + if (object == null) { + buffer.writeByte(0xff); + } else { + buffer.writeByte(object.getId()); + final MetaType type = object.getMetaType(); + buffer.writeByte(type.getTypeID()); + type.getType().write(buffer, object.getValue()); + } + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/Metadata1_12Type.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/Metadata1_12Type.java index e4890a02d..397cd1680 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/Metadata1_12Type.java +++ b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/Metadata1_12Type.java @@ -1,29 +1,12 @@ package us.myles.ViaVersion.api.type.types.version; -import io.netty.buffer.ByteBuf; -import us.myles.ViaVersion.api.minecraft.metadata.Metadata; +import us.myles.ViaVersion.api.minecraft.metadata.MetaType; import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_12; -import us.myles.ViaVersion.api.type.types.minecraft.MetaTypeTemplate; +import us.myles.ViaVersion.api.type.types.minecraft.ModernMetaType; -public class Metadata1_12Type extends MetaTypeTemplate { +public class Metadata1_12Type extends ModernMetaType { @Override - public Metadata read(ByteBuf buffer) throws Exception { - short index = buffer.readUnsignedByte(); - - if (index == 0xff) return null; //End of metadata - MetaType1_12 type = MetaType1_12.byId(buffer.readByte()); - - return new Metadata(index, type, type.getType().read(buffer)); - } - - @Override - public void write(ByteBuf buffer, Metadata object) throws Exception { - if (object == null) { - buffer.writeByte(255); - } else { - buffer.writeByte(object.getId()); - buffer.writeByte(object.getMetaType().getTypeID()); - object.getMetaType().getType().write(buffer, object.getValue()); - } + protected MetaType getType(final int index) { + return MetaType1_12.byId(index); } } diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/Metadata1_13Type.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/Metadata1_13Type.java index 4f76773d6..5e8e0ea49 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/Metadata1_13Type.java +++ b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/Metadata1_13Type.java @@ -1,29 +1,12 @@ package us.myles.ViaVersion.api.type.types.version; -import io.netty.buffer.ByteBuf; -import us.myles.ViaVersion.api.minecraft.metadata.Metadata; +import us.myles.ViaVersion.api.minecraft.metadata.MetaType; import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_13; -import us.myles.ViaVersion.api.type.types.minecraft.MetaTypeTemplate; +import us.myles.ViaVersion.api.type.types.minecraft.ModernMetaType; -public class Metadata1_13Type extends MetaTypeTemplate { +public class Metadata1_13Type extends ModernMetaType { @Override - public Metadata read(ByteBuf buffer) throws Exception { - short index = buffer.readUnsignedByte(); - - if (index == 0xff) return null; //End of metadata - MetaType1_13 type = MetaType1_13.byId(buffer.readByte()); - - return new Metadata(index, type, type.getType().read(buffer)); - } - - @Override - public void write(ByteBuf buffer, Metadata object) throws Exception { - if (object == null) { - buffer.writeByte(255); - } else { - buffer.writeByte(object.getId()); - buffer.writeByte(object.getMetaType().getTypeID()); - object.getMetaType().getType().write(buffer, object.getValue()); - } + protected MetaType getType(final int index) { + return MetaType1_13.byId(index); } } diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/Metadata1_13_2Type.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/Metadata1_13_2Type.java index 468ec6b00..c4a14fb9c 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/Metadata1_13_2Type.java +++ b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/Metadata1_13_2Type.java @@ -1,29 +1,12 @@ package us.myles.ViaVersion.api.type.types.version; -import io.netty.buffer.ByteBuf; -import us.myles.ViaVersion.api.minecraft.metadata.Metadata; +import us.myles.ViaVersion.api.minecraft.metadata.MetaType; import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_13_2; -import us.myles.ViaVersion.api.type.types.minecraft.MetaTypeTemplate; +import us.myles.ViaVersion.api.type.types.minecraft.ModernMetaType; -public class Metadata1_13_2Type extends MetaTypeTemplate { +public class Metadata1_13_2Type extends ModernMetaType { @Override - public Metadata read(ByteBuf buffer) throws Exception { - short index = buffer.readUnsignedByte(); - - if (index == 0xff) return null; //End of metadata - MetaType1_13_2 type = MetaType1_13_2.byId(buffer.readByte()); - - return new Metadata(index, type, type.getType().read(buffer)); - } - - @Override - public void write(ByteBuf buffer, Metadata object) throws Exception { - if (object == null) { - buffer.writeByte(255); - } else { - buffer.writeByte(object.getId()); - buffer.writeByte(object.getMetaType().getTypeID()); - object.getMetaType().getType().write(buffer, object.getValue()); - } + protected MetaType getType(final int index) { + return MetaType1_13_2.byId(index); } } diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/Metadata1_14Type.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/Metadata1_14Type.java index f39bec3f2..dedcb794c 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/Metadata1_14Type.java +++ b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/Metadata1_14Type.java @@ -1,29 +1,12 @@ package us.myles.ViaVersion.api.type.types.version; -import io.netty.buffer.ByteBuf; -import us.myles.ViaVersion.api.minecraft.metadata.Metadata; +import us.myles.ViaVersion.api.minecraft.metadata.MetaType; import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_14; -import us.myles.ViaVersion.api.type.types.minecraft.MetaTypeTemplate; +import us.myles.ViaVersion.api.type.types.minecraft.ModernMetaType; -public class Metadata1_14Type extends MetaTypeTemplate { +public class Metadata1_14Type extends ModernMetaType { @Override - public Metadata read(ByteBuf buffer) throws Exception { - short index = buffer.readUnsignedByte(); - - if (index == 0xff) return null; //End of metadata - MetaType1_14 type = MetaType1_14.byId(buffer.readByte()); - - return new Metadata(index, type, type.getType().read(buffer)); + protected MetaType getType(final int index) { + return MetaType1_14.byId(index); } - - @Override - public void write(ByteBuf buffer, Metadata object) throws Exception { - if (object == null) { - buffer.writeByte(255); - } else { - buffer.writeByte(object.getId()); - buffer.writeByte(object.getMetaType().getTypeID()); - object.getMetaType().getType().write(buffer, object.getValue()); - } - } -} \ No newline at end of file +} diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/Metadata1_9Type.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/Metadata1_9Type.java index 7d87f7931..03ac5affd 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/Metadata1_9Type.java +++ b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/Metadata1_9Type.java @@ -1,30 +1,12 @@ package us.myles.ViaVersion.api.type.types.version; -import io.netty.buffer.ByteBuf; -import us.myles.ViaVersion.api.minecraft.metadata.Metadata; +import us.myles.ViaVersion.api.minecraft.metadata.MetaType; import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_9; -import us.myles.ViaVersion.api.type.types.minecraft.MetaTypeTemplate; - -public class Metadata1_9Type extends MetaTypeTemplate { +import us.myles.ViaVersion.api.type.types.minecraft.ModernMetaType; +public class Metadata1_9Type extends ModernMetaType { @Override - public Metadata read(ByteBuf buffer) throws Exception { - short index = buffer.readUnsignedByte(); - - if (index == 0xff) return null; //End of metadata - MetaType1_9 type = MetaType1_9.byId(buffer.readByte()); - - return new Metadata(index, type, type.getType().read(buffer)); - } - - @Override - public void write(ByteBuf buffer, Metadata object) throws Exception { - if (object == null) { - buffer.writeByte(255); - } else { - buffer.writeByte(object.getId()); - buffer.writeByte(object.getMetaType().getTypeID()); - object.getMetaType().getType().write(buffer, object.getValue()); - } + protected MetaType getType(final int index) { + return MetaType1_9.byId(index); } } diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/MetadataList1_12Type.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/MetadataList1_12Type.java index 1ae3d5daf..496c385d2 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/MetadataList1_12Type.java +++ b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/MetadataList1_12Type.java @@ -1,32 +1,12 @@ package us.myles.ViaVersion.api.type.types.version; -import io.netty.buffer.ByteBuf; import us.myles.ViaVersion.api.minecraft.metadata.Metadata; -import us.myles.ViaVersion.api.type.types.minecraft.MetaListTypeTemplate; +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.api.type.types.minecraft.ModernMetaListType; -import java.util.ArrayList; -import java.util.List; - -public class MetadataList1_12Type extends MetaListTypeTemplate { +public class MetadataList1_12Type extends ModernMetaListType { @Override - public List read(ByteBuf buffer) throws Exception { - List list = new ArrayList<>(); - Metadata meta; - do { - meta = Types1_12.METADATA.read(buffer); - if (meta != null) - list.add(meta); - } while (meta != null); - - return list; - } - - @Override - public void write(ByteBuf buffer, List object) throws Exception { - for (Metadata m : object) - Types1_12.METADATA.write(buffer, m); - - // Write end of list - Types1_12.METADATA.write(buffer, null); + protected Type getType() { + return Types1_12.METADATA; } } diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/MetadataList1_13Type.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/MetadataList1_13Type.java index 2cf75514c..59c09bc99 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/MetadataList1_13Type.java +++ b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/MetadataList1_13Type.java @@ -1,32 +1,12 @@ package us.myles.ViaVersion.api.type.types.version; -import io.netty.buffer.ByteBuf; import us.myles.ViaVersion.api.minecraft.metadata.Metadata; -import us.myles.ViaVersion.api.type.types.minecraft.MetaListTypeTemplate; +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.api.type.types.minecraft.ModernMetaListType; -import java.util.ArrayList; -import java.util.List; - -public class MetadataList1_13Type extends MetaListTypeTemplate { +public class MetadataList1_13Type extends ModernMetaListType { @Override - public List read(ByteBuf buffer) throws Exception { - List list = new ArrayList<>(); - Metadata meta; - do { - meta = Types1_13.METADATA.read(buffer); - if (meta != null) - list.add(meta); - } while (meta != null); - - return list; - } - - @Override - public void write(ByteBuf buffer, List object) throws Exception { - for (Metadata m : object) - Types1_13.METADATA.write(buffer, m); - - // Write end of list - Types1_13.METADATA.write(buffer, null); + protected Type getType() { + return Types1_13.METADATA; } } diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/MetadataList1_13_2Type.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/MetadataList1_13_2Type.java index 9f38c0936..d72b0edb8 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/MetadataList1_13_2Type.java +++ b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/MetadataList1_13_2Type.java @@ -1,32 +1,12 @@ package us.myles.ViaVersion.api.type.types.version; -import io.netty.buffer.ByteBuf; import us.myles.ViaVersion.api.minecraft.metadata.Metadata; -import us.myles.ViaVersion.api.type.types.minecraft.MetaListTypeTemplate; +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.api.type.types.minecraft.ModernMetaListType; -import java.util.ArrayList; -import java.util.List; - -public class MetadataList1_13_2Type extends MetaListTypeTemplate { +public class MetadataList1_13_2Type extends ModernMetaListType { @Override - public List read(ByteBuf buffer) throws Exception { - List list = new ArrayList<>(); - Metadata meta; - do { - meta = Types1_13_2.METADATA.read(buffer); - if (meta != null) - list.add(meta); - } while (meta != null); - - return list; - } - - @Override - public void write(ByteBuf buffer, List object) throws Exception { - for (Metadata m : object) - Types1_13_2.METADATA.write(buffer, m); - - // Write end of list - Types1_13_2.METADATA.write(buffer, null); + protected Type getType() { + return Types1_13_2.METADATA; } } diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/MetadataList1_14Type.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/MetadataList1_14Type.java index 154e07b62..376063e51 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/MetadataList1_14Type.java +++ b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/MetadataList1_14Type.java @@ -1,32 +1,12 @@ package us.myles.ViaVersion.api.type.types.version; -import io.netty.buffer.ByteBuf; import us.myles.ViaVersion.api.minecraft.metadata.Metadata; -import us.myles.ViaVersion.api.type.types.minecraft.MetaListTypeTemplate; +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.api.type.types.minecraft.ModernMetaListType; -import java.util.ArrayList; -import java.util.List; - -public class MetadataList1_14Type extends MetaListTypeTemplate { +public class MetadataList1_14Type extends ModernMetaListType { @Override - public List read(ByteBuf buffer) throws Exception { - List list = new ArrayList<>(); - Metadata meta; - do { - meta = Types1_14.METADATA.read(buffer); - if (meta != null) - list.add(meta); - } while (meta != null); - - return list; - } - - @Override - public void write(ByteBuf buffer, List object) throws Exception { - for (Metadata m : object) - Types1_14.METADATA.write(buffer, m); - - // Write end of list - Types1_14.METADATA.write(buffer, null); + protected Type getType() { + return Types1_14.METADATA; } } diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/MetadataList1_8Type.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/MetadataList1_8Type.java index 5e8c6e5a1..540d45cdd 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/MetadataList1_8Type.java +++ b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/MetadataList1_8Type.java @@ -2,31 +2,17 @@ package us.myles.ViaVersion.api.type.types.version; import io.netty.buffer.ByteBuf; import us.myles.ViaVersion.api.minecraft.metadata.Metadata; -import us.myles.ViaVersion.api.type.types.minecraft.MetaListTypeTemplate; - -import java.util.ArrayList; -import java.util.List; - -public class MetadataList1_8Type extends MetaListTypeTemplate { +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.api.type.types.minecraft.AbstractMetaListType; +public class MetadataList1_8Type extends AbstractMetaListType { @Override - public List read(ByteBuf buffer) throws Exception { - List list = new ArrayList<>(); - Metadata m; - do { - m = Types1_8.METADATA.read(buffer); - if (m != null) { - list.add(m); - } - } while (m != null); - - return list; + protected Type getType() { + return Types1_8.METADATA; } @Override - public void write(ByteBuf buffer, List object) throws Exception { - for (Metadata data : object) - Types1_8.METADATA.write(buffer, data); - buffer.writeByte(127); + protected void writeEnd(final Type type, final ByteBuf buffer) throws Exception { + buffer.writeByte(0x7f); } } diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/MetadataList1_9Type.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/MetadataList1_9Type.java index 665e62fa6..b12e848e9 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/MetadataList1_9Type.java +++ b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/MetadataList1_9Type.java @@ -1,33 +1,12 @@ package us.myles.ViaVersion.api.type.types.version; -import io.netty.buffer.ByteBuf; import us.myles.ViaVersion.api.minecraft.metadata.Metadata; -import us.myles.ViaVersion.api.type.types.minecraft.MetaListTypeTemplate; - -import java.util.ArrayList; -import java.util.List; - -public class MetadataList1_9Type extends MetaListTypeTemplate { +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.api.type.types.minecraft.ModernMetaListType; +public class MetadataList1_9Type extends ModernMetaListType { @Override - public List read(ByteBuf buffer) throws Exception { - List list = new ArrayList<>(); - Metadata meta; - do { - meta = Types1_9.METADATA.read(buffer); - if (meta != null) - list.add(meta); - } while (meta != null); - - return list; - } - - @Override - public void write(ByteBuf buffer, List object) throws Exception { - for (Metadata m : object) - Types1_9.METADATA.write(buffer, m); - - // Write end of list - Types1_9.METADATA.write(buffer, null); + protected Type getType() { + return Types1_9.METADATA; } }