From a49d73c4c66c912392aaae0cc3fa688455a2eb51 Mon Sep 17 00:00:00 2001 From: RK_01 <50594595+RaphiMC@users.noreply.github.com> Date: Sat, 30 Oct 2021 15:56:24 +0200 Subject: [PATCH] Abstract 1.8 metadata type (#2720) --- .../com/viaversion/viaversion/api/ViaAPI.java | 2 +- .../types/minecraft/AbstractMetaListType.java | 61 ------------------- .../type/types/minecraft/MetaListType.java | 26 +++++++- ...dernMetaListType.java => OldMetaType.java} | 25 ++++++-- .../type/types/version/Metadata1_8Type.java | 24 ++------ .../types/version/MetadataList1_8Type.java | 40 ------------ .../api/type/types/version/Types1_8.java | 3 +- 7 files changed, 52 insertions(+), 129 deletions(-) delete mode 100644 api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/AbstractMetaListType.java rename api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/{ModernMetaListType.java => OldMetaType.java} (60%) delete mode 100644 api/src/main/java/com/viaversion/viaversion/api/type/types/version/MetadataList1_8Type.java diff --git a/api/src/main/java/com/viaversion/viaversion/api/ViaAPI.java b/api/src/main/java/com/viaversion/viaversion/api/ViaAPI.java index 580d4f07f..9c8b3ad52 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/ViaAPI.java +++ b/api/src/main/java/com/viaversion/viaversion/api/ViaAPI.java @@ -64,7 +64,7 @@ public interface ViaAPI { * @return API version incremented with meaningful API changes */ default int apiVersion() { - return 8; + return 9; } /** diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/AbstractMetaListType.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/AbstractMetaListType.java deleted file mode 100644 index 3493df97f..000000000 --- a/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/AbstractMetaListType.java +++ /dev/null @@ -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 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/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/MetaListType.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/MetaListType.java index 7bcd7b020..674f23668 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/MetaListType.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/MetaListType.java @@ -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 type; public MetaListType(Type type) { @@ -35,7 +39,23 @@ public final class MetaListType extends ModernMetaListType { } @Override - protected Type getType() { - return type; + public List read(final ByteBuf buffer) throws Exception { + final List 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 object) throws Exception { + for (final Metadata metadata : object) { + this.type.write(buffer, metadata); + } + this.type.write(buffer, null); } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/ModernMetaListType.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OldMetaType.java similarity index 60% rename from api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/ModernMetaListType.java rename to api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OldMetaType.java index edb03d1b4..45817c794 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/ModernMetaListType.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OldMetaType.java @@ -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 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()); + } } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/version/Metadata1_8Type.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/version/Metadata1_8Type.java index 73edde63a..b0e398cd6 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/types/version/Metadata1_8Type.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/types/version/Metadata1_8Type.java @@ -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); } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/version/MetadataList1_8Type.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/version/MetadataList1_8Type.java deleted file mode 100644 index abe3ae071..000000000 --- a/api/src/main/java/com/viaversion/viaversion/api/type/types/version/MetadataList1_8Type.java +++ /dev/null @@ -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 getType() { - return Types1_8.METADATA; - } - - @Override - protected void writeEnd(final Type type, final ByteBuf buffer) throws Exception { - buffer.writeByte(0x7f); - } -} diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/version/Types1_8.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/version/Types1_8.java index 2dbb57f92..92d16a59e 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/types/version/Types1_8.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/types/version/Types1_8.java @@ -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> METADATA_LIST = new MetadataList1_8Type(); + public static final Type> METADATA_LIST = new MetaListType(METADATA); public static final Type CHUNK_SECTION = new ChunkSectionType1_8(); }