Mirror von
https://github.com/ViaVersion/ViaVersion.git
synchronisiert 2024-09-08 13:52:50 +02:00
Add optional type for smol cleanup
Dieser Commit ist enthalten in:
Ursprung
63ffb51df5
Commit
56e1d0a69d
@ -24,13 +24,15 @@ package com.viaversion.viaversion.api.type;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface ByteBufReader<T> {
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
@ -24,13 +24,15 @@ package com.viaversion.viaversion.api.type;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface ByteBufWriter<T> {
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
@ -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<T> extends Type<T> {
|
||||
private final Type<T> type;
|
||||
|
||||
protected OptionalType(final Type<T> 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -114,6 +114,7 @@ public abstract class Type<T> implements ByteBufReader<T>, ByteBufWriter<T> {
|
||||
|
||||
public static final Type<UUID> UUID = new UUIDType();
|
||||
public static final Type<UUID> OPTIONAL_UUID = new OptUUIDType();
|
||||
@Deprecated/*(forRemoval = true)*/
|
||||
public static final Type<UUID> UUID_INT_ARRAY = new UUIDIntArrayType();
|
||||
public static final Type<UUID[]> UUID_ARRAY = new ArrayType<>(Type.UUID);
|
||||
|
||||
@ -151,14 +152,14 @@ public abstract class Type<T> implements ByteBufReader<T>, ByteBufWriter<T> {
|
||||
|
||||
/* MC Types */
|
||||
public static final Type<Position> POSITION = new PositionType();
|
||||
public static final Type<Position> OPTIONAL_POSITION = new OptPositionType();
|
||||
public static final Type<Position> POSITION1_14 = new Position1_14Type();
|
||||
public static final Type<Position> OPTIONAL_POSITION_1_14 = new OptPosition1_14Type();
|
||||
public static final Type<EulerAngle> ROTATION = new EulerAngleType();
|
||||
public static final Type<Vector> VECTOR = new VectorType();
|
||||
public static final Type<CompoundTag> NBT = new NBTType();
|
||||
public static final Type<CompoundTag[]> NBT_ARRAY = new ArrayType<>(Type.NBT);
|
||||
|
||||
public static final Type<Position> OPTIONAL_POSITION = new OptPositionType();
|
||||
public static final Type<Position> OPTIONAL_POSITION_1_14 = new OptPosition1_14Type();
|
||||
public static final Type<GlobalPosition> OPTIONAL_GLOBAL_POSITION = new OptionalGlobalPositionType();
|
||||
|
||||
public static final Type<BlockChangeRecord> BLOCK_CHANGE_RECORD = new BlockChangeRecordType();
|
||||
|
@ -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<Position> {
|
||||
public class OptPosition1_14Type extends OptionalType<Position> {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -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<Position> {
|
||||
public class OptPositionType extends OptionalType<Position> {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -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<UUID> {
|
||||
public class OptUUIDType extends OptionalType<UUID> {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -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<JsonElement> {
|
||||
public class OptionalComponentType extends OptionalType<JsonElement> {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
@ -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<ProfileKey> {
|
||||
public class OptionalProfileKeyType extends OptionalType<ProfileKey> {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -26,20 +26,23 @@ import com.viaversion.viaversion.api.type.Type;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
public class OptionalVarIntType extends Type<Integer> {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren