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:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,9 @@ net.minecraft.network.play.server.SPacketEntity.Move net.minecraft.network.play.
|
||||
net.minecraftforge.fml.network.NetworkRegistry net.minecraftforge.fml.common.network.NetworkRegistry
|
||||
net.minecraft.client.particle.ParticleManager queue queueEntityFX
|
||||
org.apache.maven.artifact.versioning.ComparableVersion net.minecraftforge.fml.common.versioning.ComparableVersion
|
||||
org.lwjgl.glfw.GLFW com.replaymod.core.versions.GLFW
|
||||
net.minecraft.client.MainWindow com.replaymod.core.versions.Window
|
||||
net.minecraft.client.audio.SimpleSound net.minecraft.client.audio.PositionedSoundRecord
|
||||
|
||||
net.minecraft.resources.FolderPack getInputStream() getInputStreamByName()
|
||||
net.minecraft.client.gui.GuiYesNoCallback confirmResult() confirmClicked()
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.replaymod.core;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraftforge.fml.ModList;
|
||||
|
||||
import static com.replaymod.core.ReplayMod.MOD_ID;
|
||||
@@ -8,4 +9,12 @@ public class ReplayModBackend {
|
||||
public String getVersion() {
|
||||
return ModList.get().getModContainerById(MOD_ID).get().getModInfo().getVersion().toString();
|
||||
}
|
||||
|
||||
public String getMinecraftVersion() {
|
||||
return Minecraft.getInstance().getMinecraftGame().getVersion().getName();
|
||||
}
|
||||
|
||||
public boolean isModLoaded(String id) {
|
||||
return ModList.get().isLoaded(id.toLowerCase());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.replaymod.core.versions;
|
||||
|
||||
import net.minecraft.client.renderer.Tessellator;
|
||||
|
||||
public class BufferBuilder {
|
||||
|
||||
private final Tessellator inner;
|
||||
|
||||
public BufferBuilder(Tessellator inner) {
|
||||
this.inner = inner;
|
||||
}
|
||||
|
||||
public void startDrawing(int glQuads) {
|
||||
inner.startDrawing(glQuads);
|
||||
}
|
||||
|
||||
public void addVertexWithUV(double x, double y, double z, float u, float v) {
|
||||
inner.addVertexWithUV(x, y, z, u, v);
|
||||
}
|
||||
|
||||
public void setColorRGBA(int r, int g, int b, int a) {
|
||||
inner.setColorRGBA(r, g, b, a);
|
||||
}
|
||||
|
||||
public void addVertex(double x, double y, double z) {
|
||||
inner.addVertex(x, y, z);
|
||||
}
|
||||
|
||||
public static class VertexFormats {}
|
||||
}
|
||||
@@ -36,3 +36,5 @@ net.minecraft.client.renderer.tileentity.TileEntityEndPortalRenderer net.minecra
|
||||
net.minecraft.network.play.server.S3FPacketCustomPayload getChannelName() func_149169_c()
|
||||
net.minecraft.network.play.server.S01PacketJoinGame getEntityId() func_149197_c()
|
||||
net.minecraft.client.network.NetHandlerLoginClient networkManager field_147393_d
|
||||
net.minecraft.client.renderer.WorldRenderer com.replaymod.core.versions.BufferBuilder
|
||||
net.minecraft.client.renderer.vertex.DefaultVertexFormats com.replaymod.core.versions.BufferBuilder.VertexFormats
|
||||
|
||||
@@ -20,6 +20,7 @@ net.minecraft.network.play.server.SPacketSpawnMob dataManager field_149043_l
|
||||
net.minecraft.network.play.server.SPacketSpawnMob getDataManagerEntries() func_149027_c()
|
||||
net.minecraft.network.play.server.SPacketSpawnPlayer getDataManagerEntries() func_148944_c()
|
||||
net.minecraft.client.multiplayer.ServerList saveSingleServer() func_147414_b()
|
||||
net.minecraft.util.SoundEvent com.replaymod.core.versions.MCVer.SoundEvent
|
||||
|
||||
net.minecraft.network.play.server.SPacketJoinGame net.minecraft.network.play.server.S01PacketJoinGame
|
||||
net.minecraft.network.play.server.SPacketChat net.minecraft.network.play.server.S02PacketChat
|
||||
|
||||
Reference in New Issue
Block a user