Skip to content

Commit b9c383d

Browse files
committed
Release 2.2.7
-Added more resource methods to `ModInfo` to handle NeoForge changes -Added `FactoryConfigDisplay.Builder` class together with `FactoryConfigDisplay.valueToComponent` and `FactoryConfigDisplay.messageFunction` methods to have an easier building and wider compatibility with widgets
1 parent aa227f1 commit b9c383d

13 files changed

Lines changed: 254 additions & 115 deletions

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ publishMods {
249249

250250
def isFabric = loader == "fabric"
251251
displayName = "$mod_name [$modPlatformName] $project.version"
252-
type = BETA
252+
type = STABLE
253253
changelog = releaseChangelog()
254254
modLoaders.add(loader)
255255
if (isFabric) modLoaders.add "quilt"

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
org.gradle.jvmargs=-Xmx3072M
22

33
archives_base_name=FactoryAPI
4-
mod_version=2.2.6.2545.1
4+
mod_version=2.2.7
55
mod_id=factory_api
66
mod_name=Factory API
77
mod_description=An API that is the basis for mods like Legacy4J, Factocrafty and Better Furnaces Reforged, that work on different mod loaders.

src/main/java/wily/factoryapi/FactoryAPIPlatform.java

Lines changed: 69 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@
134134
import wily.factoryapi.util.ListMap;
135135
import wily.factoryapi.util.ModInfo;
136136

137+
import java.io.IOException;
138+
import java.io.InputStream;
139+
import java.net.URI;
137140
import java.net.URL;
138141
import java.nio.file.Files;
139142
import java.nio.file.Path;
@@ -624,70 +627,83 @@ public boolean isHidden() {
624627
};
625628
//?} else if forge || neoforge {
626629
/*return new ModInfo() {
627-
IModFileInfo info = ModList.get().getModFileById(modId);
628-
Optional<? extends ModContainer> opt = ModList.get().getModContainerById(modId);
629-
@Override
630-
public Collection<String> getAuthors() {
631-
return opt.flatMap(c->c.getModInfo().getConfig().getConfigElement("authors").map(s-> Collections.singleton(String.valueOf(s)))).orElse(Collections.emptySet());
632-
}
630+
IModFileInfo info = ModList.get().getModFileById(modId);
631+
Optional<? extends ModContainer> opt = ModList.get().getModContainerById(modId);
632+
@Override
633+
public Collection<String> getAuthors() {
634+
return opt.flatMap(c->c.getModInfo().getConfig().getConfigElement("authors").map(s-> Collections.singleton(String.valueOf(s)))).orElse(Collections.emptySet());
635+
}
633636
634-
@Override
635-
public Optional<String> getHomepage() {
636-
return opt.flatMap(c->c.getModInfo().getConfig().getConfigElement("displayURL").map(String::valueOf));
637-
}
637+
@Override
638+
public Optional<String> getHomepage() {
639+
return opt.flatMap(c->c.getModInfo().getConfig().getConfigElement("displayURL").map(String::valueOf));
640+
}
638641
639-
@Override
640-
public Optional<String> getIssues() {
641-
return Optional.ofNullable(info instanceof ModFileInfo i ? i.getIssueURL() : null).map(URL::toString);
642-
}
642+
@Override
643+
public Optional<String> getIssues() {
644+
return Optional.ofNullable(info instanceof ModFileInfo i ? i.getIssueURL() : null).map(URL::toString);
645+
}
643646
644-
@Override
645-
public Optional<String> getSources() {
646-
return Optional.empty();
647-
}
647+
@Override
648+
public Optional<String> getSources() {
649+
return Optional.empty();
650+
}
648651
649-
@Override
650-
public Collection<String> getCredits() {
651-
return opt.flatMap(c->c.getModInfo().getConfig().getConfigElement("credits").map(o-> Set.of(String.valueOf(o)))).orElse(Collections.emptySet());
652-
}
652+
@Override
653+
public Collection<String> getCredits() {
654+
return opt.flatMap(c->c.getModInfo().getConfig().getConfigElement("credits").map(o-> Set.of(String.valueOf(o)))).orElse(Collections.emptySet());
655+
}
653656
654-
@Override
655-
public Collection<String> getLicense() {
656-
return Collections.singleton(info.getLicense());
657-
}
657+
@Override
658+
public Collection<String> getLicense() {
659+
return Collections.singleton(info.getLicense());
660+
}
658661
659-
@Override
660-
public String getDescription() {
661-
return opt.map(c->c.getModInfo().getDescription()).orElse("");
662-
}
662+
@Override
663+
public String getDescription() {
664+
return opt.map(c->c.getModInfo().getDescription()).orElse("");
665+
}
663666
664-
@Override
665-
public Optional<String> getLogoFile(int i) {
666-
return this.info.getMods().stream().filter(m->m.getModId().equals(modId)).findFirst().flatMap(IModInfo::getLogoFile);
667-
}
668-
@Override
669-
public Optional<Path> findResource(String s) {
670-
//? if forge || (neoforge && <1.21.9) {
671-
/^return Optional.of(this.info.getFile().findResource(s)).filter(Files::exists);
672-
^///?} else if neoforge {
673-
/^return Optional.of(this.info.getFile().getFilePath().resolve(s));
667+
@Override
668+
public Optional<String> getLogoFile(int i) {
669+
return this.info.getMods().stream().filter(m->m.getModId().equals(modId)).findFirst().flatMap(IModInfo::getLogoFile);
670+
}
671+
672+
@Override
673+
public Optional<Path> findResource(String s) {
674+
//? if forge || (neoforge && <1.21.9) {
675+
/^return Optional.of(this.info.getFile().findResource(s)).filter(Files::exists);
676+
^///?} else if neoforge {
677+
/^return Optional.of(this.info.getFile().getFilePath().resolve(s));
678+
^///?}
679+
}
680+
681+
//? if neoforge && >=1.21.9 {
682+
/^@Override
683+
public boolean containsResource(String s) {
684+
return this.info.getFile().getContents().containsFile(s);
685+
}
686+
687+
@Override
688+
public InputStream openResource(String s) throws IOException {
689+
return this.info.getFile().getContents().openFile(s);
690+
}
674691
^///?}
675-
}
676692
677-
@Override
678-
public String getId() {
679-
return modId;
680-
}
693+
@Override
694+
public String getId() {
695+
return modId;
696+
}
681697
682-
@Override
683-
public String getVersion() {
684-
return opt.map(c->c.getModInfo().getVersion().toString()).orElse("");
685-
}
698+
@Override
699+
public String getVersion() {
700+
return opt.map(c->c.getModInfo().getVersion().toString()).orElse("");
701+
}
686702
687-
@Override
688-
public String getName() {
689-
return opt.map(c->c.getModInfo().getDisplayName()).orElse("");
690-
}
703+
@Override
704+
public String getName() {
705+
return opt.map(c->c.getModInfo().getDisplayName()).orElse("");
706+
}
691707
692708
};
693709
*///?} else

src/main/java/wily/factoryapi/FactoryEvent.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,13 @@
3939
import net.minecraftforge.network.payload.PayloadProtocol;
4040
^///?}
4141
*///?} elif neoforge {
42-
/*import net.neoforged.fml.ModList;
42+
/*import net.minecraft.server.packs.repository.BuiltInPackSource;
43+
import net.neoforged.fml.ModList;
4344
import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent;
45+
//? if >=1.21.9 {
46+
import net.neoforged.fml.jarcontents.JarContents;
47+
import net.neoforged.neoforge.resource.JarContentsPackResources;
48+
//?}
4449
import net.neoforged.fml.loading.FMLEnvironment;
4550
import net.neoforged.neoforge.common.NeoForge;
4651
import net.neoforged.neoforge.event.*;
@@ -307,7 +312,7 @@ public static void registerBuiltInPacks(Consumer<PackRegistry> registry){
307312
Path resourcePath = ModList.get().getModFileById(name.getNamespace()).getFile().findResource(path);
308313
for (PackType type : PackType.values()) {
309314
if (event.getPackType() != type || !Files.isDirectory(resourcePath.resolve(type.getDirectory()))) continue;
310-
Pack pack = createBuiltInPack(name, displayName, defaultEnabled,type,position,resourcePath);
315+
Pack pack = createBuiltInPack(name, displayName, defaultEnabled, type, position, resourcePath);
311316
event.addRepositorySource((packConsumer) -> packConsumer.accept(pack));
312317
}
313318
}));
@@ -316,7 +321,7 @@ public static void registerBuiltInPacks(Consumer<PackRegistry> registry){
316321
Path resourcePath = ModList.get().getModFileById(name.getNamespace()).getFile().findResource(path);
317322
for (PackType type : PackType.values()) {
318323
if (event.getPackType() != type || !Files.isDirectory(resourcePath.resolve(type.getDirectory()))) continue;
319-
Pack pack = createBuiltInPack(name, displayName, defaultEnabled,type,position,resourcePath);
324+
Pack pack = createBuiltInPack(name, displayName, defaultEnabled, type, position, resourcePath);
320325
event.addRepositorySource((packConsumer) -> packConsumer.accept(pack));
321326
}
322327
}));
@@ -325,16 +330,19 @@ public static void registerBuiltInPacks(Consumer<PackRegistry> registry){
325330
Path resourcePath = ModList.get().getModFileById(name.getNamespace()).getFile().findResource(path);
326331
for (PackType type : PackType.values()) {
327332
if (event.getPackType() != type || !Files.isDirectory(resourcePath.resolve(type.getDirectory()))) continue;
328-
Pack pack = createBuiltInPack(name, displayName, defaultEnabled,type,position,resourcePath);
333+
Pack pack = createBuiltInPack(name, displayName, defaultEnabled, type, position, resourcePath);
329334
event.addRepositorySource((packConsumer) -> packConsumer.accept(pack));
330335
}
331336
}));
332337
*///?} elif neoforge {
333338
/*FactoryAPIPlatform.getModEventBus().addListener(EventPriority.NORMAL, false, AddPackFindersEvent.class, event-> registry.accept((path, name, displayName, position, defaultEnabled) -> {
334-
Path resourcePath = ModList.get().getModFileById(name.getNamespace()).getFile().getFilePath().resolve(path);
339+
var modInfo = ModList.get().getModFileById(name.getNamespace());
335340
for (PackType type : PackType.values()) {
336-
if (event.getPackType() != type || !Files.isDirectory(resourcePath.resolve(type.getDirectory()))) continue;
337-
Pack pack = createBuiltInPack(name, displayName, defaultEnabled,type,position,resourcePath);
341+
if (event.getPackType() != type) continue;
342+
Pack pack = Pack.readMetaAndCreate(new PackLocationInfo(name.toString(), displayName, PackSource.create(PackSource.BUILT_IN::decorate, defaultEnabled), Optional.of(new KnownPack(name.getNamespace(), name.toString(), SharedConstants.getCurrentVersion().id()))), BuiltInPackSource.fromName((locationInfo) -> {
343+
JarContents contents = modInfo.getFile().getContents();
344+
return new JarContentsPackResources(locationInfo, contents, path);
345+
}), type, new PackSelectionConfig(false, position, false));
338346
event.addRepositorySource((packConsumer) -> packConsumer.accept(pack));
339347
}
340348
}));

src/main/java/wily/factoryapi/base/client/FactoryConfigWidgets.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import net.minecraft.network.chat.Component;
1414
import wily.factoryapi.base.config.FactoryConfig;
1515
import wily.factoryapi.base.config.FactoryConfigControl;
16+
import wily.factoryapi.base.config.FactoryConfigDisplay;
1617

1718

1819
import java.io.StringReader;
@@ -24,15 +25,15 @@ public class FactoryConfigWidgets {
2425
private static final List<WidgetOverride<?>> overrides = new ArrayList<>();
2526
public static final Function<Component,Tooltip> TOOLTIP_CACHE = Util.memoize(c->Tooltip.create(c));
2627

27-
public static Tooltip getCachedTooltip(Component component){
28+
public static Tooltip getCachedTooltip(Component component) {
2829
return component == null ? null : TOOLTIP_CACHE.apply(component);
2930
}
3031

3132
public interface WidgetOverride<T> {
3233
AbstractWidget createWidget(FactoryConfig<T> config, Function<T,Tooltip> tooltip, int x, int y, int width, Consumer<T> afterSet);
3334
}
3435

35-
public static <T> AbstractWidget getOverride(FactoryConfig<T> config, Function<T,Tooltip> tooltipFunction, int x, int y, int width, Consumer<T> afterSet){
36+
public static <T> AbstractWidget getOverride(FactoryConfig<T> config, Function<T,Tooltip> tooltipFunction, int x, int y, int width, Consumer<T> afterSet) {
3637
for (WidgetOverride<?> override : overrides) {
3738
AbstractWidget widgetOverride = ((WidgetOverride<T>)override).createWidget(config, tooltipFunction, x, y, width, afterSet);
3839
if (widgetOverride != null) return widgetOverride;
@@ -41,29 +42,30 @@ public static <T> AbstractWidget getOverride(FactoryConfig<T> config, Function<T
4142
}
4243

4344
public static <T> AbstractWidget createWidget(FactoryConfig<T> config, int x, int y, int width, Consumer<T> afterSet) {
44-
Function<T,Tooltip> tooltipFunction = v-> getCachedTooltip(config.getDisplay().tooltip().apply(v));
45+
FactoryConfigDisplay<T> display = config.getDisplay();
46+
Function<T,Tooltip> tooltipFunction = v -> getCachedTooltip(display.tooltip().apply(v));
4547
AbstractWidget override = getOverride(config, tooltipFunction, x, y, width, afterSet);
4648
if (override != null) return override;
47-
if (config.control().equals(FactoryConfigControl.TOGGLE)){
48-
return CycleButton.<Boolean>builder(b-> config.getDisplay().captionFunction().apply(config.getDisplay().name(), (T) b)).withValues(OptionInstance.BOOLEAN_VALUES.valueListSupplier()).withTooltip(((Function<Boolean, Tooltip>) tooltipFunction)::apply).withInitialValue((Boolean) config.get()).create(x, y, width, 20, config.getDisplay().name(), (cycleButton, object) -> FactoryConfig.saveOptionAndConsume(config, (T)object, afterSet));
49-
} else if (config.control() instanceof FactoryConfigControl.FromInt<T> c){
50-
return CycleButton.<T>builder(b-> config.getDisplay().captionFunction().apply(config.getDisplay().name(), b)).withValues(listSupplier(c.valueGetter(), c.valuesSize())).withTooltip(tooltipFunction::apply).withInitialValue(config.get()).create(x, y, width, 20, config.getDisplay().name(), (cycleButton, object) -> FactoryConfig.saveOptionAndConsume(config, object,afterSet));
51-
} else if (config.control() instanceof FactoryConfigControl.FromDouble<T> c){
49+
if (config.control().equals(FactoryConfigControl.TOGGLE)) {
50+
return CycleButton.<Boolean>builder(b -> display.valueToComponent().apply((T) b)).withValues(OptionInstance.BOOLEAN_VALUES.valueListSupplier()).withTooltip(((Function<Boolean, Tooltip>) tooltipFunction)::apply).withInitialValue((Boolean) config.get()).create(x, y, width, 20, display.name(), (cycleButton, object) -> FactoryConfig.saveOptionAndConsume(config, (T)object, afterSet));
51+
} else if (config.control() instanceof FactoryConfigControl.FromInt<T> c) {
52+
return CycleButton.builder(display.valueToComponent()).withValues(listSupplier(c.valueGetter(), c.valuesSize())).withTooltip(tooltipFunction::apply).withInitialValue(config.get()).create(x, y, width, 20, display.name(), (cycleButton, object) -> FactoryConfig.saveOptionAndConsume(config, object,afterSet));
53+
} else if (config.control() instanceof FactoryConfigControl.FromDouble<T> c) {
5254
return createSlider(config, x, y, width, afterSet, c.valueGetter(), c.valueSetter(), tooltipFunction);
5355
} else if (config.control() instanceof FactoryConfigControl.Int c) {
5456
return createSlider((FactoryConfig<Integer>)config, x, y, width, (Consumer<Integer>)afterSet, d-> (int)((c.max().getAsInt() - c.min()) * d) + c.min(), i-> (double)(i - c.min()) / (c.max().getAsInt() - c.min()), (Function<Integer, Tooltip>) tooltipFunction);
55-
} else if (config.control() instanceof FactoryConfigControl.TextEdit<T> c){
56-
EditBox editBox = new EditBox(Minecraft.getInstance().font, x, y, width, 20, config.getDisplay().name());
57+
} else if (config.control() instanceof FactoryConfigControl.TextEdit<T> c) {
58+
EditBox editBox = new EditBox(Minecraft.getInstance().font, x, y, width, 20, display.name());
5759

5860
c.codec().encodeStart(JsonOps.INSTANCE, config.get()).result().ifPresent(v-> editBox.setValue(v.toString()));
59-
editBox.setResponder(s->{
61+
editBox.setResponder(s -> {
6062
DataResult<T> result;
6163
try {
6264
result = c.codec().parse(JsonOps.INSTANCE, JsonParser.parseReader(new StringReader(s)));
6365
} catch (JsonIOException | JsonSyntaxException e) {
6466
result = DataResult.error(e::getMessage);
6567
}
66-
if (result.result().isPresent()){
68+
if (result.result().isPresent()) {
6769
editBox.setTextColor(0xE0E0E0);
6870
config.set(result.result().get());
6971
config.save();
@@ -78,11 +80,11 @@ public static <T> AbstractWidget createWidget(FactoryConfig<T> config, int x, in
7880
return null;
7981
}
8082

81-
public static <T> AbstractSliderButton createSlider(FactoryConfig<T> config, int x, int y, int width, Consumer<T> afterSet, Function<Double,T> valueGetter, Function<T,Double> valueSetter, Function<T,Tooltip> tooltipFunction){
82-
return new AbstractSliderButton(x, y, width, 20, config.getDisplay().captionFunction().apply(config.getDisplay().name(), config.get()), valueSetter.apply(config.get())) {
83+
public static <T> AbstractSliderButton createSlider(FactoryConfig<T> config, int x, int y, int width, Consumer<T> afterSet, Function<Double,T> valueGetter, Function<T,Double> valueSetter, Function<T,Tooltip> tooltipFunction) {
84+
return new AbstractSliderButton(x, y, width, 20, config.getDisplay().getMessage(config.get()), valueSetter.apply(config.get())) {
8385
@Override
8486
protected void updateMessage() {
85-
setMessage(config.getDisplay().captionFunction().apply(config.getDisplay().name(), valueGetter.apply(value)));
87+
setMessage(config.getDisplay().getMessage(valueGetter.apply(value)));
8688
setTooltip(tooltipFunction.apply(valueGetter.apply(value)));
8789
}
8890

@@ -98,7 +100,7 @@ public static <T> AbstractWidget createWidget(FactoryConfig<T> config) {
98100
return createWidget(config, 0, 0, 0, v-> {});
99101
}
100102

101-
public static <T> CycleButton.ValueListSupplier<T> listSupplier(Function<Integer,T> valueGetter, Supplier<Integer> valuesSize){
103+
public static <T> CycleButton.ValueListSupplier<T> listSupplier(Function<Integer,T> valueGetter, Supplier<Integer> valuesSize) {
102104
List<T> list = new ArrayList<>();
103105
for (int i = 0; i < valuesSize.get(); i++) {
104106
list.add(valueGetter.apply(i));

0 commit comments

Comments
 (0)