SFSRoomSettings immutable support

Post here your suggestions for new possible features in SmartFoxServer 2X.

Moderators: Lapo, Bax

Post Reply
YoungDeveloper
Posts: 74
Joined: 02 Mar 2014, 21:38
Location: Latvia

SFSRoomSettings immutable support

Post by YoungDeveloper »

When creating an MMORoom you pass SFSRoomSettings

Code: Select all

final Set<SFSRoomSettings> roomSettings = Set.of(
                SFSRoomSettings.ROOM_NAME_CHANGE,
                SFSRoomSettings.PASSWORD_STATE_CHANGE,
                SFSRoomSettings.PUBLIC_MESSAGES,
                SFSRoomSettings.CAPACITY_CHANGE,
                SFSRoomSettings.USER_ENTER_EVENT,
                SFSRoomSettings.USER_EXIT_EVENT,
                SFSRoomSettings.USER_COUNT_CHANGE_EVENT,
                SFSRoomSettings.USER_VARIABLES_UPDATE_EVENT
);


If you did it this way you will get an internal exception.

Set.of() creates an immutable set, but inside SFSRoomManager.createRoom() it uses .remove() to alter the original collection.

Code: Select all

Set<SFSRoomSettings> roomSettings = params.getRoomSettings();
        if (roomSettings == null) {
            roomSettings = this.getDefaultRoomSettings(params);
        }

        if (params.isSuppressUserList()) {
            newRoom.setSuppressUserList(true);
            roomSettings.remove(SFSRoomSettings.USER_ENTER_EVENT);
            roomSettings.remove(SFSRoomSettings.USER_EXIT_EVENT);
        }

        if (newRoom instanceof MMORoom) {
            if (roomSettings.contains(SFSRoomSettings.USER_ENTER_EVENT)) {
                roomSettings.remove(SFSRoomSettings.USER_ENTER_EVENT);
            }

            if (roomSettings.contains(SFSRoomSettings.USER_EXIT_EVENT)) {
                roomSettings.remove(SFSRoomSettings.USER_EXIT_EVENT);
            }
        }



Suggestion:
Instead of altering the original collection, maybe create a new HashSet from the existing passed one.
If not then altering docs would be good as for me took some time to realize this issue as errors started to popup and 'rooms missing'.

Of course, from the internal code it's clear that using user_enter and user_exit for mmo room is pointless, but still most likely should not crash on this.

Thank you
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: SFSRoomSettings immutable support

Post by Lapo »

Yeah, makes sense.
I think this was done pre-Java 9 where List.of / Set.of didn't even exist.

However I think in the docs we use EnumSet.of() (which is from Java 5, how much time! :shock: )
and it should work with the removal.

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
Post Reply