Complicate Holder a bit

This commit is contained in:
Camotoy 2024-04-22 16:32:38 -04:00
parent 8bc6990525
commit 4ee05b629f
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F
4 changed files with 96 additions and 63 deletions

View file

@ -132,15 +132,15 @@ public class MinecraftCodecHelper extends BasePacketCodecHelper {
public <T, E extends Throwable> Holder<T> readHolder(ByteBuf buf, CheckedFunction<ByteBuf, T, E> readCustom) throws E {
int registryId = this.readVarInt(buf);
return registryId == 0 ? new Holder<>(readCustom.apply(buf)) : new Holder<>(registryId - 1);
return registryId == 0 ? Holder.ofCustom(readCustom.apply(buf)) : Holder.ofId(registryId - 1);
}
public <T, E extends Throwable> void writeHolder(ByteBuf buf, Holder<T> holder, CheckedBiConsumer<ByteBuf, T, E> writeCustom) throws E {
if (holder.isCustom()) {
this.writeVarInt(buf, 0);
writeCustom.accept(buf, holder.getCustomValue());
writeCustom.accept(buf, holder.custom());
} else {
this.writeVarInt(buf, holder.getId() + 1);
this.writeVarInt(buf, holder.id() + 1);
}
}

View file

@ -1,39 +1,90 @@
package com.github.steveice10.mc.protocol.data.game;
import lombok.Data;
import java.util.function.Consumer;
import java.util.function.Supplier;
@Data
public class Holder<T> {
private final int id;
private final T customValue;
private final boolean custom;
public Holder(int id) {
this.id = id;
this.customValue = null;
this.custom = false;
/**
* Represents an object that could either be a network ID, or a custom-defined one.
*/
public interface Holder<T> {
static <T> Holder<T> ofId(int id) {
return new IdHolder<>(id);
}
public Holder(T customValue) {
this.id = -1;
this.customValue = customValue;
this.custom = true;
static <T> Holder<T> ofCustom(T object) {
return new CustomHolder<>(object);
}
boolean isId();
int id();
boolean isCustom();
T custom();
Holder<T> ifId(Consumer<Holder<T>> action);
Holder<T> ifCustom(Consumer<Holder<T>> action);
record IdHolder<T>(int id) implements Holder<T> {
@Override
public boolean isId() {
return true;
}
@Override
public boolean isCustom() {
return false;
}
@Override
public T custom() {
throw new IllegalStateException("Check isCustom first!");
}
@Override
public Holder<T> ifId(Consumer<Holder<T>> action) {
if (!custom) {
action.accept(this);
}
return this;
}
@Override
public Holder<T> ifCustom(Consumer<Holder<T>> action) {
if (custom) {
action.accept(this);
}
// no-op
return this;
}
}
record CustomHolder<T>(T object) implements Holder<T> {
@Override
public boolean isId() {
return false;
}
@Override
public int id() {
throw new IllegalStateException("Check isId first!");
}
@Override
public boolean isCustom() {
return true;
}
@Override
public T custom() {
return object;
}
@Override
public Holder<T> ifId(Consumer<Holder<T>> action) {
return this;
}
@Override
public Holder<T> ifCustom(Consumer<Holder<T>> action) {
action.accept(this);
return this;
}
}
}

View file

@ -6,29 +6,11 @@ import lombok.AllArgsConstructor;
import lombok.Data;
import net.kyori.adventure.text.Component;
@Data
@AllArgsConstructor
public class ArmorTrim {
private final Holder<TrimMaterial> material;
private final Holder<TrimPattern> pattern;
private final boolean showInTooltip;
@Data
@AllArgsConstructor
public static class TrimMaterial {
private final String assetName;
private final int ingredientId;
private final float itemModelIndex;
private final Int2ObjectMap<String> overrideArmorMaterials;
private final Component description;
public record ArmorTrim(Holder<TrimMaterial> material, Holder<TrimPattern> pattern, boolean showInTooltip) {
public record TrimMaterial(String assetName, int ingredientId, float itemModelIndex,
Int2ObjectMap<String> overrideArmorMaterials, Component description) {
}
@Data
@AllArgsConstructor
public static class TrimPattern {
private final String assetId;
private final int templateItemId;
private final Component description;
private final boolean decal;
public record TrimPattern(String assetId, int templateItemId, Component description, boolean decal) {
}
}

View file

@ -396,9 +396,9 @@ public class ItemCodecHelper extends MinecraftCodecHelper {
}
public void writeArmorTrim(ByteBuf buf, ArmorTrim trim) throws IOException {
this.writeHolder(buf, trim.getMaterial(), this::writeTrimMaterial);
this.writeHolder(buf, trim.getPattern(), this::writeTrimPattern);
buf.writeBoolean(trim.isShowInTooltip());
this.writeHolder(buf, trim.material(), this::writeTrimMaterial);
this.writeHolder(buf, trim.pattern(), this::writeTrimPattern);
buf.writeBoolean(trim.showInTooltip());
}
public ArmorTrim.TrimMaterial readTrimMaterial(ByteBuf buf) throws IOException {
@ -417,17 +417,17 @@ public class ItemCodecHelper extends MinecraftCodecHelper {
}
public void writeTrimMaterial(ByteBuf buf, ArmorTrim.TrimMaterial material) throws IOException {
this.writeString(buf, material.getAssetName());
this.writeVarInt(buf, material.getIngredientId());
buf.writeFloat(material.getItemModelIndex());
this.writeString(buf, material.assetName());
this.writeVarInt(buf, material.ingredientId());
buf.writeFloat(material.itemModelIndex());
this.writeVarInt(buf, material.getOverrideArmorMaterials().size());
for (Int2ObjectMap.Entry<String> entry : material.getOverrideArmorMaterials().int2ObjectEntrySet()) {
this.writeVarInt(buf, material.overrideArmorMaterials().size());
for (Int2ObjectMap.Entry<String> entry : material.overrideArmorMaterials().int2ObjectEntrySet()) {
this.writeVarInt(buf, entry.getIntKey());
this.writeString(buf, entry.getValue());
}
this.writeComponent(buf, material.getDescription());
this.writeComponent(buf, material.description());
}
public ArmorTrim.TrimPattern readTrimPattern(ByteBuf buf) throws IOException {
@ -439,10 +439,10 @@ public class ItemCodecHelper extends MinecraftCodecHelper {
}
public void writeTrimPattern(ByteBuf buf, ArmorTrim.TrimPattern pattern) throws IOException {
this.writeResourceLocation(buf, pattern.getAssetId());
this.writeVarInt(buf, pattern.getTemplateItemId());
this.writeComponent(buf, pattern.getDescription());
buf.writeBoolean(pattern.isDecal());
this.writeResourceLocation(buf, pattern.assetId());
this.writeVarInt(buf, pattern.templateItemId());
this.writeComponent(buf, pattern.description());
buf.writeBoolean(pattern.decal());
}
public Holder<Instrument> readInstrument(ByteBuf buf) {