Compare commits
1 Commits
master
...
dev/experi
| Author | SHA1 | Date | |
|---|---|---|---|
|
346d64aea5
|
@@ -1,13 +1,12 @@
|
||||
# MC-SERVER
|
||||
|
||||

|
||||

|
||||

|
||||

|
||||

|
||||
|
||||
Написанный с нуля сервер **Minecraft 1.12.2**.
|
||||
|
||||
На данный момент сервер может показывать о себе информацию в списке серверов (motd, онлайн, иконка) и позволять
|
||||
игрокам подключиться к себе. Загружается пустой мир.
|
||||
На данный момент может только показывать информацию о себе. Подключение к серверу не возможно.
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
project.group=mc-project
|
||||
project.name=mc-server
|
||||
project.version=1.1
|
||||
project.version=1.1-SNAPSHOT
|
||||
@@ -2,16 +2,12 @@ package mc.protocol;
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.experimental.Accessors;
|
||||
import mc.protocol.api.ConnectionContext;
|
||||
import mc.protocol.packets.ServerSidePacket;
|
||||
import mc.protocol.pool.Passivable;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
@EqualsAndHashCode
|
||||
public class NettyConnectionContext implements ConnectionContext, Passivable {
|
||||
|
||||
@@ -19,14 +15,6 @@ public class NettyConnectionContext implements ConnectionContext, Passivable {
|
||||
@Setter
|
||||
private ChannelHandlerContext ctx;
|
||||
|
||||
/**
|
||||
* @deprecated костыль
|
||||
*/
|
||||
@Deprecated
|
||||
@Getter
|
||||
@Setter
|
||||
private boolean usedContext;
|
||||
|
||||
@Override
|
||||
public State getState() {
|
||||
return ctx.channel().attr(NetworkAttributes.STATE).get();
|
||||
@@ -37,27 +25,6 @@ public class NettyConnectionContext implements ConnectionContext, Passivable {
|
||||
ctx.channel().attr(NetworkAttributes.STATE).set(state);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated костыль
|
||||
*/
|
||||
@Deprecated
|
||||
@Override
|
||||
public <T> void setCustomProperty(String key, T value) {
|
||||
Map<String, Object> map = ctx.channel().attr(NetworkAttributes.CUSTOM_PROPERTIES).get();
|
||||
map.put(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated костыль
|
||||
*/
|
||||
@Deprecated
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T> Optional<T> getCustomProperty(String key, Class<T> classResult) {
|
||||
Map<String, Object> map = ctx.channel().attr(NetworkAttributes.CUSTOM_PROPERTIES).get();
|
||||
return (Optional<T>) Optional.ofNullable(map.getOrDefault(key, null));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(ServerSidePacket packet) {
|
||||
ctx.write(packet);
|
||||
|
||||
@@ -3,16 +3,8 @@ package mc.protocol;
|
||||
import io.netty.util.AttributeKey;
|
||||
import lombok.experimental.UtilityClass;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@UtilityClass
|
||||
public class NetworkAttributes {
|
||||
|
||||
public static final AttributeKey<State> STATE = AttributeKey.newInstance("STATE");
|
||||
|
||||
/**
|
||||
* @deprecated костыль
|
||||
*/
|
||||
@Deprecated
|
||||
public static final AttributeKey<Map<String, Object>> CUSTOM_PROPERTIES = AttributeKey.newInstance("CUSTOM_PROPERTIES");
|
||||
}
|
||||
|
||||
@@ -3,20 +3,14 @@ package mc.protocol;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.protocol.packets.ClientSidePacket;
|
||||
import mc.protocol.event.EventBus;
|
||||
import mc.protocol.pool.PacketPool;
|
||||
import org.apache.commons.pool2.ObjectPool;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class PacketInboundHandler extends SimpleChannelInboundHandler<ClientSidePacket> {
|
||||
|
||||
private static final String CLIENT_FORCE_DISCONNECTED_IOEXCEPTION_MESSAGE_RU = "Программа на вашем хост-компьютере разорвала установленное подключение";
|
||||
|
||||
private final ObjectPool<NettyConnectionContext> poolNettyConnectionContext;
|
||||
private final PacketPool poolPackets;
|
||||
private final EventBus eventBus;
|
||||
@@ -28,21 +22,7 @@ public class PacketInboundHandler extends SimpleChannelInboundHandler<ClientSide
|
||||
NettyConnectionContext context = poolNettyConnectionContext.borrowObject().setCtx(ctx);
|
||||
eventBus.emit(state, context, packet);
|
||||
|
||||
if (!context.isUsedContext()) {
|
||||
poolNettyConnectionContext.returnObject(context);
|
||||
}
|
||||
poolNettyConnectionContext.returnObject(context);
|
||||
poolPackets.returnObject(packet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
||||
if (cause instanceof IOException && cause.getLocalizedMessage().equalsIgnoreCase(CLIENT_FORCE_DISCONNECTED_IOEXCEPTION_MESSAGE_RU)) {
|
||||
log.warn("Client '{}' force disconnected", ctx.channel().remoteAddress());
|
||||
if (log.isTraceEnabled()) {
|
||||
log.trace("", cause);
|
||||
}
|
||||
} else {
|
||||
log.error("{}", cause.getMessage(), cause);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import mc.protocol.packets.ClientSidePacket;
|
||||
import mc.protocol.packets.Packet;
|
||||
import mc.protocol.packets.KeepAlivePacket;
|
||||
import mc.protocol.packets.PingPacket;
|
||||
import mc.protocol.packets.ServerSidePacket;
|
||||
import mc.protocol.packets.client.*;
|
||||
import mc.protocol.packets.server.*;
|
||||
@@ -24,12 +24,12 @@ public enum State {
|
||||
// client side
|
||||
Map.of(
|
||||
0x00, StatusServerRequestPacket.class,
|
||||
0x01, KeepAlivePacket.class
|
||||
0x01, PingPacket.class
|
||||
),
|
||||
// server side
|
||||
Map.of(
|
||||
StatusServerResponse.class, 0x00,
|
||||
KeepAlivePacket.class, 0x01
|
||||
PingPacket.class, 0x01
|
||||
)
|
||||
),
|
||||
LOGIN(2,
|
||||
@@ -47,7 +47,7 @@ public enum State {
|
||||
0x00, TeleportConfirmPacket.class,
|
||||
0x04, ClientSettingsPacket.class,
|
||||
0x09, PluginMessagePacket.class,
|
||||
0x0B, KeepAlivePacket.class,
|
||||
0x0B, PingPacket.class,
|
||||
0x0D, PlayerPositionPacket.class,
|
||||
0x0E, CPlayerPositionAndLookPacket.class,
|
||||
0x0F, PlayerLookPacket.class,
|
||||
@@ -55,11 +55,12 @@ public enum State {
|
||||
),
|
||||
// client bound
|
||||
Map.of(
|
||||
KeepAlivePacket.class, 0x1F,
|
||||
PingPacket.class, 0x1F,
|
||||
ChunkDataPacket.class, 0x20,
|
||||
JoinGamePacket.class, 0x23,
|
||||
PlayerAbilitiesPacket.class,0x2C,
|
||||
SPlayerPositionAndLookPacket.class, 0x2F,
|
||||
SetExperiencePacket.class, 0x40,
|
||||
SpawnPositionPacket.class, 0x46
|
||||
)
|
||||
);
|
||||
|
||||
@@ -3,37 +3,11 @@ package mc.protocol.api;
|
||||
import mc.protocol.State;
|
||||
import mc.protocol.packets.ServerSidePacket;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface ConnectionContext {
|
||||
|
||||
/**
|
||||
* @deprecated костыль
|
||||
*/
|
||||
@Deprecated
|
||||
void setUsedContext(boolean value);
|
||||
|
||||
/**
|
||||
* @deprecated костыль
|
||||
*/
|
||||
@Deprecated
|
||||
boolean isUsedContext();
|
||||
|
||||
State getState();
|
||||
void setState(State state);
|
||||
|
||||
/**
|
||||
* @deprecated костыль
|
||||
*/
|
||||
@Deprecated
|
||||
<T> void setCustomProperty(String key, T value);
|
||||
|
||||
/**
|
||||
* @deprecated костыль
|
||||
*/
|
||||
@Deprecated
|
||||
<T> Optional<T> getCustomProperty(String key, Class<T> classResult);
|
||||
|
||||
void send(ServerSidePacket packet);
|
||||
void sendNow(ServerSidePacket packet);
|
||||
void flushSending();
|
||||
|
||||
@@ -17,7 +17,6 @@ import mc.protocol.pool.PacketPool;
|
||||
import org.apache.commons.pool2.ObjectPool;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
@@ -37,8 +36,6 @@ public class ProtocolDecoder extends ByteToMessageDecoder {
|
||||
|
||||
@Override
|
||||
public void channelActive(@Nonnull ChannelHandlerContext ctx) throws Exception {
|
||||
ctx.channel().attr(NetworkAttributes.CUSTOM_PROPERTIES).set(new HashMap<>());
|
||||
|
||||
NettyConnectionContext context = poolNettyConnectionContext.borrowObject().setCtx(ctx);
|
||||
consumerNewConnection.accept(context);
|
||||
|
||||
@@ -51,9 +48,6 @@ public class ProtocolDecoder extends ByteToMessageDecoder {
|
||||
NettyConnectionContext context = poolNettyConnectionContext.borrowObject().setCtx(ctx);
|
||||
consumerDisconnect.accept(context);
|
||||
|
||||
ctx.channel().attr(NetworkAttributes.CUSTOM_PROPERTIES).get().clear();
|
||||
ctx.channel().attr(NetworkAttributes.CUSTOM_PROPERTIES).set(null);
|
||||
|
||||
poolNettyConnectionContext.returnObject(context);
|
||||
super.channelInactive(ctx);
|
||||
}
|
||||
|
||||
@@ -9,16 +9,4 @@ public class Location {
|
||||
private double x;
|
||||
private double y;
|
||||
private double z;
|
||||
|
||||
public int getIntX() {
|
||||
return (int) x;
|
||||
}
|
||||
|
||||
public int getIntZ() {
|
||||
return (int) z;
|
||||
}
|
||||
|
||||
public Location toChunkXZ() {
|
||||
return new Location(this.getIntX() >> 4, 0d, this.getIntZ() >> 4);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ import mc.protocol.io.NetByteBuf;
|
||||
* @see <a href="https://wiki.vg/index.php?title=Protocol&oldid=7368#Keep_Alive">Keep Alive</a>
|
||||
*/
|
||||
@Data
|
||||
public class KeepAlivePacket implements ClientSidePacket, ServerSidePacket {
|
||||
public class PingPacket implements ClientSidePacket, ServerSidePacket {
|
||||
|
||||
private Long payload;
|
||||
|
||||
@@ -28,17 +28,13 @@ import mc.protocol.packets.ServerSidePacket;
|
||||
@Data
|
||||
public class ChunkDataPacket implements ServerSidePacket {
|
||||
|
||||
private static NetByteBuf voidData;
|
||||
|
||||
private int x;
|
||||
private int z;
|
||||
|
||||
@SuppressWarnings("java:S125")
|
||||
@Override
|
||||
public void writeSelf(NetByteBuf netByteBuf) {
|
||||
netByteBuf.writeInt(x);
|
||||
netByteBuf.writeInt(z);
|
||||
/* Временное отключение кода
|
||||
netByteBuf.writeBoolean(true); // Is Full chunk
|
||||
netByteBuf.writeVarInt(0b11111111); // Available Sections
|
||||
|
||||
@@ -68,41 +64,6 @@ public class ChunkDataPacket implements ServerSidePacket {
|
||||
netByteBuf.writeVarInt(data.readableBytes()); // Size of Data
|
||||
netByteBuf.writeBytes(data); // Data
|
||||
netByteBuf.writeVarInt(0); // Number of block entities
|
||||
// write NBT's
|
||||
*/
|
||||
|
||||
netByteBuf.writeBytes(voidData);
|
||||
|
||||
voidData.resetReaderIndex();
|
||||
voidData.resetWriterIndex();
|
||||
}
|
||||
|
||||
static {
|
||||
voidData = new NetByteBuf(Unpooled.buffer());
|
||||
voidData.writeBoolean(true); // Is Full chunk
|
||||
voidData.writeVarInt(0b11111111); // Available Sections
|
||||
|
||||
NetByteBuf data = new NetByteBuf(Unpooled.buffer());
|
||||
for (int i = 0; i < 16; i++) {
|
||||
NetByteBuf dataBuff = new NetByteBuf(Unpooled.wrappedBuffer(new byte[4096]));
|
||||
NetByteBuf blockLight = new NetByteBuf(Unpooled.wrappedBuffer(new byte[2048]));
|
||||
NetByteBuf skyLight = new NetByteBuf(Unpooled.wrappedBuffer(new byte[2048]));
|
||||
NetByteBuf biomes = new NetByteBuf(Unpooled.wrappedBuffer(new byte[256]));
|
||||
|
||||
data.writeUnsignedByte(13);
|
||||
data.writeUnsignedByte(0);
|
||||
data.writeVarInt(dataBuff.readableBytes());
|
||||
data.writeBytes(dataBuff);
|
||||
data.writeBytes(blockLight);
|
||||
data.writeBytes(skyLight);
|
||||
data.writeBytes(biomes);
|
||||
}
|
||||
|
||||
voidData.writeVarInt(data.readableBytes());
|
||||
voidData.writeBytes(data);
|
||||
voidData.writeVarInt(0);
|
||||
|
||||
voidData.markReaderIndex();
|
||||
voidData.markWriterIndex();
|
||||
/* write NBT's */
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package mc.protocol.packets.server;
|
||||
|
||||
import lombok.Data;
|
||||
import mc.protocol.io.NetByteBuf;
|
||||
import mc.protocol.packets.ServerSidePacket;
|
||||
|
||||
/**
|
||||
* Set Experience packet.
|
||||
*
|
||||
* <p>Структура пакета</p>
|
||||
* <pre>
|
||||
* | FIELD | TYPE | NOTES |
|
||||
* |------------------|----------|------------------------|
|
||||
* | Experience bar | Float | Значение от 0.0 до 1.0 |
|
||||
* | Level | VarInt | |
|
||||
* | Total Experience | VarInt | |
|
||||
* </pre>
|
||||
*
|
||||
* @see <a href="https://wiki.vg/index.php?title=Protocol&oldid=14204#Set_Experience">Set Experience</a>
|
||||
* @see <a href="https://minecraft.fandom.com/wiki/Experience#Leveling_up">Experience: Leveling up</a>
|
||||
*/
|
||||
@Data
|
||||
public class SetExperiencePacket implements ServerSidePacket {
|
||||
|
||||
private float experienceBar;
|
||||
private int level;
|
||||
private int totalExperience;
|
||||
|
||||
@Override
|
||||
public void writeSelf(NetByteBuf netByteBuf) {
|
||||
netByteBuf.writeFloat(this.experienceBar);
|
||||
netByteBuf.writeVarInt(this.level);
|
||||
netByteBuf.writeVarInt(this.totalExperience);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package mc.protocol.world;
|
||||
|
||||
public interface Chunk {
|
||||
|
||||
int getX();
|
||||
int getZ();
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package mc.protocol.world;
|
||||
|
||||
import mc.protocol.model.Location;
|
||||
import mc.protocol.utils.LevelType;
|
||||
|
||||
public interface World {
|
||||
|
||||
LevelType getLevelType();
|
||||
|
||||
Location getSpawn();
|
||||
|
||||
Chunk getChunk(int x, int z);
|
||||
}
|
||||
@@ -12,7 +12,7 @@ import mc.protocol.api.Server;
|
||||
import mc.protocol.di.DaggerProtocolComponent;
|
||||
import mc.protocol.di.ProtocolComponent;
|
||||
import mc.protocol.di.ProtocolModule;
|
||||
import mc.protocol.packets.KeepAlivePacket;
|
||||
import mc.protocol.packets.PingPacket;
|
||||
import mc.protocol.packets.client.HandshakePacket;
|
||||
import mc.protocol.packets.client.LoginStartPacket;
|
||||
import mc.protocol.packets.client.StatusServerRequestPacket;
|
||||
@@ -20,7 +20,6 @@ import mc.server.config.Config;
|
||||
import mc.server.di.ConfigModule;
|
||||
import mc.server.di.DaggerServerComponent;
|
||||
import mc.server.di.ServerComponent;
|
||||
import mc.server.service.PlayerManager;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -49,7 +48,6 @@ public class Main {
|
||||
.build();
|
||||
|
||||
Config config = serverComponent.getConfig();
|
||||
PlayerManager playerManager = serverComponent.getPlayerManager();
|
||||
|
||||
ProtocolComponent protocolComponent = DaggerProtocolComponent.builder()
|
||||
.protocolModule(new ProtocolModule(true))
|
||||
@@ -59,16 +57,13 @@ public class Main {
|
||||
PacketHandler packetHandler = serverComponent.getPacketHandler();
|
||||
|
||||
server.onNewConnect(connectionContext -> connectionContext.setState(State.HANDSHAKING));
|
||||
server.onDisonnect(connectionContext -> {
|
||||
connectionContext.setState(null);
|
||||
connectionContext.getCustomProperty("player", Player.class).ifPresent(playerManager::remove);
|
||||
});
|
||||
server.onDisonnect(connectionContext -> connectionContext.setState(null));
|
||||
|
||||
server.listenPacket(State.HANDSHAKING, HandshakePacket.class, packetHandler::onHandshake);
|
||||
server.listenPacket(State.STATUS, KeepAlivePacket.class, packetHandler::onKeepAlive);
|
||||
server.listenPacket(State.STATUS, PingPacket.class, packetHandler::onKeepAlive);
|
||||
server.listenPacket(State.STATUS, StatusServerRequestPacket.class, packetHandler::onServerStatus);
|
||||
server.listenPacket(State.LOGIN, LoginStartPacket.class, packetHandler::onLoginStart);
|
||||
server.listenPacket(State.PLAY, KeepAlivePacket.class, packetHandler::onKeepAlivePlay);
|
||||
server.listenPacket(State.PLAY, PingPacket.class, packetHandler::onKeepAlivePlay);
|
||||
|
||||
server.bind(config.server().host(), config.server().port());
|
||||
}
|
||||
|
||||
@@ -2,12 +2,13 @@ package mc.server;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.protocol.*;
|
||||
import mc.protocol.ProtocolConstant;
|
||||
import mc.protocol.State;
|
||||
import mc.protocol.api.ConnectionContext;
|
||||
import mc.protocol.model.Location;
|
||||
import mc.protocol.model.Look;
|
||||
import mc.protocol.model.ServerInfo;
|
||||
import mc.protocol.packets.KeepAlivePacket;
|
||||
import mc.protocol.packets.PingPacket;
|
||||
import mc.protocol.packets.client.HandshakePacket;
|
||||
import mc.protocol.packets.client.LoginStartPacket;
|
||||
import mc.protocol.packets.client.StatusServerRequestPacket;
|
||||
@@ -15,10 +16,8 @@ import mc.protocol.packets.server.*;
|
||||
import mc.protocol.serializer.TextSerializer;
|
||||
import mc.protocol.utils.Difficulty;
|
||||
import mc.protocol.utils.GameMode;
|
||||
import mc.protocol.world.Chunk;
|
||||
import mc.protocol.world.World;
|
||||
import mc.protocol.utils.LevelType;
|
||||
import mc.server.config.Config;
|
||||
import mc.server.service.PlayerManager;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -27,6 +26,7 @@ import java.nio.file.Path;
|
||||
import java.util.Base64;
|
||||
import java.util.Collections;
|
||||
import java.util.Random;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Slf4j
|
||||
@@ -35,27 +35,18 @@ public class PacketHandler {
|
||||
|
||||
private final Random random = new Random(System.currentTimeMillis());
|
||||
private final Config config;
|
||||
private final World world;
|
||||
private final PlayerManager playerManager;
|
||||
|
||||
public void onHandshake(ConnectionContext context, HandshakePacket packet) {
|
||||
context.setState(packet.getNextState());
|
||||
}
|
||||
|
||||
public void onKeepAlive(ConnectionContext context, KeepAlivePacket packet) {
|
||||
public void onKeepAlive(ConnectionContext context, PingPacket packet) {
|
||||
context.sendNow(packet);
|
||||
context.disconnect();
|
||||
}
|
||||
|
||||
public void onKeepAlivePlay(ConnectionContext context, KeepAlivePacket packet) {
|
||||
try {
|
||||
TimeUnit.MILLISECONDS.sleep(50);
|
||||
context.sendNow(packet);
|
||||
} catch (InterruptedException e) {
|
||||
if (log.isTraceEnabled()) {
|
||||
log.trace("{}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
public void onKeepAlivePlay(ConnectionContext context, PingPacket packet) {
|
||||
context.sendNow(packet);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@@ -64,11 +55,7 @@ public class PacketHandler {
|
||||
serverInfo.version().name(ProtocolConstant.PROTOCOL_NAME);
|
||||
serverInfo.version().protocol(ProtocolConstant.PROTOCOL_NUMBER);
|
||||
serverInfo.players().max(config.players().maxOnlile());
|
||||
if (config.players().fakeOnline().enable()) {
|
||||
serverInfo.players().online(config.players().fakeOnline().value());
|
||||
} else {
|
||||
serverInfo.players().online(playerManager.online());
|
||||
}
|
||||
serverInfo.players().online(config.players().onlile());
|
||||
serverInfo.players().sample(Collections.emptyList());
|
||||
serverInfo.description(TextSerializer.fromPlain(config.motd()));
|
||||
|
||||
@@ -82,29 +69,28 @@ public class PacketHandler {
|
||||
context.sendNow(response);
|
||||
}
|
||||
|
||||
@SuppressWarnings("java:S2589")
|
||||
@SuppressWarnings("java:S2189")
|
||||
public void onLoginStart(ConnectionContext context, LoginStartPacket loginStartPacket) {
|
||||
Player player = playerManager.addAndCreate(context, loginStartPacket.getName(), GameMode.SURVIVAL, world.getSpawn());
|
||||
context.setCustomProperty("player", player);
|
||||
|
||||
var loginSuccessPacket = new LoginSuccessPacket();
|
||||
loginSuccessPacket.setUuid(player.getUuid());
|
||||
loginSuccessPacket.setName(player.getName());
|
||||
loginSuccessPacket.setUuid(UUID.randomUUID());
|
||||
loginSuccessPacket.setName(loginStartPacket.getName());
|
||||
|
||||
context.sendNow(loginSuccessPacket);
|
||||
context.setState(State.PLAY);
|
||||
|
||||
var joinGamePacket = new JoinGamePacket();
|
||||
joinGamePacket.setEntityId(random.nextInt());
|
||||
joinGamePacket.setGameMode(player.getGameMode());
|
||||
joinGamePacket.setGameMode(GameMode.SURVIVAL);
|
||||
joinGamePacket.setDimension(0/*Overworld*/);
|
||||
joinGamePacket.setDifficulty(Difficulty.PEACEFUL);
|
||||
joinGamePacket.setLevelType(world.getLevelType());
|
||||
joinGamePacket.setLevelType(LevelType.FLAT);
|
||||
|
||||
context.send(joinGamePacket);
|
||||
|
||||
Location spawnLocation = new Location(7d, 130d, 7d);
|
||||
|
||||
var spawnPositionPacket = new SpawnPositionPacket();
|
||||
spawnPositionPacket.setSpawn(player.getLocation());
|
||||
spawnPositionPacket.setSpawn(spawnLocation);
|
||||
|
||||
context.send(spawnPositionPacket);
|
||||
|
||||
@@ -120,49 +106,49 @@ public class PacketHandler {
|
||||
|
||||
context.flushSending();
|
||||
|
||||
Location chunkLocation = player.getLocation().toChunkXZ();
|
||||
Chunk chunk = world.getChunk(chunkLocation.getIntX(), chunkLocation.getIntZ());
|
||||
|
||||
var chunkDataPacket = new ChunkDataPacket();
|
||||
chunkDataPacket.setX(chunk.getX());
|
||||
chunkDataPacket.setZ(chunk.getZ());
|
||||
chunkDataPacket.setX(0);
|
||||
chunkDataPacket.setZ(0);
|
||||
|
||||
context.send(chunkDataPacket);
|
||||
|
||||
for (int i = 1; i <= config.world().viewDistance(); i++) {
|
||||
int minX = chunkLocation.getIntX() - i;
|
||||
int minZ = chunkLocation.getIntZ() - i;
|
||||
int maxX = chunkLocation.getIntX() + i;
|
||||
int maxZ = chunkLocation.getIntZ() + i;
|
||||
|
||||
for (int z = minZ; z <= maxZ; z++) {
|
||||
for (int x = minX; x <= maxX; x++) {
|
||||
if ((z == minZ || z == maxZ) || (x == minX || x == maxX)) {
|
||||
chunkDataPacket = new ChunkDataPacket();
|
||||
chunkDataPacket.setX(x);
|
||||
chunkDataPacket.setZ(z);
|
||||
|
||||
context.send(chunkDataPacket);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
context.flushSending();
|
||||
context.sendNow(chunkDataPacket);
|
||||
|
||||
var playerPositionAndLookPacket = new SPlayerPositionAndLookPacket();
|
||||
playerPositionAndLookPacket.setPosition(player.getLocation());
|
||||
playerPositionAndLookPacket.setPosition(spawnLocation);
|
||||
playerPositionAndLookPacket.setLook(new Look(0f, 0f));
|
||||
playerPositionAndLookPacket.setTeleportId(random.nextInt());
|
||||
|
||||
context.send(playerPositionAndLookPacket);
|
||||
|
||||
KeepAlivePacket keepAlivePacket = new KeepAlivePacket();
|
||||
keepAlivePacket.setPayload(System.currentTimeMillis());
|
||||
PingPacket pingPacket = new PingPacket();
|
||||
pingPacket.setPayload(System.currentTimeMillis());
|
||||
|
||||
context.send(keepAlivePacket);
|
||||
context.send(pingPacket);
|
||||
|
||||
context.flushSending();
|
||||
|
||||
// -- Эксперименты -- //
|
||||
|
||||
var setExperiencePacket = new SetExperiencePacket();
|
||||
setExperiencePacket.setExperienceBar(0f);
|
||||
setExperiencePacket.setLevel(0);
|
||||
setExperiencePacket.setTotalExperience(100);
|
||||
|
||||
while (true) {
|
||||
context.sendNow(setExperiencePacket);
|
||||
|
||||
setExperiencePacket.setExperienceBar(setExperiencePacket.getExperienceBar() + 0.01f);
|
||||
setExperiencePacket.setLevel(setExperiencePacket.getLevel() + 1);
|
||||
if (setExperiencePacket.getExperienceBar() > 1.0f) {
|
||||
setExperiencePacket.setExperienceBar(0f);
|
||||
setExperiencePacket.setLevel(0);
|
||||
}
|
||||
|
||||
try {
|
||||
TimeUnit.MILLISECONDS.sleep(10);
|
||||
} catch (InterruptedException e) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String faviconToBase64(Path iconPath) {
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
package mc.server;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import mc.protocol.api.ConnectionContext;
|
||||
import mc.protocol.model.Location;
|
||||
import mc.protocol.utils.GameMode;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Getter
|
||||
public class Player {
|
||||
|
||||
private final ConnectionContext connectionContext;
|
||||
private final UUID uuid;
|
||||
private final String name;
|
||||
private final GameMode gameMode;
|
||||
private final Location location;
|
||||
}
|
||||
@@ -15,7 +15,6 @@ public class Config {
|
||||
|
||||
private final Server server = new Server();
|
||||
private final Players players = new Players();
|
||||
private final World world = new World();
|
||||
|
||||
private String motd;
|
||||
private String disconnectReason;
|
||||
@@ -33,23 +32,7 @@ public class Config {
|
||||
@Setter
|
||||
@ToString
|
||||
public static class Players {
|
||||
private final FakeOnline fakeOnline = new FakeOnline();
|
||||
|
||||
private int maxOnlile;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
public static class FakeOnline {
|
||||
private boolean enable;
|
||||
private int value;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
public static class World {
|
||||
private int viewDistance;
|
||||
private int onlile;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,15 +27,10 @@ public class ConfigModule {
|
||||
|
||||
config.server().host(fromYamlPath("server/host", map, "127.0.0.1"));
|
||||
config.server().port(fromYamlPath("server/port", map, 25565));
|
||||
|
||||
config.motd(fromYamlPath("motd", map, ""));
|
||||
config.disconnectReason(fromYamlPath("disconnect-reason", map, ""));
|
||||
|
||||
config.players().maxOnlile(fromYamlPath("players/max-online", map, 0));
|
||||
config.players().fakeOnline().enable(fromYamlPath("players/fake-online/enable", map, false));
|
||||
config.players().fakeOnline().value(fromYamlPath("players/fake-online/value", map, 0));
|
||||
|
||||
config.world().viewDistance(fromYamlPath("world/view-distance", map, 0));
|
||||
config.players().onlile(fromYamlPath("players/online", map, 0));
|
||||
|
||||
if (Boolean.TRUE.equals(fromYamlPath("icon/enable", map, false))) {
|
||||
config.iconPath(Paths.get(fromYamlPath("icon/path", map, "favicon.png")));
|
||||
|
||||
@@ -2,16 +2,14 @@ package mc.server.di;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
import mc.protocol.world.World;
|
||||
import mc.server.PacketHandler;
|
||||
import mc.server.config.Config;
|
||||
import mc.server.service.PlayerManager;
|
||||
|
||||
@Module
|
||||
public class PacketHandlerModule {
|
||||
|
||||
@Provides
|
||||
public PacketHandler providePacketHandler(Config config, World world, PlayerManager playerManager) {
|
||||
return new PacketHandler(config, world, playerManager);
|
||||
public PacketHandler providePacketHandler(Config config) {
|
||||
return new PacketHandler(config);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
package mc.server.di;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
import mc.protocol.di.ServerScope;
|
||||
import mc.server.service.PlayerManager;
|
||||
|
||||
@Module
|
||||
public class PlayersModule {
|
||||
|
||||
@Provides
|
||||
@ServerScope
|
||||
PlayerManager providePlayerManager() {
|
||||
return new PlayerManager();
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,14 @@
|
||||
package mc.server.di;
|
||||
|
||||
import dagger.Component;
|
||||
import mc.protocol.di.ServerScope;
|
||||
import mc.server.PacketHandler;
|
||||
import mc.server.config.Config;
|
||||
import mc.server.service.PlayerManager;
|
||||
|
||||
@Component(modules = {
|
||||
ConfigModule.class, PacketHandlerModule.class, WorldModule.class, PlayersModule.class
|
||||
ConfigModule.class, PacketHandlerModule.class
|
||||
})
|
||||
@ServerScope
|
||||
public interface ServerComponent {
|
||||
|
||||
Config getConfig();
|
||||
PacketHandler getPacketHandler();
|
||||
PlayerManager getPlayerManager();
|
||||
}
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
package mc.server.di;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
import mc.protocol.di.ServerScope;
|
||||
import mc.protocol.world.World;
|
||||
import mc.server.world.VoidWorld;
|
||||
|
||||
@Module
|
||||
public class WorldModule {
|
||||
|
||||
@Provides
|
||||
@ServerScope
|
||||
public World provideWorld() {
|
||||
return new VoidWorld();
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package mc.server.service;
|
||||
|
||||
import mc.protocol.api.ConnectionContext;
|
||||
import mc.protocol.model.Location;
|
||||
import mc.protocol.utils.GameMode;
|
||||
import mc.server.Player;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.UUID;
|
||||
|
||||
public class PlayerManager {
|
||||
|
||||
private final LinkedList<Player> players = new LinkedList<>();
|
||||
|
||||
public Player addAndCreate(ConnectionContext context, String name, GameMode gameMode, Location location) {
|
||||
context.setUsedContext(true);
|
||||
Player player = new Player(context, UUID.randomUUID(), name, gameMode, location);
|
||||
players.add(player);
|
||||
return player;
|
||||
}
|
||||
|
||||
public void remove(Player player) {
|
||||
players.remove(player);
|
||||
}
|
||||
|
||||
public int online() {
|
||||
return players.size();
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package mc.server.world;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import mc.protocol.world.Chunk;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Getter
|
||||
public class VoidChunk implements Chunk {
|
||||
|
||||
private final int x;
|
||||
private final int z;
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package mc.server.world;
|
||||
|
||||
import mc.protocol.model.Location;
|
||||
import mc.protocol.utils.LevelType;
|
||||
import mc.protocol.world.Chunk;
|
||||
import mc.protocol.world.World;
|
||||
|
||||
public class VoidWorld implements World {
|
||||
|
||||
private static final Location spawn = new Location(7d, 130d, 7d);
|
||||
|
||||
@Override
|
||||
public LevelType getLevelType() {
|
||||
return LevelType.FLAT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location getSpawn() {
|
||||
return VoidWorld.spawn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Chunk getChunk(int x, int z) {
|
||||
return new VoidChunk(x, z);
|
||||
}
|
||||
}
|
||||
@@ -10,14 +10,9 @@ disconnect-reason: '&4Server is not available.'
|
||||
|
||||
players:
|
||||
max-online: 0
|
||||
fake-online:
|
||||
enable: false
|
||||
value: 0
|
||||
online: 0
|
||||
|
||||
# Размер значка: 64x64 px
|
||||
icon:
|
||||
enable: false
|
||||
path: favicon.png
|
||||
|
||||
world:
|
||||
view-distance: 1
|
||||
Reference in New Issue
Block a user