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

@@ -31,7 +31,6 @@ import net.minecraft.util.math.Box;
//#if FABRIC>=1
//#else
//$$ import com.replaymod.core.versions.MCVer;
//$$ import net.minecraftforge.client.event.EntityViewRenderEvent;
//$$ import net.minecraftforge.client.event.RenderGameOverlayEvent;
//$$ import net.minecraftforge.common.MinecraftForge;
@@ -185,7 +184,7 @@ public class CameraEntity
* @param z Delta in Z direction
*/
public void moveCamera(double x, double y, double z) {
setCameraPosition(Entity_getX(this) + x, Entity_getY(this) + y, Entity_getZ(this) + z);
setCameraPosition(this.getX() + x, this.getY() + y, this.getZ() + z);
}
/**
@@ -198,7 +197,7 @@ public class CameraEntity
this.lastRenderX = this.prevX = x;
this.lastRenderY = this.prevY = y;
this.lastRenderZ = this.prevZ = z;
Entity_setPos(this, x, y, z);
this.setPos(x, y, z);
updateBoundingBox();
}
@@ -239,7 +238,7 @@ public class CameraEntity
this.prevZ = to.prevZ;
this.prevYaw = to.prevYaw;
this.prevPitch = to.prevPitch;
Entity_setPos(this, Entity_getX(to), Entity_getY(to), Entity_getZ(to));
this.setPos(to.getX(), to.getY(), to.getZ());
this.yaw = to.yaw;
this.pitch = to.pitch;
this.lastRenderX = to.lastRenderX;
@@ -258,17 +257,18 @@ public class CameraEntity
//#else
//$$ this.boundingBox.setBB(AxisAlignedBB.getBoundingBox(
//#endif
Entity_getX(this) - width / 2, Entity_getY(this), Entity_getZ(this) - width / 2,
Entity_getX(this) + width / 2, Entity_getY(this) + height, Entity_getZ(this) + width / 2));
this.getX() - width / 2, this.getY(), this.getZ() - width / 2,
this.getX() + width / 2, this.getY() + height, this.getZ() + width / 2));
}
@Override
public void tick() {
//#if MC>=10800
Entity view = getRenderViewEntity(this.client);
Entity view =
//#else
//$$ EntityLivingBase view = getRenderViewEntity(this.mc);
//$$ EntityLivingBase view =
//#endif
this.client.getCameraEntity();
if (view != null) {
// Make sure we're always spectating the right entity
// This is important if the spectated player respawns as their
@@ -284,9 +284,9 @@ public class CameraEntity
}
view = this.world.getPlayerByUuid(spectating);
if (view != null) {
setRenderViewEntity(this.client, view);
this.client.setCameraEntity(view);
} else {
setRenderViewEntity(this.client, this);
this.client.setCameraEntity(this);
return;
}
}
@@ -309,7 +309,7 @@ public class CameraEntity
@Override
public void setRotation(float yaw, float pitch) {
if (getRenderViewEntity(this.client) == this) {
if (this.client.getCameraEntity() == this) {
// Only update camera rotation when the camera is the view
super.setRotation(yaw, pitch);
}
@@ -357,7 +357,7 @@ public class CameraEntity
}
private boolean falseUnlessSpectating(Function<Entity, Boolean> property) {
Entity view = getRenderViewEntity(this.client);
Entity view = this.client.getCameraEntity();
if (view != null && view != this) {
return property.apply(view);
}
@@ -407,7 +407,7 @@ public class CameraEntity
//#if MC>=10800
@Override
public float getSpeed() {
Entity view = getRenderViewEntity(this.client);
Entity view = this.client.getCameraEntity();
if (view != this && view instanceof AbstractClientPlayerEntity) {
return ((AbstractClientPlayerEntity) view).getSpeed();
}
@@ -422,7 +422,7 @@ public class CameraEntity
@Override
public boolean isInvisible() {
Entity view = getRenderViewEntity(this.client);
Entity view = this.client.getCameraEntity();
if (view != this) {
return view.isInvisible();
}
@@ -431,7 +431,7 @@ public class CameraEntity
@Override
public Identifier getSkinTexture() {
Entity view = getRenderViewEntity(this.client);
Entity view = this.client.getCameraEntity();
if (view != this && view instanceof PlayerEntity) {
return Utils.getResourceLocationForPlayerUUID(view.getUuid());
}
@@ -450,7 +450,7 @@ public class CameraEntity
@Override
public boolean isPartVisible(PlayerModelPart modelPart) {
Entity view = getRenderViewEntity(this.client);
Entity view = this.client.getCameraEntity();
if (view != this && view instanceof PlayerEntity) {
return ((PlayerEntity) view).isPartVisible(modelPart);
}
@@ -460,7 +460,7 @@ public class CameraEntity
@Override
public float getHandSwingProgress(float renderPartialTicks) {
Entity view = getRenderViewEntity(this.client);
Entity view = this.client.getCameraEntity();
if (view != this && view instanceof PlayerEntity) {
return ((PlayerEntity) view).getHandSwingProgress(renderPartialTicks);
}
@@ -705,7 +705,7 @@ public class CameraEntity
{ on(PreRenderHandCallback.EVENT, this::onRenderHand); }
private boolean onRenderHand() {
// Unless we are spectating another player, don't render our hand
Entity view = getRenderViewEntity(mc);
Entity view = mc.getCameraEntity();
if (view == CameraEntity.this || !(view instanceof PlayerEntity)) {
return true; // cancel hand rendering
} else {
@@ -761,7 +761,7 @@ public class CameraEntity
//#else
//$$ @SubscribeEvent
//$$ public void preRenderGameOverlay(RenderGameOverlayEvent.Pre event) {
//$$ switch (MCVer.getType(event)) {
//$$ switch (event.getType()) {
//$$ case ALL:
//$$ heldItemTooltipsWasTrue = mc.gameSettings.heldItemTooltips;
//$$ mc.gameSettings.heldItemTooltips = false;
@@ -797,7 +797,7 @@ public class CameraEntity
//$$
//$$ @SubscribeEvent
//$$ public void postRenderGameOverlay(RenderGameOverlayEvent.Post event) {
//$$ if (MCVer.getType(event) != RenderGameOverlayEvent.ElementType.ALL) return;
//$$ if (event.getType() != RenderGameOverlayEvent.ElementType.ALL) return;
//$$ mc.gameSettings.heldItemTooltips = heldItemTooltipsWasTrue;
//$$ }
//#endif

View File

@@ -1,9 +1,11 @@
package com.replaymod.replay.camera;
import de.johni0702.minecraft.gui.utils.lwjgl.vector.Vector3f;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.options.KeyBinding;
import static com.replaymod.core.versions.MCVer.*;
import static net.minecraft.util.math.MathHelper.cos;
import static net.minecraft.util.math.MathHelper.sin;
// TODO: Marius is responsible for this. Please, someone clean it up.
public class ClassicCameraController implements CameraController {
@@ -32,7 +34,7 @@ public class ClassicCameraController implements CameraController {
public void update(float partialTicksPassed) {
boolean forward = false, backward = false, left = false, right = false, up = false, down = false;
speedup = false;
for(KeyBinding kb : getMinecraft().options.keysAll) {
for(KeyBinding kb : MinecraftClient.getInstance().options.keysAll) {
if(!kb.isPressed()) continue;
if(kb.getTranslationKey().equals("key.forward")) {
forward = true;

View File

@@ -52,9 +52,9 @@ public class SpectatorCameraController implements CameraController {
// Always make sure the camera is in the exact same spot as the spectated entity
// This is necessary as some rendering code for the hand doesn't respect the view entity
// and always uses mc.thePlayer
Entity view = getRenderViewEntity(mc);
Entity view = mc.getCameraEntity();
if (view != null && view != camera) {
camera.setCameraPosRot(getRenderViewEntity(mc));
camera.setCameraPosRot(mc.getCameraEntity());
// If it's a player, also 'steal' its inventory so the rendering code knows what item to render
if (view instanceof PlayerEntity) {
PlayerEntity viewPlayer = (PlayerEntity) view;