Skip to content

Commit

Permalink
refactor(api): encapsulate static fields
Browse files Browse the repository at this point in the history
  • Loading branch information
sylv256 committed Nov 15, 2024
1 parent a11f5bf commit b74884e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,20 @@
*/
@ApiStatus.Experimental
public record Broken(int level) {
public static final Codec<Broken> CODEC = RecordCodecBuilder.create(
private static final Codec<Broken> CODEC = RecordCodecBuilder.create(
instance -> instance.group(
Codec.INT.fieldOf("level").forGetter(Broken::level)
).apply(instance, Broken::new)
);

public static final StreamCodec<ByteBuf, Broken> STREAM_CODEC = StreamCodec.composite(
private static final StreamCodec<ByteBuf, Broken> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.INT, Broken::level,
Broken::new
);

public static final Broken UNBROKEN = new Broken(0);
private static final Broken UNBROKEN = new Broken(0);

/**
* A {@link Broken} with level {@code -1} that indicates that conditions checking this will always be equal.
*/
public static final Broken ALWAYS = new Broken(-1);
private static final Broken ALWAYS = new Broken(-1);

/**
* A LUT to avoid duplication.
Expand All @@ -42,6 +39,25 @@ public record Broken(int level) {
}
}

public static Codec<Broken> codec() {
return CODEC;
}

public static StreamCodec<ByteBuf, Broken> streamCodec() {
return STREAM_CODEC;
}

public static Broken unbroken() {
return UNBROKEN;
}

/**
* A {@link Broken} with level {@code -1} that indicates that conditions checking this will always be equal.
*/
public static Broken always() {
return ALWAYS;
}

@Override
public boolean equals(Object obj) {
if (obj instanceof Broken broken) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public final class LegacyComponents {
public static final DeferredHolder<DataComponentType<?>, DataComponentType<Broken>> BROKEN = DATA_COMPONENTS.registerComponentType(
"broken",
builder -> builder
.persistent(Broken.CODEC)
.networkSynchronized(Broken.STREAM_CODEC)
.persistent(Broken.codec())
.networkSynchronized(Broken.streamCodec())
);

private LegacyComponents() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static void transmuteWater(EntityTickEvent.Post event) {
event.getEntity() instanceof ItemEntity entity &&
entity.isInWater() &&
entity.getItem().is(LegacyItems.JAPPAS_WAND) &&
Objects.requireNonNullElse(entity.getItem().get(LegacyComponents.BROKEN), Broken.UNBROKEN).equals(Broken.of(1)) &&
Objects.requireNonNullElse(entity.getItem().get(LegacyComponents.BROKEN), Broken.unbroken()).equals(Broken.of(1)) &&
!entity.level().isClientSide()
) {
ServerLevel level = (ServerLevel) entity.level();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected boolean showTooltip(
@NotNull TooltipFlag tooltipFlag
) {
Broken brokenLevel = ((ConditionalText) tooltip).legacy_landscape$getBroken();
return brokenLevel.equals(stack.getOrDefault(LegacyComponents.BROKEN, Broken.UNBROKEN));
return brokenLevel.equals(stack.getOrDefault(LegacyComponents.BROKEN, Broken.unbroken()));
}

@Override
Expand Down Expand Up @@ -83,7 +83,7 @@ protected boolean showTooltip(
return InteractionResult.PASS;
}

boolean broken = context.getItemInHand().getOrDefault(LegacyComponents.BROKEN, Broken.UNBROKEN).equals(Broken.of(1));
boolean broken = context.getItemInHand().getOrDefault(LegacyComponents.BROKEN, Broken.unbroken()).equals(Broken.of(1));

if (chunk.hasData(LegacyAttachments.LEGACY_CHUNK) || broken) {
if (broken) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public Properties moreInfo() {
return this
.tooltip(
TooltipCondition.not(TooltipCondition::hasModifierKey),
Broken.ALWAYS,
Broken.always(),
Component.translatable("tooltip.legacy_landscape.more_info")
.withStyle(
ChatFormatting.ITALIC,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class Mixin_MutableComponent implements ConditionalText {

@Override
public @NotNull Broken legacy_landscape$getBroken() {
return Objects.requireNonNullElse(legacy_landscape$broken, Broken.UNBROKEN);
return Objects.requireNonNullElse(legacy_landscape$broken, Broken.unbroken());
}

@Override
Expand Down

0 comments on commit b74884e

Please sign in to comment.