in memory fake db players
This commit is contained in:
14
src/main/java/mc/core/Look.java
Normal file
14
src/main/java/mc/core/Look.java
Normal file
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-22
|
||||
*/
|
||||
package mc.core;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class Look {
|
||||
private float yaw, pitch;
|
||||
}
|
||||
@@ -8,8 +8,20 @@ import mc.core.network.NetChannel;
|
||||
|
||||
public interface Player {
|
||||
int getId();
|
||||
void setId(int value);
|
||||
|
||||
String getName();
|
||||
void setName(String value);
|
||||
|
||||
boolean isOnline();
|
||||
void setOnline(boolean status);
|
||||
|
||||
NetChannel getChannel();
|
||||
void setChannel(NetChannel channel);
|
||||
|
||||
Location getLocation();
|
||||
void setLocation(Location location);
|
||||
|
||||
Look getLook();
|
||||
void setLook(Look look);
|
||||
}
|
||||
|
||||
@@ -6,10 +6,12 @@ package mc.core;
|
||||
|
||||
import mc.core.network.NetChannel;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface PlayerManager {
|
||||
void addPlayer(Player player);
|
||||
Player getPlayer(String name);
|
||||
Player getPlayerById(int id);
|
||||
Optional<Player> getPlayer(String name);
|
||||
Optional<Player> getPlayerById(int id);
|
||||
void removePlayer(Player player);
|
||||
NetChannel getBroadcastChannel();
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Slf4j
|
||||
public class InMemoryPlayerManager implements PlayerManager, Runnable {
|
||||
@@ -23,6 +24,7 @@ public class InMemoryPlayerManager implements PlayerManager, Runnable {
|
||||
private final Object lock = new Object();
|
||||
@Setter
|
||||
private int keepAliveInterval = 1;
|
||||
private BroadcastNetChannel broadcastNetChannel = new BroadcastNetChannel();
|
||||
|
||||
@Autowired
|
||||
public InMemoryPlayerManager(Config config) {
|
||||
@@ -41,30 +43,34 @@ public class InMemoryPlayerManager implements PlayerManager, Runnable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Player getPlayer(final String name) {
|
||||
public Optional<Player> getPlayer(final String name) {
|
||||
return players.stream()
|
||||
.filter(player -> player.getName().equalsIgnoreCase(name))
|
||||
.findFirst()
|
||||
.get();
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Player getPlayerById(final int id) {
|
||||
public Optional<Player> getPlayerById(final int id) {
|
||||
return players.stream()
|
||||
.filter(player -> player.getId() == id)
|
||||
.findFirst()
|
||||
.get();
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removePlayer(Player player) {
|
||||
players.remove(player);
|
||||
player.setOnline(false);
|
||||
player.setChannel(null);
|
||||
log.info("Player \"{}\" left server", player.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public NetChannel getBroadcastChannel() {
|
||||
return new BroadcastNetChannel(players.stream());
|
||||
broadcastNetChannel.setPlayerStream(
|
||||
players.stream()
|
||||
.filter(Player::isOnline)
|
||||
.filter(player -> player.getChannel() != null)
|
||||
);
|
||||
return broadcastNetChannel;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -80,9 +86,7 @@ public class InMemoryPlayerManager implements PlayerManager, Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
players.stream()
|
||||
.parallel()
|
||||
.forEach(player -> player.getChannel().sendKeepAlive());
|
||||
getBroadcastChannel().sendKeepAlive();
|
||||
|
||||
try {
|
||||
Thread.sleep(keepAliveInterval);
|
||||
|
||||
@@ -4,14 +4,14 @@
|
||||
*/
|
||||
package mc.core.network;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import mc.core.Player;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class BroadcastNetChannel implements NetChannel {
|
||||
private final Stream<Player> playerStream;
|
||||
@Setter
|
||||
private Stream<Player> playerStream;
|
||||
|
||||
@Override
|
||||
public void sendKeepAlive() {
|
||||
|
||||
@@ -7,6 +7,7 @@ package mc.core.network.proto_125.netty;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import mc.core.Location;
|
||||
import mc.core.Look;
|
||||
import mc.core.Player;
|
||||
import mc.core.network.NetChannel;
|
||||
|
||||
@@ -15,6 +16,8 @@ import mc.core.network.NetChannel;
|
||||
public class NettyPlayer implements Player {
|
||||
private int id;
|
||||
private String name;
|
||||
private boolean online;
|
||||
private NetChannel channel;
|
||||
private Location location;
|
||||
private Look look;
|
||||
}
|
||||
|
||||
@@ -10,11 +10,8 @@ import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import io.netty.util.AttributeKey;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.Location;
|
||||
import mc.core.Player;
|
||||
import mc.core.PlayerManager;
|
||||
import mc.core.*;
|
||||
import mc.core.network.CSPacket;
|
||||
import mc.core.Config;
|
||||
import mc.core.network.proto_125.netty.wrappers.WrapperNetChannel;
|
||||
import mc.core.network.proto_125.packets.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -68,16 +65,26 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
|
||||
}
|
||||
|
||||
public void onLoginPacket(Channel channel, LoginPacket packet) {
|
||||
int pId = random.nextInt(9999);
|
||||
Location spawnLoc = new Location(0, 65, 0);
|
||||
final Location spawnLoc = new Location(0, 65, 0);
|
||||
Player player;
|
||||
|
||||
NettyPlayer player = new NettyPlayer();
|
||||
player.setId(pId);
|
||||
player.setName(packet.getPlayerName());
|
||||
player.setChannel(new WrapperNetChannel(channel));
|
||||
Optional<Player> optPlayer = playerManager.getPlayer(packet.getPlayerName());
|
||||
if (optPlayer.isPresent()) {
|
||||
player = optPlayer.get();
|
||||
} else {
|
||||
int pId = random.nextInt(9999);
|
||||
|
||||
player = new NettyPlayer();
|
||||
player.setId(pId);
|
||||
player.setName(packet.getPlayerName());
|
||||
player.setLocation(spawnLoc);
|
||||
player.setLook(new Look(0f, 0f));
|
||||
player.setOnline(true);
|
||||
playerManager.addPlayer(player);
|
||||
}
|
||||
|
||||
// Response login
|
||||
packet.setPlayerId(pId);
|
||||
packet.setPlayerId(player.getId());
|
||||
packet.setLevelType("flat");
|
||||
packet.setServerMode(1/*creative*/);
|
||||
packet.setDimension(0/*Overworld*/);
|
||||
@@ -149,15 +156,16 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
|
||||
|
||||
// send Position and look
|
||||
PositionAndLookPacket posLookPkt = new PositionAndLookPacket();
|
||||
posLookPkt.setLocation(spawnLoc);
|
||||
posLookPkt.setStance(spawnLoc.getY() + 1.64d);
|
||||
posLookPkt.setYaw(0f);
|
||||
posLookPkt.setPitch(0f);
|
||||
posLookPkt.setLocation(player.getLocation());
|
||||
posLookPkt.setStance(player.getLocation().getY() + 1.64d);
|
||||
posLookPkt.setYaw(player.getLook().getYaw());
|
||||
posLookPkt.setPitch(player.getLook().getPitch());
|
||||
posLookPkt.setOnGround(false);
|
||||
channel.write(posLookPkt);
|
||||
|
||||
playerManager.addPlayer(player);
|
||||
channel.attr(ATTR_PLAYER).set(player);
|
||||
player.setChannel(new WrapperNetChannel(channel));
|
||||
player.setOnline(true);
|
||||
channel.flush();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user