Small refactor

This commit is contained in:
Nassim Jahnke 2024-03-01 14:10:44 +01:00
parent 92878a39ef
commit 2d03110f08
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
4 changed files with 32 additions and 27 deletions

View File

@ -22,9 +22,9 @@
*/
package com.viaversion.viaversion.api.minecraft;
import com.viaversion.viaversion.util.Either;
import com.google.common.base.Preconditions;
public final class Holder<T> implements Either<Integer, T> {
public final class Holder<T> {
private final T value;
private final int id;
@ -39,27 +39,28 @@ public final class Holder<T> implements Either<Integer, T> {
this.id = -1;
}
/**
* Returns true if this holder is backed by a direct value.
*
* @return true if the holder is direct
* @see #hasId()
*/
public boolean isDirect() {
return id == -1;
}
/**
* Returns true if this holder has an id.
*
* @return true if this holder has an id
* @see #isDirect()
*/
public boolean hasId() {
return id != -1;
}
@Override
public boolean isLeft() {
return value != null;
}
@Override
public boolean isRight() {
return value == null;
}
@Override
public Integer left() {
return id;
}
@Override
public T right() {
public T value() {
Preconditions.checkArgument(isDirect(), "Holder is not direct");
return value;
}

View File

@ -26,19 +26,23 @@ import org.checkerframework.checker.nullness.qual.Nullable;
public final class SoundEvent {
private final String resourceLocation;
private final String identifier;
private final Float fixedRange;
public SoundEvent(final String resourceLocation, @Nullable final Float fixedRange) {
this.resourceLocation = resourceLocation;
public SoundEvent(final String identifier, @Nullable final Float fixedRange) {
this.identifier = identifier;
this.fixedRange = fixedRange;
}
public String resourceLocation() {
return resourceLocation;
public String identifier() {
return identifier;
}
public @Nullable Float fixedRange() {
return fixedRange;
}
public SoundEvent withIdentifier(final String identifier) {
return new SoundEvent(identifier, this.fixedRange);
}
}

View File

@ -43,11 +43,11 @@ public abstract class HolderType<T> extends Type<Holder<T>> {
@Override
public void write(final ByteBuf buffer, final Holder<T> object) throws Exception {
if (object.isLeft()) {
if (object.hasId()) {
Type.VAR_INT.writePrimitive(buffer, object.id() + 1); // Normalize id
} else {
Type.VAR_INT.writePrimitive(buffer, 0);
writeDirect(buffer, object.right());
writeDirect(buffer, object.value());
}
}

View File

@ -37,7 +37,7 @@ public final class SoundEventType extends HolderType<SoundEvent> {
@Override
public void writeDirect(final ByteBuf buffer, final SoundEvent value) throws Exception {
Type.STRING.write(buffer, value.resourceLocation());
Type.STRING.write(buffer, value.identifier());
Type.OPTIONAL_FLOAT.write(buffer, value.fixedRange());
}
}