New scripting language support: Java

Started by maxorator, Apr 02, 2016, 10:11 PM

Previous topic - Next topic

.

#15
Quote from: maxorator on Apr 04, 2016, 08:08 PM... Sure you can throw together a few simple lines of Javascript without much knowledge about it, but the moment you try to do something more complex, you will face issues that are orders of magnitude more difficult to understand than what a beginner Java developer has to deal with. ...

That's what I've been telling to every god damn hippie that comes to my face once in a while and starts bragging about certain scripting languages where it's so damn easy to write "hello world" applications in them. But as soon as you bring some complexity you start hitting walls like you're in the middle of a maze.

Yeah, I like dynamically typed languages for playing around and doing simple tasks quick and dirty. But anything beyond that and I start loosing my mind.
.

NewK

#16
This is great and will bring a whole new world of possibilities previously not possible in Squirrel.


I've tried the demo you provided and it was really helpful, it's pretty much all that one needs to understand how to make a script using the plugin. For me, everything's worked so far with no issues, except for that bug with the reload script method which you've already fixed in version 67 of the plugin. The debbuger support is also working great aswell. :))


I'm not sure if you got my last IRC question last night so I'm just going repeat it here. I asked if you had given any tought yet about how/if you're going to implement Squirrel's compilestring() function. From my understanding this is a native Squirrel function so it's not really so much of a feature from the Squirrel vcmp plugin but more of a language feature itself (proficient squirrel scripters do correct me, if I'm wrong about this assumption). Since Java's a statically typed language and all, it doesn't have such a feature which is normal and completely expected. However if you're willing to implement this I'd suggest using BeanShell which is a java interpreter. I don't know if you've used it before or not, so you may already know what I'm talking about, but I'm currently using it at work for a workflow/bpm application and it's been working great so far. And it's really easy to use aswell, I've been doing some testing using it on this java plugin inside the onPlayerCommand() event and I got it working from ingame very easily with just a few lines. I just passed the main class instance that extends the RootEventHandler (   in your demo that was : ModeEventHandler.java ) to the beanshell interpreter and I was able to call it successfully from ingame while maintaining the context of the class and with the ability to call any methods or global variables inside it. The only reason I ask for this, is because this compilestring() function on squirrel has become such a popular and useful command in alot of servers. Going inside the server and calling /exec <code> to fix a bug rather than restarting the whole server or even running the command from IRC instead, it's really useful, server scripters can access any of the script's global variables from inside the game and manipulate them how they wish,  aswell as any other functions they want to access, it's like a live command line directly into their server/script. It would be great to have this with this plugin aswell, and with BeanShell it's definitely a possibility. If you believe that it should not belong inside the java plugin itself I guess that's fine too since it can be implemented directly on the server script instead like I did. My idea for having it exist as a feature on the plugin instead was just to make it easier for whoever decides to make the  transition from the squirrel plugin to this one.

I've been testing more stuff on a blank script and I haven't found any more bugs yet but I'll let you know if I do. I was never really an avid vcmp scripter, mostly due to just lack of interest. I've played with some scripts from time to time and I can understand it just fine, but I'm not really a fan of dynamically typed languages so Squirrel was just never that appealing to me (not enough for me to get interested in "vcmp scripting" anyway) , despite being a pretty good and easy language to work with. Having that said, this java plugin interests me alot, and enough to actually get involved with "vcmp scripting". I've been working with java for quite sometime now, I use it at my job and for personal projects aswell, so this is alot more appealing to me.  I'm familiar with  the tools/frameworks you mentioned so I guess what I'm really trying to say here is that my feedback will come more from the point of view of a regular java developer rather than a  vcmp scripter, so forgive me if I anything  miss any obvious stuff :P



now for the "Java" discussion

Java is a very underrated language, it gets alof of flak for being verbose but this is not 2006 anymore, Java's no longer as verbose as it once was, heck, local-variable type Inference might actually come to happen in JDK9!  It has evolved alot and still managed keep portability without breaking stuff. However, no mattter how much the language seems to evolve, some people still hold a false, past notion of what Java is today. Modern Java is great, it's portable, it's fast and focuses alot on code readability which is why it's accused of being verbose in the first place, but code readability matters alot, I'd rather have some long ass method name which purpose I can understand by simply reading its name, rather than having a cryptic 4 letter method name to which I have no idea what it does. My first server-side language was PHP and I later moved on to Java and got the amazing opportunity of working for/to some very very big multinational companies (some of them, Industry leaders on their areas) and this allowed me to have some perspective into how and where Java is being used and how it relates to the concepts from the new trendy languages like node.js for instance. I still use PHP once in a while (if I really really have to) and node.js too, they're languages I've used for small and simple projects before, but I'd never dream of doing the work I do daily with some of these big corporate webapps with those languages. And the fact that we can now use a language such as Java for scripting VCMP servers is amazing, the ammount of doors this opens up for server owners is something to look up to. The Java community is huge, there's  excelent tooling, extensive documentation about pretty much everything, and a whole world of libraries out there to pick from. And the best of it all and most satisfying for me is that is scales beautifully. A properly organized Java project from the start is very easy to maintain while keeping your sanity intact.

DizzasTeR

I'm very much convinced for Java now, I'll see how it goes, thanks for the suggestions @maxorator.

ysc3839

Quote from: S.L.C on Apr 04, 2016, 03:50 PM
Quote from: ysc3839 on Apr 04, 2016, 03:27 PMI think node.js(JavaScript) would be better.

Node.js thrives on asynchronous I/O and that's something where VCMP doesn't really perform well. Besides, Node.js is not really what you intended to say there (I think). I believe you mean the V8 JavaScript engine on which Node.js was build. But let me stop you there for a second and tell you that compiling and distributing almost any decent JavaScript engine that features JIT (link) is not an easy task. Because those were probably created to be embedded into specific web-browsers and not into any kind application. At least not without great effort.



Quote from: sseebbyy on Apr 04, 2016, 03:24 PMIs java scripting more efficient than squirrel scripting ?

Much more efficient. Because Java is very similar to C++, meaning that you have statically typed language with a strong emphasis on OOP. Let me ask you another thing. How many times have you misspelled a variable name and then your Squirrel script compiled just fine, only to screw you later when that code was actually executed. How much code do you actually write to test if a variable is what you're expecting it to be? Well, those are the horrors that come with a dynamically typed language such as Squirrel (also known as weakly typed). While they look good on paper and help with quick prototyping. When it comes to the actual development, they're a pain in the a$$.

But that's not what makes Java more efficient in therms of performance (if that's what you mean). What makes Java more efficient is the JIT (just in time) compiler that comes with the JVM (java virtual machine). Basically, when you execute a piece of Java code, that code is transformed into machine code so you get the best performance possible. Which means you get the benefits of C++ that means you get fast native machine instructions but also the benefits of a scripting language that means you don't have to compile your code before you run it. Well, technically, you still compile it but it's a different story than with C++.

But probably the biggest benefit of all is the plethora of libraries out there. You don't have to worry about asking someone to make a plugin for MySQL/SQLite/PostgreSQL etc. or any kind of utility out there. You have a vast library of things to pick and use in your project. Not to mention the amount of utilities, IDEs and debugging software that you have at your disposal.

The only downside of Java is that you need to have a decent amount of programming and software development experience to use it. It's no longer just a few clicks to get you started. You need to know sh!t before you get started with something like Java.
I remembered there's some program communicate with node.exe directly. Maybe in this way we don't need to build it, just use it.

.

#19
Quote from: ysc3839 on Apr 06, 2016, 03:29 PMI remembered there's some program communicate with node.exe directly. Maybe in this way we don't need to build it, just use it.

Yeah, but that will introduce latency. If you don't care about too much latency then you could implement something like inter process communication or some kind of shared memory. But either way, that's going to bring some nasty latency. Imagine you call 20 VC:MP function calls and each function call has a latency of 1ms (AT BEST!). You just added 20ms of extra time to every event. Imagine a loop that calls FindPlayer(...) which is quite common. Where does that get you?

This brings us back to the era of IRC scripting. Or so I've been told. I wasn't around when that was "popular".
.

NewK

Quote from: ysc3839 on Apr 06, 2016, 03:29 PMI remembered there's some program communicate with node.exe directly. Maybe in this way we don't need to build it, just use it.
The JVM has had a fully embedded Javascript engine in it since 2006 (Java 6) , it's capable of running javascript code and it has the capability  to make interactions between both languages (javascript->java and java->javascript), like passing Java variables to the Javascript engine and viceversa. With the release of jdk7, "Rhino" was released which is another javascript engine similar to the one in jdk6 but with a few more optimizations. And with jdk8 there's another one called "Nashorn" with more optimizations and it's capable of running full featured node.js apps with the use of an aditional Java library which I now don't remember the name of, but if you google it, there's tons of tutorials about using node.js inside the JVM. Although I don't understand why would one even want to do this in the first place.... also this "Nashorn" engine is obviously not as fast as V8, I remember reading somewhere on a benchmark that it was about 2 times slower than Google's V8, which is normal since the JVM was never meant to execute Javascript code in the first place.


BABA1

#21
Does that mean we can compile the scripts using JIT Compiler and then hand it over to someone with JVM installed, so that he/she wouldn't get the source code but will be able to host the server?
@maxorator, don't you think Eclipse else BlueJ would be a good IDE to start with, why shall we use IntelliJ?

maxorator

Quote from: BABA1 on Apr 06, 2016, 06:42 PMDoes that mean we can compile the scripts using JIT Compiler and then hand it over to someone with JVM installed, so that he/she wouldn't get the source code but will be able to host the server?
No. You will compile to class files (which are easily decompilable) with the usual compiler. JIT compiler is a different thing, its name comes from Just In Time, which means it compiles during runtime. You cannot compile to machine code in advance.

Quote from: BABA1 on Apr 06, 2016, 06:42 PM@maxorator, don't you think Eclipse else BlueJ would be a good IDE to start with, why shall we use IntelliJ?
Because it's better. Seriously. Google switched their Android Studio from Eclipse to IntelliJ a few years ago. At a Java conference last year, we were asked what IDE we used at a talk, pretty much everyone used IntelliJ and there were just a few Eclipse and Netbeans users. I also like it the best, and I used Eclipse before I tried IntelliJ. About BlueJ, I haven't tried it, but it seems quite primitive.

Gudio

Finally my favourite language is supported here. :)

KAKAN

Well, I'm pretty new to Java and just use eclipse to make some basic applications. I want to ask how would you import some .jar files to your project?
Can you give us a .jar file that would contain the basic commands you made. I want to see the structure of the .jar files. I would really appreciate your help :)
oh no

BABA1

I want to know all the functions like sendClientMessage and etc. Where can I find those functions? I'm not quite acquainted with the plugin. I want a list of functions and something about their formal parameters type in the method signature. I would really appreciate some help! 

maxorator

#26
Quote from: KAKAN on Apr 07, 2016, 11:05 AMWell, I'm pretty new to Java and just use eclipse to make some basic applications. I want to ask how would you import some .jar files to your project?
Via gradle dependencies. Look at this for example: https://bitbucket.org/maxorator/vcmp-java-plugin-demo . There, if you want to add some new library to your project, then go to http://search.maven.org/ and find whatever you're looking for. For example if you want MySQL, then you search there for "mysql", then click on the version number on the line with groupId 'mysql' and artifactId 'mysql-connector-java'. On that page, on the left, there is a list of build tools (maven, gradle), pick "Gradle/Grails" from there and copy the line compile 'mysql:mysql-connector-java:5.1.38' near the other lines like that in the build.gradle file of the project.

Not sure if you've managed to load this project into IntelliJ yet, but to do that, you have to open it by selecting "File->New->Project from existing sources", select the project directory, then select "Gradle" from the list, then don't touch any settings and just click "Finish" and wait a bit. After it shows the project, it will still take a bit of time to download the dependencies and the code will look all red until it has finished.

If this is your first time touching IntelliJ, you should get familiar with its shortcuts. You can see a more complete list here: https://resources.jetbrains.com/assets/products/intellij-idea/IntelliJIDEA_ReferenceCard.pdf , but I find that the most important ones are:
  • Alt+Enter - Auto... everything. Whenever there's a problem, alt+enter will try to suggest a fix for it which it can automatically apply. For example if you are using a class you haven't imported, it will offer you the option to import that class (and select which one if there are multiple options).
  • Ctrl+B - Goes to the definition of whatever you have selected
  • Ctrl+N - Search for a class
  • Ctrl+Shift+N - Search for a file
  • Alt+F7 - Find all references to whatever you have selected
  • Shift+F6 - Rename the selected variable/class/method/field.
  • Ctrl+O - Gives you a list of superclass methods which you can override. For example in the event handler, gives you a list of all events that you can handle, and selecting them will create the methods for that.
  • Alt+Insert - Code generator, offers you the option to automatically create constructors, setters, getters.

Quote from: KAKAN on Apr 07, 2016, 11:05 AMCan you give us a .jar file that would contain the basic commands you made. I want to see the structure of the .jar files. I would really appreciate your help :)
Not sure what you mean. If you mean how to create the jar file to use with the plugin (as classPath) in your configuration file, then open the Gradle panel from the menu with View->Tool Windows->Gradle and from there you can see a list of tasks. What you want is the Tasks->shadow->shadowJar task. If you run that task, it will generate the jar file to build/libs/vcmp-java-plugin-demo-0.0.1-all.jar in the project directory.

Actually you don't need an IDE to compile the project, but actually changing something in the code without an IDE would be a pain. Anyway, you can just run the same task from the command line by typing "gradlew shadowJar".

maxorator

Quote from: BABA1 on Apr 07, 2016, 03:51 PMI want to know all the functions like sendClientMessage and etc. Where can I find those functions? I'm not quite acquainted with the plugin. I want a list of functions and something about their formal parameters type in the method signature. I would really appreciate some help! 
There is an instance of Server in the initial event handler class. It contains all those functions:
com/maxorator/vcmp/java/plugin/integration/server/Server.java

Same thing exists for players, vehicles, etc:
com/maxorator/vcmp/java/plugin/integration/player/Player.java
com/maxorator/vcmp/java/plugin/integration/vehicle/Vehicle.java

If you create another class for handling events and commands, you need to pass the Server instance to it via the contructor, like here:
com/maxorator/vcmp/mode/jdbctest/RegistrationManager.java

The best option to split events and commands into separate classes is to use the DelegatingEventHandler, so use this project as an example: https://bitbucket.org/maxorator/vcmp-java-plugin-demo .

Klaus

As a Java programmer myself, I'm glad to see this.

NewK

For anyone that's starting with Java now, I would advise to stay clear of Eclipse. Either use IntelliJ or Netbeans. Eclipse is the work of the devil. BlueJ I've heard about it before but never really used it either. I know some local schools in my area use it alot to teach beginner "Intro to Java"  classes. I've tried Eclipse, IntelliJ and Netbeans and I opted with Netbeans. It's a pretty good IDE which in my opinion has been forgotten by alot of Java devs which is a shame because it's a  really good IDE and also completely free. I also find it to be alot more intuitive than IntelliJ, the learning curve is not as steep for beginners.  Although I might be biased since it's the IDE I've used the longest. However, I've had to teach a few interns before with zero java experience and they always seemed to catch on quick with Netbeans.