mirror of
https://github.com/ViaVersion/ViaVersion.git
synced 2024-11-26 03:55:28 +01:00
Abstract 1.8 metadata type (#2720)
This commit is contained in:
parent
ab7cd4955b
commit
a49d73c4c6
@ -64,7 +64,7 @@ public interface ViaAPI<T> {
|
||||
* @return API version incremented with meaningful API changes
|
||||
*/
|
||||
default int apiVersion() {
|
||||
return 8;
|
||||
return 9;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,61 +0,0 @@
|
||||
/*
|
||||
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
|
||||
* Copyright (C) 2016-2021 ViaVersion and contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
package com.viaversion.viaversion.api.type.types.minecraft;
|
||||
|
||||
import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
|
||||
import com.viaversion.viaversion.api.type.Type;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
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;
|
||||
}
|
@ -25,8 +25,12 @@ package com.viaversion.viaversion.api.type.types.minecraft;
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
|
||||
import com.viaversion.viaversion.api.type.Type;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
public final class MetaListType extends ModernMetaListType {
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public final class MetaListType extends MetaListTypeTemplate {
|
||||
private final Type<Metadata> type;
|
||||
|
||||
public MetaListType(Type<Metadata> type) {
|
||||
@ -35,7 +39,23 @@ public final class MetaListType extends ModernMetaListType {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Type<Metadata> getType() {
|
||||
return type;
|
||||
public List<Metadata> read(final ByteBuf buffer) throws Exception {
|
||||
final List<Metadata> list = new ArrayList<>();
|
||||
Metadata meta;
|
||||
do {
|
||||
meta = this.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 {
|
||||
for (final Metadata metadata : object) {
|
||||
this.type.write(buffer, metadata);
|
||||
}
|
||||
this.type.write(buffer, null);
|
||||
}
|
||||
}
|
||||
|
@ -22,14 +22,31 @@
|
||||
*/
|
||||
package com.viaversion.viaversion.api.type.types.minecraft;
|
||||
|
||||
import com.viaversion.viaversion.api.minecraft.metadata.MetaType;
|
||||
import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
|
||||
import com.viaversion.viaversion.api.type.Type;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
public abstract class ModernMetaListType extends AbstractMetaListType {
|
||||
public abstract class OldMetaType extends MetaTypeTemplate {
|
||||
private static final int END = 127;
|
||||
|
||||
@Override
|
||||
protected void writeEnd(final Type<Metadata> type, final ByteBuf buffer) throws Exception {
|
||||
type.write(buffer, null);
|
||||
public Metadata read(final ByteBuf buffer) throws Exception {
|
||||
final byte index = buffer.readByte();
|
||||
if (index == END) return null; // End of metadata
|
||||
final MetaType type = this.getType((index & 224) >> 5);
|
||||
return new Metadata(index & 31, type, type.type().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(END);
|
||||
} else {
|
||||
final int index = (object.metaType().typeId() << 5 | object.id() & 31) & 255;
|
||||
buffer.writeByte(index);
|
||||
object.metaType().type().write(buffer, object.getValue());
|
||||
}
|
||||
}
|
||||
}
|
@ -23,27 +23,13 @@
|
||||
package com.viaversion.viaversion.api.type.types.version;
|
||||
|
||||
|
||||
import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
|
||||
import com.viaversion.viaversion.api.minecraft.metadata.MetaType;
|
||||
import com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_8;
|
||||
import com.viaversion.viaversion.api.type.types.minecraft.MetaTypeTemplate;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
public class Metadata1_8Type extends MetaTypeTemplate {
|
||||
import com.viaversion.viaversion.api.type.types.minecraft.OldMetaType;
|
||||
|
||||
public class Metadata1_8Type extends OldMetaType {
|
||||
@Override
|
||||
public Metadata read(ByteBuf buffer) throws Exception {
|
||||
byte item = buffer.readByte();
|
||||
if (item == 127) return null; // end of metadata
|
||||
int typeID = (item & 0xE0) >> 5;
|
||||
MetaType1_8 type = MetaType1_8.byId(typeID);
|
||||
int id = item & 0x1F;
|
||||
return new Metadata(id, type, type.type().read(buffer));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(ByteBuf buffer, Metadata meta) throws Exception {
|
||||
byte item = (byte) (meta.metaType().typeId() << 5 | meta.id() & 0x1F);
|
||||
buffer.writeByte(item);
|
||||
meta.metaType().type().write(buffer, meta.getValue());
|
||||
protected MetaType getType(int index) {
|
||||
return MetaType1_8.byId(index);
|
||||
}
|
||||
}
|
||||
|
@ -1,40 +0,0 @@
|
||||
/*
|
||||
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
|
||||
* Copyright (C) 2016-2021 ViaVersion and contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
package com.viaversion.viaversion.api.type.types.version;
|
||||
|
||||
import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
|
||||
import com.viaversion.viaversion.api.type.Type;
|
||||
import com.viaversion.viaversion.api.type.types.minecraft.AbstractMetaListType;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
public class MetadataList1_8Type extends AbstractMetaListType {
|
||||
@Override
|
||||
protected Type<Metadata> getType() {
|
||||
return Types1_8.METADATA;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeEnd(final Type<Metadata> type, final ByteBuf buffer) throws Exception {
|
||||
buffer.writeByte(0x7f);
|
||||
}
|
||||
}
|
@ -25,6 +25,7 @@ package com.viaversion.viaversion.api.type.types.version;
|
||||
import com.viaversion.viaversion.api.minecraft.chunks.ChunkSection;
|
||||
import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
|
||||
import com.viaversion.viaversion.api.type.Type;
|
||||
import com.viaversion.viaversion.api.type.types.minecraft.MetaListType;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -37,7 +38,7 @@ public class Types1_8 {
|
||||
/**
|
||||
* Metadata list type for 1.8
|
||||
*/
|
||||
public static final Type<List<Metadata>> METADATA_LIST = new MetadataList1_8Type();
|
||||
public static final Type<List<Metadata>> METADATA_LIST = new MetaListType(METADATA);
|
||||
|
||||
public static final Type<ChunkSection> CHUNK_SECTION = new ChunkSectionType1_8();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user