Skip to content

Commit

Permalink
split beta networking impl in two
Browse files Browse the repository at this point in the history
to fix a mixin issue and a no such method error
  • Loading branch information
SpaceWalkerRS committed Apr 14, 2024
1 parent 5de9a5a commit 20bbe37
Show file tree
Hide file tree
Showing 61 changed files with 1,782 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
setUpModule(project,
'entrypoints-client-mca1.0.6-mc12w30e',
'lifecycle-events-client-mcb1.3-1750-mcb1.7.3'
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
environment = client
min_mc_version = b1.0
max_mc_version = b1.4_01
mc_version_range = >=1.0.0-beta.0 <=1.0.0-beta.4.1

feather_build = 12
nests_build = 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package net.ornithemc.osl.networking.impl;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import net.minecraft.client.network.handler.ClientNetworkHandler;
import net.minecraft.network.PacketHandler;
import net.minecraft.network.packet.Packet;

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

public class CustomPayloadPacket extends Packet {

public String channel;
public int size;
public byte[] data;

public CustomPayloadPacket() {
}

public CustomPayloadPacket(String channel, byte[] data) {
this.channel = channel;
this.data = data;
if (data != null) {
this.size = data.length;
if (this.size > Short.MAX_VALUE) {
throw new IllegalArgumentException("Payload may not be larger than 32k");
}
}
}

// the IOException has been stripped from the read/write methods
// by the obfuscator, thus we catch it and re-throw it as a
// runtime exception - it will be caught in Connection#read anyhow

@Override
public void read(DataInputStream input) {
try {
this.channel = input.readUTF();
this.size = input.readShort();
if (this.size > 0 && this.size < Short.MAX_VALUE) {
this.data = new byte[this.size];
input.readFully(this.data);
}
} catch (IOException e) {
throw new IllegalStateException(e);
}
}

@Override
public void write(DataOutputStream output) {
try {
output.writeUTF(this.channel);
output.writeShort(this.size);
if (this.data != null) {
output.write(this.data);
}
} catch (IOException e) {
throw new IllegalStateException(e);
}
}

@Override
public void handle(PacketHandler handler) {
ClientPlayNetworkingImpl.handle((ClientNetworkHandler)handler, this);
}

@Override
public int getSize() {
return 2 + this.channel.length() * 2 + 2 + this.size;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package net.ornithemc.osl.networking.impl;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.Set;

import net.ornithemc.osl.networking.api.CustomPayload;
import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl;

public class HandshakePayload implements CustomPayload {

public static final String CHANNEL = "OSL|Handshake";

public Set<String> channels;

public HandshakePayload() {
}

public HandshakePayload(Set<String> channels) {
this.channels = channels;
}

public static HandshakePayload client() {
return new HandshakePayload(ClientPlayNetworkingImpl.LISTENERS.keySet());
}

public static HandshakePayload server() {
throw new UnsupportedOperationException();
}

@Override
public void read(DataInputStream input) throws IOException {
channels = new LinkedHashSet<>();
int channelCount = input.readInt();

if (channelCount > 0) {
for (int i = 0; i < channelCount; i++) {
channels.add(input.readUTF());
}
}
}

@Override
public void write(DataOutputStream output) throws IOException {
output.writeInt(channels.size());

for (String channel : channels) {
output.writeUTF(channel);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package net.ornithemc.osl.networking.impl;

import net.ornithemc.osl.entrypoints.api.ModInitializer;
import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer;
import net.ornithemc.osl.lifecycle.api.MinecraftEvents;
import net.ornithemc.osl.networking.api.client.ClientConnectionEvents;
import net.ornithemc.osl.networking.api.client.ClientPlayNetworking;
import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl;
import net.ornithemc.osl.networking.impl.interfaces.mixin.IClientNetworkHandler;
import net.ornithemc.osl.networking.impl.mixin.common.PacketAccessor;

public class Networking implements ModInitializer, ClientModInitializer {

@Override
public void init() {
PacketAccessor.register(Constants.CUSTOM_PAYLOAD_PACKET_ID, CustomPayloadPacket.class);
}

@Override
public void initClient() {
MinecraftEvents.START.register(minecraft -> {
ClientPlayNetworkingImpl.setUp(minecraft);
});
MinecraftEvents.STOP.register(minecraft -> {
ClientPlayNetworkingImpl.destroy(minecraft);
});
ClientPlayNetworking.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (minecraft, handler, payload) -> {
// send channel registration data as a response to receiving server channel registration data
ClientPlayNetworking.doSend(HandshakePayload.CHANNEL, HandshakePayload.client());

((IClientNetworkHandler)handler).osl$networking$registerServerChannels(payload.channels);
ClientConnectionEvents.PLAY_READY.invoker().accept(minecraft);

return true;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.ornithemc.osl.networking.impl.mixin.common;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

import net.minecraft.network.packet.Packet;

@Mixin(Packet.class)
public interface PacketAccessor {

@Invoker("register")
public static void register(int id, Class<? extends Packet> type) {
throw new AssertionError();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
environment = client
min_mc_version = b1.0
min_mc_version = b1.5
max_mc_version = 11w48a
mc_version_range = >=1.0.0-beta.0 <=1.1-alpha.11.48.a
mc_version_range = >=1.0.0-beta.5 <=1.1-alpha.11.48.a

feather_build = 12
nests_build = 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package net.ornithemc.osl.networking.api.client;

import java.util.function.Consumer;

import net.minecraft.client.Minecraft;

import net.ornithemc.osl.core.api.events.Event;

/**
* Events related to the client side of a client-server connection.
*/
public class ClientConnectionEvents {

/**
* This event is fired after a successful login occurs.
*
* <p>
* Note that channel registration happens after login,
* and until then data cannot safely be sent to the server.
*
* <p>
* Callbacks to this event should be registered in your mod's entrypoint,
* and can be done as follows:
*
* <pre>
* {@code
* ClientConnectionEvents.LOGIN.register(minecraft -> {
* ...
* });
* }
* </pre>
*/
public static final Event<Consumer<Minecraft>> LOGIN = Event.consumer();
/**
* This event is fired after login, once channel registration is complete.
*
* <p>
* This marks the moment data can safely be sent to the server.
*
* <p>
* Callbacks to this event should be registered in your mod's entrypoint,
* and can be done as follows:
*
* <pre>
* {@code
* ClientConnectionEvents.PLAY_READY.register(minecraft -> {
* ...
* });
* }
* </pre>
*/
public static final Event<Consumer<Minecraft>> PLAY_READY = Event.consumer();
/**
* This event is fired when the client disconnects from the server.
*
* <p>
* Callbacks to this event should be registered in your mod's entrypoint,
* and can be done as follows:
*
* <pre>
* {@code
* ClientConnectionEvents.DISCONNECT.register(minecraft -> {
* ...
* });
* }
* </pre>
*/
public static final Event<Consumer<Minecraft>> DISCONNECT = Event.consumer();

}
Loading

0 comments on commit 20bbe37

Please sign in to comment.