master branch cleaning
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -5,6 +5,3 @@ out/
|
||||
*.ipr
|
||||
*.iws
|
||||
*.ids
|
||||
|
||||
## MAVEN ##
|
||||
target/
|
||||
|
||||
139
pom.xml
139
pom.xml
@@ -1,139 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<name>MC Core</name>
|
||||
|
||||
<groupId>mc</groupId>
|
||||
<artifactId>core</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
<slf4j.version>1.7.21</slf4j.version>
|
||||
<log4j.version>2.5</log4j.version>
|
||||
<spring.version>4.2.5.RELEASE</spring.version>
|
||||
<netty.version>4.1.22.Final</netty.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- LOGGER -->
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>${slf4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>jcl-over-slf4j</artifactId>
|
||||
<version>${slf4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-slf4j-impl</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- SPRING -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>commons-logging</groupId>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<!-- Netty -->
|
||||
<dependency>
|
||||
<groupId>io.netty</groupId>
|
||||
<artifactId>netty-all</artifactId>
|
||||
<version>${netty.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- COMPONENTS -->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.16.16</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>24.1-jre</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.8.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>${project.artifactId}-${project.version}</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.5.1</version>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
<encoding>${project.build.sourceEncoding}</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.15</version>
|
||||
<configuration>
|
||||
<argLine>-Dfile.encoding=${project.build.sourceEncoding}</argLine>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<!-- Copy dependency -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>2.4</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
<classpathPrefix>dependency/</classpathPrefix>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<version>2.8</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-dependencies</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-08
|
||||
*/
|
||||
package mc.core;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
public class ByteArrayOutputNetStream extends NetStream {
|
||||
private ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
|
||||
@Override
|
||||
public byte readByte() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readBytes(byte[] buffer) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int readUnsignedShort() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeByte(int value) {
|
||||
baos.write(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeBytes(byte[] buffer) {
|
||||
baos.write(buffer, 0, buffer.length);
|
||||
}
|
||||
|
||||
public byte[] toByteArray() {
|
||||
return baos.toByteArray();
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-08
|
||||
*/
|
||||
package mc.core;
|
||||
|
||||
public interface CSPacket {
|
||||
default void readSelf(NetStream netStream) {
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-08
|
||||
*/
|
||||
package mc.core;
|
||||
|
||||
public interface Config {
|
||||
int getMaxPlayers();
|
||||
String getDescriptionServer();
|
||||
byte[] getFaviconBase64();
|
||||
String getHost();
|
||||
int getPort();
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-03-25
|
||||
*/
|
||||
package mc.core;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
@Slf4j
|
||||
public class Main {
|
||||
public static ApplicationContext appContext; //FIXME
|
||||
|
||||
public static void main(String[] args) {
|
||||
appContext = new ClassPathXmlApplicationContext("spring.xml");
|
||||
Config config = appContext.getBean("config", Config.class);
|
||||
|
||||
Server server = appContext.getBean("server", Server.class);
|
||||
try {
|
||||
server.start(config.getHost(), config.getPort());
|
||||
} catch (StartServerException e) {
|
||||
log.error("Can't start server", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-03-28
|
||||
*/
|
||||
package mc.core;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@Slf4j
|
||||
public abstract class NetStream {
|
||||
@Getter
|
||||
@Setter
|
||||
private int expectedSize;
|
||||
|
||||
public static int sizeVarInt(final int value) {
|
||||
byte size = 0;
|
||||
int v = value;
|
||||
|
||||
do {
|
||||
v >>>= 7;
|
||||
size++;
|
||||
} while (v != 0);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
public int readVarInt() {
|
||||
int result = 0;
|
||||
byte read;
|
||||
byte numRead = 0;
|
||||
|
||||
do {
|
||||
read = readByte();
|
||||
int value = (read & 0b01111111);
|
||||
result |= (value << (7 * numRead));
|
||||
|
||||
numRead++;
|
||||
if (numRead > 5) {
|
||||
log.debug("VarInt is too big!");
|
||||
break;
|
||||
}
|
||||
} while ((read & 0b10000000) != 0);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void writeVarInt(int value) {
|
||||
do {
|
||||
byte temp = (byte)(value & 0b01111111);
|
||||
value >>>= 7;
|
||||
if (value != 0) {
|
||||
temp |= 0b10000000;
|
||||
}
|
||||
|
||||
writeByte(temp);
|
||||
} while (value != 0);
|
||||
}
|
||||
|
||||
public String readString() {
|
||||
int length = readVarInt();
|
||||
|
||||
byte[] buffer = new byte[length];
|
||||
readBytes(buffer);
|
||||
|
||||
return new String(buffer, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
public void writeString(String value) {
|
||||
writeVarInt(value.length());
|
||||
writeBytes(value.getBytes());
|
||||
}
|
||||
|
||||
public abstract byte readByte();
|
||||
public abstract void readBytes(byte[] buffer);
|
||||
public abstract int readUnsignedShort();
|
||||
|
||||
public abstract void writeByte(int value);
|
||||
public abstract void writeBytes(byte[] buffer);
|
||||
|
||||
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-08
|
||||
*/
|
||||
package mc.core;
|
||||
|
||||
public interface SCPacket {
|
||||
byte[] toByteArray();
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-03-25
|
||||
*/
|
||||
package mc.core;
|
||||
|
||||
public interface Server {
|
||||
void start(String host, int port) throws StartServerException;
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-03-25
|
||||
*/
|
||||
package mc.core;
|
||||
|
||||
public class StartServerException extends Exception {
|
||||
public StartServerException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-08
|
||||
*/
|
||||
package mc.core.embedded;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.Config;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Base64;
|
||||
|
||||
@Slf4j
|
||||
@Getter
|
||||
public class ConfigFromSpring implements Config {
|
||||
@Setter
|
||||
private String descriptionServer;
|
||||
private byte[] faviconBase64;
|
||||
@Setter
|
||||
private int maxPlayers;
|
||||
@Setter
|
||||
private String host;
|
||||
@Setter
|
||||
private int port;
|
||||
|
||||
public void setFaviconBase64(File faviconImageFile) {
|
||||
log.debug("faviconImageFile: {}", faviconImageFile.getAbsolutePath());
|
||||
try {
|
||||
faviconBase64 = Base64.getEncoder().encode(
|
||||
FileUtils.readFileToByteArray(faviconImageFile)
|
||||
);
|
||||
} catch (IOException e) {
|
||||
log.warn("Can't load favicon", e);
|
||||
faviconBase64 = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-03-25
|
||||
*/
|
||||
package mc.core.netty;
|
||||
|
||||
import io.netty.bootstrap.ServerBootstrap;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
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.logging.LoggingHandler;
|
||||
import mc.core.Server;
|
||||
import mc.core.StartServerException;
|
||||
|
||||
public class NettyServer implements Server {
|
||||
private EventLoopGroup bossGroup, workerGroup;
|
||||
|
||||
private ChannelInitializer buildChannelInitializer() {
|
||||
return new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
protected void initChannel(SocketChannel socketChannel) {
|
||||
socketChannel.pipeline().addLast(
|
||||
new LoggingHandler(),
|
||||
new PacketEncoder(),
|
||||
new PacketDecoder(),
|
||||
new PacketHandler()
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private ServerBootstrap buildServerBootstrap() {
|
||||
ServerBootstrap bootstrap = new ServerBootstrap();
|
||||
|
||||
bootstrap.group(bossGroup, workerGroup)
|
||||
.channel(NioServerSocketChannel.class)
|
||||
.childHandler(buildChannelInitializer());
|
||||
|
||||
return bootstrap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(String host, int port) throws StartServerException {
|
||||
bossGroup = new NioEventLoopGroup(1);
|
||||
workerGroup = new NioEventLoopGroup();
|
||||
|
||||
ServerBootstrap serverBootstrap = buildServerBootstrap();
|
||||
|
||||
try {
|
||||
serverBootstrap.bind(host, port).sync().channel().closeFuture().sync();
|
||||
} catch (InterruptedException e) {
|
||||
throw new StartServerException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-03-25
|
||||
*/
|
||||
package mc.core.netty;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.ByteToMessageDecoder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.CSPacket;
|
||||
import mc.core.NetStream;
|
||||
import mc.core.netty.packets.RawPacket;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
public class PacketDecoder extends ByteToMessageDecoder {
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
ctx.channel().attr(State.ATTR_STATE).set(State.STATUS);
|
||||
ctx.fireChannelActive();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
ctx.channel().attr(State.ATTR_STATE).set(null);
|
||||
ctx.fireChannelInactive();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
|
||||
State state = ctx.channel().attr(State.ATTR_STATE).get();
|
||||
NetStream netStream = new WrapperNetStream(in);
|
||||
|
||||
log.debug("ByteBuf readableBytes: {}", in.readableBytes());
|
||||
|
||||
int size = netStream.readVarInt();
|
||||
log.debug("Pkt-Size: {}", size);
|
||||
int id = netStream.readVarInt();
|
||||
log.debug("Pkt-Id: {}", id);
|
||||
|
||||
Class<? extends CSPacket> clientSidePacketClass = state.getClientSidePacket(id);
|
||||
if (clientSidePacketClass == null) {
|
||||
log.warn("Unknown packet: {}:{}", state.name(), id);
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
byte[] rawData;
|
||||
if (size > in.readableBytes()) {
|
||||
rawData = new byte[in.readableBytes()];
|
||||
} else {
|
||||
rawData = new byte[size - NetStream.sizeVarInt(id)];
|
||||
}
|
||||
in.readBytes(rawData);
|
||||
|
||||
RawPacket packet = new RawPacket();
|
||||
packet.setRawData(rawData);
|
||||
out.add(packet);
|
||||
}
|
||||
} else {
|
||||
CSPacket packet = clientSidePacketClass.newInstance();
|
||||
netStream.setExpectedSize(size - NetStream.sizeVarInt(id));
|
||||
packet.readSelf(netStream);
|
||||
out.add(packet);
|
||||
}
|
||||
|
||||
log.debug("ByteBuf readableBytes: {}", in.readableBytes());
|
||||
in.skipBytes(in.readableBytes());
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-03-25
|
||||
*/
|
||||
package mc.core.netty;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToByteEncoder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.NetStream;
|
||||
import mc.core.SCPacket;
|
||||
|
||||
@Slf4j
|
||||
public class PacketEncoder extends MessageToByteEncoder<SCPacket> {
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext ctx, SCPacket pkt, ByteBuf out) throws Exception {
|
||||
State state = ctx.channel().attr(State.ATTR_STATE).get();
|
||||
Integer id = state.getServerSidePaclet(pkt.getClass());
|
||||
if (id == null) {
|
||||
log.error("Packet not found: {}:{}", state.name(), pkt.getClass().getSimpleName());
|
||||
return;
|
||||
}
|
||||
|
||||
byte[] bytes = pkt.toByteArray();
|
||||
NetStream netStream = new WrapperNetStream(out);
|
||||
|
||||
netStream.writeVarInt(bytes.length + NetStream.sizeVarInt(id));
|
||||
netStream.writeVarInt(id);
|
||||
netStream.writeBytes(bytes);
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-03-25
|
||||
*/
|
||||
package mc.core.netty;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.CSPacket;
|
||||
import mc.core.Config;
|
||||
import mc.core.Main;
|
||||
import mc.core.netty.packets.PingPacket;
|
||||
import mc.core.netty.packets.StatusRequest;
|
||||
import mc.core.netty.packets.StatusResponse;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
|
||||
@Slf4j
|
||||
public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext ctx, CSPacket packet) throws Exception {
|
||||
log.debug("{}: {}", packet.getClass().getSimpleName(), packet.toString());
|
||||
|
||||
Optional<Method> optionalMethod = Arrays.stream(this.getClass().getDeclaredMethods())
|
||||
.filter(method -> method.getName().equals("on" + packet.getClass().getSimpleName())
|
||||
&& method.getParameterCount() == 2
|
||||
&& method.getParameterTypes()[0].isAssignableFrom(Channel.class)
|
||||
&& method.getParameterTypes()[1].isAssignableFrom(packet.getClass()))
|
||||
.findFirst();
|
||||
|
||||
if (optionalMethod.isPresent()) {
|
||||
Method method = optionalMethod.get();
|
||||
method.invoke(this, ctx.channel(), packet);
|
||||
}
|
||||
}
|
||||
|
||||
public void onStatusRequest(Channel channel, StatusRequest packet) {
|
||||
if (!packet.getNextState().equals(State.UNKNOWN)) {
|
||||
channel.attr(State.ATTR_STATE).set(packet.getNextState());
|
||||
}
|
||||
|
||||
if (packet.getNextState().equals(State.STATUS)) {
|
||||
Config config = Main.appContext.getBean("config", Config.class); //FIXME
|
||||
StatusResponse pkt = new StatusResponse();
|
||||
pkt.setDescription(config.getDescriptionServer());
|
||||
pkt.setMaxOnline(config.getMaxPlayers());
|
||||
pkt.setFaviconBase64(config.getFaviconBase64());
|
||||
|
||||
channel.writeAndFlush(pkt);
|
||||
}
|
||||
}
|
||||
|
||||
public void onPingPacket(Channel channel, PingPacket packet) {
|
||||
channel.writeAndFlush(packet);
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-03-25
|
||||
*/
|
||||
package mc.core.netty;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import io.netty.util.AttributeKey;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import mc.core.CSPacket;
|
||||
import mc.core.SCPacket;
|
||||
import mc.core.netty.packets.PingPacket;
|
||||
import mc.core.netty.packets.StatusRequest;
|
||||
import mc.core.netty.packets.StatusResponse;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public enum State {
|
||||
UNKNOWN(0, ImmutableMap.of(), ImmutableMap.of()),
|
||||
STATUS(1,
|
||||
ImmutableMap.of(
|
||||
0, StatusRequest.class,
|
||||
1, PingPacket.class
|
||||
),
|
||||
ImmutableMap.of(
|
||||
StatusResponse.class, 0,
|
||||
PingPacket.class, 1
|
||||
)
|
||||
);
|
||||
|
||||
public static final AttributeKey<State> ATTR_STATE = AttributeKey.newInstance("ATTR_STATE");
|
||||
|
||||
public static State getStateById(final int id) {
|
||||
Optional<State> optionalState = Arrays.stream(State.values())
|
||||
.filter(state -> state.id == id)
|
||||
.findFirst();
|
||||
|
||||
return optionalState.orElse(UNKNOWN);
|
||||
}
|
||||
|
||||
private final int id;
|
||||
private final Map<Integer, Class<? extends CSPacket>> clientSidePackets;
|
||||
private final Map<Class<? extends SCPacket>, Integer> serverSidePackets;
|
||||
|
||||
public Class<? extends CSPacket> getClientSidePacket(final int packetId) {
|
||||
return clientSidePackets.get(packetId);
|
||||
}
|
||||
|
||||
public Integer getServerSidePaclet(final Class<? extends SCPacket> clazz) {
|
||||
return serverSidePackets.get(clazz);
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-08
|
||||
*/
|
||||
package mc.core.netty;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import mc.core.NetStream;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class WrapperNetStream extends NetStream {
|
||||
private final ByteBuf byteBuf;
|
||||
|
||||
@Override
|
||||
public byte readByte() {
|
||||
return byteBuf.readByte();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readBytes(byte[] buffer) {
|
||||
byteBuf.readBytes(buffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int readUnsignedShort() {
|
||||
return byteBuf.readUnsignedShort();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeByte(int value) {
|
||||
byteBuf.writeByte(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeBytes(byte[] buffer) {
|
||||
byteBuf.writeBytes(buffer);
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-03-26
|
||||
*/
|
||||
package mc.core.netty.packets;
|
||||
|
||||
import mc.core.CSPacket;
|
||||
import mc.core.NetStream;
|
||||
import mc.core.SCPacket;
|
||||
|
||||
public class PingPacket implements CSPacket, SCPacket {
|
||||
private byte[] rawData;
|
||||
|
||||
@Override
|
||||
public void readSelf(NetStream netStream) {
|
||||
rawData = new byte[netStream.getExpectedSize()];
|
||||
netStream.readBytes(rawData);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toByteArray() {
|
||||
return rawData;
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-08
|
||||
*/
|
||||
package mc.core.netty.packets;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import mc.core.CSPacket;
|
||||
|
||||
@ToString
|
||||
public class RawPacket implements CSPacket {
|
||||
@Getter
|
||||
@Setter
|
||||
private byte[] rawData;
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-08
|
||||
*/
|
||||
package mc.core.netty.packets;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.CSPacket;
|
||||
import mc.core.NetStream;
|
||||
import mc.core.netty.State;
|
||||
|
||||
@Slf4j
|
||||
@Getter
|
||||
@ToString
|
||||
public class StatusRequest implements CSPacket {
|
||||
private int protocolVersion;
|
||||
private String serverAddress;
|
||||
private int serverPort;
|
||||
private State nextState;
|
||||
|
||||
@Override
|
||||
public void readSelf(NetStream netStream) {
|
||||
protocolVersion = netStream.readVarInt();
|
||||
serverAddress = netStream.readString();
|
||||
serverPort = netStream.readUnsignedShort();
|
||||
|
||||
int nextStateId = netStream.readVarInt();
|
||||
nextState = State.getStateById(nextStateId);
|
||||
if (nextState.equals(State.UNKNOWN)){
|
||||
log.warn("Unknown state ({})!", nextStateId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-08
|
||||
*/
|
||||
package mc.core.netty.packets;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import mc.core.ByteArrayOutputNetStream;
|
||||
import mc.core.SCPacket;
|
||||
|
||||
@ToString
|
||||
public class StatusResponse implements SCPacket {
|
||||
private static final String name = "1.12.2";
|
||||
private static final int protocol = 340;
|
||||
|
||||
@Setter
|
||||
private int maxOnline;
|
||||
@Setter
|
||||
private int online;
|
||||
@Setter
|
||||
private String description;
|
||||
@Setter
|
||||
private byte[] faviconBase64;
|
||||
|
||||
@Override
|
||||
public byte[] toByteArray() {
|
||||
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
|
||||
|
||||
JsonObject versionObj = new JsonObject();
|
||||
versionObj.addProperty("name", name);
|
||||
versionObj.addProperty("protocol", protocol);
|
||||
|
||||
JsonObject playersObj = new JsonObject();
|
||||
playersObj.addProperty("max", maxOnline);
|
||||
playersObj.addProperty("online", online);
|
||||
|
||||
JsonObject descriptionObj = new JsonObject();
|
||||
descriptionObj.addProperty("text", description);
|
||||
|
||||
JsonObject rootObj = new JsonObject();
|
||||
rootObj.add("version", versionObj);
|
||||
rootObj.add("players", playersObj);
|
||||
rootObj.add("description", descriptionObj);
|
||||
|
||||
if (faviconBase64 != null && faviconBase64.length > 0) {
|
||||
rootObj.addProperty("favicon",
|
||||
"data:image/png;base64," + new String(faviconBase64)
|
||||
);
|
||||
}
|
||||
|
||||
netStream.writeString(rootObj.toString());
|
||||
return netStream.toByteArray();
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 1.6 KiB |
@@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration>
|
||||
<Appenders>
|
||||
<Console name="Console" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%-5level] \{%20.20c\:%-4L} %msg%n"/>
|
||||
</Console>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="trace">
|
||||
<AppenderRef ref="Console"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
@@ -1,14 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
|
||||
<bean id="config" class="mc.core.embedded.ConfigFromSpring">
|
||||
<property name="descriptionServer" value="MC Core"/>
|
||||
<property name="maxPlayers" value="100"/>
|
||||
<property name="faviconBase64" value="icon.png"/>
|
||||
<property name="host" value="127.0.0.1"/>
|
||||
<property name="port" value="25565"/>
|
||||
</bean>
|
||||
|
||||
<bean id="server" class="mc.core.netty.NettyServer"/>
|
||||
</beans>
|
||||
Reference in New Issue
Block a user