Skip to content

Commit

Permalink
Add ExtendedResourceReader
Browse files Browse the repository at this point in the history
  • Loading branch information
FirstMegaGame4 committed Dec 29, 2023
1 parent 109a584 commit 8e6eb04
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package fr.firstmegagame4.env.json.api.resource;

import java.io.BufferedReader;

public interface ExtendedResourceReader {

ExtendedResource getExtendedResource();

BufferedReader asBufferedReader();
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
11 changes: 11 additions & 0 deletions src/main/java/fr/firstmegagame4/env/json/mixin/ResourceMixin.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<EnvJson> envJsonSupplier) {
this.envJsonSupplier = envJsonSupplier;
Expand Down

0 comments on commit 8e6eb04

Please sign in to comment.