mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-20 14:21:29 +01:00
Allow FakePlayer to be registered internally
This commit is contained in:
parent
88db08001a
commit
2fdebae7e5
@ -1,6 +1,8 @@
|
||||
package net.minestom.server.entity.fakeplayer;
|
||||
|
||||
import net.minestom.server.MinecraftServer;
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.network.ConnectionManager;
|
||||
import net.minestom.server.network.player.FakePlayerConnection;
|
||||
|
||||
import java.util.UUID;
|
||||
@ -8,9 +10,27 @@ import java.util.UUID;
|
||||
public class FakePlayer extends Player {
|
||||
|
||||
private FakePlayerController fakePlayerController;
|
||||
private boolean registered;
|
||||
|
||||
/**
|
||||
* @param uuid the player uuid
|
||||
* @param username the player username
|
||||
* @param addInCache should the player be registered internally
|
||||
* (gettable with {@link ConnectionManager#getOnlinePlayers()}
|
||||
* and {@link ConnectionManager#getPlayer(String)})
|
||||
*/
|
||||
public FakePlayer(UUID uuid, String username, boolean addInCache) {
|
||||
super(uuid, username, new FakePlayerConnection());
|
||||
|
||||
this.registered = addInCache;
|
||||
|
||||
if (registered) {
|
||||
MinecraftServer.getConnectionManager().createPlayer(this);
|
||||
}
|
||||
}
|
||||
|
||||
public FakePlayer(UUID uuid, String username) {
|
||||
super(uuid, username, new FakePlayerConnection());
|
||||
this(uuid, username, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -23,4 +43,11 @@ public class FakePlayer extends Player {
|
||||
public FakePlayerController getController() {
|
||||
return fakePlayerController;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if the player is registered in {@link ConnectionManager}, false otherwise
|
||||
*/
|
||||
public boolean isRegistered() {
|
||||
return registered;
|
||||
}
|
||||
}
|
||||
|
@ -66,15 +66,42 @@ public class ConnectionManager {
|
||||
this.playerInitializations.add(playerInitialization);
|
||||
}
|
||||
|
||||
// Is only used at LoginStartPacket#process
|
||||
public void createPlayer(UUID uuid, String username, PlayerConnection connection) {
|
||||
Player player = new Player(uuid, username, connection);
|
||||
/**
|
||||
* Add a new player in the players list
|
||||
* Is currently used at
|
||||
* {@link net.minestom.server.network.packet.client.login.LoginStartPacket#process(PlayerConnection, ConnectionManager)}
|
||||
* and in {@link net.minestom.server.entity.fakeplayer.FakePlayer#FakePlayer(UUID, String, boolean)}
|
||||
*
|
||||
* @param player the player to add
|
||||
*/
|
||||
public void createPlayer(Player player) {
|
||||
this.players.add(player);
|
||||
this.connectionPlayerMap.put(connection, player);
|
||||
this.connectionPlayerMap.put(player.getPlayerConnection(), player);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a player object and register it
|
||||
*
|
||||
* @param uuid the new player uuid
|
||||
* @param username the new player username
|
||||
* @param connection the new player connection
|
||||
*/
|
||||
public void createPlayer(UUID uuid, String username, PlayerConnection connection) {
|
||||
Player player = new Player(uuid, username, connection);
|
||||
createPlayer(player);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a player from the players list
|
||||
* used at player disconnection
|
||||
*
|
||||
* @param connection the player connection
|
||||
*/
|
||||
public void removePlayer(PlayerConnection connection) {
|
||||
Player player = this.connectionPlayerMap.get(connection);
|
||||
if (player == null)
|
||||
return;
|
||||
|
||||
this.players.remove(player);
|
||||
this.connectionPlayerMap.remove(player);
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package net.minestom.server.network.player;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.minestom.server.MinecraftServer;
|
||||
import net.minestom.server.entity.fakeplayer.FakePlayer;
|
||||
import net.minestom.server.network.packet.server.ServerPacket;
|
||||
|
||||
@ -38,7 +39,8 @@ public class FakePlayerConnection extends PlayerConnection {
|
||||
|
||||
@Override
|
||||
public void disconnect() {
|
||||
|
||||
if (fakePlayer.isRegistered())
|
||||
MinecraftServer.getConnectionManager().removePlayer(this);
|
||||
}
|
||||
|
||||
public FakePlayer getFakePlayer() {
|
||||
|
Loading…
Reference in New Issue
Block a user