109 lines
4.2 KiB
Java
109 lines
4.2 KiB
Java
package io.gitlab.jfronny.respackopts.util;
|
|
|
|
import io.gitlab.jfronny.muscript.data.dynamic.*;
|
|
import io.gitlab.jfronny.muscript.StandardLib;
|
|
import io.gitlab.jfronny.muscript.data.Scope;
|
|
import net.fabricmc.loader.api.FabricLoader;
|
|
import net.fabricmc.loader.api.VersionParsingException;
|
|
import net.fabricmc.loader.api.metadata.version.VersionPredicate;
|
|
|
|
import java.time.LocalDate;
|
|
import java.time.LocalTime;
|
|
import java.util.Map;
|
|
import java.util.function.Supplier;
|
|
|
|
public class MuUtils {
|
|
public static Scope addDefault(Scope parameter) {
|
|
StandardLib.addTo(parameter);
|
|
parameter.set("version", args -> {
|
|
if (args.size() != 2) throw new IllegalArgumentException("Expected 2 arguments on version but got " + args.size());
|
|
VersionPredicate predicate;
|
|
try {
|
|
predicate = VersionPredicate.parse(args.get(1).asString().getValue());
|
|
} catch (VersionParsingException e) {
|
|
throw new RuntimeException("Could not parse version predicate", e);
|
|
}
|
|
return DFinal.of(FabricLoader.getInstance().getModContainer(args.get(0).asString().getValue())
|
|
.map(c -> predicate.test(c.getMetadata().getVersion()))
|
|
.orElse(false));
|
|
});
|
|
parameter.set("date", new DCallableObject(Map.of(
|
|
"today", new DDate(LocalDate::now)
|
|
), DFinal.of(args -> {
|
|
// Constructor
|
|
if (args.size() == 1) return new DDate(() -> LocalDate.ofEpochDay(args.get(0).asNumber().getValue().longValue()));
|
|
if (args.size() != 3) throw new IllegalArgumentException("Expected 3 arguments for full date constructor");
|
|
int a0 = args.get(0).asNumber().getValue().intValue();
|
|
int a1 = args.get(1).asNumber().getValue().intValue();
|
|
int a2 = args.get(2).asNumber().getValue().intValue();
|
|
return new DDate(() -> LocalDate.of(a0, a1, a2));
|
|
})));
|
|
parameter.set("time", new DCallableObject(Map.of(
|
|
"now", new DTime(LocalTime::now)
|
|
), DFinal.of(args -> {
|
|
// Constructor
|
|
if (args.size() == 1) return new DTime(() -> LocalTime.ofSecondOfDay(args.get(0).asNumber().getValue().intValue()));
|
|
if (args.size() != 3) throw new IllegalArgumentException("Expected 3 arguments for full time constructor");
|
|
int a0 = args.get(0).asNumber().getValue().intValue();
|
|
int a1 = args.get(1).asNumber().getValue().intValue();
|
|
int a2 = args.get(2).asNumber().getValue().intValue();
|
|
return new DTime(() -> LocalTime.of(a0, a1, a2));
|
|
})));
|
|
return parameter;
|
|
}
|
|
|
|
private record DCallableObject(Map<String, Dynamic<?>> value, DCallable callable) implements DObject {
|
|
@Override
|
|
public Map<String, Dynamic<?>> getValue() {
|
|
return value;
|
|
}
|
|
|
|
@Override
|
|
public DCallable asCallable() {
|
|
return callable;
|
|
}
|
|
}
|
|
|
|
private record DDate(Supplier<LocalDate> date) implements DObject {
|
|
@Override
|
|
public Map<String, Dynamic<?>> getValue() {
|
|
return Map.of(
|
|
"year", DFinal.of(date.get().getYear()),
|
|
"month", DFinal.of(date.get().getMonthValue()),
|
|
"day", DFinal.of(date.get().getDayOfMonth())
|
|
);
|
|
}
|
|
|
|
@Override
|
|
public DString asString() {
|
|
return DFinal.of(date.get().toString());
|
|
}
|
|
|
|
@Override
|
|
public DNumber asNumber() {
|
|
return DFinal.of(date.get().toEpochDay());
|
|
}
|
|
}
|
|
|
|
private record DTime(Supplier<LocalTime> time) implements DObject {
|
|
@Override
|
|
public Map<String, Dynamic<?>> getValue() {
|
|
return Map.of(
|
|
"hour", DFinal.of(time.get().getHour()),
|
|
"minute", DFinal.of(time.get().getMinute()),
|
|
"second", DFinal.of(time.get().getSecond())
|
|
);
|
|
}
|
|
|
|
@Override
|
|
public DString asString() {
|
|
return DFinal.of(time.get().toString());
|
|
}
|
|
|
|
@Override
|
|
public DNumber asNumber() {
|
|
return DFinal.of(time.get().toSecondOfDay());
|
|
}
|
|
}
|
|
}
|