Mirror von
https://github.com/ViaVersion/ViaVersion.git
synchronisiert 2024-12-25 15:50:10 +01:00
Merge pull request #1330 from kashike/cleanup
Reduce duplication in various classes
Dieser Commit ist enthalten in:
Commit
bdd32688fa
@ -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;
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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<Metadata> read(ByteBuf buffer) throws Exception {
|
||||
List<Metadata> 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<Metadata> 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<Metadata> getType() {
|
||||
return Types1_12.METADATA;
|
||||
}
|
||||
}
|
||||
|
@ -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<Metadata> read(ByteBuf buffer) throws Exception {
|
||||
List<Metadata> 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<Metadata> 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<Metadata> getType() {
|
||||
return Types1_13.METADATA;
|
||||
}
|
||||
}
|
||||
|
@ -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<Metadata> read(ByteBuf buffer) throws Exception {
|
||||
List<Metadata> 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<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);
|
||||
protected Type<Metadata> getType() {
|
||||
return Types1_13_2.METADATA;
|
||||
}
|
||||
}
|
||||
|
@ -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<Metadata> read(ByteBuf buffer) throws Exception {
|
||||
List<Metadata> 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<Metadata> 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<Metadata> getType() {
|
||||
return Types1_14.METADATA;
|
||||
}
|
||||
}
|
||||
|
@ -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<Metadata> read(ByteBuf buffer) throws Exception {
|
||||
List<Metadata> 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<Metadata> getType() {
|
||||
return Types1_8.METADATA;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(ByteBuf buffer, List<Metadata> object) throws Exception {
|
||||
for (Metadata data : object)
|
||||
Types1_8.METADATA.write(buffer, data);
|
||||
buffer.writeByte(127);
|
||||
protected void writeEnd(final Type<Metadata> type, final ByteBuf buffer) throws Exception {
|
||||
buffer.writeByte(0x7f);
|
||||
}
|
||||
}
|
||||
|
@ -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<Metadata> read(ByteBuf buffer) throws Exception {
|
||||
List<Metadata> 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<Metadata> 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<Metadata> getType() {
|
||||
return Types1_9.METADATA;
|
||||
}
|
||||
}
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren