Skip to content

Commit

Permalink
API Fixes and changes around networking and world accessing
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilligans committed Jul 26, 2024
1 parent 0863d8b commit e673339
Show file tree
Hide file tree
Showing 22 changed files with 317 additions and 61 deletions.
13 changes: 8 additions & 5 deletions src/main/java/dev/hilligans/ourcraft/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
import dev.hilligans.ourcraft.GameInstance;
import dev.hilligans.ourcraft.item.ItemStack;
import dev.hilligans.ourcraft.mod.handler.events.client.OpenScreenEvent;
import dev.hilligans.ourcraft.network.ClientNetwork;
import dev.hilligans.ourcraft.network.IClientPacketHandler;
import dev.hilligans.ourcraft.network.Network;
import dev.hilligans.ourcraft.network.PacketBase;
import dev.hilligans.ourcraft.network.*;
import dev.hilligans.ourcraft.network.packet.client.CCloseScreen;
import dev.hilligans.ourcraft.network.packet.client.CDropItem;
import dev.hilligans.ourcraft.network.packet.client.COpenScreen;
Expand All @@ -41,6 +38,8 @@
import dev.hilligans.ourcraft.server.MultiPlayerServer;
import dev.hilligans.ourcraft.util.Settings;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelId;
import org.lwjgl.BufferUtils;
import org.lwjgl.openal.AL11;

Expand Down Expand Up @@ -444,8 +443,12 @@ public GameInstance getGameInstance() {
return gameInstance;
}

@Override
public Network getNetwork() {
return network;
}

@Override
public Protocol getSendProtocol(ChannelId channelId) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
import dev.hilligans.ourcraft.entity.IPlayerEntity;
import dev.hilligans.ourcraft.entity.living.entities.PlayerEntity;
import dev.hilligans.ourcraft.item.ItemStack;
import dev.hilligans.ourcraft.network.IServerPacketHandler;
import dev.hilligans.ourcraft.network.Network;
import dev.hilligans.ourcraft.network.ServerNetworkHandler;
import dev.hilligans.ourcraft.network.*;
import dev.hilligans.ourcraft.save.WorldLoader;
import dev.hilligans.ourcraft.server.IServer;
import dev.hilligans.ourcraft.tag.CompoundNBTTag;
import dev.hilligans.ourcraft.util.EntityPosition;
import dev.hilligans.ourcraft.util.Settings;
import dev.hilligans.ourcraft.world.newworldsystem.IServerWorld;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelId;

import java.util.HashMap;

public class ServerPlayerData implements IServerPacketHandler {
public class ServerPlayerData implements IServerPacketHandler, NetworkProfile {

IServer server;

Expand All @@ -49,8 +49,15 @@ public class ServerPlayerData implements IServerPacketHandler {
public UUID playerID;
public ChannelId channelId;

GameInstance gameInstance;

public Protocol sendProtocol;
public Protocol receriveProtocol;
public Channel channel;

public ServerPlayerData(GameInstance gameInstance, PlayerEntity playerEntity, String id) {
this.playerEntity = playerEntity;
this.gameInstance = gameInstance;
this.id = id;
playerInventory = playerEntity.inventory;
openContainer = new InventoryContainer(playerInventory).setPlayerId(playerEntity.id);
Expand Down Expand Up @@ -241,16 +248,55 @@ public PlayerEntity getPlayerEntity() {

@Override
public ServerNetworkHandler getServerNetworkHandler() {
return null;
return serverNetworkHandler;
}

@Override
public void disconnect(String reason) {

}

@Override
public Network getNetwork() {
return serverNetworkHandler.network;
}

@Override
public void setSendProtocol(Protocol protocol) {
this.sendProtocol = protocol;
}

@Override
public void setReceiveProtocol(Protocol protocol) {
this.receriveProtocol = protocol;
}

@Override
public Protocol getSendProtocol() {
return sendProtocol;
}

@Override
public Protocol getReceiveProtocol() {
return receriveProtocol;
}

@Override
public void setChannel(Channel channel) {
this.channel = channel;
}

@Override
public Channel getChannel() {
return channel;
}

@Override
public Protocol getSendProtocol(ChannelHandlerContext ctx) {
return sendProtocol;
}

@Override
public Protocol getSendProtocol(ChannelId channelId) {
return sendProtocol;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package dev.hilligans.ourcraft.entity;

import dev.hilligans.ourcraft.data.other.server.ServerPlayerData;
import dev.hilligans.ourcraft.server.IServer;

public interface IPlayerEntity extends IEntity {

String getName();

ServerPlayerData getPlayerData();
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package dev.hilligans.ourcraft.entity;

import dev.hilligans.ourcraft.data.other.server.ServerPlayerData;

public class NewPlayerEntity extends NewLivingEntity implements IPlayerEntity {

public String name;
public ServerPlayerData playerData;

public NewPlayerEntity(EntityType entityType) {
super(entityType);
Expand All @@ -12,4 +15,9 @@ public NewPlayerEntity(EntityType entityType) {
public String getName() {
return name;
}

@Override
public ServerPlayerData getPlayerData() {
return playerData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public ClientNetwork(GameInstance gameInstance, Protocol protocol) {
}

public ClientNetwork(GameInstance gameInstance, Protocol sendProtocol, Protocol receiveProtocol, int packetIdWidth) {
super(gameInstance, sendProtocol, receiveProtocol, packetIdWidth, false);
super(gameInstance, sendProtocol, receiveProtocol, packetIdWidth);
}

public void joinServer(String ip, String port, Client client) throws Exception {
Expand Down Expand Up @@ -56,7 +56,7 @@ public ClientNetwork debug(boolean debug) {
public void sendPacket(PacketBase<?> packetBase) {
// System.out.println("Sending packet:" + packetBase.getClass());
if(networkHandler != null && ((ClientNetworkHandler)networkHandler).enabled) {
packetBase.packetId = sendProtocol.packetMap.get(packetBase.getClass());
// packetBase.packetId = sendProtocol.packetMap.get(packetBase.getClass());
sendPacketDirect(packetBase);
} else {
packets.add(packetBase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

import java.util.concurrent.ConcurrentLinkedQueue;

public class ClientNetworkHandler extends NetworkHandler {
public class ClientNetworkHandler extends NetworkHandler implements NetworkProfile{

public ClientNetwork network;
public ConcurrentLinkedQueue<PacketBase<?>> packets = new ConcurrentLinkedQueue<>();
public Protocol sendProtocol;
public Protocol receiveProtocol;

public ClientNetworkHandler(ClientNetwork network) {
this.network = network;
Expand All @@ -34,7 +36,7 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {

@Override
protected void channelRead0(ChannelHandlerContext ctx, IPacketByteArray msg) throws Exception {
PacketBase<?> packetBase = msg.createPacket(network.receiveProtocol);
PacketBase<?> packetBase = msg.createPacket(receiveProtocol);
packets.add(packetBase);
}

Expand All @@ -50,4 +52,34 @@ public void processPackets() {
packetBase.handle(network.client);
}
}

@Override
public void setSendProtocol(Protocol protocol) {
this.sendProtocol = protocol;
}

@Override
public void setReceiveProtocol(Protocol protocol) {
this.receiveProtocol = protocol;
}

@Override
public Protocol getSendProtocol() {
return this.sendProtocol;
}

@Override
public Protocol getReceiveProtocol() {
return this.receiveProtocol;
}

@Override
public void setChannel(Channel channel) {

}

@Override
public Channel getChannel() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
import dev.hilligans.ourcraft.network.debug.PacketTraceByteArray;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelId;

public interface IPacketHandler {

default ChannelFuture sendPacket(PacketBase<?> packetBase, ChannelHandlerContext ctx) {
packetBase.packetId = getNetwork().sendProtocol.packetMap.get(packetBase.getClass());
packetBase.packetId = getSendProtocol(ctx).packetMap.get(packetBase.getClass());
return ctx.channel().writeAndFlush(new PacketByteArray(packetBase));
}

Network getNetwork();
default Protocol getSendProtocol(ChannelHandlerContext ctx) {
return getSendProtocol(ctx.channel().id());
}

Protocol getSendProtocol(ChannelId channelId);
}
20 changes: 3 additions & 17 deletions src/main/java/dev/hilligans/ourcraft/network/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,21 @@ public class Network extends ChannelInitializer<SocketChannel> {
public SslContext sslCtx;
public ChannelPipeline channelPipeline;

public Protocol sendProtocol;
public Protocol receiveProtocol;
public int packetIdWidth;
public boolean compressed;
public boolean debug = false;
public GameInstance gameInstance;

public Network(GameInstance gameInstance, Protocol protocol) {
this(gameInstance, protocol,protocol,2);
System.out.println("yes1");
}

public Network(GameInstance gameInstance, Protocol sendProtocol, Protocol receiveProtocol, int packetIdWidth) {
this(gameInstance, sendProtocol,receiveProtocol,packetIdWidth,false);
}

public Network(GameInstance gameInstance, Protocol sendProtocol, Protocol receiveProtocol, int packetIdWidth, boolean compressed) {
System.out.println("Instance" + gameInstance);
this.gameInstance = gameInstance;
this.sendProtocol = sendProtocol;
this.receiveProtocol = receiveProtocol;
this.packetIdWidth = packetIdWidth;
this.compressed = compressed;
}

public void setSendProtocol(String name) {
sendProtocol = gameInstance.PROTOCOLS.getExcept(name);
}

public void setReceiveProtocol(String name) {
receiveProtocol = gameInstance.PROTOCOLS.getExcept(name);
this.compressed = false;
}

@Override
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/dev/hilligans/ourcraft/network/NetworkProfile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.hilligans.ourcraft.network;

import io.netty.channel.Channel;

public interface NetworkProfile {

void setSendProtocol(Protocol protocol);

void setReceiveProtocol(Protocol protocol);

Protocol getSendProtocol();

Protocol getReceiveProtocol();

void setChannel(Channel channel);



Channel getChannel();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

public class ServerNetwork extends Network {

public GameInstance gameInstance;
public IServer server;

public EventLoopGroup bossGroup;
Expand All @@ -29,7 +28,7 @@ public ServerNetwork(GameInstance gameInstance, Protocol protocol, IServer serve
}

public void startServer(String port) throws Exception {
networkHandler = new ServerNetworkHandler(this, server);
networkHandler = new ServerNetworkHandler(this, server, gameInstance.getExcept("ourcraft:Auth", Protocol.class), gameInstance.get("ourcraft:Auth", Protocol.class));
ServerNetworkHandler.debug = Ourcraft.getArgumentContainer().getBoolean("--tracePacket", false);

final int PORT = Integer.parseInt(System.getProperty("port", port));
Expand Down
Loading

0 comments on commit e673339

Please sign in to comment.