Skip to content

Commit

Permalink
chore: more mixin cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
CallMeEchoCodes committed Oct 4, 2024
1 parent 37149a9 commit 984fb75
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.llamalad7.mixinextras.injector.v2.WrapWithCondition;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.llamalad7.mixinextras.sugar.Local;
Expand Down Expand Up @@ -33,9 +34,8 @@ private static String load(JsonElement element, String name, Operation<String> o
return "";
}

@WrapOperation(method = "load(Ljava/io/InputStream;Ljava/util/function/BiConsumer;)V", at = @At(value = "INVOKE", target = "Ljava/util/function/BiConsumer;accept(Ljava/lang/Object;Ljava/lang/Object;)V"))
private static <T, U> void skip(BiConsumer<T, U> instance, T t, U u, Operation<Void> original, @Share("skip") LocalBooleanRef skip) {
if (skip.get()) return;
instance.accept(t, u);
@WrapWithCondition(method = "load(Ljava/io/InputStream;Ljava/util/function/BiConsumer;)V", at = @At(value = "INVOKE", target = "Ljava/util/function/BiConsumer;accept(Ljava/lang/Object;Ljava/lang/Object;)V"))
private static <T, U> boolean skip(BiConsumer<T, U> instance, T t, U u, @Share("skip") LocalBooleanRef skip) {
return !skip.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,62 +17,63 @@

@Mixin(TextCodecs.class)
public class TextCodecsMixin {
@ModifyVariable(method = "dispatchingCodec", at = @At("STORE"), ordinal = 0)
private static <T extends StringIdentifiable, E> MapCodec<E> dispatchingCodec(MapCodec<E> codec, T[] types) {
if (!types.getClass().getComponentType().isAssignableFrom(TextContent.Type.class)) return codec;
@ModifyVariable(method = "dispatchingCodec", at = @At("STORE"), ordinal = 0)
private static <T extends StringIdentifiable, E> MapCodec<E> dispatchingCodec(MapCodec<E> codec, T[] types) {
if (!types.getClass().getComponentType().isAssignableFrom(TextContent.Type.class)) return codec;

return new MapCodec<>() {
@Override
public <T1> RecordBuilder<T1> encode(E input, DynamicOps<T1> ops, RecordBuilder<T1> prefix) {
return codec.encode(input, ops, prefix);
}
return new MapCodec<>() {
@Override
public <T1> RecordBuilder<T1> encode(E input, DynamicOps<T1> ops, RecordBuilder<T1> prefix) {
return codec.encode(input, ops, prefix);
}

@SuppressWarnings("unchecked")
@Override
public <T1> DataResult<E> decode(DynamicOps<T1> ops, MapLike<T1> input) {
DataResult<E> originalResult = codec.decode(ops, input);
if (originalResult.result().isPresent()) return originalResult;
@SuppressWarnings("unchecked")
@Override
public <T1> DataResult<E> decode(DynamicOps<T1> ops, MapLike<T1> input) {
DataResult<E> originalResult = codec.decode(ops, input);
if (originalResult.result().isPresent()) return originalResult;

return TextContentRegistryImpl.getTypes().values().stream()
.filter(entry -> input.get(entry.field()) != null)
.findFirst()
.map(entry -> (DataResult<E>) entry.type().codec().decode(ops, input))
.orElse(originalResult);
}
return TextContentRegistryImpl.getTypes().values().stream()
.filter(entry -> input.get(entry.field()) != null)
.findFirst()
.map(entry -> (DataResult<E>) entry.type().codec().decode(ops, input))
.orElse(originalResult);
}

@Override
public <T1> Stream<T1> keys(DynamicOps<T1> ops) {
return Stream.concat(
codec.keys(ops),
TextContentRegistryImpl.getTypes().values().stream().flatMap(entry -> entry.type().codec().keys(ops))
);
}
};
}
@Override
public <T1> Stream<T1> keys(DynamicOps<T1> ops) {
return Stream.concat(
codec.keys(ops),
TextContentRegistryImpl.getTypes().values().stream()
.flatMap(entry -> entry.type().codec().keys(ops))
);
}
};
}

@SuppressWarnings("unchecked")
@WrapOperation(method = "dispatchingCodec", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/StringIdentifiable;createBasicCodec(Ljava/util/function/Supplier;)Lcom/mojang/serialization/Codec;"))
private static <T extends StringIdentifiable> Codec<T> dispatchingCodec(Supplier<T[]> values, Operation<Codec<T>> original) {
Codec<T> originalCodec = original.call(values);
if (!values.get().getClass().getComponentType().isAssignableFrom(TextContent.Type.class)) return originalCodec;
Codec<T> textContentTypeCodec = Codec.stringResolver(StringIdentifiable::asString, id -> (T) TextContentRegistryImpl.getTypes().get(id).type());
@SuppressWarnings("unchecked")
@WrapOperation(method = "dispatchingCodec", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/StringIdentifiable;createBasicCodec(Ljava/util/function/Supplier;)Lcom/mojang/serialization/Codec;"))
private static <T extends StringIdentifiable> Codec<T> dispatchingCodec(Supplier<T[]> values, Operation<Codec<T>> original) {
Codec<T> originalCodec = original.call(values);
if (!values.get().getClass().getComponentType().isAssignableFrom(TextContent.Type.class)) return originalCodec;
Codec<T> textContentTypeCodec = Codec.stringResolver(StringIdentifiable::asString, id -> (T) TextContentRegistryImpl.getTypes().get(id).type());

return new Codec<>() {
@Override
public <T1> DataResult<T1> encode(T input, DynamicOps<T1> ops, T1 prefix) {
DataResult<T1> originalResult = originalCodec.encode(input, ops, prefix);
if (originalResult.result().isPresent()) return originalResult;
return new Codec<>() {
@Override
public <T1> DataResult<T1> encode(T input, DynamicOps<T1> ops, T1 prefix) {
DataResult<T1> originalResult = originalCodec.encode(input, ops, prefix);
if (originalResult.result().isPresent()) return originalResult;

return textContentTypeCodec.encode(input, ops, prefix);
}
return textContentTypeCodec.encode(input, ops, prefix);
}

@Override
public <T1> DataResult<Pair<T, T1>> decode(DynamicOps<T1> ops, T1 input) {
DataResult<Pair<T, T1>> originalResult = originalCodec.decode(ops, input);
if (originalResult.result().isPresent()) return originalResult;
@Override
public <T1> DataResult<Pair<T, T1>> decode(DynamicOps<T1> ops, T1 input) {
DataResult<Pair<T, T1>> originalResult = originalCodec.decode(ops, input);
if (originalResult.result().isPresent()) return originalResult;

return textContentTypeCodec.decode(ops, input);
}
};
}
return textContentTypeCodec.decode(ops, input);
}
};
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.spiritstudios.specter.mixin.serialization.client;

import com.google.common.collect.ImmutableList;
import com.llamalad7.mixinextras.sugar.Local;
import dev.spiritstudios.specter.impl.serialization.SpecterSerializationClient;
import dev.spiritstudios.specter.impl.serialization.text.TextTranslationSupplier;
import net.minecraft.text.StringVisitable;
Expand Down Expand Up @@ -57,9 +58,15 @@ private <T> void pop(CallbackInfoReturnable<Optional<T>> cir) {
}


@Inject(method = "updateTranslations", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/Language;get(Ljava/lang/String;)Ljava/lang/String;"), cancellable = true)
private void updateTranslations(CallbackInfo ci) {
Language language = Language.getInstance();
@Inject(
method = "updateTranslations",
at = {
@At(value = "INVOKE", target = "Lnet/minecraft/util/Language;get(Ljava/lang/String;)Ljava/lang/String;"),
@At(value = "INVOKE", target = "Lnet/minecraft/util/Language;get(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;")
},
cancellable = true
)
private void updateTranslations(CallbackInfo ci, @Local Language language) {
if (!(language instanceof TextTranslationSupplier supplier))
return;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package dev.spiritstudios.specter.mixin.serialization.client;

import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.llamalad7.mixinextras.sugar.Local;
import dev.spiritstudios.specter.impl.serialization.SpecterSerializationClient;
import dev.spiritstudios.specter.impl.serialization.text.TextTranslationSupplier;
Expand All @@ -19,13 +17,12 @@ public class TranslationStorageMixin implements TextTranslationSupplier {
@Unique
private Map<String, Text> textTranslations;

@WrapOperation(method = "load(Lnet/minecraft/resource/ResourceManager;Ljava/util/List;Z)Lnet/minecraft/client/resource/language/TranslationStorage;", at = @At(value = "NEW", target = "(Ljava/util/Map;Z)Lnet/minecraft/client/resource/language/TranslationStorage;"))
private static TranslationStorage skipImmutable(Map<String, String> translations, boolean rightToLeft, Operation<TranslationStorage> original, @Local Map<String, String> map) {
TranslationStorage storage = original.call(map, rightToLeft);
((TranslationStorageMixin) (Object) storage).textTranslations = SpecterSerializationClient.TEXT_TRANSLATIONS_BUILDER.get().build();
@ModifyReturnValue(method = "load(Lnet/minecraft/resource/ResourceManager;Ljava/util/List;Z)Lnet/minecraft/client/resource/language/TranslationStorage;", at = @At("RETURN"))
private static TranslationStorage load(TranslationStorage original) {
((TranslationStorageMixin) (Object) original).textTranslations = SpecterSerializationClient.TEXT_TRANSLATIONS_BUILDER.get().build();

SpecterSerializationClient.TEXT_TRANSLATIONS_BUILDER.remove();
return storage;
return original;
}

@ModifyReturnValue(method = "hasTranslation", at = @At("RETURN"))
Expand All @@ -36,15 +33,12 @@ private boolean hasTranslation(boolean original, @Local(argsOnly = true) String

@ModifyReturnValue(method = "get", at = @At("RETURN"))
private String get(String original) {
if (textTranslations == null) return original;

Text text = textTranslations.get(original);
return text != null ? text.getString() : original;
}

@Override
public Text specter_serialization$getText(String key) {
if (textTranslations == null) return null;
return textTranslations.get(key);
}
}

0 comments on commit 984fb75

Please sign in to comment.