Hooking new events

    Joined
    Oct 19, 2013
    Messages
    47
    Reaction score
    1
    Hi guys,

    some examples for using aspects to hook into other events and override the launcher on start to create some more fun mods that you can simply package up as a .jar file which others can load instead of loading starmade.jar

    This is not complete code, you will need to do the rest of the work - please just take it more as notes

    Sample Aspect

    Aspects should be defined in your packages aop.xml in META-INF for example

    Code:
    <aspectj>
      <aspects>
        <aspect name="com.mixxit.starmadeex.Aspects.ControllerStateAspect"/>
        <aspect name="com.mixxit.starmadeex.Aspects.ModManagerAspect"/>
        <aspect name="com.mixxit.starmadeex.Aspects.PlayerStateAspect"/>
        <aspect name="com.mixxit.starmadeex.Aspects.GameServerControllerAspect"/>
        <aspect name="com.mixxit.starmadeex.Aspects.TtAspect"/>
        <aspect name="com.mixxit.starmadeex.Aspects.ServerControllerAspect"/>
      </aspects>
      <weaver options="-Xset:weaveJavaxPackages=true -Xset:weaveJavaPackages=true">
        <include within="org.schema..*"/>
        <include within="obfuscated..*"/>
      </weaver>
    </aspectj>
    then you can define a custom aspect with the following example

    Code:
    public aspect PlayerStateAspect {
        @After("execution(* org.schema.game.common.data.player.PlayerState.setState(..)) && args(paramStateInterface) && this(p)")
        public void aroundServiceMethodAdvice(StateInterface paramStateInterface, PlayerState p)
        {
            if (p.getOwnerState().getState() instanceof ServerStateEx)
            {
                ServerStateEx server = (ServerStateEx) p.getOwnerState().getState();
                server.GetServer().GetManager().OnPlayerSetState(p,paramStateInterface);
            }
        }
    
    }
    Overriding the Launcher
    How to communicate back to your mod from events fired from aspects would require being able to redirect your messages to functions that do not exist in the core

    Due to this i override the server on launch

    How I start the server manually

    Code:
    public class ServerStateEx extends ajs {
    
        StarMadeServer server;
        public StarMadeServer GetServer()
        {
            return this.server;
        }
    
        public ServerStateEx(StarMadeServer starMadeServer) {
            super();
            // TODO Auto-generated constructor stub
            this.server = starMadeServer;
        }
    
        public HashMap GetAllClients()
        {
            return this.getClients();
        }
    }
    Launch game manually

    Code:
    public void Start(StarMadeExManager manager) throws InterruptedException, IOException
        {
            System.err.println("[STARMADEEX][SERVER] Initializing");
      
            apy.a(new gx());
      
            SetManager(manager);
      
            try
            {  
                serverstate = new ServerStateEx(this);
            } catch (Exception localSQLException)
            {
                localSQLException.printStackTrace();
                e.a(localSQLException);
                System.exit(-1);  
            }
            servercontroller = new GameServerController(serverstate);
            servercontroller.startServerAndListen();
            while(!servercontroller.isListenting())
            {
                Thread.sleep(30L);
            }
      
            File localFile;
            if ((localFile = new File("./debugPlayerLock.lock")).exists()) {
              localFile.delete();
            }
      
            localFile.createNewFile();
            DataOutputStream dos = new DataOutputStream(new FileOutputStream(localFile));
            synchronized (serverstate.getClients())
            {
                dos.writeInt(serverstate.getClients().size());
            }
            dos.close();
      
            System.err.println("[STARMADEEX][SERVER] INTIALIZED!");
      
        }

    Launching your custom server source with aspect support

    Code:
    java -Xms256m -Xmx2048m -javaagent:org.aspectj.weaver_1.8.6.20150608154200.jar -Xbootclasspath/a:org.aspectj.runtime_1.8.6.20150608154200.jar -jar StarMadeEx.jar -server
    Overview

    Using aspects you can create your own custom server that launches starmade server from inside itself, holding onto a reference for it and also communicating back with enhanced methods

    For example I will load ./StartMadeEx.jar which in turns launches the subserver that normally StarMade.jar would load

    Obviously obfuscation means some methods such as ajs will change each iteration, until this can be expanded on by the development team

    There are some developers I believe maybe mincrafter127 has a list of mapped methods that we can use for now

    Have fun with AOP!
     
    Last edited:

    therimmer96

    The Cake Network Staff Senior button unpusher
    Joined
    Jun 21, 2013
    Messages
    3,603
    Reaction score
    1,053
    Code:
    You know, it would be nice to put the code in a code box right?
    [code]