From 56e1d0a69d51502dd9d80dde2d6eb4ef06b02754 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Sat, 9 Jul 2022 11:47:58 +0200 Subject: [PATCH] Add optional type for smol cleanup --- .../viaversion/api/type/ByteBufReader.java | 10 ++-- .../viaversion/api/type/ByteBufWriter.java | 12 +++-- .../viaversion/api/type/OptionalType.java | 50 +++++++++++++++++++ .../viaversion/viaversion/api/type/Type.java | 5 +- .../types/minecraft/OptPosition1_14Type.java | 22 ++------ .../type/types/minecraft/OptPositionType.java | 21 ++------ .../api/type/types/minecraft/OptUUIDType.java | 25 ++-------- .../minecraft/OptionalComponentType.java | 22 ++------ .../minecraft/OptionalProfileKeyType.java | 23 ++------- .../types/minecraft/OptionalVarIntType.java | 17 ++++--- 10 files changed, 94 insertions(+), 113 deletions(-) create mode 100644 api/src/main/java/com/viaversion/viaversion/api/type/OptionalType.java diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/ByteBufReader.java b/api/src/main/java/com/viaversion/viaversion/api/type/ByteBufReader.java index 356b3c44e..f5d13de85 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/ByteBufReader.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/ByteBufReader.java @@ -24,13 +24,15 @@ package com.viaversion.viaversion.api.type; import io.netty.buffer.ByteBuf; +@FunctionalInterface public interface ByteBufReader { + /** - * Read a value from a ByteBuf + * Reads a value from a ByteBuf. * - * @param buffer The buffer to read from. - * @return The type based on the class type. - * @throws Exception Throws exception if it failed reading. + * @param buffer buffer to read from + * @return type based on the class type + * @throws Exception if it failed reading */ T read(ByteBuf buffer) throws Exception; } diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/ByteBufWriter.java b/api/src/main/java/com/viaversion/viaversion/api/type/ByteBufWriter.java index 852faf72f..3c8c93902 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/ByteBufWriter.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/ByteBufWriter.java @@ -24,13 +24,15 @@ package com.viaversion.viaversion.api.type; import io.netty.buffer.ByteBuf; +@FunctionalInterface public interface ByteBufWriter { + /** - * Write an object to a type to a ByteBuf + * Writes an object to a type to a ByteBuf. * - * @param buffer The buffer to write to - * @param object The object to write - * @throws Exception Throws if it failed to write + * @param buffer buffer to write to + * @param value value to write + * @throws Exception if it failed to write */ - void write(ByteBuf buffer, T object) throws Exception; + void write(ByteBuf buffer, T value) throws Exception; } diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/OptionalType.java b/api/src/main/java/com/viaversion/viaversion/api/type/OptionalType.java new file mode 100644 index 000000000..261ffc750 --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/api/type/OptionalType.java @@ -0,0 +1,50 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2022 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; + +import io.netty.buffer.ByteBuf; +import org.checkerframework.checker.nullness.qual.Nullable; + +public abstract class OptionalType extends Type { + private final Type type; + + protected OptionalType(final Type type) { + super(type.getOutputClass()); + this.type = type; + } + + @Override + public @Nullable T read(ByteBuf buffer) throws Exception { + return buffer.readBoolean() ? type.read(buffer) : null; + } + + @Override + public void write(final ByteBuf buffer, @Nullable final T value) throws Exception { + if (value == null) { + buffer.writeBoolean(false); + } else { + buffer.writeBoolean(true); + type.write(buffer, value); + } + } +} diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/Type.java b/api/src/main/java/com/viaversion/viaversion/api/type/Type.java index 20e0a5ac2..f4a08a6f5 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/Type.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/Type.java @@ -114,6 +114,7 @@ public abstract class Type implements ByteBufReader, ByteBufWriter { public static final Type UUID = new UUIDType(); public static final Type OPTIONAL_UUID = new OptUUIDType(); + @Deprecated/*(forRemoval = true)*/ public static final Type UUID_INT_ARRAY = new UUIDIntArrayType(); public static final Type UUID_ARRAY = new ArrayType<>(Type.UUID); @@ -151,14 +152,14 @@ public abstract class Type implements ByteBufReader, ByteBufWriter { /* MC Types */ public static final Type POSITION = new PositionType(); + public static final Type OPTIONAL_POSITION = new OptPositionType(); public static final Type POSITION1_14 = new Position1_14Type(); + public static final Type OPTIONAL_POSITION_1_14 = new OptPosition1_14Type(); public static final Type ROTATION = new EulerAngleType(); public static final Type VECTOR = new VectorType(); public static final Type NBT = new NBTType(); public static final Type NBT_ARRAY = new ArrayType<>(Type.NBT); - public static final Type OPTIONAL_POSITION = new OptPositionType(); - public static final Type OPTIONAL_POSITION_1_14 = new OptPosition1_14Type(); public static final Type OPTIONAL_GLOBAL_POSITION = new OptionalGlobalPositionType(); public static final Type BLOCK_CHANGE_RECORD = new BlockChangeRecordType(); diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OptPosition1_14Type.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OptPosition1_14Type.java index 53a19b9d9..ca43da989 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OptPosition1_14Type.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OptPosition1_14Type.java @@ -23,26 +23,12 @@ package com.viaversion.viaversion.api.type.types.minecraft; import com.viaversion.viaversion.api.minecraft.Position; +import com.viaversion.viaversion.api.type.OptionalType; import com.viaversion.viaversion.api.type.Type; -import io.netty.buffer.ByteBuf; -public class OptPosition1_14Type extends Type { +public class OptPosition1_14Type extends OptionalType { + public OptPosition1_14Type() { - super(Position.class); - } - - @Override - public Position read(ByteBuf buffer) throws Exception { - boolean present = buffer.readBoolean(); - if (!present) return null; - return Type.POSITION1_14.read(buffer); - } - - @Override - public void write(ByteBuf buffer, Position object) throws Exception { - buffer.writeBoolean(object != null); - if (object != null) { - Type.POSITION1_14.write(buffer, object); - } + super(Type.POSITION1_14); } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OptPositionType.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OptPositionType.java index 430fe1851..c2fe4cf11 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OptPositionType.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OptPositionType.java @@ -23,25 +23,12 @@ package com.viaversion.viaversion.api.type.types.minecraft; import com.viaversion.viaversion.api.minecraft.Position; +import com.viaversion.viaversion.api.type.OptionalType; import com.viaversion.viaversion.api.type.Type; -import io.netty.buffer.ByteBuf; -public class OptPositionType extends Type { +public class OptPositionType extends OptionalType { + public OptPositionType() { - super(Position.class); - } - - @Override - public Position read(ByteBuf buffer) throws Exception { - boolean present = buffer.readBoolean(); - if (!present) return null; - return Type.POSITION.read(buffer); - } - - @Override - public void write(ByteBuf buffer, Position object) throws Exception { - buffer.writeBoolean(object != null); - if (object != null) - Type.POSITION.write(buffer, object); + super(Type.POSITION); } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OptUUIDType.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OptUUIDType.java index 8245c3ebe..10e8c71e1 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OptUUIDType.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OptUUIDType.java @@ -22,31 +22,14 @@ */ package com.viaversion.viaversion.api.type.types.minecraft; +import com.viaversion.viaversion.api.type.OptionalType; import com.viaversion.viaversion.api.type.Type; -import io.netty.buffer.ByteBuf; import java.util.UUID; -public class OptUUIDType extends Type { +public class OptUUIDType extends OptionalType { + public OptUUIDType() { - super(UUID.class); - } - - @Override - public UUID read(ByteBuf buffer) { - boolean present = buffer.readBoolean(); - if (!present) return null; - return new UUID(buffer.readLong(), buffer.readLong()); - } - - @Override - public void write(ByteBuf buffer, UUID object) { - if (object == null) { - buffer.writeBoolean(false); - } else { - buffer.writeBoolean(true); - buffer.writeLong(object.getMostSignificantBits()); - buffer.writeLong(object.getLeastSignificantBits()); - } + super(Type.UUID); } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OptionalComponentType.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OptionalComponentType.java index 0a9abc77e..3ad255586 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OptionalComponentType.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OptionalComponentType.java @@ -23,28 +23,12 @@ package com.viaversion.viaversion.api.type.types.minecraft; import com.google.gson.JsonElement; +import com.viaversion.viaversion.api.type.OptionalType; import com.viaversion.viaversion.api.type.Type; -import io.netty.buffer.ByteBuf; -public class OptionalComponentType extends Type { +public class OptionalComponentType extends OptionalType { public OptionalComponentType() { - super(JsonElement.class); - } - - @Override - public JsonElement read(ByteBuf buffer) throws Exception { - boolean present = buffer.readBoolean(); - return present ? Type.COMPONENT.read(buffer) : null; - } - - @Override - public void write(ByteBuf buffer, JsonElement object) throws Exception { - if (object == null) { - buffer.writeBoolean(false); - } else { - buffer.writeBoolean(true); - Type.COMPONENT.write(buffer, object); - } + super(Type.COMPONENT); } } \ No newline at end of file diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OptionalProfileKeyType.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OptionalProfileKeyType.java index 842c21c5e..3ddac9eab 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OptionalProfileKeyType.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OptionalProfileKeyType.java @@ -23,29 +23,12 @@ package com.viaversion.viaversion.api.type.types.minecraft; import com.viaversion.viaversion.api.minecraft.ProfileKey; +import com.viaversion.viaversion.api.type.OptionalType; import com.viaversion.viaversion.api.type.Type; -import io.netty.buffer.ByteBuf; -import org.checkerframework.checker.nullness.qual.Nullable; -//TODO generify optional types -public class OptionalProfileKeyType extends Type { +public class OptionalProfileKeyType extends OptionalType { public OptionalProfileKeyType() { - super(ProfileKey.class); - } - - @Override - public @Nullable ProfileKey read(final ByteBuf buffer) throws Exception { - return buffer.readBoolean() ? Type.PROFILE_KEY.read(buffer) : null; - } - - @Override - public void write(final ByteBuf buffer, @Nullable final ProfileKey object) throws Exception { - if (object != null) { - buffer.writeBoolean(true); - Type.PROFILE_KEY.write(buffer, object); - } else { - buffer.writeBoolean(false); - } + super(Type.PROFILE_KEY); } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OptionalVarIntType.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OptionalVarIntType.java index 13c3e0527..16a7273b9 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OptionalVarIntType.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/types/minecraft/OptionalVarIntType.java @@ -26,20 +26,23 @@ import com.viaversion.viaversion.api.type.Type; import io.netty.buffer.ByteBuf; public class OptionalVarIntType extends Type { + public OptionalVarIntType() { super(Integer.class); } @Override - public Integer read(ByteBuf buffer) throws Exception { - int read = Type.VAR_INT.readPrimitive(buffer); - if (read == 0) return null; - return read - 1; + public Integer read(final ByteBuf buffer) throws Exception { + final int value = Type.VAR_INT.readPrimitive(buffer); + return value == 0 ? null : value - 1; } @Override - public void write(ByteBuf buffer, Integer object) throws Exception { - if (object == null) Type.VAR_INT.writePrimitive(buffer, 0); - else Type.VAR_INT.writePrimitive(buffer, object + 1); + public void write(final ByteBuf buffer, final Integer object) throws Exception { + if (object == null) { + Type.VAR_INT.writePrimitive(buffer, 0); + } else { + Type.VAR_INT.writePrimitive(buffer, object + 1); + } } }