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 @@ -1030,4 +1030,11 @@ public enum FungusGrowthMode {
category = {SURVIVAL, FEATURE}
)
public static FungusGrowthMode thickFungusGrowth = FungusGrowthMode.FALSE;


@Rule(
desc = "make fake players ticked in entity phase, like legacy carpet.",
category = CREATIVE
)
public static boolean fakePlayerTicksLikeEntities = true;
}
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.add(handler);
return handler;
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package carpet.mixins;

import carpet.CarpetSettings;
import carpet.patches.FakePlayerManager;
import net.minecraft.server.network.ServerConnectionListener;
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.fakePlayerTicksLikeEntities) {
FakePlayerManager.tick();
}
}
}
14 changes: 13 additions & 1 deletion src/main/java/carpet/mixins/ServerPlayer_actionPackMixin.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package carpet.mixins;

import carpet.CarpetSettings;
import carpet.fakes.ServerPlayerInterface;
import carpet.helpers.EntityPlayerActionPack;
import com.mojang.authlib.GameProfile;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.TickTask;
import net.minecraft.server.level.ClientInformation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -16,8 +20,10 @@
@Mixin(ServerPlayer.class)
public abstract class ServerPlayer_actionPackMixin implements ServerPlayerInterface
{
@Shadow @Final public MinecraftServer server;
@Unique
public EntityPlayerActionPack actionPack;

@Override
public EntityPlayerActionPack getActionPack()
{
Expand All @@ -33,6 +39,12 @@ private void onServerPlayerEntityContructor(MinecraftServer minecraftServer, Ser
@Inject(method = "tick", at = @At(value = "HEAD"))
private void onTick(CallbackInfo ci)
{
actionPack.onUpdate();
if (CarpetSettings.fakePlayerTicksLikeEntities) {
actionPack.onUpdate();
}
else {
// submit to main thread like other c2s packets
server.tell(new TickTask(server.getTickCount(), 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 @@ -31,8 +33,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 @@ -166,7 +166,9 @@ public void tick()
try
{
super.tick();
this.doTick();
if (CarpetSettings.fakePlayerTicksLikeEntities) {
this.doTick();
}
}
catch (NullPointerException ignored)
{
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/carpet/patches/FakePlayerManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package carpet.patches;

import net.minecraft.server.network.ServerGamePacketListenerImpl;

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

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

public static void reset() {
connections.clear();
}

public static void add(ServerGamePacketListenerImpl connection) {
connections.add(connection);
}

public static void tick() {
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();

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

// todo: vehicle?
}
connections.removeIf(connection -> connection.player.hasDisconnected());
}
}
1 change: 1 addition & 0 deletions src/main/java/carpet/patches/NetHandlerPlayServerFake.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.minecraft.server.network.CommonListenerCookie;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import net.minecraft.world.entity.RelativeMovement;

import java.util.Set;

public class NetHandlerPlayServerFake extends ServerGamePacketListenerImpl
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 @@ -56,6 +56,7 @@
"MinecraftServer_scarpetMixin",
"ExperienceOrb_xpNoCooldownMixin",
"Player_xpNoCooldownMixin",
"ServerConnectionListener_fakePlayersMixin",
"ServerPlayer_actionPackMixin",
"PlayerList_coreMixin",
"PlayerList_fakePlayersMixin",
Expand Down