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.JsonParser;
|
||||
import net.fabricmc.api.ClientModInitializer;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.gui.DrawContext;
|
||||
import net.minecraft.client.gui.screen.Screen;
|
||||
import net.minecraft.client.util.ScreenshotRecorder;
|
||||
import net.minecraft.entity.LivingEntity;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.registry.Registries;
|
||||
import net.minecraft.text.Text;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.Screenshot;
|
||||
import net.minecraft.client.gui.GuiGraphics;
|
||||
import net.minecraft.client.gui.screens.Screen;
|
||||
import net.minecraft.core.registries.BuiltInRegistries;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.resources.Identifier;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
|
||||
public final class RenderCatalogClient implements ClientModInitializer {
|
||||
private static final Gson GSON = new Gson();
|
||||
@@ -52,7 +51,7 @@ public final class RenderCatalogClient implements ClientModInitializer {
|
||||
return;
|
||||
}
|
||||
|
||||
MinecraftClient client = MinecraftClient.getInstance();
|
||||
Minecraft client = Minecraft.getInstance();
|
||||
client.execute(() -> client.setScreen(new RenderScreen()));
|
||||
}
|
||||
|
||||
@@ -102,13 +101,13 @@ public final class RenderCatalogClient implements ClientModInitializer {
|
||||
String renderPath = renderPathElement.getAsString();
|
||||
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()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (customName != null) {
|
||||
stack.setCustomName(Text.literal(customName));
|
||||
stack.setCustomName(Component.literal(customName));
|
||||
}
|
||||
|
||||
jobs.add(new RenderJob(renderPath, stack));
|
||||
@@ -123,9 +122,9 @@ public final class RenderCatalogClient implements ClientModInitializer {
|
||||
return Path.of(value);
|
||||
}
|
||||
|
||||
private void renderCurrentJob(MinecraftClient client, RenderScreen screen) {
|
||||
private void renderCurrentJob(Minecraft client, RenderScreen screen) {
|
||||
if (currentJobIndex >= jobs.size()) {
|
||||
client.scheduleStop();
|
||||
client.stop();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -133,7 +132,7 @@ public final class RenderCatalogClient implements ClientModInitializer {
|
||||
Path jobPath = outputDir.resolve(job.outputPath()).normalize();
|
||||
Path parent = jobPath.getParent();
|
||||
if (parent == null) {
|
||||
client.scheduleStop();
|
||||
client.stop();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -144,10 +143,37 @@ public final class RenderCatalogClient implements ClientModInitializer {
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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() {
|
||||
currentJobIndex++;
|
||||
MinecraftClient client = MinecraftClient.getInstance();
|
||||
@@ -189,20 +215,16 @@ public final class RenderCatalogClient implements ClientModInitializer {
|
||||
}
|
||||
|
||||
@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);
|
||||
|
||||
if (currentJobIndex < jobs.size()) {
|
||||
RenderJob job = jobs.get(currentJobIndex);
|
||||
context.getMatrices().push();
|
||||
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();
|
||||
context.renderItem(job.stack(), (width / 2) - 8, (height / 2) - 8);
|
||||
|
||||
if (!captureQueued) {
|
||||
captureQueued = true;
|
||||
MinecraftClient client = MinecraftClient.getInstance();
|
||||
Minecraft client = Minecraft.getInstance();
|
||||
client.execute(() -> renderCurrentJob(client, this));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user