New scripting language support: Java

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

Previous topic - Next topic

maxorator

This is a sneak peek into a plugin that implements the bindings between Java and the server. Even though it is ready for use, I am posting it here for now to get some initial feedback and iron out some issues based on that. Also, I am not currently writing up a beginner tutorial for setting this up. Once this goes official, I will try to use any feedback and questions here to provide a proper tutorial (for setting up a project and development environment, not for the language, there are plenty of those) and an FAQ.

A very short intro:

This initial release is aimed at people who are familiar with Java and its build tools (or know how to google). Beginner tutorials will come later.

MatheuS

Liked it ;D

Why not create a plugin to support pawn? <3
if( !sucess ) tryAgain();
Thanks to the VCMP community. It was the happiest period of my life.

ysc3839

#2
Quote from: MatheuS on Apr 03, 2016, 02:13 AMLiked it ;D

Why not create a plugin to support pawn? <3
Because Pawn is not suitable for server scripting. @Stormeus has said about it.

Stormeus

No one on the dev team is going to support Pawn ever again. Stop asking.

KAKAN

#4
Wow! That's awesome, now my bro would easily make a server xd
:edit:
Quote from: maxorator on Apr 02, 2016, 10:11 PM....Once this goes official....
What do you mean? Do you mean that you're going to remove the squirrel plugin?
oh no

DizzasTeR

No @KAKAN, He means that this is an initial release of the plugin which is in beta sort of. So people with java skills (like your brother as you said) will probably use it to script a server and give feedback to maxorator. Once satisfied this will be included in the plugins list just like the other official plugins.

Thijn

Quote from: vito on Apr 03, 2016, 08:38 AMIs it the squirrel will be keeping support? (updates)
Of course it will.

maxorator

Quote from: Doom_Kill3R on Apr 03, 2016, 05:50 AMNo @KAKAN, He means that this is an initial release of the plugin which is in beta sort of. So people with java skills (like your brother as you said) will probably use it to script a server and give feedback to maxorator. Once satisfied this will be included in the plugins list just like the other official plugins.
This is correct. This plugin doesn't affect the squirrel plugin in any way.

Answering a question from IRC:
Quote    public boolean onPlayerCommand(Player player, String message) {
        return commandRegistry.processCommand(player, message);
    }
Can you explain to me what is the work of this statement?
I have included a class called CommandRegistry, which will automatically call the command methods defined with the @Command annotation in classes that have been added to the CommandRegistry. In the examples I have this in the event handler constructor:
commandRegistry = new CommandRegistry(server);This in the onServerLoadScripts:
commandRegistry.addController(new OtherCommands(server));And now when the player types in /breakcar <vehicleid>, then this method in OtherCommands gets called when you call commandRegistry.processCommand:
    @Command
    public void breakCar(Player player, Vehicle vehicle) {
        VehicleDamage damage = vehicle.getDamage();
        damage.setDoorStatus(VehicleDamage.Door.Bonnet, VehicleDamage.DoorStatus.Flapping);
        damage.setTyreStatus(VehicleDamage.Tyre.LeftRear, VehicleDamage.TyreStatus.Flat);
        damage.setPanelStatus(VehicleDamage.Panel.Windscreen, VehicleDamage.PanelStatus.Damaged);
        vehicle.setDamage(damage);

        server.sendClientMessage(player, COLOUR_YELLOWISH, "Broke that car.");
    }

The DelegatingEventHandler used in the demo gamemode already has the command handling built into it, so you don't have to call commandRegistry.processCommand manually. Calling delegatingEventHandler.add will make both command and callback methods from the specified class to be called.

I have updated the command examples to also use DelegatingEventHandler, since it's easier to use that instead of passing around events and calling timer/command processing manually.

Sebastian

#8
I have no idea about how java works, so that's the reason of the next question:
Is java scripting more efficient than squirrel scripting ?
I didn't hear of any other mp mod to use JAVA, which is just a great idea !
Some scripters may be more familiar with it; and while working on a project they are actually boosting their skills, which again, is just great. JAVA is pretty used, so there are many scripters.
Good job !

ysc3839

Quote from: sseebbyy on Apr 04, 2016, 03:24 PMI have no idea about how java works, so that's the reason of the next question:
Is java scripting more efficient than squirrel scripting ?
I didn't hear of any other mp mod to use JAVA, which is just a great idea ! Some scripters may be more familiar with it. :)
Good job !
I think node.js(JavaScript) would be better.

Xmair


Credits to Boystang!

VU Full Member | VCDC 6 Coordinator & Scripter | EG A/D Contributor | Developer of VCCNR | Developer of KTB | Ex-Scripter of EAD

KAKAN

Quote from: Xmair on Apr 04, 2016, 03:33 PMMinecraft uses java.
Yeah. Java was mainly made for cross-compatibility. You don't have to compile everytime you need to run it in another OS.
Quote from: ysc3839 on Apr 04, 2016, 03:27 PM
Quote from: sseebbyy on Apr 04, 2016, 03:24 PMI have no idea about how java works, so that's the reason of the next question:
Is java scripting more efficient than squirrel scripting ?
I didn't hear of any other mp mod to use JAVA, which is just a great idea ! Some scripters may be more familiar with it. :)
Good job !
I think node.js(JavaScript) would be better.
That's easy, not better. I think :P
oh no

.

#12
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.
.

DizzasTeR


maxorator

#14
As has been said before, Java has some major advantages:

It's fast
To have a language that runs fast, it must either be efficiently compiled to machine code directly (C, C++, Rust), or run on a virtual machine with a good JIT. Currently there aren't that many good JIT-s. Java and .NET have really good ones, Javascript (V8), Lua also have decent ones.

It's statically typed
What this means is that you can't compile code that has syntax errors or tries to use variables or functions that you haven't defined, or have any of other hundreds of errors that only reveal themselves during runtime in dynamic languages. In fact, if you use a proper IDE, it will already show you errors as you type the code and tell you what's wrong there.

It's well structured
This is a bit related to the previous point. It makes it simple to properly divide your application into logical parts. It forces you to use classes. It has well established naming conventions. Also, it doesn't overload you with too many language features - it has those which are necessary to have at least one good way to do everything. It is a lot easier to start writing good code in Java than in many other languages.

It has good tools
Being statically typed and having very strict structure makes it easy for IDEs to do a lot for you. While Java might look very verbose (uses a lot of keywords, longer code than many dynamically typed languages), most Java developers actually don't type more than people who use dynamic languages. That's because IDEs are very good at autocompletion, code generation and refactoring. Also, it has good debugging and build tools.

It has many libraries
Whatever feature you can think of, most likely there's a library out there that does it for you. Adding them is as simple as adding a line to your build script.

It has a large community
Java is actually one of the most used programming languages. According to some sources, it is currently the most used language. You can find active support forums and chatrooms for it. If you have a problem, there are most likely many examples, tutorials or forum topics already about it somewhere.

It's in high demand
Continuing from the previous point, the market for Java is huge. Every big company is very likely to have an internal system that's running Java, either ordered the project from another company and just have a small team to maintain it, or they have their own in-house development team. There is no need to worry about your knowledge about Java being useless in the long run. It has been used for a long time and will be used for a long time.

It's actually quite simple
Feature-wise Java is a pretty minimalistic language. Instead of throwing in feature after feature, they have concentrated on carefully selecting the features that are the most optimal without littering the language with dozens of different ways to do the same thing. I would actually say that it is way simpler than Javascript. 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.

There is currently only one other (set of) managed languages that shares most of the same benefits, and that is C#. However, it isn't yet as well supported on other platforms than Windows, and personally there are things that I think are done better in Java than C#, although I'm biased since I have worked with Java for several years now.

Java doesn't focus on trying to give you a quick start without any tools and proper structure. You are not going to have a good time when you try to start writing it in Notepad, that's not how it's meant to be done. You will have to choose the right tools and set up the structure of your project properly. And from that point on (assuming you got the right tools for the job), things will be very simple.

For anyone who is starting to look into Java, I have two suggestions:
  • Start with IntelliJ Idea as your IDE
  • Use Gradle as your build tool
These are the current industry standards. They are designed to make your life easy.