Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tick fake players in network updating phase #1847

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions src/main/java/carpet/CarpetServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import carpet.commands.PerimeterInfoCommand;
import carpet.commands.PlayerCommand;
import carpet.commands.ProfileCommand;
import carpet.patches.FakePlayerManager;
import carpet.script.ScriptCommand;
import carpet.commands.SpawnCommand;
import carpet.commands.TestCommand;
Expand Down Expand Up @@ -190,6 +191,7 @@ public static void onServerClosed(MinecraftServer server)
LoggerRegistry.stopLoggers();
HUDController.resetScarpetHUDs();
ParticleParser.resetCache();
FakePlayerManager.reset();
extensions.forEach(e -> e.onServerClosed(server));
minecraft_server = null;
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/carpet/CarpetSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -1062,4 +1062,11 @@ public enum FungusGrowthMode {
category = {SURVIVAL, FEATURE}
)
public static FungusGrowthMode thickFungusGrowth = FungusGrowthMode.FALSE;


@Rule(
desc = "make fake players ticked in entity update phase, like legacy carpet.",
category = CREATIVE
)
public static boolean fakePlayerTicksInEU = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a default value that changes the existing behavior is BAD. It breaks people's contraptions that rely on the old behavior sliently

}
5 changes: 4 additions & 1 deletion src/main/java/carpet/mixins/PlayerList_fakePlayersMixin.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package carpet.mixins;

import carpet.patches.FakePlayerManager;
import com.mojang.authlib.GameProfile;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.Connection;
Expand Down Expand Up @@ -41,7 +42,9 @@ private ServerGamePacketListenerImpl replaceNetworkHandler(MinecraftServer serve
{
if (playerIn instanceof EntityPlayerMPFake fake)
{
return new NetHandlerPlayServerFake(this.server, clientConnection, fake, cookie);
var handler = new NetHandlerPlayServerFake(this.server, clientConnection, fake, cookie);
FakePlayerManager.connections.add(handler);
zly2006 marked this conversation as resolved.
Show resolved Hide resolved
return handler;
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package carpet.mixins;

import carpet.CarpetSettings;
import carpet.fakes.ServerPlayerInterface;
import carpet.patches.FakePlayerManager;
import net.minecraft.server.network.ServerConnectionListener;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
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;

@Mixin(ServerConnectionListener.class)
public class ServerConnectionListener_fakePlayersMixin {
@Inject(
method = "tick",
at = @At("RETURN")
)
private void tickFakePlayers(CallbackInfo ci) {
if (!CarpetSettings.fakePlayerTicksInEU) {
for (ServerGamePacketListenerImpl connection : FakePlayerManager.connections) {
// from ServerGamePacketListenerImpl#tick

connection.player.xo = connection.player.getX();
connection.player.yo = connection.player.getY();
connection.player.zo = connection.player.getZ();

// action packet update
((ServerPlayerInterface) connection.player).getActionPack().onUpdate();
zly2006 marked this conversation as resolved.
Show resolved Hide resolved

connection.player.doTick();
// connection.player.absMoveTo(connection.firstGoodX, connection.firstGoodY, connection.firstGoodZ, connection.player.getYRot(), connection.player.getXRot());

// todo: vehicle?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll suggest to mark the PR as a draft, if it's not completed yet

}
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/carpet/mixins/ServerLevel_tickMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,4 @@ private void endRandomTicks(CallbackInfo ci) {
}
}

}
}
15 changes: 14 additions & 1 deletion src/main/java/carpet/mixins/ServerPlayer_actionPackMixin.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package carpet.mixins;

import carpet.CarpetSettings;
import carpet.fakes.ServerPlayerInterface;
import carpet.helpers.EntityPlayerActionPack;
import carpet.patches.EntityPlayerMPFake;
import com.mojang.authlib.GameProfile;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ClientInformation;
Expand All @@ -18,6 +20,7 @@ public abstract class ServerPlayer_actionPackMixin implements ServerPlayerInterf
{
@Unique
public EntityPlayerActionPack actionPack;

@Override
public EntityPlayerActionPack getActionPack()
{
Expand All @@ -33,6 +36,16 @@ private void onServerPlayerEntityContructor(MinecraftServer minecraftServer, Ser
@Inject(method = "tick", at = @At(value = "HEAD"))
private void onTick(CallbackInfo ci)
{
actionPack.onUpdate();
if (CarpetSettings.fakePlayerTicksInEU) {
actionPack.onUpdate();
}
}

@Inject(method = "doTick", at = @At(value = "HEAD"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are alternating where real player's action pack gets ticked right? then the same as https://github.com/gnembon/fabric-carpet/pull/1847/files#r1412213260

private void tickActionPack(CallbackInfo ci)
{
if (!((Object) this instanceof EntityPlayerMPFake)) {
actionPack.onUpdate();
}
}
}
8 changes: 5 additions & 3 deletions src/main/java/carpet/patches/EntityPlayerMPFake.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package carpet.patches;

import carpet.CarpetSettings;
import carpet.fakes.ServerPlayerInterface;
import carpet.utils.Messenger;
import com.mojang.authlib.GameProfile;
import net.minecraft.core.BlockPos;
import net.minecraft.core.UUIDUtil;
Expand Down Expand Up @@ -30,8 +32,6 @@
import net.minecraft.world.level.block.entity.SkullBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.Vec3;
import carpet.fakes.ServerPlayerInterface;
import carpet.utils.Messenger;

import java.util.Optional;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -164,7 +164,9 @@ public void tick()
try
{
super.tick();
this.doTick();
if (CarpetSettings.fakePlayerTicksInEU) {
this.doTick();
}
}
catch (NullPointerException ignored)
{
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/carpet/patches/FakePlayerManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package carpet.patches;

import net.minecraft.server.network.ServerGamePacketListenerImpl;

import java.util.ArrayList;
import java.util.List;

public class FakePlayerManager {
public static List<ServerGamePacketListenerImpl> connections = new ArrayList<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: use concurrency-proof containers to handle potential concurrent access in certain situations (e.g. using mods)


public static void reset() {
connections.clear();
}
}
12 changes: 12 additions & 0 deletions src/main/java/carpet/patches/NetHandlerPlayServerFake.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package carpet.patches;

import net.minecraft.Util;
import net.minecraft.network.Connection;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.contents.TranslatableContents;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientboundBlockChangedAckPacket;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.CommonListenerCookie;
Expand All @@ -23,6 +25,11 @@ public void send(final Packet<?> packetIn)
{
}

@Override
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any explanations for the changes in these files? they don't seem related

protected void keepConnectionAlive()
{
}

@Override
public void disconnect(Component message)
{
Expand All @@ -42,6 +49,11 @@ public void teleport(double d, double e, double f, float g, float h, Set<Relativ
}
}

@Override
public void tick()
{
System.out.println("NetHandlerPlayServerFake tick -- not implemented yet");
}
}


Expand Down
1 change: 1 addition & 0 deletions src/main/resources/carpet.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"MinecraftServer_scarpetMixin",
"ExperienceOrb_xpNoCooldownMixin",
"Player_xpNoCooldownMixin",
"ServerConnectionListener_fakePlayersMixin",
"ServerPlayer_actionPackMixin",
"PlayerList_coreMixin",
"PlayerList_fakePlayersMixin",
Expand Down