Make use of new @Pattern feature to centralize version-aware code

That is, most of the business code should not be aware that it is being compiled
to multiple versions even when it heavily interacts with MC, preprocessor
statements should be an escape hatch, not the norm.
Similarly, code should not be forced to do `MCVer.getWindow(mc)` instead of the
much more intuitive `mc.getWindow()`, and a new preprocessor (technically remap)
feature makes this possible by defining "search and replace"-like patterns (but
smarter in that they are type-aware) in one or more central places (the
"Patterns.java" files) which then are applied all over the code base.

In a way, this is another step in the automatic back-porting process where
preprocessor statements are used when we cannot yet do something automatically.
Previously we "merely" automatically converted between different mapping, this
new feature now also allows us to automatically perform simple refactoring
tasks like changing field access to a getter+setter (e.g. `mc.getWindow()`), or
changing how a method is called (e.g. `BufferBuilder.begin`), or changing a
method call chain (e.g. `dispatcher.camera.getYaw()`), or most other
search-and-replace-like changes and any combination of those.
The only major limitation is that the replacement itself is not smart, so
arguments must be kept in same order (or be temporarily assigned to local
variables which then can be used in any order).
This commit is contained in:
Jonas Herzig
2021-02-27 17:54:49 +01:00
parent 06a46e6f38
commit cfb9e15b8a
56 changed files with 890 additions and 872 deletions

View File

@@ -1,5 +1,6 @@
package com.replaymod.compat.oranges17animations;
import com.replaymod.core.ReplayMod;
import com.replaymod.replay.camera.CameraEntity;
import de.johni0702.minecraft.gui.utils.EventRegistrations;
import net.minecraft.client.Minecraft;
@@ -7,8 +8,6 @@ import net.minecraftforge.client.event.RenderLivingEvent;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import static com.replaymod.core.versions.MCVer.*;
/**
* Orange seems to have copied vast parts of the RendererLivingEntity into their ArmorAnimation class which cancels the RenderLivingEvent.Pre and calls its own code instead.
* This breaks our mixin which assures that, even though the camera is in spectator mode, it cannot see invisible entities.
@@ -17,12 +16,12 @@ import static com.replaymod.core.versions.MCVer.*;
*/
public class HideInvisibleEntities extends EventRegistrations {
private final Minecraft mc = Minecraft.getMinecraft();
private final boolean modLoaded = isModLoaded("animations");
private final boolean modLoaded = ReplayMod.instance.isModLoaded("animations");
@SubscribeEvent(priority = EventPriority.HIGH)
public void preRenderLiving(RenderLivingEvent.Pre event) {
if (modLoaded) {
if (mc.player instanceof CameraEntity && getEntity(event).isInvisible()) {
if (mc.player instanceof CameraEntity && event.getEntity().isInvisible()) {
event.setCanceled(true);
}
}

View File

@@ -61,6 +61,14 @@ public class ReplayModBackend {
return Loader.instance().getIndexedModList().get(MOD_ID).getVersion();
}
public String getMinecraftVersion() {
return Loader.MC_VERSION;
}
public boolean isModLoaded(String id) {
return Loader.isModLoaded(id);
}
static { // Note: even preInit is too late and we'd have to issue another resource reload
List<IResourcePack> defaultResourcePacks = ((MinecraftAccessor) getMinecraft()).getDefaultResourcePacks();

View File

@@ -0,0 +1,9 @@
package com.replaymod.core.versions;
import org.lwjgl.opengl.Display;
public class GLFW {
public static boolean glfwWindowShouldClose(@SuppressWarnings("unused") long handle) {
return Display.isCloseRequested();
}
}

View File

@@ -0,0 +1,69 @@
package com.replaymod.core.versions;
import com.replaymod.render.mixin.MainWindowAccessor;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.ScaledResolution;
import org.lwjgl.opengl.Display;
public class Window implements MainWindowAccessor {
private final Minecraft mc;
private ScaledResolution scaledResolution;
public Window(Minecraft mc) {
this.mc = mc;
}
@Override
public int getFramebufferWidth() {
return mc.displayWidth;
}
@Override
public void setFramebufferWidth(int value) {
mc.displayWidth = value;
}
@Override
public int getFramebufferHeight() {
return mc.displayHeight;
}
@Override
public void setFramebufferHeight(int value) {
mc.displayHeight = value;
}
public long getHandle() {
return 0;
}
public int getWidth() {
return Display.getWidth();
}
public int getHeight() {
return Display.getHeight();
}
private ScaledResolution scaledResolution() {
ScaledResolution scaledResolution = this.scaledResolution;
if (scaledResolution == null) {
//#if MC>=10809
scaledResolution = new ScaledResolution(mc);
//#else
//$$ scaledResolution = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight);
//#endif
this.scaledResolution = scaledResolution;
}
return scaledResolution;
}
public int getScaledWidth() {
return scaledResolution().getScaledWidth();
}
public int getScaledHeight() {
return scaledResolution().getScaledWidth();
}
}

View File

@@ -55,7 +55,7 @@ public class EventsAdapter extends EventRegistrations {
@SubscribeEvent
public void preRenderGameOverlay(RenderGameOverlayEvent.Pre event) {
Boolean result = null;
switch (MCVer.getType(event)) {
switch (event.getType()) {
case CROSSHAIRS:
result = RenderSpectatorCrosshairCallback.EVENT.invoker().shouldRenderSpectatorCrosshair();
break;

View File

@@ -0,0 +1,47 @@
package com.replaymod.core.versions.forge;
import com.replaymod.gradle.remap.Pattern;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.entity.EntityLivingBase;
import net.minecraftforge.client.event.GuiScreenEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.client.event.RenderLivingEvent;
class Patterns {
@Pattern
private static GuiButton getButton(GuiScreenEvent.ActionPerformedEvent event) {
//#if MC>=10904
return event.getButton();
//#else
//$$ return event.button;
//#endif
}
@Pattern
private static GuiScreen getGui(GuiScreenEvent event) {
//#if MC>=10904
return event.getGui();
//#else
//$$ return event.gui;
//#endif
}
@Pattern
private static EntityLivingBase getEntity(RenderLivingEvent event) {
//#if MC>=10904
return event.getEntity();
//#else
//$$ return event.entity;
//#endif
}
@Pattern
private static RenderGameOverlayEvent.ElementType getType(RenderGameOverlayEvent event) {
//#if MC>=10904
return event.getType();
//#else
//$$ return event.type;
//#endif
}
}