diff options
author | isXander <xandersmith2008@gmail.com> | 2023-06-03 23:10:03 +0100 |
---|---|---|
committer | isXander <xandersmith2008@gmail.com> | 2023-06-04 16:25:09 +0100 |
commit | 3e36feeef60e56ef8cb7f737ac8eeab9fbcd6abb (patch) | |
tree | f9c3395b4da2235681b87a35ac5056a0724a181b /common/src/main/java/dev/isxander/yacl3/impl/OptionDescriptionImpl.java | |
parent | d00a486d3bdf6105f8ca8af1034c384058b8c832 (diff) | |
download | YetAnotherConfigLib-3e36feeef60e56ef8cb7f737ac8eeab9fbcd6abb.tar.gz YetAnotherConfigLib-3e36feeef60e56ef8cb7f737ac8eeab9fbcd6abb.tar.bz2 YetAnotherConfigLib-3e36feeef60e56ef8cb7f737ac8eeab9fbcd6abb.zip |
Change package and modid to yacl3 and yet_another_config_lib_3 respectively
Diffstat (limited to 'common/src/main/java/dev/isxander/yacl3/impl/OptionDescriptionImpl.java')
-rw-r--r-- | common/src/main/java/dev/isxander/yacl3/impl/OptionDescriptionImpl.java | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/common/src/main/java/dev/isxander/yacl3/impl/OptionDescriptionImpl.java b/common/src/main/java/dev/isxander/yacl3/impl/OptionDescriptionImpl.java new file mode 100644 index 0000000..9ea9456 --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl3/impl/OptionDescriptionImpl.java @@ -0,0 +1,146 @@ +package dev.isxander.yacl3.impl; + +import dev.isxander.yacl3.api.OptionDescription; +import dev.isxander.yacl3.gui.ImageRenderer; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.MutableComponent; +import net.minecraft.resources.ResourceLocation; +import org.apache.commons.lang3.Validate; + +import java.io.FileInputStream; +import java.io.IOException; +import java.nio.file.Path; +import java.util.*; +import java.util.concurrent.CompletableFuture; + +public record OptionDescriptionImpl(Component text, CompletableFuture<Optional<ImageRenderer>> image) implements OptionDescription { + public static class BuilderImpl implements Builder { + private final List<Component> descriptionLines = new ArrayList<>(); + private CompletableFuture<Optional<ImageRenderer>> image = CompletableFuture.completedFuture(Optional.empty()); + private boolean imageUnset = true; + + @Override + public Builder text(Component... description) { + this.descriptionLines.addAll(Arrays.asList(description)); + return this; + } + + @Override + public Builder text(Collection<? extends Component> lines) { + this.descriptionLines.addAll(lines); + return this; + } + + @Override + public Builder image(ResourceLocation image, int width, int height) { + Validate.isTrue(imageUnset, "Image already set!"); + Validate.isTrue(width > 0, "Width must be greater than 0!"); + Validate.isTrue(height > 0, "Height must be greater than 0!"); + + this.image = ImageRenderer.getOrMakeSync(image, () -> Optional.of(new ImageRenderer.TextureBacked(image, 0, 0, width, height, width, height))); + imageUnset = false; + return this; + } + + @Override + public Builder image(ResourceLocation image, float u, float v, int width, int height, int textureWidth, int textureHeight) { + Validate.isTrue(imageUnset, "Image already set!"); + Validate.isTrue(width > 0, "Width must be greater than 0!"); + Validate.isTrue(height > 0, "Height must be greater than 0!"); + + this.image = ImageRenderer.getOrMakeSync(image, () -> Optional.of(new ImageRenderer.TextureBacked(image, u, v, width, height, textureWidth, textureHeight))); + imageUnset = false; + return this; + } + + @Override + public Builder image(Path path, ResourceLocation uniqueLocation) { + Validate.isTrue(imageUnset, "Image already set!"); + this.image = ImageRenderer.getOrMakeAsync(uniqueLocation, () -> ImageRenderer.NativeImageBacked.createFromPath(path, uniqueLocation)); + imageUnset = false; + return this; + } + + @Override + public Builder gifImage(ResourceLocation image) { + Validate.isTrue(imageUnset, "Image already set!"); + this.image = ImageRenderer.getOrMakeAsync(image, () -> { + try { + return Optional.of(ImageRenderer.AnimatedNativeImageBacked.createGIFFromTexture(image)); + } catch (IOException e) { + e.printStackTrace(); + return Optional.empty(); + } + }); + imageUnset = false; + return this; + } + + @Override + public Builder gifImage(Path path, ResourceLocation uniqueLocation) { + Validate.isTrue(imageUnset, "Image already set!"); + this.image = ImageRenderer.getOrMakeAsync(uniqueLocation, () -> { + try { + return Optional.of(ImageRenderer.AnimatedNativeImageBacked.createGIF(new FileInputStream(path.toFile()), uniqueLocation)); + } catch (IOException e) { + e.printStackTrace(); + return Optional.empty(); + } + }); + imageUnset = false; + return this; + } + + @Override + public Builder webpImage(ResourceLocation image) { + Validate.isTrue(imageUnset, "Image already set!"); + this.image = ImageRenderer.getOrMakeAsync(image, () -> { + try { + return Optional.of(ImageRenderer.AnimatedNativeImageBacked.createWEBPFromTexture(image)); + } catch (IOException e) { + e.printStackTrace(); + return Optional.empty(); + } + }); + imageUnset = false; + return this; + } + + @Override + public Builder webpImage(Path path, ResourceLocation uniqueLocation) { + Validate.isTrue(imageUnset, "Image already set!"); + this.image = ImageRenderer.getOrMakeAsync(uniqueLocation, () -> { + try { + return Optional.of(ImageRenderer.AnimatedNativeImageBacked.createWEBP(new FileInputStream(path.toFile()), uniqueLocation)); + } catch (IOException e) { + e.printStackTrace(); + return Optional.empty(); + } + }); + imageUnset = false; + return this; + } + + @Override + public Builder customImage(CompletableFuture<Optional<ImageRenderer>> image) { + Validate.notNull(image, "Image cannot be null!"); + Validate.isTrue(imageUnset, "Image already set!"); + + this.image = image; + this.imageUnset = false; + return this; + } + + @Override + public OptionDescription build() { + MutableComponent concatenatedDescription = Component.empty(); + Iterator<Component> iter = descriptionLines.iterator(); + while (iter.hasNext()) { + concatenatedDescription.append(iter.next()); + if (iter.hasNext()) concatenatedDescription.append("\n"); + } + + return new OptionDescriptionImpl(concatenatedDescription, image); + } + } +} |