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