Clean up a bit and add netty to core

This commit is contained in:
s809
2023-06-26 21:16:11 +05:00
parent d57192f76a
commit 014b49246d
16 changed files with 5 additions and 387 deletions
+3
View File
@@ -182,6 +182,9 @@ subprojects { p ->
implementation("org.apache.logging.log4j:log4j-api:${rootProject.log4j_version}")
implementation("org.apache.logging.log4j:log4j-core:${rootProject.log4j_version}")
// Netty
implementation("io.netty:netty-all:${rootProject.netty_version}")
// JOML
implementation("org.joml:joml:${rootProject.joml_version}")
@@ -1,24 +0,0 @@
package com.seibel.distanthorizons.common.networking;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import net.minecraft.client.Minecraft;
public class LodClient {
public static final LodClient INSTANCE = new LodClient();
private final Minecraft client;
private final NetworkHandler networkHandler;
private LodClient() {
client = Minecraft.getInstance();
this.networkHandler = new NetworkHandler();
}
public void connect() {
}
public void disconnect() {
}
}
@@ -1,59 +0,0 @@
package com.seibel.distanthorizons.common.networking;
import com.seibel.distanthorizons.common.networking.messages.MessageHandler;
import com.seibel.distanthorizons.common.networking.messages.MessageHandlerSide;
import com.seibel.distanthorizons.common.networking.messages.MessageRegistry;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
public class LodServer {
// TODO move to config of some sort
static final int PORT = 25049;
public void start() throws InterruptedException {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(getInitializer());
b.bind(PORT).sync().channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
public void stop() {
}
private ChannelInitializer<SocketChannel> getInitializer() {
return new ChannelInitializer<>() {
@Override
public void initChannel(SocketChannel ch) {
ChannelPipeline pipeline = ch.pipeline();
var messageRegistry = new MessageRegistry();
// Encoding
pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 2, 0, 2));
pipeline.addLast(new MessageDecoder(messageRegistry));
// TODO packet encoder
pipeline.addLast(new MessageHandler(MessageHandlerSide.SERVER));
}
};
}
}
@@ -1,21 +0,0 @@
package com.seibel.distanthorizons.common.networking;
import com.seibel.distanthorizons.common.networking.messages.MessageRegistry;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import java.util.List;
public class MessageDecoder extends ByteToMessageDecoder {
private MessageRegistry messageRegistry;
public MessageDecoder(MessageRegistry messageRegistry) {
this.messageRegistry = messageRegistry;
}
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
out.add(messageRegistry.createMessage(in.readShort()).decode(in));
}
}
@@ -1,39 +0,0 @@
/*
* This file is part of the Distant Horizons mod (formerly the LOD Mod),
* licensed under the GNU LGPL v3 License.
*
* Copyright (C) 2020-2022 James Seibel
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.seibel.distanthorizons.common.networking;
import net.minecraft.client.Minecraft;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.player.Player;
/**
* This is packet handler for our mod
* It basically handles the packets sent from the server & client
*
* @author Ran
*/
// TODO: Server sided stuff here
public class NetworkHandler {
public static void receivePacket(FriendlyByteBuf buf) {
// This just exists here for testing purposes, it'll be removed in the future
System.out.println("Received int " + buf.readInt());
}
}
@@ -1,61 +0,0 @@
/*
* This file is part of the Distant Horizons mod (formerly the LOD Mod),
* licensed under the GNU LGPL v3 License.
*
* Copyright (C) 2020-2022 James Seibel
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.seibel.distanthorizons.common.networking;
/*
#if MC_1_16_5
import me.shedaniel.architectury.networking.NetworkManager;
#else
import dev.architectury.networking.NetworkManager;
#endif
import net.minecraft.client.Minecraft;
import net.minecraft.network.FriendlyByteBuf;
*/
/**
* @author Ran
*/
// Comment: What does the 'server' side mean? Dedicated server? Or does it include the internal server?
// (I removed the hookup that calls the register method, since I'm not sure what it is doing yet)
public class NetworkReceiver {
/*
public void register_Client() {
NetworkManager.registerReceiver(NetworkManager.serverToClient(), Networking.RESOURCE_LOCATION, new ClientReceiver());
}
public void register_Server() {
NetworkManager.registerReceiver(NetworkManager.clientToServer(), Networking.RESOURCE_LOCATION, new ServerReceiver());
}
static class ServerReceiver implements NetworkManager.NetworkReceiver {
@Override
public void receive(FriendlyByteBuf buf, NetworkManager.PacketContext context) {
com.seibel.distanthorizons.common.networking.NetworkHandler.receivePacketServer(buf, context.getPlayer());
}
}
static class ClientReceiver implements NetworkManager.NetworkReceiver {
@Override
public void receive(FriendlyByteBuf buf, NetworkManager.PacketContext context) {
com.seibel.distanthorizons.common.networking.NetworkHandler.receivePacketClient(Minecraft.getInstance(), buf, context.getPlayer());
}
}
*/
}
@@ -1,67 +0,0 @@
/*
* This file is part of the Distant Horizons mod (formerly the LOD Mod),
* licensed under the GNU LGPL v3 License.
*
* Copyright (C) 2020-2022 James Seibel
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.seibel.distanthorizons.common.networking;
import com.seibel.distanthorizons.coreapi.ModInfo;
//#if MC_1_16_5
//import me.shedaniel.architectury.networking.NetworkManager;
//#else
//import dev.architectury.networking.NetworkManager;
//#endif
import io.netty.buffer.Unpooled;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
/**
* This class holds most of the networking code for the mod.
* @author Ran
*/
public class Networking {
public static final ResourceLocation RESOURCE_LOCATION = new ResourceLocation("lod", "data");
public static FriendlyByteBuf createNew() {
// TODO: Probably replace the Unpooled.buffer()
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
buf.writeInt(ModInfo.PROTOCOL_VERSION);
return buf;
}
/**
* Sends a packet to a player.
*
* @param player the player to send the packet to
* @param buf the payload of the packet.
*/
public static void send(ServerPlayer player, FriendlyByteBuf buf) {
// NetworkManager.sendToPlayer(player, RESOURCE_LOCATION, buf);
}
/**
* Sends a packet to the connected server.
*
* @param buf the payload of the packet
* @throws IllegalStateException if the client is not connected to a server
*/
public static void send(FriendlyByteBuf buf) throws IllegalStateException {
// NetworkManager.sendToServer(RESOURCE_LOCATION, buf);
}
}
@@ -1,5 +0,0 @@
package com.seibel.distanthorizons.common.networking.messages;
public class HelloMessage extends Message {
public int version = 1;
}
@@ -1,23 +0,0 @@
package com.seibel.distanthorizons.common.networking.messages;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
public abstract class Message {
public Message() { }
public void encode(ByteBuf out) {
throw new UnsupportedOperationException();
}
public Message decode(ByteBuf in) {
throw new UnsupportedOperationException();
}
protected void handle_Server(ChannelHandlerContext ctx) {
throw new UnsupportedOperationException();
}
protected void handle_Client(ChannelHandlerContext ctx) {
throw new UnsupportedOperationException();
}
}
@@ -1,21 +0,0 @@
package com.seibel.distanthorizons.common.networking.messages;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
public class MessageHandler extends SimpleChannelInboundHandler<Message> {
private final MessageHandlerSide side;
public MessageHandler(MessageHandlerSide side) {
this.side = side;
}
@Override
public void channelRead0(ChannelHandlerContext ctx, Message msg) {
switch (side) {
case CLIENT -> msg.handle_Client(ctx);
case SERVER -> msg.handle_Server(ctx);
default -> throw new IllegalStateException("Invalid handler side");
}
}
}
@@ -1,6 +0,0 @@
package com.seibel.distanthorizons.common.networking.messages;
public enum MessageHandlerSide {
CLIENT,
SERVER
}
@@ -1,32 +0,0 @@
package com.seibel.distanthorizons.common.networking.messages;
import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.Collectors;
public class MessageRegistry {
private final Map<Integer, Supplier<Message>> idToConstructor = Map.ofEntries(
// Do NOT remove messages from middle of the list;
// It will break backwards compatibility.
// (Stub indexes out instead)
Map.entry(1, HelloMessage::new)
);
private final Map<Class<?>, Integer> classToId = idToConstructor.entrySet().stream()
.collect(Collectors.toMap(
e -> e.getValue().getClass(),
Map.Entry::getKey
));
public Message createMessage(int id) throws IllegalArgumentException {
try {
return idToConstructor.get(id).get();
} catch (NullPointerException e) {
throw new IllegalArgumentException("Invalid message ID");
}
}
public int getMessageId(Message message) {
return classToId.get(message.getClass());
}
}
@@ -1,6 +1,5 @@
package com.seibel.distanthorizons.fabric;
import com.seibel.distanthorizons.common.networking.Networking;
import com.seibel.distanthorizons.common.wrappers.chunk.ChunkWrapper;
import com.seibel.distanthorizons.common.wrappers.world.ClientLevelWrapper;
import com.seibel.distanthorizons.common.wrappers.world.ServerLevelWrapper;
@@ -70,9 +69,6 @@ public class FabricServerProxy
/* Register the mod needed event callbacks */
// TEST EVENT
//ServerTickEvents.END_SERVER_TICK.register(this::tester);
// ServerTickEvent
ServerTickEvents.END_SERVER_TICK.register((server) -> SERVER_API.serverTickEvent());
@@ -124,17 +120,4 @@ public class FabricServerProxy
});
// ServerChunkSaveEvent - Done in MixinChunkMap
}
// This just exists here for testing purposes, it'll be removed in the future
public void tester(MinecraftServer server)
{ // I disabled the Networking functions for now so this will not work atm - coolGi
for (ServerPlayer player : server.getPlayerList().getPlayers())
{
FriendlyByteBuf payload = Networking.createNew();
payload.writeInt(1);
System.out.println("Sending int 1");
Networking.send(player, payload);
}
}
}
@@ -1,6 +1,5 @@
package com.seibel.distanthorizons.forge;
import com.seibel.distanthorizons.common.networking.Networking;
import com.seibel.distanthorizons.common.wrappers.chunk.ChunkWrapper;
import com.seibel.distanthorizons.common.wrappers.world.ServerLevelWrapper;
import com.seibel.distanthorizons.common.wrappers.worldGeneration.BatchGenerationEnvironment;
@@ -145,14 +144,4 @@ public class ForgeServerProxy
}
}
}
// This just exists here for testing purposes, it'll be removed in the future
public void tester(MinecraftServer server) {
for (ServerPlayer player : server.getPlayerList().getPlayers()) {
FriendlyByteBuf payload = Networking.createNew();
payload.writeInt(1);
System.out.println("Sending int 1");
Networking.send(player, payload);
}
}
}
+1
View File
@@ -25,6 +25,7 @@ flatlaf_version=3.0
svgSalamander_version=1.1.3
log4j_version=2.20.0
netty_version=4.1.94.Final
lwjgl_version=3.2.3
joml_version=1.10.2