Skip to content

Commit

Permalink
Fix config exchange request is not working if using Forge server.
Browse files Browse the repository at this point in the history
  • Loading branch information
KosmX committed Jun 11, 2022
1 parent f6e2350 commit d1ee6d3
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,46 @@
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
import java.util.concurrent.atomic.AtomicInteger;

public class ClientNetworkInstance extends AbstractNetworkInstance implements C2SPlayChannelEvents.Register, ClientPlayConnectionEvents.Disconnect {

private int remoteVersion = 0;

public static ClientNetworkInstance networkInstance = new ClientNetworkInstance();
private final AtomicInteger connectState = new AtomicInteger(0);

public void init(){
ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> ClientPlayNetworking.registerReceiver(ServerNetwork.channelID, this::receiveMessage));
ClientPlayConnectionEvents.JOIN.register(this::playerJoin);
C2SPlayChannelEvents.REGISTER.register(this);

ClientPlayConnectionEvents.DISCONNECT.register(this);
}

@Override
public void onChannelRegister(ClientPacketListener handler, PacketSender sender, Minecraft client, List<ResourceLocation> channels) {
if(channels.contains(ServerNetwork.channelID)){
if(channels.contains(ServerNetwork.channelID) && connectState.incrementAndGet() == 2){
connectState.set(0);
this.sendConfigCallback();
}
}

private void playerJoin(ClientPacketListener clientPacketListener, PacketSender packetSender, Minecraft minecraft) {
ClientPlayNetworking.registerReceiver(ServerNetwork.channelID, this::receiveMessage);
if (connectState.incrementAndGet() == 2) {
connectState.set(0);
this.sendConfigCallback();
EmoteInstance.instance.getLogger().log(Level.INFO, "Sending presence to server");
}
}

@Override
public void onPlayDisconnect(ClientPacketListener handler, Minecraft client) {
connectState.set(0);
this.disconnect(); //:D
}

void receiveMessage(Minecraft client, ClientPacketListener handler, FriendlyByteBuf buf, PacketSender responseSender){
if(buf.isDirect() || buf.isReadOnly()){ //If the received ByteBuf is direct i have to copy that onto the heap
byte[] bytes = new byte[buf.readableBytes()];
Expand All @@ -54,6 +66,17 @@ void receiveMessage(Minecraft client, ClientPacketListener handler, FriendlyByte
}
}

private boolean disableNBS = false;
@Override
public HashMap<Byte, Byte> getVersions() {
if(disableNBS){
HashMap<Byte, Byte> map = new HashMap<>();
map.put((byte)3, (byte) 0);
return map;
}
return null;
}

@Override
public boolean sendPlayerID() {
return false;
Expand All @@ -69,6 +92,10 @@ public void sendMessage(EmotePacket.Builder builder, @Nullable UUID target) thro
if(target != null){
builder.configureTarget(target);
}
ClientPlayNetworking.send(ServerNetwork.channelID, new FriendlyByteBuf(Unpooled.wrappedBuffer(builder.build().write().array())));
EmotePacket writer = builder.build();
ClientPlayNetworking.send(ServerNetwork.channelID, new FriendlyByteBuf(Unpooled.wrappedBuffer(writer.write().array())));
if(writer.data.emoteData != null && writer.data.emoteData.song != null && !writer.data.writeSong){
EmoteInstance.instance.getClientMethods().sendChatMessage(EmoteInstance.instance.getDefaults().newTranslationText("emotecraft.song_too_big_to_send"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@
import net.minecraftforge.client.event.ClientPlayerNetworkEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.network.NetworkEvent;
import org.jetbrains.annotations.Nullable;

import javax.annotation.Nullable;
import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

public class ClientNetworkInstance extends AbstractNetworkInstance {

boolean isRemotePresent = false;
private final AtomicInteger connectState = new AtomicInteger(0);

public static ClientNetworkInstance networkInstance = new ClientNetworkInstance();

Expand All @@ -30,11 +33,16 @@ public void init(){
}

private void connectServerCallback(ClientPlayerNetworkEvent.LoggedInEvent event){
this.isRemotePresent = false;
if (connectState.incrementAndGet() == 2) {
connectState.set(0);
this.sendConfigCallback();
}
}

private void disconnectEvent(ClientPlayerNetworkEvent.LoggedOutEvent event){
this.disconnect();
this.isRemotePresent = false;
this.connectState.set(0);
}

private void receiveJunk(NetworkEvent.ServerCustomPayloadEvent event){
Expand All @@ -44,18 +52,31 @@ private void receiveJunk(NetworkEvent.ServerCustomPayloadEvent event){

private void registerServerSide(NetworkEvent.ChannelRegistrationChangeEvent event){
this.isRemotePresent = event.getRegistrationChangeType() == NetworkEvent.RegistrationChangeType.REGISTER;
this.sendConfigCallback();
if (isRemotePresent && connectState.incrementAndGet() == 2) {
connectState.set(0);
this.sendConfigCallback();
}
}

void receiveMessage(FriendlyByteBuf buf){
if(buf.isDirect()){ //If the received ByteBuf is direct i have to copy that onto the heap
if(buf.isDirect()){
byte[] bytes = new byte[buf.readableBytes()];
buf.getBytes(buf.readerIndex(), bytes);
receiveMessage(bytes);
}
else {
receiveMessage(buf.array()); //if heap, I can just use it's byte-array
receiveMessage(buf.array());
}
}

@Override
public HashMap<Byte, Byte> getVersions() {
if(disableNBS){
HashMap<Byte, Byte> map = new HashMap<>();
map.put((byte)3, (byte) 0);
return map;
}
return null;
}

@Override
Expand All @@ -71,10 +92,7 @@ public boolean isActive() {
}

public static ServerboundCustomPayloadPacket newC2SEmotePacket(NetData data) throws IOException {
ServerboundCustomPayloadPacket packet = new ServerboundCustomPayloadPacket();
packet.setName(ServerNetwork.channelID);
packet.setData(new FriendlyByteBuf(Unpooled.wrappedBuffer(new EmotePacket.Builder(data).build().write().array())));
return packet;
return new ServerboundCustomPayloadPacket(ServerNetwork.channelID, new FriendlyByteBuf(Unpooled.wrappedBuffer(new EmotePacket.Builder(data).build().write().array())));
}

@Override
Expand All @@ -83,6 +101,6 @@ public void sendMessage(EmotePacket.Builder builder, @Nullable UUID target) thro
builder.configureTarget(target);
}
if(Minecraft.getInstance().getConnection() != null)
Minecraft.getInstance().getConnection().send(newC2SEmotePacket(builder.copyAndGetData()));
Minecraft.getInstance().getConnection().send(newC2SEmotePacket(builder.copyAndGetData()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,40 @@
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
import java.util.concurrent.atomic.AtomicInteger;

public class ClientNetworkInstance extends AbstractNetworkInstance implements C2SPlayChannelEvents.Register, ClientPlayConnectionEvents.Disconnect {


public static ClientNetworkInstance networkInstance = new ClientNetworkInstance();
private final AtomicInteger connectState = new AtomicInteger(0);

public void init(){
ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> ClientPlayNetworking.registerReceiver(ServerNetwork.channelID, this::receiveMessage));
ClientPlayConnectionEvents.JOIN.register(this::playerJoin);
C2SPlayChannelEvents.REGISTER.register(this);

ClientPlayConnectionEvents.DISCONNECT.register(this);
}

@Override
public void onChannelRegister(ClientPacketListener handler, PacketSender sender, Minecraft client, List<ResourceLocation> channels) {
if(channels.contains(ServerNetwork.channelID)){
if(channels.contains(ServerNetwork.channelID) && connectState.incrementAndGet() == 2){
connectState.set(0);
this.sendConfigCallback();
EmoteInstance.instance.getLogger().log(Level.INFO, "Sending presence to server");
}
}

private void playerJoin(ClientPacketListener clientPacketListener, PacketSender packetSender, Minecraft minecraft) {
ClientPlayNetworking.registerReceiver(ServerNetwork.channelID, this::receiveMessage);
if (connectState.incrementAndGet() == 2) {
connectState.set(0);
this.sendConfigCallback();
}
}

@Override
public void onPlayDisconnect(ClientPacketListener handler, Minecraft client) {
connectState.set(0);
this.disconnect(); //:D
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

public class ClientNetworkInstance extends AbstractNetworkInstance {

boolean isRemotePresent = false;
private int remoteVersion = 0;
private final AtomicInteger connectState = new AtomicInteger(0);

public static ClientNetworkInstance networkInstance = new ClientNetworkInstance();

Expand All @@ -32,11 +33,16 @@ public void init(){
}

private void connectServerCallback(ClientPlayerNetworkEvent.LoggedInEvent event){
this.isRemotePresent = false;
if (connectState.incrementAndGet() == 2) {
connectState.set(0);
this.sendConfigCallback();
}
}

private void disconnectEvent(ClientPlayerNetworkEvent.LoggedOutEvent event){
this.disconnect();
this.isRemotePresent = false;
this.connectState.set(0);
}

private void receiveJunk(NetworkEvent.ServerCustomPayloadEvent event){
Expand All @@ -46,7 +52,10 @@ private void receiveJunk(NetworkEvent.ServerCustomPayloadEvent event){

private void registerServerSide(NetworkEvent.ChannelRegistrationChangeEvent event){
this.isRemotePresent = event.getRegistrationChangeType() == NetworkEvent.RegistrationChangeType.REGISTER;
this.sendConfigCallback();
if (isRemotePresent && connectState.incrementAndGet() == 2) {
connectState.set(0);
this.sendConfigCallback();
}
}

void receiveMessage(FriendlyByteBuf buf){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,40 @@
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
import java.util.concurrent.atomic.AtomicInteger;

public class ClientNetworkInstance extends AbstractNetworkInstance implements C2SPlayChannelEvents.Register, ClientPlayConnectionEvents.Disconnect {


public static ClientNetworkInstance networkInstance = new ClientNetworkInstance();
private final AtomicInteger connectState = new AtomicInteger(0);

public void init(){
ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> ClientPlayNetworking.registerReceiver(ServerNetwork.channelID, this::receiveMessage));
ClientPlayConnectionEvents.JOIN.register(this::playerJoin);
C2SPlayChannelEvents.REGISTER.register(this);

ClientPlayConnectionEvents.DISCONNECT.register(this);
}

@Override
public void onChannelRegister(ClientPacketListener handler, PacketSender sender, Minecraft client, List<ResourceLocation> channels) {
if(channels.contains(ServerNetwork.channelID)){
if(channels.contains(ServerNetwork.channelID) && connectState.incrementAndGet() == 2){
connectState.set(0);
this.sendConfigCallback();
EmoteInstance.instance.getLogger().log(Level.INFO, "Sending presence to server");
}
}

private void playerJoin(ClientPacketListener clientPacketListener, PacketSender packetSender, Minecraft minecraft) {
ClientPlayNetworking.registerReceiver(ServerNetwork.channelID, this::receiveMessage);
if (connectState.incrementAndGet() == 2) {
connectState.set(0);
this.sendConfigCallback();
}
}

@Override
public void onPlayDisconnect(ClientPacketListener handler, Minecraft client) {
connectState.set(0);
this.disconnect(); //:D
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

public class ClientNetworkInstance extends AbstractNetworkInstance {

boolean isRemotePresent = false;
private int remoteVersion = 0;
private final AtomicInteger connectState = new AtomicInteger(0);

public static ClientNetworkInstance networkInstance = new ClientNetworkInstance();

Expand All @@ -32,11 +33,16 @@ public void init(){
}

private void connectServerCallback(ClientPlayerNetworkEvent.LoggedInEvent event){
this.isRemotePresent = false;
if (connectState.incrementAndGet() == 2) {
connectState.set(0);
this.sendConfigCallback();
}
}

private void disconnectEvent(ClientPlayerNetworkEvent.LoggedOutEvent event){
this.disconnect();
this.isRemotePresent = false;
this.connectState.set(0);
}

private void receiveJunk(NetworkEvent.ServerCustomPayloadEvent event){
Expand All @@ -46,7 +52,10 @@ private void receiveJunk(NetworkEvent.ServerCustomPayloadEvent event){

private void registerServerSide(NetworkEvent.ChannelRegistrationChangeEvent event){
this.isRemotePresent = event.getRegistrationChangeType() == NetworkEvent.RegistrationChangeType.REGISTER;
this.sendConfigCallback();
if (isRemotePresent && connectState.incrementAndGet() == 2) {
connectState.set(0);
this.sendConfigCallback();
}
}

void receiveMessage(FriendlyByteBuf buf){
Expand Down Expand Up @@ -92,6 +101,6 @@ public void sendMessage(EmotePacket.Builder builder, @Nullable UUID target) thro
builder.configureTarget(target);
}
if(Minecraft.getInstance().getConnection() != null)
Minecraft.getInstance().getConnection().send(newC2SEmotePacket(builder.copyAndGetData()));
Minecraft.getInstance().getConnection().send(newC2SEmotePacket(builder.copyAndGetData()));
}
}

0 comments on commit d1ee6d3

Please sign in to comment.