Create account / Log in

Adding a Custom Class

Discussion area for the development team.

Moderators: Tim M, uckelman

Adding a Custom Class

Postby drimmer » November 6th, 2019, 6:05 pm

I would like to add a custom java class to a module created by using the VASSAL Editor. The only references I can find in the forum related to importing an existing VASSAL class. I want to add a class that I have created myself to add a specialized button to the toolbar.

Any pointers?

Thanks.

Doug
Doug
drimmer
 
Posts: 21
Joined: May 21st, 2014, 4:48 pm

Re: Adding a Custom Class

Postby Malnorma » November 6th, 2019, 7:19 pm

This page is the most useful one on that topic, I think:
http://www.vassalengine.org/wiki/Programming_Tutorial
Malnorma
 
Posts: 123
Joined: October 14th, 2015, 9:26 am

Re: Adding a Custom Class

Postby drimmer » November 7th, 2019, 3:26 pm

Malnorma wrote:This page is the most useful one on that topic, I think:
http://www.vassalengine.org/wiki/Programming_Tutorial


This was certainly helpful. I had overlooked it completely.

However, after adding my custom class to the zip file and then trying to Edit the module to import the class, I get an error message that "The file 'C:\Users\dougr_000\Documents\Programming Documents\almodule\mymodule.vmod' is not a valid VASSAL module."

Thus, I can't complete the process. Any thoughts about what is triggering the error message?
Doug
drimmer
 
Posts: 21
Joined: May 21st, 2014, 4:48 pm

Re: Adding a Custom Class

Postby Malnorma » November 8th, 2019, 2:48 am

How did you add your class to the zip file? The most common cause for this, I think, is not having the correct directory structure inside the zip (a lot of zip utilities like to automatically include the parent directory), but if you dragged new files into an existing zip archive to update it, that shouldn't happen.
Malnorma
 
Posts: 123
Joined: October 14th, 2015, 9:26 am

Re: Adding a Custom Class

Postby drimmer » November 10th, 2019, 7:44 pm

Malnorma wrote:How did you add your class to the zip file? The most common cause for this, I think, is not having the correct directory structure inside the zip (a lot of zip utilities like to automatically include the parent directory), but if you dragged new files into an existing zip archive to update it, that shouldn't happen.


I extracted the contents of the original module zip file to a directory. Then I added my custom class to it. Then I rezipped it. I will try it again by simply dragging the class file into the module zip file. I tried it this way because of the focus in the instructions on getting the full path correct. Perhaps I misunderstood.
Doug
drimmer
 
Posts: 21
Joined: May 21st, 2014, 4:48 pm

Re: Adding a Custom Class

Postby drimmer » November 10th, 2019, 7:54 pm

Just tried dragging the class file into the module zip file. I get a "The Compressed folder is invalid or corrupted" error message. trying this in Win10 using File Explorer.

When I try to run the module zip file as a .vmod file, it works perfectly. But when I rename it to a .zip file, it won't let me drag a file into it.
Doug
drimmer
 
Posts: 21
Joined: May 21st, 2014, 4:48 pm

Re: Adding a Custom Class

Postby Malnorma » November 10th, 2019, 8:46 pm

Maybe grab a copy of 7-zip and try using that instead. I'm not familiar with Windows 10's zip support, but it wouldn't surprise me if it was a bit crap. It's kind of a theme I'm seeing in operating systems lately.
Malnorma
 
Posts: 123
Joined: October 14th, 2015, 9:26 am

Re: Adding a Custom Class

Postby drimmer » November 11th, 2019, 8:09 pm

Malnorma wrote:Maybe grab a copy of 7-zip and try using that instead. I'm not familiar with Windows 10's zip support, but it wouldn't surprise me if it was a bit crap. It's kind of a theme I'm seeing in operating systems lately.


7-zip worked. I was able to drag-and-drop the file into the zip .vmod file and then open the zip .vmod file in the VASSAL editor.

So, thanks very much for your help on this. I really appreciate it.

However, I am still having a problem. I am trying to use the Add Imported Class feature to incorporate the class into my game. I am getting a ClassNotFound error. So I suspect I am not giving it the correct full path.

Before adding the custom class to the zip file, I created a directory inside the zip file called "MyGame.module". This matches the package name for the class which is MyGame.module. The name of the class is Buttons.java. So, when prompted for the full path name to add the imported class, I type MyGame.module/Buttons.java. And get the ClassNotFound error. Can you see where I am making a mistake in the path?

Thanks
Doug
drimmer
 
Posts: 21
Joined: May 21st, 2014, 4:48 pm

Re: Adding a Custom Class

Postby Malnorma » November 11th, 2019, 10:23 pm

Try just the java name: MyGame.module.Buttons

However, I think you need to use two directories rather than one with a dot in it. I could be mistaken, but if the above name also doesn't work, try structuring it in the module as MyGame/module/Buttons.java.

[Edit] Buttons.class, not Buttons.java, sorry.
Malnorma
 
Posts: 123
Joined: October 14th, 2015, 9:26 am

Re: Adding a Custom Class

Postby Brent Easton » November 12th, 2019, 12:47 am

Yes, the directory structure within the module must match the package
name structure.

So inside your module there will need to be a directory named MyGame,
which must contain a directory named module, which must contain the
compiled Buttons.class file.

You would then enter the fully qualified name of the class which is
Mygame.module.Buttons

Regards,
Brent.


On 12/11/2019 9:25 am, Malnorma wrote:
> [This message has been edited.]
>
> Try just the java name:  MyGame.module.Buttons
>
> However, I think you need to use two directories rather than one with a
> dot in it.  I could be mistaken, but if the above name also doesn't
> work, try structuring it in the module as MyGame/module/Buttons.java.
>
> [Edit] __Buttons.class__, not Buttons.java, sorry.
>
> _______________________________________________
> Read this topic online here:
> viewtopic.php?p=57995#p57995
> _______________________________________________
> messages mailing list
> messages@vassalengine.org
> http://www.vassalengine.org/mailman/listinfo/messages
>


--
____________________________________________________________
Brent Easton
Analyst/Programmer
Western Sydney University
Email: b.easton@exemail.com.au

_______________________________________________
messages mailing list
messages@vassalengine.org
http://www.vassalengine.org/mailman/listinfo/messages
User avatar
Brent Easton
 
Posts: 2755
Joined: December 21st, 2007, 3:06 am
Location: Berry, NSW, Australia

Re: Adding a Custom Class

Postby drimmer » November 12th, 2019, 3:07 am

[quote="Brent Easton"]Yes, the directory structure within the module must match the package
name structure.

So inside your module there will need to be a directory named MyGame,
which must contain a directory named module, which must contain the
compiled Buttons.class file.

You would then enter the fully qualified name of the class which is
Mygame.module.Buttons

Regards,
Brent.

Thanks to you both for your continued help. I tried MyGame.module.Buttons and got a different error message: "There was an error loading or instantiating the class WingLeader.module.Buttons, which is provided by this module."

I assume that this means that MyGame.module.Buttons worked in that the class was found but that there is an error in the class. I created the class based on the programming example on the site, with the addTo method creating two buttons which is just what I needed my class to do. I have not added the Buttons class to the buildfile in the .vmod - I assumed that this would happen if it was successfully added by the Editor. I was trying to add the class to the Main Map window as it's purpose is to put two buttons on the toolbar. Should I try elsewhere?
Doug
drimmer
 
Posts: 21
Joined: May 21st, 2014, 4:48 pm

Re: Adding a Custom Class

Postby Malnorma » November 12th, 2019, 4:05 am

Is the class actually supposed to be named WingLeader.module.Buttons? i.e. in your code, do you have:

Code: Select all
package WingLeader.module;
public class Buttons {
...
}


If so, you should rename the MyGame folder to WingLeader and use the name WingLeader.module.Buttons instead. If WingLeader.module.Buttons is in another class file entirely, you'll need to add it to the module as well (and any other related classes). If there's a problem instantiating your class, you'll need to either debug it or check Vassal's log to try and understand where it went wrong.

If you're willing to share the code you've written, you'll probably get more useful comments :)
Malnorma
 
Posts: 123
Joined: October 14th, 2015, 9:26 am

Re: Adding a Custom Class

Postby drimmer » November 12th, 2019, 2:04 pm

Malnorma wrote:Is the class actually supposed to be named WingLeader.module.Buttons? i.e. in your code, do you have:

Code: Select all
package WingLeader.module;
public class Buttons {
...
}


If so, you should rename the MyGame folder to WingLeader and use the name WingLeader.module.Buttons instead. If WingLeader.module.Buttons is in another class file entirely, you'll need to add it to the module as well (and any other related classes). If there's a problem instantiating your class, you'll need to either debug it or check Vassal's log to try and understand where it went wrong.

If you're willing to share the code you've written, you'll probably get more useful comments :)


Sorry about that. The project I am working on is WingLeader. I generally use a generic name rather than the particular name when asking of help because sometimes the exact names can cause confusion in and of themselves. But maybe I am wrong about that. Using a generic name certainly caused confusion this time.

Here is the VASSAL log I am getting with this recent error message. You will see it uses yet another different package name as I have tried to simplify the path by getting rid of the ".module".

I have tried this with a directory structure of src\WingLeader\Buttons.java and with WingLeader\Buttons.java (without changing the package name). Neither work. I have tried using Buttons, Buttons.class and Buttons.java as the file name.

As you can see, the problem is still that VASSAL can't seem to find the imported class. Thanks again for your help.

java.lang.ClassNotFoundException: Unable to load class WingLeader\Buttons
at VASSAL.tools.DataArchive.findClass(DataArchive.java:453)
at VASSAL.tools.DataArchive.loadClass(DataArchive.java:424)
at java.lang.ClassLoader.loadClass(Unknown Source)
at VASSAL.configure.ConfigureTree.importConfigurable(ConfigureTree.java:849)
at VASSAL.configure.ConfigureTree$5.actionPerformed(ConfigureTree.java:465)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.io.FileNotFoundException: 'WingLeader\Buttons.class' not found in C:\Users\dougr_000\Documents\Programming Documents\almodule\WL v4 LAYER TEST.vmod
at VASSAL.tools.DataArchive.getInputStream(DataArchive.java:240)
at VASSAL.tools.DataArchive.findClass(DataArchive.java:449)
... 42 more
Doug
drimmer
 
Posts: 21
Joined: May 21st, 2014, 4:48 pm

Re: Adding a Custom Class

Postby zgrose » November 12th, 2019, 3:10 pm

I think if you show them your source file, they can tell you exactly where the compiled class has to go in the module.

>>I have tried this with a directory structure of src\WingLeader\Buttons.java and with WingLeader\Buttons.java (without changing the package name). Neither work. I have tried using Buttons, Buttons.class and Buttons.java as the file name.

The *.java file is the (plaintext) source file. It doesn't go into the module. When you compile the class, foo.java is turned into foo.class (and perhaps other files if there is more than one class in the source file). That *.class file is what you need to put into the module. You do not want to rename anything. You do need to compile your source into a class.
zgrose
 
Posts: 46
Joined: September 8th, 2010, 4:31 am

Re: Adding a Custom Class

Postby Malnorma » November 12th, 2019, 6:51 pm

As zgrose said, but the exact name of the class (package + class name) should match the directory structure too. You can't pick the name afterwards without changing the source.

So if you have:
Code: Select all
package WingLeader;
class Buttons { ... }

then you need to use the path WingLeader/Buttons.class
Malnorma
 
Posts: 123
Joined: October 14th, 2015, 9:26 am

Next

Return to Developers

Who is online

Users browsing this forum: Baidu [Spider] and 0 guests