Port renderer to Mojang mappings
This commit is contained in:
+45
-23
@@ -15,15 +15,14 @@ import com.google.gson.JsonElement;
|
|||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.google.gson.JsonParser;
|
import com.google.gson.JsonParser;
|
||||||
import net.fabricmc.api.ClientModInitializer;
|
import net.fabricmc.api.ClientModInitializer;
|
||||||
import net.minecraft.client.MinecraftClient;
|
import net.minecraft.client.Minecraft;
|
||||||
import net.minecraft.client.gui.DrawContext;
|
import net.minecraft.client.Screenshot;
|
||||||
import net.minecraft.client.gui.screen.Screen;
|
import net.minecraft.client.gui.GuiGraphics;
|
||||||
import net.minecraft.client.util.ScreenshotRecorder;
|
import net.minecraft.client.gui.screens.Screen;
|
||||||
import net.minecraft.entity.LivingEntity;
|
import net.minecraft.core.registries.BuiltInRegistries;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.network.chat.Component;
|
||||||
import net.minecraft.registry.Registries;
|
import net.minecraft.resources.Identifier;
|
||||||
import net.minecraft.text.Text;
|
import net.minecraft.world.item.ItemStack;
|
||||||
import net.minecraft.util.Identifier;
|
|
||||||
|
|
||||||
public final class RenderCatalogClient implements ClientModInitializer {
|
public final class RenderCatalogClient implements ClientModInitializer {
|
||||||
private static final Gson GSON = new Gson();
|
private static final Gson GSON = new Gson();
|
||||||
@@ -52,7 +51,7 @@ public final class RenderCatalogClient implements ClientModInitializer {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
MinecraftClient client = MinecraftClient.getInstance();
|
Minecraft client = Minecraft.getInstance();
|
||||||
client.execute(() -> client.setScreen(new RenderScreen()));
|
client.execute(() -> client.setScreen(new RenderScreen()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,13 +101,13 @@ public final class RenderCatalogClient implements ClientModInitializer {
|
|||||||
String renderPath = renderPathElement.getAsString();
|
String renderPath = renderPathElement.getAsString();
|
||||||
String customName = customNameElement != null && customNameElement.isJsonPrimitive() ? customNameElement.getAsString() : null;
|
String customName = customNameElement != null && customNameElement.isJsonPrimitive() ? customNameElement.getAsString() : null;
|
||||||
|
|
||||||
ItemStack stack = new ItemStack(Registries.ITEM.get(Identifier.of("minecraft", itemId)));
|
ItemStack stack = new ItemStack(BuiltInRegistries.ITEM.get(Identifier.withDefaultNamespace(itemId)));
|
||||||
if (stack.isEmpty()) {
|
if (stack.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (customName != null) {
|
if (customName != null) {
|
||||||
stack.setCustomName(Text.literal(customName));
|
stack.setCustomName(Component.literal(customName));
|
||||||
}
|
}
|
||||||
|
|
||||||
jobs.add(new RenderJob(renderPath, stack));
|
jobs.add(new RenderJob(renderPath, stack));
|
||||||
@@ -123,9 +122,9 @@ public final class RenderCatalogClient implements ClientModInitializer {
|
|||||||
return Path.of(value);
|
return Path.of(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void renderCurrentJob(MinecraftClient client, RenderScreen screen) {
|
private void renderCurrentJob(Minecraft client, RenderScreen screen) {
|
||||||
if (currentJobIndex >= jobs.size()) {
|
if (currentJobIndex >= jobs.size()) {
|
||||||
client.scheduleStop();
|
client.stop();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,7 +132,7 @@ public final class RenderCatalogClient implements ClientModInitializer {
|
|||||||
Path jobPath = outputDir.resolve(job.outputPath()).normalize();
|
Path jobPath = outputDir.resolve(job.outputPath()).normalize();
|
||||||
Path parent = jobPath.getParent();
|
Path parent = jobPath.getParent();
|
||||||
if (parent == null) {
|
if (parent == null) {
|
||||||
client.scheduleStop();
|
client.stop();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -144,10 +143,37 @@ public final class RenderCatalogClient implements ClientModInitializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String fileName = stripPngSuffix(jobPath.getFileName().toString());
|
String fileName = stripPngSuffix(jobPath.getFileName().toString());
|
||||||
ScreenshotRecorder.saveScreenshot(parent.toFile(), fileName, client.getFramebuffer(), client.getWindow().getFramebufferWidth(), status -> client.execute(this::advanceJob));
|
Screenshot.takeScreenshot(client.gameRenderer.mainRenderTarget(), screenshot -> {
|
||||||
|
try {
|
||||||
|
Path savedPath = saveScreenshot(screenshot, fileName, parent);
|
||||||
|
if (savedPath == null) {
|
||||||
|
throw new RuntimeException("Failed to save screenshot");
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
client.execute(this::advanceJob);
|
||||||
|
}
|
||||||
|
});
|
||||||
screen.markCaptureQueued();
|
screen.markCaptureQueued();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Path saveScreenshot(com.mojang.blaze3d.platform.NativeImage screenshot, String fileName, Path destinationDir) {
|
||||||
|
try {
|
||||||
|
Files.createDirectories(destinationDir);
|
||||||
|
} catch (IOException exception) {
|
||||||
|
throw new RuntimeException("Failed to create render output directory", exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
Path screenshotFile = destinationDir.resolve(fileName + ".png");
|
||||||
|
|
||||||
|
try {
|
||||||
|
screenshot.writeToFile(screenshotFile);
|
||||||
|
} catch (IOException exception) {
|
||||||
|
throw new RuntimeException("Failed to write render output", exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
return screenshotFile;
|
||||||
|
}
|
||||||
|
|
||||||
private void advanceJob() {
|
private void advanceJob() {
|
||||||
currentJobIndex++;
|
currentJobIndex++;
|
||||||
MinecraftClient client = MinecraftClient.getInstance();
|
MinecraftClient client = MinecraftClient.getInstance();
|
||||||
@@ -189,20 +215,16 @@ public final class RenderCatalogClient implements ClientModInitializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
|
public void render(GuiGraphics context, int mouseX, int mouseY, float delta) {
|
||||||
context.fill(0, 0, width, height, 0x00000000);
|
context.fill(0, 0, width, height, 0x00000000);
|
||||||
|
|
||||||
if (currentJobIndex < jobs.size()) {
|
if (currentJobIndex < jobs.size()) {
|
||||||
RenderJob job = jobs.get(currentJobIndex);
|
RenderJob job = jobs.get(currentJobIndex);
|
||||||
context.getMatrices().push();
|
context.renderItem(job.stack(), (width / 2) - 8, (height / 2) - 8);
|
||||||
context.getMatrices().translate(width / 2.0, height / 2.0, 0.0);
|
|
||||||
context.getMatrices().scale(8.0f, 8.0f, 1.0f);
|
|
||||||
context.drawItem((LivingEntity) null, job.stack(), -1, -1, 0);
|
|
||||||
context.getMatrices().pop();
|
|
||||||
|
|
||||||
if (!captureQueued) {
|
if (!captureQueued) {
|
||||||
captureQueued = true;
|
captureQueued = true;
|
||||||
MinecraftClient client = MinecraftClient.getInstance();
|
Minecraft client = Minecraft.getInstance();
|
||||||
client.execute(() -> renderCurrentJob(client, this));
|
client.execute(() -> renderCurrentJob(client, this));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user