Fractal Softworks Forum

Please login or register.

Login with username, password and session length

Author Topic: Help the newb learn the java  (Read 2312 times)

dmaiski

  • Captain
  • ****
  • Posts: 422
  • resistance is futile
    • View Profile
Help the newb learn the java
« on: October 12, 2013, 01:14:22 PM »

im curently reading up on java but while i do this

who do i put this
Code: java
    public PluginPick<MissileAIPlugin> pickMissileAI(final MissileAPI missile, final ShipAPI launchingShip) {  
       if (missile.getWeapon() != null && missile.getWeapon().getId().equals("harpoon")) {  
          return new PluginPick<MissileAIPlugin>(new MissileAIPlugin() {  
             public void advance(float amount) {  
                missile.giveCommand(ShipCommand.TURN_RIGHT);  
                missile.giveCommand(ShipCommand.ACCELERATE);  
             }  
          }, PickPriority.MOD_GENERAL);  
       }  
       return null;  
    }  

into my BISO mod? or any mod in general, sans crashing.

i have tried:
Spoiler
Code: java
package data.scripts;

import com.fs.starfarer.api.BaseModPlugin;
import com.fs.starfarer.api.Global;

import data.scripts.world.BISOGen;


public class BISOModPlugin extends BaseModPlugin
{
    private static void initBISO()
    {
new BISOGen().generate(Global.getSector());
    }

public PluginPick<MissileAIPlugin> pickMissileAI(final MissileAPI missile, final ShipAPI launchingShip) {  
       if (missile.getWeapon() != null && missile.getWeapon().getId().equals("harpoon")) {  
          return new PluginPick<MissileAIPlugin>(new MissileAIPlugin() {  
             public void advance(float amount) {  
                missile.giveCommand(ShipCommand.TURN_RIGHT);  
                missile.giveCommand(ShipCommand.ACCELERATE);  
             }  
          }, PickPriority.MOD_GENERAL);  
       }  
       return null;  
    }  

    @Override
    public void onNewGame()
    {
        initBISO();
    }

// the following code doesn't work right now, but it will in 0.6.1a!
// its stuff that will let you add it to existing saved games, rather than just new games

    // @Override
    // public void onEnabled(boolean wasEnabledBefore)
    // {
        // if (!wasEnabledBefore)
        // {
           // // Calling a separate method avoids duplicate code with onNewGame()
           // initBISO();
        // }
    // }
}
[close]
this, but it caused a crash and:
@dmaiski: That should work. Are you perhaps expecting it to work on Harpoon Pods or the single-shot Harpoon launchers? That'll only work for 3-shot Harpoon racks, the ones with the weapon id "harpoon".

Aha - I'm not sure Janino actually supports anonymous classes, that might be it. You may need to extract the missile AI implementation into its own class.

so in my noobiness i tried this:
Spoiler
Code
public class BISOpickMissileAI extends pickMissileAI
{
public PluginPick<MissileAIPlugin> pickMissileAI(final MissileAPI missile, final ShipAPI launchingShip) {  
       if (missile.getWeapon() != null && missile.getWeapon().getId().equals("harpoon")) {  
          return new PluginPick<MissileAIPlugin>(new MissileAIPlugin() {  
             public void advance(float amount) {  
                missile.giveCommand(ShipCommand.TURN_RIGHT);  
                missile.giveCommand(ShipCommand.ACCELERATE);  
             }  
          }, PickPriority.MOD_GENERAL);  
       }  
       return null;  
}
}
and this
Code
public class BISOpickMissileAI extends BaseModPlugin
{
public PluginPick<MissileAIPlugin> pickMissileAI(final MissileAPI missile, final ShipAPI launchingShip) {  
       if (missile.getWeapon() != null && missile.getWeapon().getId().equals("harpoon")) {  
          return new PluginPick<MissileAIPlugin>(new MissileAIPlugin() {  
             public void advance(float amount) {  
                missile.giveCommand(ShipCommand.TURN_RIGHT);  
                missile.giveCommand(ShipCommand.ACCELERATE);  
             }  
          }, PickPriority.MOD_GENERAL);  
       }  
       return null;  
}
}

and that
Code
public class BISOpickMissileAI
{
public PluginPick<MissileAIPlugin> pickMissileAI(final MissileAPI missile, final ShipAPI launchingShip) {  
       if (missile.getWeapon() != null && missile.getWeapon().getId().equals("harpoon")) {  
          return new PluginPick<MissileAIPlugin>(new MissileAIPlugin() {  
             public void advance(float amount) {  
                missile.giveCommand(ShipCommand.TURN_RIGHT);  
                missile.giveCommand(ShipCommand.ACCELERATE);  
             }  
          }, PickPriority.MOD_GENERAL);  
       }  
       return null;  
}
}

and some of this

Code
	public class PluginPick<MissileAIPlugin> pickMissileAI(final MissileAPI missile, final ShipAPI launchingShip) {  
       if (missile.getWeapon() != null && missile.getWeapon().getId().equals("harpoon")) {  
          return new PluginPick<MissileAIPlugin>(new MissileAIPlugin() {  
             public void advance(float amount) {  
                missile.giveCommand(ShipCommand.TURN_RIGHT);  
                missile.giveCommand(ShipCommand.ACCELERATE);  
             }  
          }, PickPriority.MOD_GENERAL);  
       }  
       return null;  
}
[close]
all with similarly crashy results
generaly telling me
Spoiler
Code
36844 [Thread-6] INFO  com.fs.graphics.TextureLoader  - Cleaned buffer for texture graphics/biso/ships/D1.png (using cast)
36844 [Thread-6] DEBUG com.fs.graphics.TextureLoader  - Loading [graphics/ships/phase/phase_strike_ff.png] as texture with id [graphics/ships/phase/phase_strike_ff.png]
36860 [Thread-6] DEBUG com.fs.graphics.TextureLoader  - Loaded 160.49 MB of texture data so far
36860 [Thread-6] INFO  com.fs.graphics.TextureLoader  - Cleaned buffer for texture graphics/ships/phase/phase_strike_ff.png (using cast)
36860 [Thread-6] DEBUG com.fs.graphics.TextureLoader  - Loading [graphics/ships/medusa.png] as texture with id [graphics/ships/medusa.png]
36891 [Thread-6] DEBUG com.fs.graphics.TextureLoader  - Loaded 160.82 MB of texture data so far
36891 [Thread-6] INFO  com.fs.graphics.TextureLoader  - Cleaned buffer for texture graphics/ships/medusa.png (using cast)
36891 [Thread-6] DEBUG com.fs.graphics.TextureLoader  - Loading [graphics/ships/buffalo_dd.png] as texture with id [graphics/ships/buffalo_dd.png]
36922 [Thread-6] DEBUG com.fs.graphics.TextureLoader  - Loaded 160.98 MB of texture data so far
36922 [Thread-6] INFO  com.fs.graphics.TextureLoader  - Cleaned buffer for texture graphics/ships/buffalo_dd.png (using cast)
36922 [Thread-6] DEBUG com.fs.graphics.TextureLoader  - Loading [graphics/ships/atlas_af.png] as texture with id [graphics/ships/atlas_af.png]
36985 [Thread-6] DEBUG com.fs.graphics.TextureLoader  - Loaded 161.65 MB of texture data so far
36985 [Thread-6] INFO  com.fs.graphics.TextureLoader  - Cleaned buffer for texture graphics/ships/atlas_af.png (using cast)
37110 [Thread-6] INFO  com.fs.profiler.Profiler  - ID                     Calls   Duration    Percent
37110 [Thread-6] INFO  com.fs.profiler.Profiler  - --------------------------------------------------
37110 [Thread-6] INFO  com.fs.profiler.Profiler  - Resource loading           1     23.89s    100.00%
37156 [Thread-6] ERROR com.fs.starfarer.combat.String  - java.lang.RuntimeException: Error compiling [data.scripts.BISOModPlugin]
java.lang.RuntimeException: Error compiling [data.scripts.BISOModPlugin]
at com.fs.starfarer.loading.scripts.ScriptStore$1.run(Unknown Source)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.ClassNotFoundException: Parsing compilation unit "com.fs.starfarer.loading.A$1@670479"
at org.codehaus.janino.JavaSourceIClassLoader.findIClass(JavaSourceIClassLoader.java:180)
at org.codehaus.janino.IClassLoader.loadIClass(IClassLoader.java:158)
at org.codehaus.janino.JavaSourceClassLoader.generateBytecodes(JavaSourceClassLoader.java:199)
at org.codehaus.janino.JavaSourceClassLoader.findClass(JavaSourceClassLoader.java:164)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
... 2 more
Caused by: org.codehaus.commons.compiler.CompileException: File data/scripts/BISOModPlugin.java, Line 14, Column 6: Expression "new MissileAI()" is not a type
at org.codehaus.janino.Java$Located.throwCompileException(Java.java:97)
at org.codehaus.janino.Java$Atom.toTypeOrPE(Java.java:1899)
at org.codehaus.janino.Parser.parseBlockStatement(Parser.java:1010)
at org.codehaus.janino.Parser.parseBlockStatements(Parser.java:938)
at org.codehaus.janino.Parser.parseMethodDeclarationRest(Parser.java:804)
at org.codehaus.janino.Parser.parseClassBodyDeclaration(Parser.java:442)
at org.codehaus.janino.Parser.parseClassBody(Parser.java:393)
at org.codehaus.janino.Parser.parseClassDeclarationRest(Parser.java:372)
at org.codehaus.janino.Parser.parsePackageMemberTypeDeclaration(Parser.java:251)
at org.codehaus.janino.Parser.parseCompilationUnit(Parser.java:153)
at org.codehaus.janino.JavaSourceIClassLoader.findIClass(JavaSourceIClassLoader.java:150)
... 7 more
[close]

can somone show me the right way to do this [puppy dog eyes]
Spoiler
[close]
Logged
BISO
(WIP) lots of shiny new weapons ( :-[ i have more weapons then sprites :-[ )

i got a cat pad
its like a mouse pad but better!

dmaiski

  • Captain
  • ****
  • Posts: 422
  • resistance is futile
    • View Profile
Re: Help the newb learn the java
« Reply #1 on: October 12, 2013, 07:19:18 PM »

Caused by: org.codehaus.commons.compiler.CompileException: File data/scripts/BISOModPlugin.java, Line 41, Column 20: JANINO does not support generics

i still have no idea how to un-generic my code...
Spoiler
Code
package data.scripts;

import com.fs.starfarer.api.BaseModPlugin;
import com.fs.starfarer.api.Global;

import data.scripts.world.BISOGen;

public class BISOModPlugin extends BaseModPlugin
{
    private static void initBISO()
    {
        // Create an instance of every generator here and call its generate() method
new BISOGen().generate(Global.getSector());
    }



    @Override
    public void onNewGame()
    {
        // Calling a separate method avoids duplicate code with onEnabled()
        initBISO();
    }

// the following code doesn't work right now, but it will in 0.6.1a!
// its stuff that will let you add it to existing saved games, rather than just new games

    // @Override
    // public void onEnabled(boolean wasEnabledBefore)
    // {
        // if (!wasEnabledBefore)
        // {
           // // Calling a separate method avoids duplicate code with onNewGame()
           // initBISO();
        // }
    // }
}

public class BISOMissileAI extends BaseModPlugin
{
public PluginPick<MissileAIPlugin> pickMissileAI(final MissileAPI missile, final ShipAPI launchingShip) { 
       if (missile.getWeapon() != null && missile.getWeapon().getId().equals("harpoon")) { 
          return new PluginPick<MissileAIPlugin>(new MissileAIPlugin() { 
             public void advance(float amount) { 
                missile.giveCommand(ShipCommand.TURN_RIGHT); 
                missile.giveCommand(ShipCommand.ACCELERATE); 
             } 
          }, PickPriority.MOD_GENERAL); 
       } 
       return null; 
}
}
[close]
Logged
BISO
(WIP) lots of shiny new weapons ( :-[ i have more weapons then sprites :-[ )

i got a cat pad
its like a mouse pad but better!

dmaiski

  • Captain
  • ****
  • Posts: 422
  • resistance is futile
    • View Profile
Re: Help the newb learn the java
« Reply #2 on: October 13, 2013, 03:06:34 AM »

Spoiler
[close]

happy dance

i still have no real idea how i made it work, but hey if it works, and you can reproduce it, it doesn't matter if the primary power source is evil in a can!

Spoiler
Code: java
package data.scripts;


import com.fs.starfarer.api.BaseModPlugin;
import com.fs.starfarer.api.Global;
import com.fs.starfarer.api.PluginPick;
import com.fs.starfarer.api.campaign.CampaignPlugin;
import com.fs.starfarer.api.combat.MissileAIPlugin;
import com.fs.starfarer.api.combat.MissileAPI;
import com.fs.starfarer.api.combat.ShipAPI;
import com.fs.starfarer.api.combat.ShipCommand;


import data.scripts.world.BISOGen;
import data.scripts.BISOMissileAI;

public class BISOModPlugin extends BaseModPlugin
{
    private static void initBISO()
    {
        // Create an instance of every generator here and call its generate() method
new BISOGen().generate(Global.getSector());
    }

    @Override
    public void onNewGame()
    {
        // Calling a separate method avoids duplicate code with onEnabled()
        initBISO();
    }


    public PluginPick<MissileAIPlugin> pickMissileAI(final MissileAPI missile, final ShipAPI launchingShip) {
        if (missile.getWeapon() != null && missile.getWeapon().getId().equals("harpoon")) {
            return new PluginPick<MissileAIPlugin>(new MissileAIPlugin() {
                public void advance(float amount) {
                    missile.giveCommand(ShipCommand.TURN_RIGHT);
                    missile.giveCommand(ShipCommand.ACCELERATE);
                }
            }, CampaignPlugin.PickPriority.MOD_GENERAL);
        }
        return null;
    }

    // the following code doesn't work right now, but it will in 0.6.1a!
// its stuff that will let you add it to existing saved games, rather than just new games

    // @Override
    // public void onEnabled(boolean wasEnabledBefore)
    // {
        // if (!wasEnabledBefore)
        // {
           // // Calling a separate method avoids duplicate code with onNewGame()
           // initBISO();
        // }
    // }
}



[close]


evil inside
Spoiler
Code: java
package data.scripts;

import com.fs.starfarer.api.BaseModPlugin;
import com.fs.starfarer.api.PluginPick;
import com.fs.starfarer.api.campaign.CampaignPlugin;
import com.fs.starfarer.api.combat.MissileAIPlugin;
import com.fs.starfarer.api.combat.MissileAPI;
import com.fs.starfarer.api.combat.ShipAPI;
import com.fs.starfarer.api.combat.ShipCommand;

public class BISOMissileAI extends BaseModPlugin{ public PluginPick<MissileAIPlugin> pickMissileAI(final MissileAPI missile, final ShipAPI launchingShip) {
        if (missile.getWeapon() != null && missile.getWeapon().getId().equals("harpoon")) {
            return new PluginPick<MissileAIPlugin>(new MissileAIPlugin() {
                public void advance(float amount) {
                    missile.giveCommand(ShipCommand.TURN_RIGHT);
                    missile.giveCommand(ShipCommand.ACCELERATE);
                }
            }, CampaignPlugin.PickPriority.MOD_GENERAL);
        }
        return null;
    }
}
[close]

now the next step is to figure out how it all works, anyone willing to explain?
« Last Edit: October 13, 2013, 03:29:30 AM by dmaiski »
Logged
BISO
(WIP) lots of shiny new weapons ( :-[ i have more weapons then sprites :-[ )

i got a cat pad
its like a mouse pad but better!