diff --git a/src/main/java/fr/firstmegagame4/env/json/api/resource/ExtendedResourceReader.java b/src/main/java/fr/firstmegagame4/env/json/api/resource/ExtendedResourceReader.java new file mode 100644 index 0000000..d51e319 --- /dev/null +++ b/src/main/java/fr/firstmegagame4/env/json/api/resource/ExtendedResourceReader.java @@ -0,0 +1,10 @@ +package fr.firstmegagame4.env.json.api.resource; + +import java.io.BufferedReader; + +public interface ExtendedResourceReader { + + ExtendedResource getExtendedResource(); + + BufferedReader asBufferedReader(); +} diff --git a/src/main/java/fr/firstmegagame4/env/json/impl/resource/ExtendedResourceReaderImpl.java b/src/main/java/fr/firstmegagame4/env/json/impl/resource/ExtendedResourceReaderImpl.java new file mode 100644 index 0000000..e69759b --- /dev/null +++ b/src/main/java/fr/firstmegagame4/env/json/impl/resource/ExtendedResourceReaderImpl.java @@ -0,0 +1,30 @@ +package fr.firstmegagame4.env.json.impl.resource; + +import fr.firstmegagame4.env.json.api.resource.ExtendedResource; +import fr.firstmegagame4.env.json.api.resource.ExtendedResourceReader; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; + +import java.io.BufferedReader; +import java.io.Reader; + +@ApiStatus.Internal +public class ExtendedResourceReaderImpl extends BufferedReader implements ExtendedResourceReader { + + private final ExtendedResource extendedResource; + + public ExtendedResourceReaderImpl(ExtendedResource extendedResource, @NotNull Reader in) { + super(in); + this.extendedResource = extendedResource; + } + + @Override + public ExtendedResource getExtendedResource() { + return this.extendedResource; + } + + @Override + public BufferedReader asBufferedReader() { + return this; + } +} diff --git a/src/main/java/fr/firstmegagame4/env/json/mixin/ResourceMixin.java b/src/main/java/fr/firstmegagame4/env/json/mixin/ResourceMixin.java index e494c0a..47371b7 100644 --- a/src/main/java/fr/firstmegagame4/env/json/mixin/ResourceMixin.java +++ b/src/main/java/fr/firstmegagame4/env/json/mixin/ResourceMixin.java @@ -1,14 +1,20 @@ package fr.firstmegagame4.env.json.mixin; import fr.firstmegagame4.env.json.api.EnvJson; +import fr.firstmegagame4.env.json.api.resource.ExtendedResource; import fr.firstmegagame4.env.json.impl.EnvJsonUtils; +import fr.firstmegagame4.env.json.impl.resource.ExtendedResourceReaderImpl; import fr.firstmegagame4.env.json.impl.resource.ResourceDuckInterface; import net.minecraft.resource.InputSupplier; import net.minecraft.resource.Resource; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; +import java.io.BufferedReader; import java.io.IOException; +import java.io.Reader; @Mixin(Resource.class) public class ResourceMixin implements ResourceDuckInterface { @@ -19,6 +25,11 @@ public class ResourceMixin implements ResourceDuckInterface { @Unique private EnvJson envJson = EnvJsonUtils.ENV_JSON_NONE; + @Redirect(method = "getReader", at = @At(value = "NEW", target = "(Ljava/io/Reader;)Ljava/io/BufferedReader;")) + private BufferedReader redirectReader(Reader in) { + return new ExtendedResourceReaderImpl(ExtendedResource.of((Resource) (Object) this), in); + } + @Override public void env_json$initEnvJsonSupplier(InputSupplier envJsonSupplier) { this.envJsonSupplier = envJsonSupplier;