3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-03 14:50:30 +01:00

Merge pull request #1330 from kashike/cleanup

Reduce duplication in various classes
Dieser Commit ist enthalten in:
Myles 2019-05-15 22:24:27 +01:00 committet von GitHub
Commit bdd32688fa
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23
14 geänderte Dateien mit 138 neuen und 259 gelöschten Zeilen

Datei anzeigen

@ -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<Metadata> getType();
@Override
public List<Metadata> read(final ByteBuf buffer) throws Exception {
final Type<Metadata> type = this.getType();
final List<Metadata> 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<Metadata> object) throws Exception {
final Type<Metadata> type = this.getType();
for (final Metadata metadata : object) {
type.write(buffer, metadata);
}
this.writeEnd(type, buffer);
}
protected abstract void writeEnd(final Type<Metadata> type, final ByteBuf buffer) throws Exception;
}

Datei anzeigen

@ -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<Metadata> type, final ByteBuf buffer) throws Exception {
type.write(buffer, null);
}
}

Datei anzeigen

@ -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());
}
}
}

Datei anzeigen

@ -1,29 +1,12 @@
package us.myles.ViaVersion.api.type.types.version; package us.myles.ViaVersion.api.type.types.version;
import io.netty.buffer.ByteBuf; import us.myles.ViaVersion.api.minecraft.metadata.MetaType;
import us.myles.ViaVersion.api.minecraft.metadata.Metadata;
import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_12; 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 @Override
public Metadata read(ByteBuf buffer) throws Exception { protected MetaType getType(final int index) {
short index = buffer.readUnsignedByte(); return MetaType1_12.byId(index);
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());
}
} }
} }

Datei anzeigen

@ -1,29 +1,12 @@
package us.myles.ViaVersion.api.type.types.version; package us.myles.ViaVersion.api.type.types.version;
import io.netty.buffer.ByteBuf; import us.myles.ViaVersion.api.minecraft.metadata.MetaType;
import us.myles.ViaVersion.api.minecraft.metadata.Metadata;
import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_13; 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 @Override
public Metadata read(ByteBuf buffer) throws Exception { protected MetaType getType(final int index) {
short index = buffer.readUnsignedByte(); return MetaType1_13.byId(index);
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());
}
} }
} }

Datei anzeigen

@ -1,29 +1,12 @@
package us.myles.ViaVersion.api.type.types.version; package us.myles.ViaVersion.api.type.types.version;
import io.netty.buffer.ByteBuf; import us.myles.ViaVersion.api.minecraft.metadata.MetaType;
import us.myles.ViaVersion.api.minecraft.metadata.Metadata;
import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_13_2; 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 @Override
public Metadata read(ByteBuf buffer) throws Exception { protected MetaType getType(final int index) {
short index = buffer.readUnsignedByte(); return MetaType1_13_2.byId(index);
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());
}
} }
} }

Datei anzeigen

@ -1,29 +1,12 @@
package us.myles.ViaVersion.api.type.types.version; package us.myles.ViaVersion.api.type.types.version;
import io.netty.buffer.ByteBuf; import us.myles.ViaVersion.api.minecraft.metadata.MetaType;
import us.myles.ViaVersion.api.minecraft.metadata.Metadata;
import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_14; 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 @Override
public Metadata read(ByteBuf buffer) throws Exception { protected MetaType getType(final int index) {
short index = buffer.readUnsignedByte(); return MetaType1_14.byId(index);
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));
} }
}
@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());
}
}
}

Datei anzeigen

@ -1,30 +1,12 @@
package us.myles.ViaVersion.api.type.types.version; package us.myles.ViaVersion.api.type.types.version;
import io.netty.buffer.ByteBuf; import us.myles.ViaVersion.api.minecraft.metadata.MetaType;
import us.myles.ViaVersion.api.minecraft.metadata.Metadata;
import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_9; import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_9;
import us.myles.ViaVersion.api.type.types.minecraft.MetaTypeTemplate; import us.myles.ViaVersion.api.type.types.minecraft.ModernMetaType;
public class Metadata1_9Type extends MetaTypeTemplate {
public class Metadata1_9Type extends ModernMetaType {
@Override @Override
public Metadata read(ByteBuf buffer) throws Exception { protected MetaType getType(final int index) {
short index = buffer.readUnsignedByte(); return MetaType1_9.byId(index);
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());
}
} }
} }

Datei anzeigen

@ -1,32 +1,12 @@
package us.myles.ViaVersion.api.type.types.version; 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.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; public class MetadataList1_12Type extends ModernMetaListType {
import java.util.List;
public class MetadataList1_12Type extends MetaListTypeTemplate {
@Override @Override
public List<Metadata> read(ByteBuf buffer) throws Exception { protected Type<Metadata> getType() {
List<Metadata> list = new ArrayList<>(); return Types1_12.METADATA;
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<Metadata> object) throws Exception {
for (Metadata m : object)
Types1_12.METADATA.write(buffer, m);
// Write end of list
Types1_12.METADATA.write(buffer, null);
} }
} }

Datei anzeigen

@ -1,32 +1,12 @@
package us.myles.ViaVersion.api.type.types.version; 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.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; public class MetadataList1_13Type extends ModernMetaListType {
import java.util.List;
public class MetadataList1_13Type extends MetaListTypeTemplate {
@Override @Override
public List<Metadata> read(ByteBuf buffer) throws Exception { protected Type<Metadata> getType() {
List<Metadata> list = new ArrayList<>(); return Types1_13.METADATA;
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<Metadata> object) throws Exception {
for (Metadata m : object)
Types1_13.METADATA.write(buffer, m);
// Write end of list
Types1_13.METADATA.write(buffer, null);
} }
} }

Datei anzeigen

@ -1,32 +1,12 @@
package us.myles.ViaVersion.api.type.types.version; 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.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; public class MetadataList1_13_2Type extends ModernMetaListType {
import java.util.List;
public class MetadataList1_13_2Type extends MetaListTypeTemplate {
@Override @Override
public List<Metadata> read(ByteBuf buffer) throws Exception { protected Type<Metadata> getType() {
List<Metadata> list = new ArrayList<>(); return Types1_13_2.METADATA;
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<Metadata> 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);
} }
} }

Datei anzeigen

@ -1,32 +1,12 @@
package us.myles.ViaVersion.api.type.types.version; 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.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; public class MetadataList1_14Type extends ModernMetaListType {
import java.util.List;
public class MetadataList1_14Type extends MetaListTypeTemplate {
@Override @Override
public List<Metadata> read(ByteBuf buffer) throws Exception { protected Type<Metadata> getType() {
List<Metadata> list = new ArrayList<>(); return Types1_14.METADATA;
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<Metadata> object) throws Exception {
for (Metadata m : object)
Types1_14.METADATA.write(buffer, m);
// Write end of list
Types1_14.METADATA.write(buffer, null);
} }
} }

Datei anzeigen

@ -2,31 +2,17 @@ package us.myles.ViaVersion.api.type.types.version;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import us.myles.ViaVersion.api.minecraft.metadata.Metadata; 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.AbstractMetaListType;
import java.util.ArrayList;
import java.util.List;
public class MetadataList1_8Type extends MetaListTypeTemplate {
public class MetadataList1_8Type extends AbstractMetaListType {
@Override @Override
public List<Metadata> read(ByteBuf buffer) throws Exception { protected Type<Metadata> getType() {
List<Metadata> list = new ArrayList<>(); return Types1_8.METADATA;
Metadata m;
do {
m = Types1_8.METADATA.read(buffer);
if (m != null) {
list.add(m);
}
} while (m != null);
return list;
} }
@Override @Override
public void write(ByteBuf buffer, List<Metadata> object) throws Exception { protected void writeEnd(final Type<Metadata> type, final ByteBuf buffer) throws Exception {
for (Metadata data : object) buffer.writeByte(0x7f);
Types1_8.METADATA.write(buffer, data);
buffer.writeByte(127);
} }
} }

Datei anzeigen

@ -1,33 +1,12 @@
package us.myles.ViaVersion.api.type.types.version; 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.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_9Type extends MetaListTypeTemplate {
public class MetadataList1_9Type extends ModernMetaListType {
@Override @Override
public List<Metadata> read(ByteBuf buffer) throws Exception { protected Type<Metadata> getType() {
List<Metadata> list = new ArrayList<>(); return Types1_9.METADATA;
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<Metadata> object) throws Exception {
for (Metadata m : object)
Types1_9.METADATA.write(buffer, m);
// Write end of list
Types1_9.METADATA.write(buffer, null);
} }
} }