1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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);
}
}
}
|