@@ -66,17 +66,48 @@ protected void createEntity(String preferredName, Color color, EntityManager ent
6666
6767 EntityRef clientInfo = findClientEntityRef ();
6868 if (!clientInfo .exists ()) {
69- String name = findUniquePlayerName (preferredName , entityManager );
70- clientInfo = createClientInfoEntity (name , color , entityManager );
69+ clientInfo = createClientInfoEntity (entityManager );
70+ }
71+
72+ addOrSetColorComponent (clientInfo , color );
73+
74+ DisplayNameComponent displayNameComponent = clientInfo .getComponent (DisplayNameComponent .class );
75+ if (displayNameComponent == null || !displayNameComponent .name .equals (preferredName )) {
76+ String bestAvailableName = findUniquePlayerName (preferredName , entityManager , clientInfo );
77+ addOrSetDisplayNameComponent (clientInfo , bestAvailableName );
7178 }
7279
7380 ClientComponent clientComponent = clientEntity .getComponent (ClientComponent .class );
7481 clientComponent .clientInfo = clientInfo ;
7582 clientEntity .saveComponent (clientComponent );
7683 }
7784
78- protected String findUniquePlayerName (String preferredName , EntityManager entityManager ) {
79- Set <String > usedNames = findUsedNames (entityManager );
85+ private void addOrSetColorComponent (EntityRef clientInfo , Color color ) {
86+ ColorComponent colorComp = clientInfo .getComponent (ColorComponent .class );
87+ if (colorComp != null ) {
88+ colorComp .color = color ;
89+ clientInfo .saveComponent (colorComp );
90+ } else {
91+ colorComp = new ColorComponent ();
92+ colorComp .color = color ;
93+ clientInfo .addComponent (colorComp );
94+ }
95+ }
96+
97+ private void addOrSetDisplayNameComponent (EntityRef clientInfo , String name ) {
98+ DisplayNameComponent component = clientInfo .getComponent (DisplayNameComponent .class );
99+ if (component != null ) {
100+ component .name = name ;
101+ clientInfo .saveComponent (component );
102+ } else {
103+ component = new DisplayNameComponent ();
104+ component .name = name ;
105+ clientInfo .addComponent (component );
106+ }
107+ }
108+
109+ protected String findUniquePlayerName (String preferredName , EntityManager entityManager , EntityRef player ) {
110+ Set <String > usedNames = findNamesOfOtherPlayers (entityManager , player );
80111
81112 String name = preferredName ;
82113 int nextSuffix = 2 ;
@@ -87,31 +118,27 @@ protected String findUniquePlayerName(String preferredName, EntityManager entity
87118 return name ;
88119 }
89120
90- private Set <String > findUsedNames (EntityManager entityManager ) {
91- Set <String > usedNames = new HashSet <String >();
121+ private Set <String > findNamesOfOtherPlayers (EntityManager entityManager , EntityRef player ) {
122+ Set <String > otherNames = new HashSet <String >();
92123 for (EntityRef clientInfo : entityManager .getEntitiesWith (ClientInfoComponent .class )) {
93- DisplayNameComponent displayInfo = clientInfo .getComponent (DisplayNameComponent .class );
94- String usedName = displayInfo .name ;
95- usedNames .add (usedName );
124+ if (!clientInfo .equals (player )) {
125+ DisplayNameComponent displayInfo = clientInfo .getComponent (DisplayNameComponent .class );
126+ String usedName = displayInfo .name ;
127+ otherNames .add (usedName );
128+ }
96129 }
97- return usedNames ;
130+ return otherNames ;
98131 }
99132
100- private EntityRef createClientInfoEntity (String name , Color color , EntityManager entityManager ) {
133+ private EntityRef createClientInfoEntity (EntityManager entityManager ) {
101134 EntityRef clientInfo ;
102135 clientInfo = entityManager .create ("engine:clientInfo" );
103- DisplayNameComponent displayInfo = clientInfo .getComponent (DisplayNameComponent .class );
104- displayInfo .name = name ;
105- clientInfo .saveComponent (displayInfo );
106136
107137 // mark clientInfo entities with a dedicated component
108138 ClientInfoComponent cic = new ClientInfoComponent ();
109139 cic .playerId = getId ();
110140 clientInfo .addComponent (cic );
111141
112- ColorComponent colorComp = new ColorComponent ();
113- colorComp .color = color ;
114- clientInfo .addComponent (colorComp );
115142 return clientInfo ;
116143 }
117144}
0 commit comments