Sets up the game to run in fabric.

This commit is contained in:
Markil3
2021-03-07 12:17:02 -07:00
parent 6d188f46f7
commit 70ff889711
20 changed files with 501 additions and 409 deletions

View File

@@ -1,119 +1,37 @@
package markil3.panorama;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screen.MainMenuScreen;
import net.minecraft.client.renderer.RenderSkybox;
import net.minecraft.client.renderer.RenderSkyboxCube;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.EntityViewRenderEvent;
import net.minecraftforge.client.event.GuiOpenEvent;
import net.minecraftforge.client.event.GuiScreenEvent;
import net.minecraftforge.client.event.RenderHandEvent;
import net.minecraftforge.client.event.ScreenshotEvent;
import net.minecraftforge.client.event.TextureStitchEvent;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraft.client.render.Camera;
import markil3.panorama.mixins.CameraAccessor;
@Mod.EventBusSubscriber(Dist.CLIENT)
public class EventBus
{
static int screenshotState = 0;
@SubscribeEvent
public static void onScreenshot(ScreenshotEvent event)
public static void onCamera(Camera event)
{
// event.setCanceled(true);
screenshotState = 1;
}
@SubscribeEvent
public static void onRender(TickEvent.RenderTickEvent event)
{
if (event.phase == TickEvent.Phase.START && screenshotState > 0)
if (Main.isRenderingPanorama())
{
Main.renderPanorama(Minecraft.getInstance());
screenshotState = 0;
}
}
@SubscribeEvent
public static void onRenderHand(RenderHandEvent event)
{
if (screenshotState > 0)
{
event.setCanceled(true);
}
}
@SubscribeEvent
public static void onFov(EntityViewRenderEvent.FOVModifier event)
{
if (screenshotState > 0)
{
event.setFOV(90);
}
}
@SubscribeEvent
public static void onCamera(EntityViewRenderEvent.CameraSetup event)
{
if (screenshotState > 0)
{
event.setRoll(0);
switch (screenshotState - 2)
switch (Main.getPanoramaStage() - 2)
{
default:
case 0:
event.setYaw(0F);
event.setPitch(0);
((CameraAccessor) event).setView(0, 0);
break;
case 1:
event.setYaw(90);
event.setPitch(0);
((CameraAccessor) event).setView(90, 0);
break;
case 2:
event.setYaw(180);
event.setPitch(0);
((CameraAccessor) event).setView(180, 0);
break;
case 3:
event.setYaw(270);
event.setPitch(0);
((CameraAccessor) event).setView(270, 0);
break;
case 4:
event.setYaw(0);
event.setPitch(-90);
((CameraAccessor) event).setView(0, -90);
break;
case 5:
event.setYaw(0);
event.setPitch(90);
((CameraAccessor) event).setView(0, 90);
break;
}
screenshotState++;
}
}
@SubscribeEvent
public static void onShowScreen(GuiScreenEvent.InitGuiEvent event)
{
if (event instanceof GuiScreenEvent.InitGuiEvent.Pre)
{
if (Main.numPanoramas > 0)
{
int panorama =
MathHelper.floor(Math.random() * Main.numPanoramas);
MainMenuScreen screen;
if (event.getGui() instanceof MainMenuScreen)
{
// Main.loadPanoramas(Minecraft.getInstance());
screen = (MainMenuScreen) event.getGui();
screen.panorama =
new RenderSkybox(new RenderSkyboxCube(new ResourceLocation(
"panorama:textures/gui/title/background/" + panorama + "/panorama")));
System.out.printf("Displaying panorama %d\n", panorama);
}
}
}
}
}

View File

@@ -1,48 +1,48 @@
package markil3.panorama;
import net.minecraft.client.renderer.texture.NativeImage;
import net.minecraft.client.renderer.texture.SimpleTexture;
import net.minecraft.client.resources.data.TextureMetadataSection;
import net.minecraft.resources.IResourceManager;
import net.minecraft.util.ResourceLocation;
import net.minecraft.client.resource.metadata.TextureResourceMetadata;
import net.minecraft.client.texture.NativeImage;
import net.minecraft.client.texture.ResourceTexture;
import net.minecraft.resource.ResourceManager;
import net.minecraft.util.Identifier;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class FileTexture extends SimpleTexture
public class FileTexture extends ResourceTexture
{
private final File file;
public FileTexture(ResourceLocation p_i1275_1_, File file)
public FileTexture(Identifier p_i1275_1_, File file)
{
super(p_i1275_1_);
this.file = file;
}
@Override
protected TextureData getTextureData(IResourceManager p_215246_1_)
protected ResourceTexture.TextureData loadTextureData(ResourceManager p_215246_1_)
{
return getTextureData(p_215246_1_, this.file);
}
public static SimpleTexture.TextureData getTextureData(IResourceManager resourceManagerIn,
public static ResourceTexture.TextureData getTextureData(ResourceManager resourceManagerIn,
File locationIn)
{
NativeImage image;
TextureMetadataSection meta;
TextureResourceMetadata meta;
try (FileInputStream stream = new FileInputStream(locationIn))
{
image =
NativeImage.read(stream);
meta = null;
return new SimpleTexture.TextureData(null,
return new ResourceTexture.TextureData(null,
image);
}
catch (IOException e)
{
return new SimpleTexture.TextureData(e);
return new ResourceTexture.TextureData(e);
}
}
}

View File

@@ -1,30 +1,23 @@
package markil3.panorama;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.FogRenderer;
import net.minecraft.client.renderer.texture.NativeImage;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.ScreenShotHelper;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.CubeMapRenderer;
import net.minecraft.client.gui.RotatingCubeMapRenderer;
import net.minecraft.client.texture.NativeImage;
import net.minecraft.client.texture.TextureManager;
import net.minecraft.client.util.ScreenshotUtils;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.resource.ResourceManager;
import net.minecraft.resource.ResourceType;
import net.minecraft.util.Identifier;
import net.minecraft.util.Util;
import net.minecraftforge.client.event.ModelBakeEvent;
import net.minecraftforge.client.event.TextureStitchEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.InterModComms;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.event.lifecycle.InterModEnqueueEvent;
import net.minecraftforge.fml.event.lifecycle.InterModProcessEvent;
import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.profiler.Profiler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -33,55 +26,37 @@ import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.stream.Collectors;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
// The value here should match an entry in the META-INF/mods.toml file
@Mod("panorama")
public class Main
public class Main implements ModInitializer, IdentifiableResourceReloadListener
{
// Directly reference a log4j logger.
private static final Logger LOGGER = LogManager.getLogger();
static int numPanoramas = 0;
private static int screenshotState = 0;
@Override
public void onInitialize()
{
// ResourceManagerHelper.get(ResourceType.CLIENT_RESOURCES)
// .registerReloadListener(this);
}
public Main()
{
// Register the setup method for modloading
FMLJavaModLoadingContext.get()
.getModEventBus()
.addListener(this::setup);
// Register the enqueueIMC method for modloading
FMLJavaModLoadingContext.get()
.getModEventBus()
.addListener(this::enqueueIMC);
// Register the processIMC method for modloading
FMLJavaModLoadingContext.get()
.getModEventBus()
.addListener(this::processIMC);
// Register the doClientStuff method for modloading
FMLJavaModLoadingContext.get()
.getModEventBus()
.addListener(this::doClientStuff);
FMLJavaModLoadingContext.get()
.getModEventBus()
.addListener(this::onStitch);
// Register ourselves for server and other game events we are
// interested in
MinecraftForge.EVENT_BUS.register(this);
}
public void onStitch(ModelBakeEvent event)
{
Main.loadPanoramas(Minecraft.getInstance());
}
static void loadPanoramas(Minecraft mc)
static void loadPanoramas(MinecraftClient mc,
ResourceManager manager,
Executor prepareExecutor,
Executor applyExecutor)
{
int i = 0;
int j;
TextureManager textureManager = mc.textureManager;
ResourceLocation loc;
File panoramas = new File(mc.gameDir, "panoramas");
TextureManager textureManager = mc.getTextureManager();
File panoramas = new File(mc.runDirectory, "panoramas");
if (panoramas.isDirectory())
{
for (File panorama : panoramas.listFiles())
@@ -90,12 +65,17 @@ public class Main
{
for (j = 0; j < 6; j++)
{
loc = new ResourceLocation(
"panorama:textures/gui/title/background/" + i + "/panorama_" + j + ".png");
textureManager.loadTexture(loc,
int finalI = i;
int finalJ = j;
Identifier loc = new Identifier(
"panorama:textures/gui/title" +
"/background/" + finalI +
"/panorama_" + finalJ + ".png");
textureManager.registerTexture(loc,
new FileTexture(loc,
new File(panorama,
"panorama_" + j + ".png")));
"panorama_" + finalJ +
".png")));
}
i++;
}
@@ -104,61 +84,79 @@ public class Main
numPanoramas = i;
}
private void setup(final FMLCommonSetupEvent event)
public static boolean isRenderingPanorama()
{
return screenshotState > 0;
}
private void doClientStuff(final FMLClientSetupEvent event)
public static int getPanoramaStage()
{
// loadPanoramas(event.getMinecraftSupplier().get());
return screenshotState;
}
private void enqueueIMC(final InterModEnqueueEvent event)
public static int nextStage()
{
return ++screenshotState;
}
private void processIMC(final InterModProcessEvent event)
public static void startRenderingPanorama()
{
screenshotState = 1;
}
// You can use SubscribeEvent and let the Event Bus discover methods to call
@SubscribeEvent
public void onServerStarting(FMLServerStartingEvent event)
public static void finishRenderingPanorama()
{
screenshotState = 0;
}
static void renderPanorama(Minecraft mc)
public static RotatingCubeMapRenderer getPanorama()
{
if (numPanoramas > 0)
{
int panorama =
MathHelper.floor(Math.random() * Main.numPanoramas);
return new RotatingCubeMapRenderer(new CubeMapRenderer(new Identifier(
"panorama:textures/gui/title/background/" + panorama +
"/panorama")));
}
return null;
}
public static void renderPanorama(MinecraftClient mc)
{
final SimpleDateFormat FORMATTER =
new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss");
final int SCREENSHOT_WIDTH = 2048;
final int SCREENSHOT_HEIGHT = 2048;
String name;
RenderSystem.viewport(0, 0, SCREENSHOT_WIDTH, SCREENSHOT_HEIGHT);
if (mc.world != null)
{
name = FORMATTER.format(new Date());
// We need to render an extra frame at the beginning, or
// otherwise it won't render correctly.
for (int i = -1; i < 6; i++)
{
mc.gameRenderer.renderWorld(mc.getRenderPartialTicks(),
Util.nanoTime(), new MatrixStack());
saveScreenshot(mc, 0, i);
mc.gameRenderer.renderWorld(mc.getTickDelta(),
Util.getMeasuringTimeNano(), new MatrixStack());
saveScreenshot(mc, name, i);
nextStage();
}
finishRenderingPanorama();
}
}
static void saveScreenshot(Minecraft mc, int panoramaNum, int panoramaSide)
static void saveScreenshot(MinecraftClient mc,
String panoramaNum,
int panoramaSide)
{
final SimpleDateFormat FORMATTER =
new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss");
final int FRAMEBUFFER_WIDTH = mc.getMainWindow().getFramebufferWidth();
final int FRAMEBUFFER_HEIGHT =
mc.getMainWindow().getFramebufferHeight();
final int SCREENSHOT_WIDTH = 2048;
final int SCREENSHOT_HEIGHT = 2048;
File file = new File(mc.gameDir,
"panoramas/" + FORMATTER.format(new Date()) + "/panorama_" + panoramaSide +
File file = new File(mc.runDirectory,
"panoramas/" + panoramaNum + "/panorama_" + panoramaSide +
".png");
file.getParentFile().mkdirs();
try
@@ -169,11 +167,11 @@ public class Main
{
e.printStackTrace();
}
NativeImage nativeimage = ScreenShotHelper.createScreenshot(
NativeImage nativeimage = ScreenshotUtils.takeScreenshot(
SCREENSHOT_WIDTH,
SCREENSHOT_HEIGHT,
mc.getFramebuffer());
Util.getRenderingService().execute(() -> {
Util.getIoWorkerExecutor().execute(() -> {
int i = nativeimage.getWidth();
int j = nativeimage.getHeight();
int k = 0;
@@ -194,7 +192,7 @@ public class Main
false))
{
nativeimage.resizeSubRectTo(k, l, i, j, nativeimage1);
nativeimage1.write(file);
nativeimage1.writeFile(file);
}
catch (IOException ioexception)
{
@@ -208,4 +206,25 @@ public class Main
});
}
@Override
public Identifier getFabricId()
{
return new Identifier("panorama", "backgrounds");
}
@Override
public CompletableFuture<Void> reload(Synchronizer synchronizer,
ResourceManager manager,
Profiler prepareProfiler,
Profiler applyProfiler,
Executor prepareExecutor,
Executor applyExecutor)
{
loadPanoramas(MinecraftClient.getInstance(),
manager,
prepareExecutor,
applyExecutor);
return CompletableFuture.completedFuture(null);
}
}

View File

@@ -0,0 +1,13 @@
package markil3.panorama.mixins;
import net.minecraft.client.render.Camera;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;
@Mixin(Camera.class)
public interface CameraAccessor
{
@Invoker("setRotation")
public void setView(float yaw, float pitch);
}

View File

@@ -0,0 +1,14 @@
package markil3.panorama.mixins;
import net.minecraft.client.render.Camera;
import net.minecraft.client.render.GameRenderer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
@Mixin(GameRenderer.class)
public interface GameRenderAccessor
{
@Accessor("renderingPanorama")
public void setPanoramic(boolean panoramic);
}

View File

@@ -0,0 +1,67 @@
package markil3.panorama.mixins;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.GameRenderer;
import net.minecraft.client.util.math.MatrixStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import markil3.panorama.EventBus;
import markil3.panorama.Main;
@Mixin(GameRenderer.class)
public class GameRenderHook
{
@Inject(method = "render", at = @At(value = "HEAD"))
public void startPanora(float tickDelta, long startTime, boolean tick,
CallbackInfo callbackInfo)
{
if (Main.isRenderingPanorama())
{
Main.renderPanorama(MinecraftClient.getInstance());
}
}
@Inject(method = "renderWorld", at = @At(value = "HEAD"))
public void startPanora(float tickDelta,
long limitTime,
MatrixStack matrix,
CallbackInfo callbackInfo)
{
if (Main.isRenderingPanorama())
{
((GameRenderAccessor) MinecraftClient.getInstance().gameRenderer).setPanoramic(
true);
}
}
@Inject(method = "renderWorld", at = @At(value = "TAIL"))
public void finishPanora(float tickDelta,
long limitTime,
MatrixStack matrix,
CallbackInfo callbackInfo)
{
if (Main.isRenderingPanorama())
{
((GameRenderAccessor) MinecraftClient.getInstance().gameRenderer).setPanoramic(
false);
}
}
@Inject(method = "renderWorld", at = @At(value = "INVOKE", target = "Lnet" +
"/minecraft/client/render/Camera;update" +
"(Lnet/minecraft/world/BlockView;Lnet/minecraft/entity/Entity;" +
"ZZF)V", shift = At.Shift.AFTER))
public void adjustCamera(float tickDelta,
long limitTime,
MatrixStack matrix,
CallbackInfo callbackInfo)
{
if (Main.isRenderingPanorama())
{
EventBus.onCamera(MinecraftClient.getInstance().gameRenderer.getCamera());
}
}
}

View File

@@ -0,0 +1,31 @@
package markil3.panorama.mixins;
import net.minecraft.client.Keyboard;
import net.minecraft.client.gl.Framebuffer;
import net.minecraft.client.util.ScreenshotUtils;
import net.minecraft.text.Text;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.io.File;
import java.util.function.Consumer;
import markil3.panorama.Main;
@Mixin(ScreenshotUtils.class)
public class ScreenshotHook
{
@Inject(method = "saveScreenshot", at = @At(value = "HEAD"))
private static void takeScreenshot(File gameDirectory,
int framebufferWidth,
int framebufferHeight,
Framebuffer framebuffer,
Consumer<Text> messageReceiver,
CallbackInfo callbackInfo)
{
Main.startRenderingPanorama();
}
}

View File

@@ -0,0 +1,14 @@
package markil3.panorama.mixins;
import net.minecraft.client.gui.RotatingCubeMapRenderer;
import net.minecraft.client.gui.screen.TitleScreen;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
@Mixin(TitleScreen.class)
public interface TitleAccessor
{
@Accessor("backgroundRenderer")
public void setPanoramic(RotatingCubeMapRenderer panoramic);
}

View File

@@ -0,0 +1,33 @@
package markil3.panorama.mixins;
import net.minecraft.client.gui.RotatingCubeMapRenderer;
import net.minecraft.client.gui.screen.TitleScreen;
import org.objectweb.asm.Opcodes;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import markil3.panorama.Main;
@Mixin(TitleScreen.class)
public class TitleHook
{
@Redirect(method = "<init>(Z)V", at = @At(value = "FIELD", target = "Lnet" +
"/minecraft/client/gui/screen/TitleScreen;" +
"backgroundRenderer:Lnet/minecraft/client/gui" +
"/RotatingCubeMapRenderer;", opcode = Opcodes.PUTFIELD))
private void injected(TitleScreen screen,
RotatingCubeMapRenderer defaultPan)
{
RotatingCubeMapRenderer pan = Main.getPanorama();
if (pan != null)
{
((TitleAccessor) screen).setPanoramic(pan);
}
else
{
((TitleAccessor) screen).setPanoramic(defaultPan);
}
}
}

View File

@@ -1 +0,0 @@
public-f net.minecraft.client.gui.screen.MainMenuScreen field_209101_K #panorama

View File

@@ -1,29 +0,0 @@
modLoader="javafml"
loaderVersion="[34,)"
license="GPL-3.0-only"
issueTrackerURL="https://github.com/Markil3/MinecraftPanorama/issues"
[[mods]]
modId="panorama"
version="${file.jarVersion}"
displayName="Panorama Mod"
updateJSONURL="https://raw.githubusercontent.com/Markil3/MinecraftPanorama/master/update-forge.json"
displayURL="https://markil3.github.io/MinecraftPanorama"
#logoFile="examplemod.png"
credits="Mojang Studios for the original screenshot code."
# A text field displayed in the mod UI
authors="Markil 3"
description='''
This Minecraft mod provides the ability to take panoramic screenshots at any time with the Screenshot key (default to F2 on most Minecraft installations). On top of that, these screenshots will occupy the main menu background, allowing you to relive your adventures every time you boot up the game.
'''
[[dependencies.panorama]]
modId="forge"
mandatory=true
versionRange="[34,)"
ordering="NONE"
side="CLIENT"
[[dependencies.panorama]]
modId="minecraft"
mandatory=true
versionRange="[1.16.3]"
ordering="NONE"
side="CLIENT"

View File

@@ -0,0 +1,41 @@
{
"schemaVersion": 1,
"id": "panorama",
"version": "${version}",
"name": "Panorama Mod",
"description": "This Minecraft mod provides the ability to take panoramic screenshots at any time with the Screenshot key (default to F2 on most Minecraft installations). On top of that, these screenshots will occupy the main menu background, allowing you to relive your adventures every time you boot up the game.",
"authors": [
"Markil 3"
],
"contact": {
"homepage": "https://www.planetminecraft.com/mod/panorama/",
"sources": "https://github.com/Markil3/MinecraftPanorama",
"issues": "https://github.com/Markil3/MinecraftPanorama/issues"
},
"license": "GPL-3.0-only",
"icon": "assets/panorama/icon.png",
"environment": "client",
"entrypoints": {
"main": [
"markil3.panorama.Main"
]
},
"mixins": [
{
"config": "modid.mixins.json",
"environment": "client"
}
],
"depends": {
"fabricloader": ">=0.7.4",
"minecraft": "1.16.x"
},
"suggests": {
"modmenu": "*",
"cloth-config2": "*"
}
}

View File

@@ -0,0 +1,19 @@
{
"required": true,
"minVersion": "0.8",
"package": "markil3.panorama.mixins",
"compatibilityLevel": "JAVA_8",
"mixins": [
],
"client": [
"ScreenshotHook",
"GameRenderHook",
"TitleHook",
"GameRenderAccessor",
"TitleAccessor",
"CameraAccessor"
],
"injectors": {
"defaultRequire": 1
}
}