68 lines
3.4 KiB
Java
68 lines
3.4 KiB
Java
package io.gitlab.jfronny.betterwhitelist;
|
|
|
|
import io.gitlab.jfronny.commons.log.Logger;
|
|
import io.gitlab.jfronny.muscript.StandardLib;
|
|
import io.gitlab.jfronny.muscript.data.Scope;
|
|
import io.gitlab.jfronny.muscript.data.dynamic.*;
|
|
import io.gitlab.jfronny.muscript.data.dynamic.additional.DFinal;
|
|
import net.fabricmc.loader.api.FabricLoader;
|
|
import net.fabricmc.loader.api.ModContainer;
|
|
import net.fabricmc.loader.api.metadata.ModDependency;
|
|
import net.fabricmc.loader.api.metadata.ModMetadata;
|
|
import net.minecraft.util.Identifier;
|
|
|
|
import java.util.Map;
|
|
import java.util.stream.Collectors;
|
|
|
|
import static io.gitlab.jfronny.muscript.data.dynamic.additional.DFinal.of;
|
|
|
|
public class BetterWhitelist {
|
|
public static final String MOD_ID = "better-whitelist";
|
|
public static final Logger LOG = Logger.forName(MOD_ID);
|
|
public static final Identifier HANDSHAKE_CHANNEL = new Identifier(MOD_ID, "handshake");
|
|
public static final Identifier CHALLENGE_CHANNEL = new Identifier(MOD_ID, "challenge");
|
|
public static final int PROTOCOL_VERSION = 1;
|
|
public static final ModMetadata MOD_METADATA = FabricLoader.getInstance().getModContainer(MOD_ID).orElseThrow().getMetadata();
|
|
public static final Scope SCOPE = StandardLib.createScope();
|
|
|
|
static {
|
|
SCOPE.set("mods", FabricLoader.getInstance()
|
|
.getAllMods()
|
|
.stream()
|
|
.collect(Collectors.toMap(
|
|
a -> a.getMetadata().getId(),
|
|
BetterWhitelist::wrap
|
|
))
|
|
).set("mod", args -> {
|
|
if (args.size() != 1) throw new IllegalArgumentException("Invalid number of arguments for mod: expected 1 but got " + args.size());
|
|
return FabricLoader.getInstance()
|
|
.getModContainer(args.get(0).asString().getValue())
|
|
.<Dynamic<?>>map(BetterWhitelist::wrap)
|
|
.orElse(new DNull());
|
|
}).set("println", args -> {
|
|
String s = args.size() == 0 ? "" : args.size() == 1 ? args.get(0).asString().getValue() : args.toString();
|
|
System.out.println(s);
|
|
return of(s);
|
|
});
|
|
}
|
|
|
|
private static DObject wrap(ModContainer mod) {
|
|
return of(Map.of(
|
|
"id", of(mod.getMetadata().getId()),
|
|
"name", of(mod.getMetadata().getName()),
|
|
"description", of(mod.getMetadata().getDescription()),
|
|
"authors", of(mod.getMetadata().getAuthors().stream().<Dynamic<?>>map(s -> of(s.getName())).toList()),
|
|
"contributors", of(mod.getMetadata().getContributors().stream().<Dynamic<?>>map(s -> of(s.getName())).toList()),
|
|
"version", of(mod.getMetadata().getVersion().getFriendlyString()),
|
|
"environment", of(switch (mod.getMetadata().getEnvironment()) {
|
|
case CLIENT -> "client";
|
|
case SERVER -> "server";
|
|
case UNIVERSAL -> "*";
|
|
}),
|
|
"license", of(mod.getMetadata().getLicense().stream().<Dynamic<?>>map(DFinal::of).toList()),
|
|
"contact", of(mod.getMetadata().getContact().asMap().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, a -> of(a.getValue())))),
|
|
"depends", of(mod.getMetadata().getDependencies().stream().filter(v -> v.getKind() == ModDependency.Kind.DEPENDS).<Dynamic<?>>map(s -> of(s.getModId())).toList())
|
|
));
|
|
}
|
|
}
|