Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libraries/networking-impl/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ library_version = 0.1.1
entrypoint_init = net.ornithemc.osl.networking.impl.Networking
entrypoint_client_init = net.ornithemc.osl.networking.impl.Networking
entrypoint_server_init = net.ornithemc.osl.networking.impl.Networking
osl_dependencies = core:>=0.7.0,entrypoints:>=0.5.0,lifecycle-events:>=0.6.0,networking:>=0.9.0
osl_dependencies = core:>=0.7.0,entrypoints:>=0.5.0,lifecycle-events:>=0.6.0,executors:>=0.1.0,text-components:>=0.1.0-,networking:>=0.9.0
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
import net.ornithemc.osl.networking.api.StringChannelIdentifierParser;
import net.ornithemc.osl.networking.api.client.ClientConnectionEvents;
import net.ornithemc.osl.networking.api.server.ServerConnectionEvents;
import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess;
import net.ornithemc.osl.networking.impl.access.ClientNetworkHandlerAccess;
import net.ornithemc.osl.networking.impl.access.ServerNetworkHandlerAccess;
import net.ornithemc.osl.networking.impl.client.ClientConnectionContext;
import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl;
import net.ornithemc.osl.networking.impl.server.ServerConnectionContext;
import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl;

public class Networking implements ModInitializer, ClientModInitializer, ServerModInitializer {
Expand All @@ -29,8 +32,11 @@ public void init() {
// send channel registration data as a response to receiving client channel registration data
ServerPlayNetworkingImpl.sendNoCheck(context.player(), HandshakePayload.CHANNEL, HandshakePayload.server());

((NetworkHandlerAccess)context.networkHandler()).osl$networking$registerChannels(payload.channels);
ServerConnectionEvents.PLAY_READY.invoker().accept(context.server(), context.player());
ServerNetworkHandlerAccess networkHandler = (ServerNetworkHandlerAccess) context.networkHandler();
ServerConnectionContext connectionContext = networkHandler.osl$networking$connectionContext();

networkHandler.osl$networking$registerChannels(payload.channels);
ServerConnectionEvents.PLAY_READY.invoker().accept(connectionContext);
});
}

Expand All @@ -41,8 +47,11 @@ public void initClient() {
ClientPlayNetworkingImpl.setUpPacketFactory((channel, data) ->
new CustomPayloadC2SPacket(StringChannelIdentifierParser.toString(channel), PacketBuffers.unwrapped(data)));
ClientPlayNetworkingImpl.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (context, payload) -> {
((NetworkHandlerAccess)context.networkHandler()).osl$networking$registerChannels(payload.channels);
ClientConnectionEvents.PLAY_READY.invoker().accept(context.minecraft());
ClientNetworkHandlerAccess networkHandler = (ClientNetworkHandlerAccess) context.networkHandler();
ClientConnectionContext connectionContext = networkHandler.osl$networking$connectionContext();

networkHandler.osl$networking$registerChannels(payload.channels);
ClientConnectionEvents.PLAY_READY.invoker().accept(connectionContext);
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.ornithemc.osl.networking.impl.access;

import net.ornithemc.osl.networking.impl.client.ClientConnectionContext;

public interface ClientNetworkHandlerAccess extends NetworkHandlerAccess {

ClientConnectionContext osl$networking$connectionContext();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.ornithemc.osl.networking.impl.access;

import net.ornithemc.osl.networking.impl.server.ServerConnectionContext;

public interface ServerNetworkHandlerAccess extends NetworkHandlerAccess {

ServerConnectionContext osl$networking$connectionContext();

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.ornithemc.osl.networking.impl.mixin.client;

import java.net.SocketAddress;
import java.util.LinkedHashSet;
import java.util.Set;

Expand All @@ -13,23 +14,36 @@

import net.minecraft.client.Minecraft;
import net.minecraft.client.network.handler.ClientPlayNetworkHandler;
import net.minecraft.network.Connection;
import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket;
import net.minecraft.network.packet.s2c.play.DisconnectS2CPacket;
import net.minecraft.server.integrated.IntegratedServer;
import net.minecraft.text.Text;

import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
import net.ornithemc.osl.networking.api.client.ClientConnectionEvents;
import net.ornithemc.osl.networking.impl.AddressParser;
import net.ornithemc.osl.networking.impl.HandshakePayload;
import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess;
import net.ornithemc.osl.networking.impl.access.ClientNetworkHandlerAccess;
import net.ornithemc.osl.networking.impl.client.ClientConnectionContext;
import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl;
import net.ornithemc.osl.text.api.TextComponents;

@Mixin(ClientPlayNetworkHandler.class)
public class ClientPlayNetworkHandlerMixin implements NetworkHandlerAccess {
public class ClientPlayNetworkHandlerMixin implements ClientNetworkHandlerAccess {

@Shadow @Final private Minecraft minecraft;
@Shadow @Final
private Minecraft minecraft;
@Shadow @Final
private Connection connection;

@Unique
private ClientConnectionContext connectionContext;
/**
* Channels that the server is listening to.
*/
@Unique private Set<NamespacedIdentifier> serverChannels;
@Unique
private Set<NamespacedIdentifier> serverChannels;

@Inject(
method = "handleLogin",
Expand All @@ -38,10 +52,34 @@ public class ClientPlayNetworkHandlerMixin implements NetworkHandlerAccess {
)
)
private void osl$networking$handleLogin(CallbackInfo ci) {
if (minecraft.isIntegratedServerRunning()) {
IntegratedServer server = minecraft.getServer();
String worldName = server.getWorldName();

connectionContext = new ClientConnectionContext(minecraft, worldName);
} else {
SocketAddress address = connection.getAddress();

String serverAddress = AddressParser.getAddress(address);
int serverPort = AddressParser.getPort(address);

connectionContext = new ClientConnectionContext(minecraft, serverAddress, serverPort);
}

// send channel registration data as soon as login occurs
ClientPlayNetworkingImpl.sendNoCheck(HandshakePayload.CHANNEL, HandshakePayload.client());

ClientConnectionEvents.LOGIN.invoker().accept(minecraft);
ClientConnectionEvents.LOGIN.invoker().accept(connectionContext);
}

@Inject(
method = "handleDisconnect",
at = @At(
value = "HEAD"
)
)
private void osl$networking$handleDisconnect(DisconnectS2CPacket packet, CallbackInfo ci) {
connectionContext.offerDisconnectReason(TextComponents.resolve(packet.getReason()));
}

@Inject(
Expand All @@ -50,9 +88,8 @@ public class ClientPlayNetworkHandlerMixin implements NetworkHandlerAccess {
value = "HEAD"
)
)
private void osl$networking$handleDisconnect(CallbackInfo ci) {
ClientConnectionEvents.DISCONNECT.invoker().accept(minecraft);
serverChannels = null;
private void osl$networking$handleDisconnect(Text reason, CallbackInfo ci) {
connectionContext.offerDisconnectReason(TextComponents.resolve(reason));
}

@Inject(
Expand All @@ -68,6 +105,11 @@ public class ClientPlayNetworkHandlerMixin implements NetworkHandlerAccess {
}
}

@Override
public ClientConnectionContext osl$networking$connectionContext() {
return connectionContext;
}

@Override
public boolean osl$networking$isPlayReady() {
return serverChannels != null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@
package net.ornithemc.osl.networking.impl.mixin.client;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.client.Minecraft;
import net.minecraft.util.BlockableEventLoop;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.network.handler.ClientPlayNetworkHandler;
import net.minecraft.client.world.ClientWorld;

import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess;
import net.ornithemc.osl.networking.api.client.ClientConnectionEvents;
import net.ornithemc.osl.networking.impl.access.ClientNetworkHandlerAccess;
import net.ornithemc.osl.networking.impl.client.ClientConnectionContext;

@Mixin(Minecraft.class)
public abstract class MinecraftMixin implements BlockableEventLoop, TaskRunnerAccess {
public class MinecraftMixin {

@Override
public boolean osl$networking$submit(Runnable task) {
this.executeTask(task);
return true;
@Shadow
private ClientWorld world;

@Shadow
private ClientPlayNetworkHandler getNetworkHandler() { return null; }

@Inject(
method = "setWorld(Lnet/minecraft/client/world/ClientWorld;Lnet/minecraft/client/gui/screen/Screen;)V",
at = @At(
value = "HEAD"
)
)
private void osl$networking$disconnect(ClientWorld world, Screen screen, CallbackInfo ci) {
if (this.world != null && world == null) {
ClientNetworkHandlerAccess networkHandler = (ClientNetworkHandlerAccess) getNetworkHandler();
ClientConnectionContext connectionContext = networkHandler.osl$networking$connectionContext();

ClientConnectionEvents.DISCONNECT.invoker().accept(connectionContext);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.network.Connection;
import com.llamalad7.mixinextras.sugar.Local;

import net.minecraft.server.MinecraftServer;
import net.minecraft.server.PlayerManager;
import net.minecraft.server.entity.living.player.ServerPlayerEntity;

import net.ornithemc.osl.networking.api.server.ServerConnectionEvents;
import net.ornithemc.osl.networking.impl.access.PlayerManagerAccess;
import net.ornithemc.osl.networking.impl.access.ServerNetworkHandlerAccess;
import net.ornithemc.osl.networking.impl.server.ServerConnectionContext;

@Mixin(PlayerManager.class)
public class PlayerManagerMixin implements PlayerManagerAccess {
Expand All @@ -29,8 +32,24 @@ public class PlayerManagerMixin implements PlayerManagerAccess {
value = "TAIL"
)
)
private void osl$networking$handleLogin(Connection connection, ServerPlayerEntity player, CallbackInfo ci) {
ServerConnectionEvents.LOGIN.invoker().accept(server, player);
private void osl$networking$handleLogin(CallbackInfo ci, @Local ServerPlayerEntity player) {
ServerNetworkHandlerAccess networkHandler = (ServerNetworkHandlerAccess) player.networkHandler;
ServerConnectionContext connectionContext = networkHandler.osl$networking$connectionContext();

ServerConnectionEvents.LOGIN.invoker().accept(connectionContext);
}

@Inject(
method = "remove",
at = @At(
value = "HEAD"
)
)
private void osl$networking$handleDisconnect(CallbackInfo ci, @Local ServerPlayerEntity player) {
ServerNetworkHandlerAccess networkHandler = (ServerNetworkHandlerAccess) player.networkHandler;
ServerConnectionContext connectionContext = networkHandler.osl$networking$connectionContext();

ServerConnectionEvents.DISCONNECT.invoker().accept(connectionContext);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,49 @@
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.entity.living.player.ServerPlayerEntity;
import net.minecraft.server.network.handler.ServerPlayNetworkHandler;
import net.minecraft.text.Text;

import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
import net.ornithemc.osl.networking.api.server.ServerConnectionEvents;
import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess;
import net.ornithemc.osl.networking.impl.access.ServerNetworkHandlerAccess;
import net.ornithemc.osl.networking.impl.server.ServerConnectionContext;
import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl;
import net.ornithemc.osl.text.api.TextComponents;

@Mixin(ServerPlayNetworkHandler.class)
public class ServerPlayNetworkHandlerMixin implements NetworkHandlerAccess {
public class ServerPlayNetworkHandlerMixin implements ServerNetworkHandlerAccess {

@Shadow @Final private MinecraftServer server;
@Shadow @Final private ServerPlayerEntity player;
@Shadow @Final
private MinecraftServer server;

@Shadow
private ServerPlayerEntity player;

@Unique
private ServerConnectionContext connectionContext;
/**
* Channels that the client is listening to.
*/
@Unique private Set<NamespacedIdentifier> clientChannels;
@Unique
private Set<NamespacedIdentifier> clientChannels;

@Inject(
method = "<init>",
at = @At(
value = "TAIL"
)
)
private void osl$networking$initConnectionContext(CallbackInfo ci) {
connectionContext = new ServerConnectionContext(server, (ServerPlayNetworkHandler) (Object) this);
}

@Inject(
method = "onDisconnect",
at = @At(
value = "HEAD"
)
)
private void osl$networking$handleDisconnect(CallbackInfo ci) {
ServerConnectionEvents.DISCONNECT.invoker().accept(server, player);
clientChannels = null;
private void osl$networking$handleDisconnect(Text reason, CallbackInfo ci) {
connectionContext.offerDisconnectReason(TextComponents.resolve(reason));
}

@Inject(
Expand All @@ -56,6 +73,11 @@ public class ServerPlayNetworkHandlerMixin implements NetworkHandlerAccess {
}
}

@Override
public ServerConnectionContext osl$networking$connectionContext() {
return connectionContext;
}

@Override
public boolean osl$networking$isPlayReady() {
return clientChannels != null;
Expand Down
Loading
Loading