74 lines
2.4 KiB
Java
74 lines
2.4 KiB
Java
|
package com.thebrokenrail.freshcoffee.util.limit;
|
||
|
|
||
|
import com.thebrokenrail.freshcoffee.config.HardcodedConfig;
|
||
|
import com.thebrokenrail.freshcoffee.util.Util;
|
||
|
import net.fabricmc.loader.api.FabricLoader;
|
||
|
import net.fabricmc.loader.api.ModContainer;
|
||
|
import net.fabricmc.loader.api.metadata.ModMetadata;
|
||
|
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.Collections;
|
||
|
import java.util.List;
|
||
|
|
||
|
public class Limit {
|
||
|
private final boolean isMinimum;
|
||
|
private int value;
|
||
|
private List<String> source = singleton("default");
|
||
|
|
||
|
private static String modToString(ModMetadata metadata) {
|
||
|
return metadata.getName() + " (" + metadata.getId() + ')';
|
||
|
}
|
||
|
|
||
|
private static List<String> singleton(String value) {
|
||
|
return new ArrayList<>(Collections.singleton(value));
|
||
|
}
|
||
|
|
||
|
Limit(boolean isMinimum) {
|
||
|
this.isMinimum = isMinimum;
|
||
|
value = isMinimum ? 0 : Integer.MAX_VALUE;
|
||
|
|
||
|
String key = isMinimum ? HardcodedConfig.MINIMUM_JSON_KEY : HardcodedConfig.MAXIMUM_JSON_KEY;
|
||
|
|
||
|
for (ModContainer mod : FabricLoader.getInstance().getAllMods()) {
|
||
|
ModMetadata metadata = mod.getMetadata();
|
||
|
if (metadata.containsCustomValue(key)) {
|
||
|
try {
|
||
|
int newValue = metadata.getCustomValue(key).getAsNumber().intValue();
|
||
|
update(newValue, modToString(metadata));
|
||
|
} catch (ClassCastException e) {
|
||
|
Util.getLogger().error("Invalid " + (isMinimum ? "Minimum" : "Maximum") + " Java Version Specified: " + metadata.getId());
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void update(int value, String source) {
|
||
|
if (isMinimum ? value > this.value : value < this.value) {
|
||
|
this.value = value;
|
||
|
this.source = singleton(source);
|
||
|
} else if (value == this.value) {
|
||
|
this.source.add(source);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public int getValue() {
|
||
|
return value;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public String toString() {
|
||
|
StringBuilder sourceBuilder = new StringBuilder();
|
||
|
for (int i = 0; i < source.size(); i++) {
|
||
|
if (i != 0) {
|
||
|
sourceBuilder.append(i == 1 && source.size() - 1 == 1 ? ' ' : ", ");
|
||
|
if (i == source.size() - 1) {
|
||
|
sourceBuilder.append("and ");
|
||
|
}
|
||
|
}
|
||
|
sourceBuilder.append(source.get(i));
|
||
|
}
|
||
|
|
||
|
return String.format(HardcodedConfig.LIMIT_TEXT, value, sourceBuilder.toString());
|
||
|
}
|
||
|
}
|