Show entity id in tab list command, accept servers uuid

This commit is contained in:
Bixilon 2021-01-01 17:46:14 +01:00
parent 91b74e6be6
commit 91bc7fea80
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
6 changed files with 77 additions and 29 deletions

View File

@ -36,32 +36,45 @@ public class Player {
private final ScoreboardManager scoreboardManager = new ScoreboardManager();
private final World world = new World();
private final HashMap<Integer, Inventory> inventories = new HashMap<>();
float health;
int food;
float saturation;
BlockPosition spawnLocation;
GameModes gameMode;
byte selectedSlot;
int level;
int totalExperience;
PlayerEntity entity;
boolean spawnConfirmed;
private float health;
private int food;
private float saturation;
private BlockPosition spawnLocation;
private GameModes gameMode;
private byte selectedSlot;
private int level;
private int totalExperience;
private PlayerEntity entity;
private boolean spawnConfirmed;
private UUID uuid;
private String playerName;
ChatComponent tabHeader;
ChatComponent tabFooter;
private ChatComponent tabHeader = ChatComponent.valueOf("");
private ChatComponent tabFooter = ChatComponent.valueOf("");
public Player(Account account) {
this.account = account;
this.uuid = account.getUUID();
this.playerName = account.getUsername();
// create our own inventory without any properties
this.inventories.put(PLAYER_INVENTORY_ID, new Inventory(null));
}
public String getPlayerName() {
return this.account.getUsername();
return this.playerName;
}
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
public UUID getPlayerUUID() {
return this.account.getUUID();
return this.uuid;
}
public void setPlayerUUID(UUID uuid) {
this.uuid = uuid;
}
public Account getAccount() {

View File

@ -13,6 +13,7 @@
package de.bixilon.minosoft.data.world;
import com.google.common.collect.HashBiMap;
import de.bixilon.minosoft.data.entities.block.BlockEntityMetaData;
import de.bixilon.minosoft.data.entities.entities.Entity;
import de.bixilon.minosoft.data.mappings.Dimension;
@ -20,13 +21,15 @@ import de.bixilon.minosoft.data.mappings.blocks.Block;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.UUID;
/**
* Collection of chunks
*/
public class World {
private final HashMap<ChunkLocation, Chunk> chunks = new HashMap<>();
private final HashMap<Integer, Entity> entities = new HashMap<>();
private final HashBiMap<Integer, Entity> entityIdMap = HashBiMap.create();
private final HashBiMap<UUID, Entity> entityUUIDMap = HashBiMap.create();
boolean hardcore;
boolean raining;
Dimension dimension; // used for sky color, etc
@ -84,19 +87,29 @@ public class World {
}
public void addEntity(Entity entity) {
this.entities.put(entity.getEntityId(), entity);
this.entityIdMap.put(entity.getEntityId(), entity);
this.entityUUIDMap.put(entity.getUUID(), entity);
}
public Entity getEntity(int id) {
return this.entities.get(id);
return this.entityIdMap.get(id);
}
public Entity getEntity(UUID uuid) {
return this.entityUUIDMap.get(uuid);
}
public void removeEntity(Entity entity) {
removeEntity(entity.getEntityId());
this.entityIdMap.inverse().remove(entity);
this.entityUUIDMap.inverse().remove(entity);
}
public void removeEntity(int entityId) {
this.entities.remove(entityId);
removeEntity(this.entityIdMap.get(entityId));
}
public void removeEntity(UUID entityUUID) {
removeEntity(this.entityUUIDMap.get(entityUUID));
}
public Dimension getDimension() {
@ -127,7 +140,11 @@ public class World {
blockEntities.forEach(this::setBlockEntityData);
}
public HashMap<Integer, Entity> getEntities() {
return this.entities;
public HashBiMap<Integer, Entity> getEntityIdMap() {
return this.entityIdMap;
}
public HashBiMap<UUID, Entity> getEntityUUIDMap() {
return this.entityUUIDMap;
}
}

View File

@ -14,6 +14,7 @@
package de.bixilon.minosoft.protocol.packets.clientbound.login;
import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.protocol.network.Connection;
import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import de.bixilon.minosoft.util.Util;
@ -24,7 +25,7 @@ import static de.bixilon.minosoft.protocol.protocol.ProtocolVersions.V_20W12A;
public class PacketLoginSuccess extends ClientboundPacket {
UUID uuid;
String username;
String playerName;
@Override
public boolean read(InByteBuffer buffer) {
@ -33,20 +34,26 @@ public class PacketLoginSuccess extends ClientboundPacket {
} else {
this.uuid = buffer.readUUID();
}
this.username = buffer.readString();
this.playerName = buffer.readString();
return true;
}
@Override
public void handle(Connection connection) {
connection.getPlayer().setPlayerUUID(getUUID());
connection.getPlayer().setPlayerName(getPlayerName());
}
@Override
public void log() {
Log.protocol(String.format("[IN] Receiving login success packet (username=%s, uuid=%s)", this.username, this.uuid));
Log.protocol(String.format("[IN] Receiving login success packet (username=%s, uuid=%s)", this.playerName, this.uuid));
}
public UUID getUUID() {
return this.uuid;
}
public String getUsername() {
return this.username;
public String getPlayerName() {
return this.playerName;
}
}

View File

@ -22,6 +22,10 @@ public abstract class Command {
private static final String ERROR_MESSAGE_SUFFIX = PostChatFormattingCodes.RESET.getANSI();
public static void print(String string, Object... format) {
if (format.length == 0) {
System.out.println(string);
return;
}
System.out.printf(string + "%n", format);
}

View File

@ -32,7 +32,7 @@ public class CommandEntities extends Command {
new CommandLiteralNode("list", (connection, stack) -> {
ArrayList<Object[]> tableData = new ArrayList<>();
for (var entry : connection.getPlayer().getWorld().getEntities().entrySet()) {
for (var entry : connection.getPlayer().getWorld().getEntityIdMap().entrySet()) {
tableData.add(new Object[]{entry.getKey(), entry.getValue().getUUID(), entry.getValue().getEntityInformation(), entry.getValue().getEquipment(), entry.getValue().getLocation(), entry.getValue().getRotation()});
}

View File

@ -16,6 +16,7 @@ package de.bixilon.minosoft.terminal.commands.commands;
import com.github.freva.asciitable.AsciiTable;
import de.bixilon.minosoft.data.commands.CommandLiteralNode;
import de.bixilon.minosoft.data.commands.CommandNode;
import de.bixilon.minosoft.data.entities.entities.player.PlayerEntity;
import java.util.ArrayList;
@ -26,13 +27,19 @@ public class CommandTabList extends Command {
parent.addChildren(
new CommandLiteralNode("tab",
new CommandLiteralNode("list", (connection, stack) -> {
print(connection.getPlayer().getTabHeader().getANSIColoredMessage());
ArrayList<Object[]> tableData = new ArrayList<>();
for (var entry : connection.getPlayer().playerList.entrySet()) {
tableData.add(new Object[]{entry.getKey(), entry.getValue().getName(), entry.getValue().getDisplayName().getLegacyText(), entry.getValue().getGameMode(), entry.getValue().getPing() + "ms"});
PlayerEntity playerEntity = (PlayerEntity) connection.getPlayer().getWorld().getEntity(entry.getValue().getUUID());
Integer entityId = playerEntity != null ? playerEntity.getEntityId() : null;
tableData.add(new Object[]{entry.getKey(), entityId, entry.getValue().getName(), entry.getValue().getDisplayName(), entry.getValue().getGameMode(), entry.getValue().getPing() + "ms"});
}
print(AsciiTable.getTable(new String[]{"UUID", "Playername", "Displayname", "Gamemode", "Ping"}, tableData.toArray(new Object[0][0])));
print(AsciiTable.getTable(new String[]{"UUID", "ENTITY ID", "PLAYER NAME", "DISPLAY NAME", "GAMEMODE", "PING"}, tableData.toArray(new Object[0][0])));
print(connection.getPlayer().getTabFooter().getANSIColoredMessage());
})));
return parent;
}