Move everything to core
This commit is contained in:
@@ -64,7 +64,7 @@ public class ServerLevelModule {
|
||||
public final AppliedConfigState<Boolean> worldGeneratorEnabledConfig;
|
||||
private final AtomicReference<WorldGenState> worldGenStateRef = new AtomicReference<>();
|
||||
|
||||
public ServerLevelModule(IDhServerLevel parent, IServerLevelWrapper levelWrapper, AbstractSaveStructure saveStructure)
|
||||
public ServerLevelModule(IDhServerLevel parent, IServerLevelWrapper levelWrapper, AbstractSaveStructure saveStructure, boolean startLodServer)
|
||||
{
|
||||
this.parent = parent;
|
||||
this.levelWrapper = levelWrapper;
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.seibel.distanthorizons.core.network;
|
||||
|
||||
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() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.seibel.distanthorizons.core.network;
|
||||
|
||||
import com.seibel.distanthorizons.core.network.messageHandling.MessageDecoder;
|
||||
import com.seibel.distanthorizons.core.network.messageHandling.MessageHandler;
|
||||
import com.seibel.distanthorizons.core.network.messageHandling.MessageHandlerSide;
|
||||
import com.seibel.distanthorizons.core.network.messageHandling.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 LodServer(/* initial settings */) {
|
||||
|
||||
}
|
||||
|
||||
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<SocketChannel>() {
|
||||
@Override
|
||||
public void initChannel(SocketChannel ch) {
|
||||
ChannelPipeline pipeline = ch.pipeline();
|
||||
|
||||
MessageRegistry 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));
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.seibel.distanthorizons.core.network.messageHandling;
|
||||
|
||||
import com.seibel.distanthorizons.core.network.messages.Message;
|
||||
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) {
|
||||
Message message = messageRegistry.createMessage(in.readShort());
|
||||
message.decode(in);
|
||||
out.add(message);
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.seibel.distanthorizons.core.network.messageHandling;
|
||||
|
||||
import com.seibel.distanthorizons.core.network.messages.Message;
|
||||
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);
|
||||
break;
|
||||
case SERVER:
|
||||
msg.handle_Server(ctx);
|
||||
break;
|
||||
default:
|
||||
throw new IllegalStateException("Invalid handler side");
|
||||
}
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package com.seibel.distanthorizons.core.network.messageHandling;
|
||||
|
||||
public enum MessageHandlerSide {
|
||||
CLIENT,
|
||||
SERVER
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package com.seibel.distanthorizons.core.network.messageHandling;
|
||||
|
||||
import com.seibel.distanthorizons.core.network.messages.HelloMessage;
|
||||
import com.seibel.distanthorizons.core.network.messages.Message;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class MessageRegistry {
|
||||
private final Map<Integer, Supplier<Message>> idToConstructor = new HashMap<Integer, Supplier<Message>>() {{
|
||||
// Note: Make sure IDs are unique
|
||||
// Also note: Removing IDs will break backwards compatibility
|
||||
put(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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.seibel.distanthorizons.core.network.messages;
|
||||
|
||||
import com.seibel.distanthorizons.coreapi.ModInfo;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
|
||||
// This message is critical to maintain backwards compatibility
|
||||
// as it's used to receive version BEFORE everything else.
|
||||
public class HelloMessage extends Message {
|
||||
public int version = ModInfo.PROTOCOL_VERSION;
|
||||
|
||||
@Override
|
||||
public void encode(ByteBuf out) {
|
||||
out.writeInt(version);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void decode(ByteBuf in) {
|
||||
version = in.readInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle_Server(ChannelHandlerContext ctx) {
|
||||
// TODO Adjust message handling to client's version
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.seibel.distanthorizons.core.network.messages;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
|
||||
public abstract class Message {
|
||||
public Message() { }
|
||||
|
||||
public abstract void encode(ByteBuf out);
|
||||
public abstract void decode(ByteBuf in);
|
||||
|
||||
public void handle_Server(ChannelHandlerContext ctx) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
public void handle_Client(ChannelHandlerContext ctx) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user