A way to replace a specific amount of lines of code in Mixin?
-
Let's say that there's a function that includes a few lines of code that a person doesn't want to see and wants to replace them in a Mixin file, how can they do that?
In dumb talk:
Original game function code:
public void onEntityPosition(EntityPositionS2CPacket packet) { NetworkThreadUtils.forceMainThread(packet, this, this.client); Entity entity = this.world.getEntityById(packet.getId()); if (entity != null) { double d = packet.getX(); double e = packet.getY(); double f = packet.getZ(); entity.updateTrackedPosition(d, e, f); if (!entity.isLogicalSideForUpdatingMovement()) { float g = (float)(packet.getYaw() * 360) / 256.0F; float h = (float)(packet.getPitch() * 360) / 256.0F; entity.updateTrackedPositionAndAngles(d, e, f, g, h, 3, true); entity.setOnGround(packet.isOnGround()); } } }
A person wants to replace/mixin out only these:
double d = packet.getX(); double e = packet.getY(); double f = packet.getZ();
After a way of replacing them:
public void onEntityPosition(EntityPositionS2CPacket packet) { NetworkThreadUtils.forceMainThread(packet, this, this.client); Entity entity = this.world.getEntityById(packet.getId()); if (entity != null) { double def = 0D; // AFTER entity.updateTrackedPosition(d, e, f); if (!entity.isLogicalSideForUpdatingMovement()) { float g = (float)(packet.getYaw() * 360) / 256.0F; float h = (float)(packet.getPitch() * 360) / 256.0F; entity.updateTrackedPositionAndAngles(d, e, f, g, h, 3, true); entity.setOnGround(packet.isOnGround()); } } }
Possible? If there's anyone with a good Mixin knowledge, mind giving a solution?
EDIT: My only working way of accomplishing it is by making different injections for each part.
-
Okay, holy crap. I managed to find a way to make it work with 1 injection only... My idea was that if
this.positionLookSetup
was false (which means that player hasn't joined the serveryet
) then keep the original code running normally. If it was set to true though, then cancel and add the custom code, and that way it will not affect the mod that sends its changes afterthis.positionLookSetup
. Tested it and there are no problems on any version now, so far. -
@Overwrite
-
@idk-my-name In my case it's too dangerous to use due to the fact that some other mods might potentially want to modify the same function too and if I overwrite it, only bad stuff will happen.
-
This post is deleted!
-
This post is deleted!
-
-
@aftery I might be wrong, but your idea kind of reminds me of injecting at the top of the function, cancelling it all, and after that, adding the original code with the changes that I want to make. That's probably not what you mean though?
That function I posted was just a random one as an example of what I want to accomplish, by the way. The function that I actually want to make changes on is this one: https://pastebin.com/95kfXWUM
And these 3 lines are the only ones I want to somehow replace/remove:
playerEntity.updatePositionAndAngles(e, g, i, j, k); this.connection.send(new TeleportConfirmC2SPacket(packet.getTeleportId())); this.connection.send(new Full(playerEntity.getX(), playerEntity.getY(), playerEntity.getZ(), playerEntity.getYaw(), playerEntity.getPitch(), false));
I should have put that function as the example instead of that random one I chose...
EDIT: My only way to solve it that I still don't really like is by making different injections for each line. The first one would be
updatePositionAndAngles
, the 2nd oneTeleportConfirmC2SPacket
and so on. -
@aftery I have used that method too and I can say it does work, but here's where things get kinda worse. There's a mod that also injects to that function, but after the
this.positionLookSetup
statement and modifying it (in my case cancelling callbackinfo) prevents me from joining servers. Click here for more info. Essentially if my chosen version for a server is 1.13.2 and below, then I have 0 chances of joining it. If higher than 1.13.2 though, it works normally.EDIT: Perhaps a way to cancel callbackinfo until it reaches the
this.positionLookSetup
statement would be cool. -
-
-
Okay, holy crap. I managed to find a way to make it work with 1 injection only... My idea was that if
this.positionLookSetup
was false (which means that player hasn't joined the serveryet
) then keep the original code running normally. If it was set to true though, then cancel and add the custom code, and that way it will not affect the mod that sends its changes afterthis.positionLookSetup
. Tested it and there are no problems on any version now, so far. -
-