| 1 | package net.minecraft.server.packs.resources;
|
| 2 |
|
| 3 | import java.io.BufferedReader;
|
| 4 | import java.io.IOException;
|
| 5 | import java.io.InputStream;
|
| 6 | import java.io.InputStreamReader;
|
| 7 | import java.io.Reader;
|
| 8 | import java.nio.charset.StandardCharsets;
|
| 9 | import java.util.Optional;
|
| 10 | import net.fabricmc.fabric.api.resource.v1.FabricResource;
|
| 11 | import net.minecraft.server.packs.PackResources;
|
| 12 | import net.minecraft.server.packs.repository.KnownPack;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 |
|
| 15 | public class Resource implements FabricResource {
|
| 16 | private final PackResources source;
|
| 17 | private final IoSupplier<InputStream> streamSupplier;
|
| 18 | private final IoSupplier<ResourceMetadata> metadataSupplier;
|
| 19 | private @Nullable ResourceMetadata cachedMetadata;
|
| 20 |
|
| 21 | public Resource(final PackResources source, final IoSupplier<InputStream> streamSupplier, final IoSupplier<ResourceMetadata> metadataSupplier) {
|
| 22 | this.source = source;
|
| 23 | this.streamSupplier = streamSupplier;
|
| 24 | this.metadataSupplier = metadataSupplier;
|
| 25 | }
|
| 26 |
|
| 27 | public Resource(final PackResources source, final IoSupplier<InputStream> streamSupplier) {
|
| 28 | this.source = source;
|
| 29 | this.streamSupplier = streamSupplier;
|
| 30 | this.metadataSupplier = ResourceMetadata.EMPTY_SUPPLIER;
|
| 31 | this.cachedMetadata = ResourceMetadata.EMPTY;
|
| 32 | }
|
| 33 |
|
| 34 | public PackResources source() {
|
| 35 | return this.source;
|
| 36 | }
|
| 37 |
|
| 38 | public String sourcePackId() {
|
| 39 | return this.source.packId();
|
| 40 | }
|
| 41 |
|
| 42 | public Optional<KnownPack> knownPackInfo() {
|
| 43 | return this.source.knownPackInfo();
|
| 44 | }
|
| 45 |
|
| 46 | public InputStream open() throws IOException {
|
| 47 | return this.streamSupplier.get();
|
| 48 | }
|
| 49 |
|
| 50 | public BufferedReader openAsReader() throws IOException {
|
| 51 | return new BufferedReader(new InputStreamReader(this.open(), StandardCharsets.UTF_8));
|
| 52 | }
|
| 53 |
|
| 54 | public String readAllAsString() throws IOException {
|
| 55 | try (Reader reader = this.openAsReader()) {
|
| 56 | return reader.readAllAsString();
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | public ResourceMetadata metadata() throws IOException {
|
| 61 | if (this.cachedMetadata == null) {
|
| 62 | this.cachedMetadata = this.metadataSupplier.get();
|
| 63 | }
|
| 64 |
|
| 65 | return this.cachedMetadata;
|
| 66 | }
|
| 67 | }
|
| 68 |
|