Skip to content

Commit 3ff929d

Browse files
committed
fixed registry things
now registry-based event should work properly
1 parent f0f3f99 commit 3ff929d

File tree

7 files changed

+104
-3
lines changed

7 files changed

+104
-3
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ apply from: 'https://files.latmod.com/public/markdown-git-changelog.gradle'
99
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_17
1010

1111
def ENV = System.getenv()
12-
version = "${mod_version}-build.62"
12+
version = "${mod_version}-build.63"
1313
archivesBaseName = project.archives_base_name
1414
group = project.maven_group
1515

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ org.gradle.daemon=false
33
loom.platform=forge
44
mod_id=probejs
55
archives_base_name=probejs
6-
mod_version=2.4.0
6+
mod_version=2.4.5
77
maven_group=com.prunoideae
88
mod_author=Prunoideae
99
minecraft_version=1.18.2

src/main/java/com/prunoideae/probejs/ProbeCommands.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.google.gson.JsonObject;
55
import com.mojang.brigadier.Command;
66
import com.mojang.brigadier.CommandDispatcher;
7+
import com.prunoideae.probejs.compiler.RegistryCompiler;
78
import com.prunoideae.probejs.compiler.SnippetCompiler;
89
import com.prunoideae.probejs.compiler.TypingCompiler;
910
import com.prunoideae.probejs.document.Manager;
@@ -100,6 +101,12 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
100101
return Command.SINGLE_SUCCESS;
101102
}))
102103
)
104+
.then(Commands.literal("test")
105+
.requires(source -> source.getServer().isSingleplayer() || source.hasPermission(2))
106+
.executes(context -> {
107+
RegistryCompiler.getBuilderTypes();
108+
return Command.SINGLE_SUCCESS;
109+
}))
103110
);
104111
}
105112

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.prunoideae.probejs.compiler;
2+
3+
import com.google.gson.Gson;
4+
import com.prunoideae.probejs.ProbePaths;
5+
import com.prunoideae.probejs.formatter.formatter.FormatterClass;
6+
import com.prunoideae.probejs.formatter.formatter.FormatterNamespace;
7+
import com.prunoideae.probejs.formatter.formatter.IFormatter;
8+
import com.prunoideae.probejs.info.type.TypeInfoClass;
9+
import dev.latvian.mods.kubejs.RegistryObjectBuilderTypes;
10+
11+
import java.io.BufferedWriter;
12+
import java.io.IOException;
13+
import java.nio.file.Files;
14+
import java.util.*;
15+
import java.util.stream.Collectors;
16+
17+
public class RegistryCompiler {
18+
public static Set<Class<?>> getRegistryClasses() {
19+
Set<Class<?>> result = new HashSet<>();
20+
result.add(RegistryObjectBuilderTypes.class);
21+
result.add(RegistryObjectBuilderTypes.RegistryEventJS.class);
22+
RegistryObjectBuilderTypes.MAP.values().forEach(v -> v.types.values().forEach(v1 -> result.add(v1.builderClass())));
23+
return result;
24+
}
25+
26+
public static void compileEventRegistries(BufferedWriter writer) throws IOException {
27+
Gson stringG = new Gson();
28+
for (var types : RegistryObjectBuilderTypes.MAP.values()) {
29+
String fullName = types.registryKey.location().getNamespace() + "." + types.registryKey.location().getPath().replace('/', '.') + ".registry";
30+
String registryName = FormatterRegistry.getFormattedRegistryName(types);
31+
writer.write("declare function onEvent(name: %s, handler: (event: Registry.%s) => void);\n".formatted(stringG.toJson(fullName), registryName));
32+
if (types.registryKey.location().getNamespace().equals("minecraft")) {
33+
String shortName = types.registryKey.location().getPath().replace('/', '.') + ".registry";
34+
writer.write("declare function onEvent(name: %s, handler: (event: Registry.%s) => void);\n".formatted(stringG.toJson(shortName), registryName));
35+
}
36+
}
37+
}
38+
39+
public static void compileRegistries() throws IOException {
40+
BufferedWriter writer = Files.newBufferedWriter(ProbePaths.GENERATED.resolve("registries.d.ts"));
41+
writer.write("/// <reference path=\"./globals.d.ts\" />\n");
42+
IFormatter namespace = new FormatterNamespace("Registry", RegistryObjectBuilderTypes.MAP.values().stream().map(FormatterRegistry::new).collect(Collectors.toList()));
43+
writer.write(String.join("\n", namespace.format(0, 4)));
44+
writer.flush();
45+
}
46+
47+
public static void getBuilderTypes() {
48+
RegistryObjectBuilderTypes.MAP.forEach((k, v) -> {
49+
System.out.println(k.registry());
50+
v.types.forEach((k1, v1) -> {
51+
System.out.println(" " + k1);
52+
System.out.println(" " + v1.builderClass());
53+
});
54+
});
55+
}
56+
57+
private static class FormatterRegistry implements IFormatter {
58+
RegistryObjectBuilderTypes<?> types;
59+
String name;
60+
61+
private static String getFormattedRegistryName(RegistryObjectBuilderTypes<?> types) {
62+
return Arrays.stream(types.registryKey.location().getPath().split("/")).map(str -> str.substring(0, 1).toUpperCase() + str.substring(1)).collect(Collectors.joining(""));
63+
}
64+
65+
private FormatterRegistry(RegistryObjectBuilderTypes<?> types) {
66+
this.types = types;
67+
this.name = getFormattedRegistryName(types);
68+
}
69+
70+
@Override
71+
public List<String> format(Integer indent, Integer stepIndent) {
72+
List<String> formatted = new ArrayList<>();
73+
int stepped = indent + stepIndent;
74+
Gson stringG = new Gson();
75+
formatted.add(" ".repeat(indent) + "class %s extends %s {".formatted(name, FormatterClass.formatTypeParameterized(new TypeInfoClass(RegistryObjectBuilderTypes.RegistryEventJS.class))));
76+
for (RegistryObjectBuilderTypes.BuilderType<?> builder : types.types.values()) {
77+
formatted.add(" ".repeat(stepped) + "create(id: string, type: %s): %s;".formatted(stringG.toJson(builder.type()), FormatterClass.formatTypeParameterized(new TypeInfoClass(builder.builderClass()))));
78+
}
79+
formatted.add(" ".repeat(indent) + "}");
80+
return formatted;
81+
}
82+
}
83+
}

src/main/java/com/prunoideae/probejs/compiler/TypingCompiler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public static void compileEvents(Map<String, Class<?>> cachedEvents, Map<String,
133133
cachedForgeEvents.putAll(WrappedForgeEventHandler.capturedEvents);
134134
BufferedWriter writer = Files.newBufferedWriter(ProbePaths.GENERATED.resolve("events.d.ts"));
135135
writer.write("/// <reference path=\"./globals.d.ts\" />\n");
136+
writer.write("/// <reference path=\"./registries.d.ts\" />\n");
136137
for (Map.Entry<String, Class<?>> entry : cachedEvents.entrySet()) {
137138
String name = entry.getKey();
138139
Class<?> event = entry.getValue();
@@ -143,6 +144,7 @@ public static void compileEvents(Map<String, Class<?>> cachedEvents, Map<String,
143144
Class<?> event = entry.getValue();
144145
writer.write("declare function onForgeEvent(name: \"%s\", handler: (event: %s) => void);\n".formatted(name, FormatterClass.formatTypeParameterized(new TypeInfoClass(event))));
145146
}
147+
RegistryCompiler.compileEventRegistries(writer);
146148
writer.flush();
147149
}
148150

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

199202
compileGlobal(bindingEvent, globalClasses);
203+
RegistryCompiler.compileRegistries();
200204
compileEvents(cachedEvents, cachedForgeEvents);
201205
compileConstants(bindingEvent);
202206
compileJava(globalClasses);

src/main/java/com/prunoideae/probejs/formatter/SpecialTypes.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.prunoideae.probejs.formatter.formatter.FormatterType;
44
import com.prunoideae.probejs.info.type.TypeInfoParameterized;
5+
import dev.latvian.mods.kubejs.util.BuilderBase;
56
import net.minecraft.nbt.CompoundTag;
67

78
import java.util.function.*;
@@ -23,6 +24,9 @@ public static void init() {
2324
return "(arg0: any) => boolean";
2425
});
2526
NameResolver.putTypeFormatter(Supplier.class, t -> {
27+
if (BuilderBase.class.isAssignableFrom(t.getResolvedClass())) {
28+
return new FormatterType(t, false).format(0, 0);
29+
}
2630
if (t instanceof TypeInfoParameterized parType && parType.getParamTypes().size() == 1) {
2731
String inner = new FormatterType(parType.getParamTypes().get(0)).format(0, 0);
2832
return "() => %s".formatted(inner);

src/main/java/com/prunoideae/probejs/plugin/WrappedEventHandler.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.prunoideae.probejs.plugin;
22

3+
import dev.latvian.mods.kubejs.RegistryObjectBuilderTypes;
34
import dev.latvian.mods.kubejs.event.EventJS;
45
import dev.latvian.mods.kubejs.event.IEventHandler;
56

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

1213
@Override
1314
public void onEvent(EventJS eventJS) {
14-
WrappedEventHandler.capturedEvents.put(this.event, eventJS.getClass());
15+
//Special handlers for registry events
16+
if (!(eventJS instanceof RegistryObjectBuilderTypes.RegistryEventJS))
17+
WrappedEventHandler.capturedEvents.put(this.event, eventJS.getClass());
1518
this.inner.onEvent(eventJS);
1619
}
1720
}

0 commit comments

Comments
 (0)