New scripting language support: Java
[C++] Vice Racing »

NewK

  • VC:MP Beta Tester
  • Posts: 83
Re: New scripting language support: Java
« Reply #45,  »
Thank you!

One small sugestion, which would come in handy is the addition of a new utility method to the generic Entity implementation ( Entity.java and EntityImpl.java ) and that is a getDataOrDefault method. This is usually a common method in API's that deal with Object and the possible presence of null. The syntax would be something like:

getDataOrDefault(String name, Class<T> klass, Object defaultValue)

And the idea here is that if the value/data that it's retrieving is null, it would return the default value instead, the one that's being passed as a parameter.

I took a look at your EntityImpl.java and this is what I had in mind:
 
Code: [Select]
   @Override
    public <T> T getDataOrDefault(String name, Class<T> klass, Object defaultValue) {
        synchronized (sync) {
            if (data == null) {
                return null;
            }

            Object value = data.get(name);

            if (klass.isInstance(defaultValue) && value == null) {
                return (T) defaultValue;
            } else if (klass.isInstance(value)) {
                return (T) value;
            }
            return null;
        }
    }
Not something super urgent, but it'll surely help writing cleaner code in the future and avoid all those pesky null checks :-X

EK.IceFlake

  • Hero Member
  • "We are the champions my fellahs" - Sufyan/VK.SuFy
  • Posts: 1,750

NewK

  • VC:MP Beta Tester
  • Posts: 83
Re: New scripting language support: Java
« Reply #47,  »

@maxorator  2 things I've noticed:

- the onPlayerCrashReport event seems to be missing. I noticed the function is there on the plugin but it's just not implemented, something you forgot maybe?

- Also noticed a really nasty bug on the data hashmap in EntityImpl.java. It would seem that sometimes the data hashmap is passed with previous data to completely new entities. For instance, if I create a new pickup with "server.createPickup(etc..)", and  attach data to it with .setData("test", "blabla.."). If I delete that pickup and create a completely new one with "server.createPickup(etc..)" sometimes this new pickup will have the data that I attached to the old one I deleted. Like if I do newPickup.getData("test", String.class), I will get "blabla..". Kinda hard to reproduce this bug but I believe it's an easy fix if we clear the data hashmap every time a new entity is created.

Seeing that maxorator is inactive, I've fixed both of these issues and decided to share the fixed versions publicly for anyone else that's thinking about doing java server development. Binaries can be found below:

Plugin (only 64bit builds for now) : https://github.com/newk5/vcmp-java-plugin/releases/tag/v75
Integration jar : https://github.com/newk5/vcmp-java-plugin-integration/releases/tag/v0.1.3

The integration.jar is to be used with tools-0.1.2.jar which you can find on thijn's maven server here