Fractal Softworks Forum

Please login or register.

Login with username, password and session length
Advanced search  

News:

Starsector 0.9.1a is out! (05/10/19); Blog post: Personal Contacts (08/13/20)

Author Topic: Working with getWeapons() from CargoAPI  (Read 2149 times)

Ghoti

  • Captain
  • ****
  • Posts: 283
    • View Profile
    • Email
Working with getWeapons() from CargoAPI
« 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?
Logged

TJJ

  • Admiral
  • *****
  • Posts: 1866
    • View Profile
    • Email
Re: Working with getWeapons() from CargoAPI
« Reply #1 on: September 17, 2012, 06:26:27 PM »

Code
String s = (String) ((CargoItemQuantity) cargo.getWeapons().get(x)).getItem();
Logged

LazyWizard

  • Global Moderator
  • Admiral
  • *****
  • Posts: 1320
    • View Profile
    • GitHub Profile
Re: Working with getWeapons() from CargoAPI
« Reply #2 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. :)
Logged

Ghoti

  • Captain
  • ****
  • Posts: 283
    • View Profile
    • Email
Re: Working with getWeapons() from CargoAPI
« Reply #3 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.
Logged

TJJ

  • Admiral
  • *****
  • Posts: 1866
    • View Profile
    • Email
Re: Working with getWeapons() from CargoAPI
« Reply #4 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.
Logged

Ghoti

  • Captain
  • ****
  • Posts: 283
    • View Profile
    • Email
Re: Working with getWeapons() from CargoAPI
« Reply #5 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.
« Last Edit: September 17, 2012, 08:14:42 PM by Ghoti »
Logged

LazyWizard

  • Global Moderator
  • Admiral
  • *****
  • Posts: 1320
    • View Profile
    • GitHub Profile
Re: Working with getWeapons() from CargoAPI
« Reply #6 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 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. :)
Logged