Skip to content

Commit

Permalink
fixed registry things
Browse files Browse the repository at this point in the history
now registry-based event should work properly
  • Loading branch information
Prunoideae committed Apr 4, 2022
1 parent f0f3f99 commit 3ff929d
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ apply from: 'https://files.latmod.com/public/markdown-git-changelog.gradle'
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_17

def ENV = System.getenv()
version = "${mod_version}-build.62"
version = "${mod_version}-build.63"
archivesBaseName = project.archives_base_name
group = project.maven_group

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ org.gradle.daemon=false
loom.platform=forge
mod_id=probejs
archives_base_name=probejs
mod_version=2.4.0
mod_version=2.4.5
maven_group=com.prunoideae
mod_author=Prunoideae
minecraft_version=1.18.2
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/prunoideae/probejs/ProbeCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.gson.JsonObject;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.prunoideae.probejs.compiler.RegistryCompiler;
import com.prunoideae.probejs.compiler.SnippetCompiler;
import com.prunoideae.probejs.compiler.TypingCompiler;
import com.prunoideae.probejs.document.Manager;
Expand Down Expand Up @@ -100,6 +101,12 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
return Command.SINGLE_SUCCESS;
}))
)
.then(Commands.literal("test")
.requires(source -> source.getServer().isSingleplayer() || source.hasPermission(2))
.executes(context -> {
RegistryCompiler.getBuilderTypes();
return Command.SINGLE_SUCCESS;
}))
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.prunoideae.probejs.compiler;

import com.google.gson.Gson;
import com.prunoideae.probejs.ProbePaths;
import com.prunoideae.probejs.formatter.formatter.FormatterClass;
import com.prunoideae.probejs.formatter.formatter.FormatterNamespace;
import com.prunoideae.probejs.formatter.formatter.IFormatter;
import com.prunoideae.probejs.info.type.TypeInfoClass;
import dev.latvian.mods.kubejs.RegistryObjectBuilderTypes;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.util.*;
import java.util.stream.Collectors;

public class RegistryCompiler {
public static Set<Class<?>> getRegistryClasses() {
Set<Class<?>> result = new HashSet<>();
result.add(RegistryObjectBuilderTypes.class);
result.add(RegistryObjectBuilderTypes.RegistryEventJS.class);
RegistryObjectBuilderTypes.MAP.values().forEach(v -> v.types.values().forEach(v1 -> result.add(v1.builderClass())));
return result;
}

public static void compileEventRegistries(BufferedWriter writer) throws IOException {
Gson stringG = new Gson();
for (var types : RegistryObjectBuilderTypes.MAP.values()) {
String fullName = types.registryKey.location().getNamespace() + "." + types.registryKey.location().getPath().replace('/', '.') + ".registry";
String registryName = FormatterRegistry.getFormattedRegistryName(types);
writer.write("declare function onEvent(name: %s, handler: (event: Registry.%s) => void);\n".formatted(stringG.toJson(fullName), registryName));
if (types.registryKey.location().getNamespace().equals("minecraft")) {
String shortName = types.registryKey.location().getPath().replace('/', '.') + ".registry";
writer.write("declare function onEvent(name: %s, handler: (event: Registry.%s) => void);\n".formatted(stringG.toJson(shortName), registryName));
}
}
}

public static void compileRegistries() throws IOException {
BufferedWriter writer = Files.newBufferedWriter(ProbePaths.GENERATED.resolve("registries.d.ts"));
writer.write("/// <reference path=\"./globals.d.ts\" />\n");
IFormatter namespace = new FormatterNamespace("Registry", RegistryObjectBuilderTypes.MAP.values().stream().map(FormatterRegistry::new).collect(Collectors.toList()));
writer.write(String.join("\n", namespace.format(0, 4)));
writer.flush();
}

public static void getBuilderTypes() {
RegistryObjectBuilderTypes.MAP.forEach((k, v) -> {
System.out.println(k.registry());
v.types.forEach((k1, v1) -> {
System.out.println(" " + k1);
System.out.println(" " + v1.builderClass());
});
});
}

private static class FormatterRegistry implements IFormatter {
RegistryObjectBuilderTypes<?> types;
String name;

private static String getFormattedRegistryName(RegistryObjectBuilderTypes<?> types) {
return Arrays.stream(types.registryKey.location().getPath().split("/")).map(str -> str.substring(0, 1).toUpperCase() + str.substring(1)).collect(Collectors.joining(""));
}

private FormatterRegistry(RegistryObjectBuilderTypes<?> types) {
this.types = types;
this.name = getFormattedRegistryName(types);
}

@Override
public List<String> format(Integer indent, Integer stepIndent) {
List<String> formatted = new ArrayList<>();
int stepped = indent + stepIndent;
Gson stringG = new Gson();
formatted.add(" ".repeat(indent) + "class %s extends %s {".formatted(name, FormatterClass.formatTypeParameterized(new TypeInfoClass(RegistryObjectBuilderTypes.RegistryEventJS.class))));
for (RegistryObjectBuilderTypes.BuilderType<?> builder : types.types.values()) {
formatted.add(" ".repeat(stepped) + "create(id: string, type: %s): %s;".formatted(stringG.toJson(builder.type()), FormatterClass.formatTypeParameterized(new TypeInfoClass(builder.builderClass()))));
}
formatted.add(" ".repeat(indent) + "}");
return formatted;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public static void compileEvents(Map<String, Class<?>> cachedEvents, Map<String,
cachedForgeEvents.putAll(WrappedForgeEventHandler.capturedEvents);
BufferedWriter writer = Files.newBufferedWriter(ProbePaths.GENERATED.resolve("events.d.ts"));
writer.write("/// <reference path=\"./globals.d.ts\" />\n");
writer.write("/// <reference path=\"./registries.d.ts\" />\n");
for (Map.Entry<String, Class<?>> entry : cachedEvents.entrySet()) {
String name = entry.getKey();
Class<?> event = entry.getValue();
Expand All @@ -143,6 +144,7 @@ public static void compileEvents(Map<String, Class<?>> cachedEvents, Map<String,
Class<?> event = entry.getValue();
writer.write("declare function onForgeEvent(name: \"%s\", handler: (event: %s) => void);\n".formatted(name, FormatterClass.formatTypeParameterized(new TypeInfoClass(event))));
}
RegistryCompiler.compileEventRegistries(writer);
writer.flush();
}

Expand Down Expand Up @@ -193,10 +195,12 @@ public static void compile() throws IOException {
Map<String, Class<?>> cachedForgeEvents = readCachedEvents("cachedForgeEvents.json");
Set<Class<?>> cachedClasses = new HashSet<>(cachedEvents.values());
cachedClasses.addAll(cachedForgeEvents.values());
cachedClasses.addAll(RegistryCompiler.getRegistryClasses());
Set<Class<?>> globalClasses = fetchClasses(typeMap, bindingEvent, cachedClasses);
globalClasses.removeIf(c -> ClassResolver.skipped.contains(c));

compileGlobal(bindingEvent, globalClasses);
RegistryCompiler.compileRegistries();
compileEvents(cachedEvents, cachedForgeEvents);
compileConstants(bindingEvent);
compileJava(globalClasses);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.prunoideae.probejs.formatter.formatter.FormatterType;
import com.prunoideae.probejs.info.type.TypeInfoParameterized;
import dev.latvian.mods.kubejs.util.BuilderBase;
import net.minecraft.nbt.CompoundTag;

import java.util.function.*;
Expand All @@ -23,6 +24,9 @@ public static void init() {
return "(arg0: any) => boolean";
});
NameResolver.putTypeFormatter(Supplier.class, t -> {
if (BuilderBase.class.isAssignableFrom(t.getResolvedClass())) {
return new FormatterType(t, false).format(0, 0);
}
if (t instanceof TypeInfoParameterized parType && parType.getParamTypes().size() == 1) {
String inner = new FormatterType(parType.getParamTypes().get(0)).format(0, 0);
return "() => %s".formatted(inner);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.prunoideae.probejs.plugin;

import dev.latvian.mods.kubejs.RegistryObjectBuilderTypes;
import dev.latvian.mods.kubejs.event.EventJS;
import dev.latvian.mods.kubejs.event.IEventHandler;

Expand All @@ -11,7 +12,9 @@ public record WrappedEventHandler(String event, IEventHandler inner) implements

@Override
public void onEvent(EventJS eventJS) {
WrappedEventHandler.capturedEvents.put(this.event, eventJS.getClass());
//Special handlers for registry events
if (!(eventJS instanceof RegistryObjectBuilderTypes.RegistryEventJS))
WrappedEventHandler.capturedEvents.put(this.event, eventJS.getClass());
this.inner.onEvent(eventJS);
}
}

0 comments on commit 3ff929d

Please sign in to comment.