Skip to content

Instantly share code, notes, and snippets.

@nahkd123
Last active April 2, 2024 07:07
Show Gist options
  • Save nahkd123/ad10adc408400d7f0636d11f055b574a to your computer and use it in GitHub Desktop.
Save nahkd123/ad10adc408400d7f0636d11f055b574a to your computer and use it in GitHub Desktop.
The "SetCameraEntityS2CPacket" trick

The SetCameraEntityS2CPacket trick

What is this?

SetCameraEntityS2CPacket is a clientbound packet that will be sent to client when player interacts an entity while in spectator mode. This packet is mainly used for setting the player's camera to entity's position.

The jank: Not in spectator mode

But what if we send SetCameraEntityS2CPacket when player is NOT in spectator mode? Surprisingly, player didn't kick themselves from server, plus their camera is now attached to target entity! The camera behavior is what you would normally expect like spectating mob the normal way (eg: hide hotbars, add linear interpolation to camera). Setting the camera to self (self as in the player whose connection handler is controlling) returns everything to normal. I haven't tested what happens when you set the camera to other players.

Another jank: Player's input

While the camera is attached to another entity, player's input like moving, sneaking, jumping and looking around will still working normally. You can test this by placing an armor stand, sit on a boat, send SetCameraEntityS2CPacket with that armor stand as target, and move around like how you would ride the boat. You'll see the boat moving around.

Use cases

Maps + this jank = Server-side GUI

Since maps can be used to draw bitmaps, you can combines with this janky tech to make server-side GUI, without requiring player to download a resource pack. This is exactly what Patbox's patch of "CC: Tweaked" is abusing to have server-side computer GUI.

3rd person + custom player model

What if you want to make a Zelda clone in Minecraft? You need a way to force player to be in 3rd person, but you also want to position player's model off-center as well (eg: when using bow).

With SetCameraEntityS2CPacket, a bit of packets intercepting (pretty easy with some Mixins) and armor stands (does display entities works?), you can force any player to be in 3rd person! Attach camera to armor stand, then teleport it around to position the camera. For the model, you can use Nylon or my Crystalize library (shameless promotion 😉) to attach it to player's location, and suddenly you got yourself Genshin Zelda clone, completely server-side.

Example code

public class SetCameraCommand {
public static LiteralArgumentBuilder<ServerCommandSource> setCamera() {
return literal("setcamera").then(argument("player", EntityArgumentType.player())
.then(literal("reset").executes(ctx -> {
ServerPlayerEntity player = EntityArgumentType.getPlayer(ctx, "player");
player.networkHandler.sendPacket(new SetCameraEntityS2CPacket(player));
ctx.getSource().sendFeedback(() -> Text.empty()
.append(player.getDisplayName())
.append("'s camera has been reseted"), true);
return 1;
}))
.then(argument("entity", EntityArgumentType.entity()).executes(ctx -> {
ServerPlayerEntity player = EntityArgumentType.getPlayer(ctx, "player");
Entity entity = EntityArgumentType.getEntity(ctx, "entity");
player.networkHandler.sendPacket(new SetCameraEntityS2CPacket(entity));
ctx.getSource().sendFeedback(() -> Text.empty()
.append(player.getDisplayName())
.append("'s camera is attached to ")
.append(entity.getDisplayName()), true);
return 1;
})));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment