41 lines
2.2 KiB
Java
41 lines
2.2 KiB
Java
package online.astradev.weapons.resource;
|
|||
|
|
|
||
|
|
import java.io.*;
|
||
|
|
import java.nio.file.*;
|
||
|
|
import java.nio.charset.StandardCharsets;
|
||
|
|
import com.google.gson.*;
|
||
|
|
import online.astradev.plus.io.PlusFiles;
|
||
|
|
|
||
|
|
/** Seeds missing addon files before registry/pack loading; preserves user-edited definitions and assets. */
|
||
|
|
public final class WeaponsResources {
|
||
|
|
private WeaponsResources() {}
|
||
|
|
public static void install(Path root) throws IOException {
|
||
|
|
online.astradev.weapons.content.TwinbladeContent.install(root);
|
||
|
|
try(var input=WeaponsResources.class.getResourceAsStream("/weapons-index.txt")) {
|
||
|
|
if(input==null) throw new IOException("Missing weapons resource index");
|
||
|
|
for(String relative:new String(input.readAllBytes(),StandardCharsets.UTF_8).lines().filter(s->!s.isBlank()).toList()) {
|
||
|
|
Path target=root.resolve("astraplus").resolve(relative).normalize();
|
||
|
|
if(!target.startsWith(root.resolve("astraplus").normalize())) throw new IOException("Invalid resource path");
|
||
|
|
PlusFiles.rejectLinks(target);
|
||
|
|
if(Files.exists(target)) continue;
|
||
|
|
try(var resource=WeaponsResources.class.getResourceAsStream("/defaults/"+relative)) {
|
||
|
|
if(resource==null) throw new IOException("Missing weapons resource: "+relative);
|
||
|
|
Files.createDirectories(target.getParent());Files.copy(resource,target);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
for(var resource:TwinbladeResources.generate().entrySet()) {
|
||
|
|
Path target=root.resolve("astraplus/resources").resolve(resource.getKey());
|
||
|
|
PlusFiles.rejectLinks(target);
|
||
|
|
if(!Files.exists(target)) PlusFiles.write(target,resource.getValue());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
public static JsonObject gauntletModel(String model) {
|
||
|
|
return JsonParser.parseString("""
|
||
|
|
{"type":"minecraft:select","property":"minecraft:display_context",
|
||
|
|
"cases":[{"when":["gui","ground","fixed"],"model":{"type":"minecraft:model","model":"astraweapons:item/iron_gauntlet_2d"}}],
|
||
|
|
"fallback":{"type":"minecraft:model","model":"PLACEHOLDER"}}
|
||
|
|
""".replace("PLACEHOLDER",model)).getAsJsonObject();
|
||
|
|
}
|
||
|
|
}
|