Fractal Softworks Forum

Starsector => Mods => Modding => Topic started by: Ghoti on September 17, 2012, 06:02:58 PM

Title: Working with getWeapons() from CargoAPI
Post by: Ghoti on September 17, 2012, 06:02:58 PM
I've being try to get the list of weapons in Cargo, the problem is that Janino doesn't play well with generics for some odd reason, and I can't define a proper return value for the list
Code
List<CargoItemQuantity<String>> container = cargo.getWeapons();
I have to define it like this
Code
List container = cargo.getWeapons();
The problem with that is now I've got a list of objects.

I can't cast like this
Code
(CargoItemQuantity<String>) container
presumably for the same reason above. Which is OK I guess cause I can do this....
Code
CargoItemQuantity ciq = (CargoItemQuantity) container.get(x);

but... and I don't know why. When I get to this long annoying rabbit hole and have to cast the String. I get this
Code
Caused by: org.codehaus.commons.compiler.CompileException: Array initializer not allowed for non-array type "java.lang.String"

grrrrr.... Does anyone have any working examples using this method?
Title: Re: Working with getWeapons() from CargoAPI
Post by: TJJ on September 17, 2012, 06:26:27 PM
Code
String s = (String) ((CargoItemQuantity) cargo.getWeapons().get(x)).getItem();
Title: Re: Working with getWeapons() from CargoAPI
Post by: LazyWizard on September 17, 2012, 06:27:13 PM
Wow, looking through my code I realized I have never used the getWeapons() method, so I can't help with that. Sorry.

However, you might have better luck looping through getStacksCopy() and filtering the stacks with isWeaponStack(). That would give you a List of CargoStackAPI objects, which pretty much obsolete CargoItemQuantity anyway. :)
Title: Re: Working with getWeapons() from CargoAPI
Post by: Ghoti on September 17, 2012, 06:39:20 PM
Code
String s = (String) ((CargoItemQuantity) cargo.getWeapons().get(x)).getItem();
tjj that's just an inline version of the same code... The same code that gives me an error. I know you can inline it. I was just breaking it down.

I forgot about that isWeaponStack method. That will work. Thanks.

Also, again. Damn fine wizard, lazy.
Title: Re: Working with getWeapons() from CargoAPI
Post by: TJJ on September 17, 2012, 06:51:09 PM
Code
String s = (String) ((CargoItemQuantity) cargo.getWeapons().get(x)).getItem();
tjj that's just an inline version of the same code... The same code that gives me an error. I know you can inline it. I was just breaking it down.

Perhaps you should post the entire code segment in which you are encountering the problem, as in isolation janino has no trouble compiling the example I posted.

Either the problem is being caused elsewhere in your code, or it's a compiler bug due to the composition of code being compiled.
Title: Re: Working with getWeapons() from CargoAPI
Post by: Ghoti on September 17, 2012, 07:02:49 PM
Ah so it does... With the use of comments and I narrowed the problem down to this:
Code
private static String makeWep[] = { ... };
private static String[] makeWep = { ... };

Apparently OK in Java 7, not in Java 5.

Damn it. Well this is embarrassing. Janino I would of liked a line number you know...

edit:
OK. I'm in business. Thanks guys.
Title: Re: Working with getWeapons() from CargoAPI
Post by: LazyWizard on September 17, 2012, 09:27:23 PM
Glad to be of help, even if it wasn't much. :)

Apparently OK in Java 7, not in Java 5.

I know you didn't say it, but Janino's restrictions (http://docs.codehaus.org/display/JANINO/Home#Home-limitations) are for the most part syntax only. Our scripts still have access to the entire Java 6 library that Starfarer uses.

Just making sure you don't artificially limit yourself. :)