Organize my code in classes? Java

Post here your questions about Actionscript and Java server side extensions development.

Moderators: Lapo, Bax

Sinkist
Posts: 9
Joined: 02 May 2017, 12:56

Organize my code in classes? Java

Postby Sinkist » 15 May 2017, 11:26

Hey,
i want to tidy up my extension code(Java extension). So i thought i put similar things in a class and access them thru this.

My project is made out of a main-class which extends SFSExtension and contains the init() start-function.

Now i created a new class itemUtil-class which extends main-class so i have access to sfsAPI() and so on... (i dont take extends SFSExtension cause this forces me to create a second init()-function that i dont want.)

Then i tried to access that class.
First i made a reference in main:

Code: Select all

public class Main extends SFSExtension {

   ItemUtil handleItemUtil = new ItemUtil();
   
   @Override
   public void init() {
      //........

In the code above is a bug. This "ItemUtil handleItemUtil = new ItemUtil();" lets the server crash.

To access from other points in my projekt i do (Only for showing purposes...):

Code: Select all

ItemUtil handleItemUtil = ((Main)getParentExtension()).handleItemUtil;
handleItemUtil.TestFunction();


itemUtil-class:

Code: Select all

public class ItemUtil extends Main {

   public void TestFunction() {
   
   }
}



How can i do it the right way? What i want is to outsource some code in files.
regards.
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Organize my code in classes? Java

Postby Lapo » 15 May 2017, 15:03

You should not be extending Main, because you inherit all of its code and fields which is unnecessary and causes a lot of duplication.

... extends main-class so i have access to sfsAPI() and so on...

You can still access the API like this:

Code: Select all

ISFSApi api = SmartFoxServer.getInstance(),getAPIManager().getSFSApi();

or even easier: by passing a reference of the SFSApi to the constructor of your ItemUtil class :)

hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
Sinkist
Posts: 9
Joined: 02 May 2017, 12:56

Re: Organize my code in classes? Java

Postby Sinkist » 15 May 2017, 16:23

Dear,
so i just create a new class with no change in superclass(default is java.lang.Object) and no interface.

In my functions in this class i can not use things like "getParentZone()" or even "trace()". In case of getParentZone() which returns a zone you mean i should pass the zone via my class constructor like its made here with an int: https://docs.oracle.com/javase/tutorial/java/javaOO/classes.html

Is this the easy and efficient way to do this?
And how do i get trace() to work?

I think im asking java basic stuff. Hope its ok.
regards.
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Organize my code in classes? Java

Postby Lapo » 15 May 2017, 16:40

Sinkist wrote:Dear,
so i just create a new class with no change in superclass(default is java.lang.Object) and no interface.

In my functions in this class i can not use things like "getParentZone()" or even "trace()". In case of getParentZone() which returns a zone you mean i should pass the zone via my class constructor like its made here with an int: https://docs.oracle.com/javase/tutorial/java/javaOO/classes.html

Is this the easy and efficient way to do this?

Yes, no problem.

And how do i get trace() to work?

See here:
http://smartfoxserver.com/blog/using-th ... sion-code/

I think im asking java basic stuff. Hope its ok.

No prob :)
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “Server Side Extension Development”

Who is online

Users browsing this forum: No registered users and 27 guests