Skip to content

Commit

Permalink
Split up anonymous classes more
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Jan 28, 2025
1 parent 3f487a0 commit 945ed53
Showing 1 changed file with 108 additions and 104 deletions.
212 changes: 108 additions & 104 deletions server/src/main/java/com/soulfiremc/server/plugins/POVServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ public void serverClosed(ServerClosedEvent event) {

@Override
public void sessionAdded(SessionAddedEvent event) {
event.getSession().addListener(new POVClientSessionAdapter(instanceManager, settingsSource));
event.getSession().addListener(new C2POVAdapter(instanceManager, settingsSource));
event.getSession().addListener(new SessionAdapter() {
private boolean fixedOnce;

Expand Down Expand Up @@ -827,7 +827,7 @@ public void sessionRemoved(SessionRemovedEvent event) {
}

@RequiredArgsConstructor
private static class POVClientSessionAdapter extends SessionAdapter {
private static class C2POVAdapter extends SessionAdapter {
private final InstanceManager instanceManager;
private final InstanceSettingsSource settingsSource;
private BotConnection botConnection;
Expand Down Expand Up @@ -865,108 +865,8 @@ public void packetReceived(Session clientSession, Packet packet) {
}

botConnection = first.get();
botConnection
.session()
.addListener(
new SessionAdapter() {
@Override
public void packetReceived(Session botSession, Packet packet) {
if (!enableForwarding
|| NOT_SYNCED.contains(packet.getClass())) {
return;
}

if (packet instanceof ClientboundPlayerChatPacket chatPacket) {
// To avoid signature issues since the signature is for the bot, not the connected user
clientSession.send(new ClientboundSystemChatPacket(botConnection.dataManager().prepareChatTypeMessage(chatPacket.getChatType(), new SFChatType.BoundChatMessageInfo(
botConnection.dataManager().getComponentForPlayerChat(chatPacket),
chatPacket.getName(),
chatPacket.getTargetName()
)), false));
return;
}

// MC Server of the bot -> MC Client
clientSession.send(packet);
}

@Override
public void packetSent(Session botSession, Packet packet) {
if (!enableForwarding
|| NOT_SYNCED.contains(packet.getClass())) {
return;
}

var clientEntity =
botConnection.dataManager().localPlayer();
// Bot -> MC Client
switch (packet) {
case ServerboundMovePlayerPosRotPacket posRot -> {
clientSession.send(
new ClientboundMoveEntityPosRotPacket(
clientEntity.entityId(),
(posRot.getX() * 32 - lastX * 32) * 128,
(posRot.getY() * 32 - lastY * 32) * 128,
(posRot.getZ() * 32 - lastZ * 32) * 128,
posRot.getYaw(),
posRot.getPitch(),
clientEntity.onGround()));
lastX = posRot.getX();
lastY = posRot.getY();
lastZ = posRot.getZ();
}
case ServerboundMovePlayerPosPacket pos -> {
clientSession.send(
new ClientboundMoveEntityPosPacket(
clientEntity.entityId(),
(pos.getX() * 32 - lastX * 32) * 128,
(pos.getY() * 32 - lastY * 32) * 128,
(pos.getZ() * 32 - lastZ * 32) * 128,
clientEntity.onGround()));
lastX = pos.getX();
lastY = pos.getY();
lastZ = pos.getZ();
}
case ServerboundMovePlayerRotPacket rot -> clientSession.send(
new ClientboundMoveEntityRotPacket(
clientEntity.entityId(),
rot.getYaw(),
rot.getPitch(),
clientEntity.onGround()));
default -> {
}
}
}
});
botConnection.scheduler().schedule(() -> {
try {
syncBotAndUser(botConnection, clientSession);
} catch (Throwable t) {
log.error("Failed to sync bot and user", t);
clientSession.send(new ClientboundSystemChatPacket(
Component.text("Error while syncing you with the bot! Report this error to SoulFire developers.")
.color(NamedTextColor.RED),
false));
}

// Give the client a few moments to process the packets
TimeUtil.waitTime(2, TimeUnit.SECONDS);

enableForwarding = true;

clientSession.send(
new ClientboundSystemChatPacket(
Component.text("Connected to bot ")
.color(NamedTextColor.GREEN)
.append(
Component.text(
botConnection.accountName())
.color(NamedTextColor.AQUA)
.decorate(TextDecoration.UNDERLINED))
.append(Component.text("!"))
.color(NamedTextColor.GREEN),
false));
});
botConnection.session().addListener(new B2SAdapter(clientSession));
botConnection.scheduler().schedule(() -> executeSync(clientSession));
}
} else if (enableForwarding && !NOT_SYNCED.contains(packet.getClass())) {
// For data consistence, ensure all packets sent from client -> server are
Expand Down Expand Up @@ -1047,6 +947,110 @@ public void disconnected(DisconnectedEvent event) {
.setCause(event.getCause())
.log("POV -> C Disconnected: {}", SoulFireAdventure.PLAIN_MESSAGE_SERIALIZER.serialize(event.getReason()));
}

private void executeSync(Session clientSession) {
try {
syncBotAndUser(botConnection, clientSession);
} catch (Throwable t) {
log.error("Failed to sync bot and user", t);
clientSession.send(new ClientboundSystemChatPacket(
Component.text("Error while syncing you with the bot! Report this error to SoulFire developers.")
.color(NamedTextColor.RED),
false));
}

// Give the client a few moments to process the packets
TimeUtil.waitTime(2, TimeUnit.SECONDS);

enableForwarding = true;

clientSession.send(
new ClientboundSystemChatPacket(
Component.text("Connected to bot ")
.color(NamedTextColor.GREEN)
.append(
Component.text(
botConnection.accountName())
.color(NamedTextColor.AQUA)
.decorate(TextDecoration.UNDERLINED))
.append(Component.text("!"))
.color(NamedTextColor.GREEN),
false));
}

@RequiredArgsConstructor
private class B2SAdapter extends SessionAdapter {
private final Session clientSession;

@Override
public void packetReceived(Session botSession, Packet packet) {
if (!enableForwarding
|| NOT_SYNCED.contains(packet.getClass())) {
return;
}

if (packet instanceof ClientboundPlayerChatPacket chatPacket) {
// To avoid signature issues since the signature is for the bot, not the connected user
clientSession.send(new ClientboundSystemChatPacket(botConnection.dataManager().prepareChatTypeMessage(chatPacket.getChatType(), new SFChatType.BoundChatMessageInfo(
botConnection.dataManager().getComponentForPlayerChat(chatPacket),
chatPacket.getName(),
chatPacket.getTargetName()
)), false));
return;
}

// MC Server of the bot -> MC Client
clientSession.send(packet);
}

@Override
public void packetSent(Session botSession, Packet packet) {
if (!enableForwarding
|| NOT_SYNCED.contains(packet.getClass())) {
return;
}

var clientEntity =
botConnection.dataManager().localPlayer();
// Bot -> MC Client
switch (packet) {
case ServerboundMovePlayerPosRotPacket posRot -> {
clientSession.send(
new ClientboundMoveEntityPosRotPacket(
clientEntity.entityId(),
(posRot.getX() * 32 - lastX * 32) * 128,
(posRot.getY() * 32 - lastY * 32) * 128,
(posRot.getZ() * 32 - lastZ * 32) * 128,
posRot.getYaw(),
posRot.getPitch(),
clientEntity.onGround()));
lastX = posRot.getX();
lastY = posRot.getY();
lastZ = posRot.getZ();
}
case ServerboundMovePlayerPosPacket pos -> {
clientSession.send(
new ClientboundMoveEntityPosPacket(
clientEntity.entityId(),
(pos.getX() * 32 - lastX * 32) * 128,
(pos.getY() * 32 - lastY * 32) * 128,
(pos.getZ() * 32 - lastZ * 32) * 128,
clientEntity.onGround()));
lastX = pos.getX();
lastY = pos.getY();
lastZ = pos.getZ();
}
case ServerboundMovePlayerRotPacket rot -> clientSession.send(
new ClientboundMoveEntityRotPacket(
clientEntity.entityId(),
rot.getYaw(),
rot.getPitch(),
clientEntity.onGround()));
default -> {
}
}
}
}
}

@NoArgsConstructor(access = AccessLevel.NONE)
Expand Down

0 comments on commit 945ed53

Please sign in to comment.