Creating a new instance of the same room

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

bhattacharyakun
Posts: 16
Joined: 14 Jul 2020, 13:06

Re: Creating a new instance of the same room

Postby bhattacharyakun » 31 Jul 2020, 12:56

Ciao, Lapo

I guess I had to restart the server for some reason, after restarting the server the extension now loads, but it doesn't triggers the class, it gives this error

Code: Select all

{ZoneExtension}: Cannot instantiate handler class: java.lang.InstantiationException: zoneextension.AutoJoiner


after googling a little I found this https://www.smartfoxserver.com/forums/v ... hp?t=16765

So I did have the classes nested at first, then I separated them in their own files, here -

ZoneExtension

Code: Select all

package zoneextension;

import com.smartfoxserver.v2.extensions.SFSExtension;

public class ZoneExtension extends SFSExtension {
   

 
    @Override
    public void init()
    {
       trace("Extension working");
        addRequestHandler("JoinOrCreateRoom", AutoJoinerHandler.class);
    }
}


and AutoJoinerHandler

Code: Select all

package com.swa.in.loka;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import com.smartfoxserver.v2.api.CreateRoomSettings;
import com.smartfoxserver.v2.entities.Room;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.exceptions.SFSCreateRoomException;
import com.smartfoxserver.v2.exceptions.SFSException;
import com.smartfoxserver.v2.exceptions.SFSJoinRoomException;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;
import com.smartfoxserver.v2.extensions.ExtensionLogLevel;

public class AutoJoinerHandler extends BaseClientRequestHandler
{
   private String roomName;
    private int maxPlayers;
 
    private final AtomicInteger roomId = new AtomicInteger();

   @Override
   public void handleClientRequest(User user, ISFSObject params) {
        roomName = params.getUtfString("roomName");
        maxPlayers = params.getInt("maxPlayers");
        try
        {
           joinUser(user);
        }
        catch(Exception err)
        {
            trace(ExtensionLogLevel.ERROR, err.toString());
        }
   }
   
   private void joinUser(User user) throws SFSException
    {
        List<Room> rList = getParentExtension().getParentZone().getRoomList();
        Room theRoom = null;
 
        for (Room room : rList)
        {
            if(room.getName() == roomName)
            {
                if (room.isFull())
                {
                    break;
                }
                else
                {
                    theRoom = room;
                    break;
                }
            }
        }
 
        if (theRoom == null)
            theRoom = makeNewRoom(user);
 
        try
        {
            getApi().joinRoom(user, theRoom);
        }
        catch (SFSJoinRoomException e)
        {
            trace(ExtensionLogLevel.ERROR, e.toString());
        }
    }
 
    private Room makeNewRoom(User owner) throws SFSCreateRoomException
    {
        Room room = null;
 
        CreateRoomSettings rs = new CreateRoomSettings();
        rs.setGame(false);
        rs.setDynamic(true);
        rs.setName(roomName + roomId.getAndIncrement());
        rs.setMaxUsers(maxPlayers);
 
        room = getApi().createRoom(getParentExtension().getParentZone(), rs, owner);
        return room;
    }
   
}


And its still giving me the same error as mentioned before, this one

Code: Select all

{ZoneExtension}: Cannot instantiate handler class: java.lang.InstantiationException: zoneextension.AutoJoiner
bhattacharyakun
Posts: 16
Joined: 14 Jul 2020, 13:06

Re: Creating a new instance of the same room

Postby bhattacharyakun » 01 Aug 2020, 10:13

Okay it worked, I had to change the zone extension class from

Code: Select all

import com.smartfoxserver.v2.extensions.SFSExtension;

public class ZoneExtension extends SFSExtension {
    @Override
    public void init()
    {
       trace("Extension working");
        addRequestHandler("JoinOrCreateRoom", AutoJoinerHandler.class);
    }
}


to

Code: Select all

public class ZoneExtension extends SFSExtension {
   
   private loka.AutoJoinerHandler autoJoiner;
   
    @Override
    public void init()
    {
       this.autoJoiner = new loka.AutoJoinerHandler();
        addRequestHandler("JoinOrCreateRoom", this.autoJoiner);
    }
}

Return to “SFS2X Questions”

Who is online

Users browsing this forum: Aldenea, DavinVicy, DerikFent, JerodSa, Kevengar and 15 guests