forked from ServerOpenMC/PluginV2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOMCDatapack.java
More file actions
56 lines (45 loc) · 1.71 KB
/
Copy pathOMCDatapack.java
File metadata and controls
56 lines (45 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package fr.openmc.api.datapacks;
import fr.openmc.api.datapacks.injectors.PackMetadataInjector;
import io.papermc.paper.plugin.bootstrap.BootstrapContext;
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
import lombok.Getter;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.Set;
@Getter
@SuppressWarnings("UnstableApiUsage")
public class OMCDatapack {
private final String packName;
private final String namespace;
private final Set<DatapackInjector> injectors = new HashSet<>();
public OMCDatapack(String packName, String namespace) {
this.packName = packName;
this.namespace = namespace;
}
public void build(BootstrapContext context) throws IOException {
Path tempDir = Files.createTempDirectory("datapacks-openmc");
runInjector(tempDir, new PackMetadataInjector());
for (DatapackInjector injector : injectors) {
runInjector(tempDir, injector);
}
context.getLifecycleManager().registerEventHandler(LifecycleEvents.DATAPACK_DISCOVERY.newHandler(
event -> {
try {
URI uri = tempDir.toUri();
event.registrar().discoverPack(uri, "openmc-injected");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
));
}
public void addInjector(DatapackInjector injector) {
injectors.add(injector);
}
private static void runInjector(Path datapackRoot, DatapackInjector injector) {
injector.inject(datapackRoot.toFile());
}
}