Mirror von
https://github.com/ViaVersion/ViaVersion.git
synchronisiert 2024-12-27 00:22:51 +01:00
Small refactor
Dieser Commit ist enthalten in:
Ursprung
92878a39ef
Commit
2d03110f08
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren