# Disabling this will make it impossible for you to listen to events
# in your javascript's
LoadCustomEventsHandler: true
# DO NOT DISABLE! Unless you don't use the //!import feature
# and the log.(...) feature inside event handlers
UseCustomInterpreter: true
# Prints in the console, whenever a javascript has been loaded
# by default, scripts that failed to load will print in the console
PrintScriptActivations: true
# Will send messages regarding errors and warnings to operators or users who have
# the permission to use the commands from OpenJS
BroadcastToOps: true
# You can disable this, but if you use schedules inside your scripts
# you need to keep it enabled
LoadCustomScheduler: true
# Checks for updates and notifies admins and the console for an update
# you can disable this, but it is better to stay up to date
UpdateNotifications: true
# Allows usage of feature flags, disabling this can decrease script loading times
# feature flags are being used like this "//!Flagname"
# see https://www.spigotmc.org/resources/117328/field?field=documentation
# under the feature flags section for more info
AllowFeatureFlags: true
Main Command: /oj This command is used to manage JavaScript scripts within the OpenJavascript plugin.
Subcommands
Help:
Usage: /oj help
Description: Displays the usage and description of available subcommands.
Version:
Usage: /oj version
Description: Displays the version of this plugin.
Reload:
Usage: /oj reload or /oj reload <script>
Description: Reloads all scripts and the config file. If a script name is specified, reloads only that script.
Load:
Usage: /oj load <script>
Description: Loads the specified script. The script must not be disabled.
Enable:
Usage: /oj enable <script>
Description: Enables and loads the specified script.
Disable:
Usage: /oj disable <script>
Description: Disables and unloads the specified script.
List:
Usage: /oj list or /oj list enabled, /oj list disabled, /oj list not_loaded
Description: Lists all scripts. Optionally, lists only enabled, disabled, or not loaded scripts.
To create a script/javascript file you simply go inside the plugins script folder
(plugins/OpenJS/scripts), then make a js file, example "file.js" and put it there, then start coding (
I recommend using Notepad++) and make any script you want. Also
make sure that your script is loaded and enabled, if you don't know how to check that then take a look at the commands.
What is a feature flag?
With a feature flag, you can add flags at the beginning of your script and make the script use features which can bypass safety features provided by the plugin
With this feature flag at the first few lines of your script, the script will not load automatically upon plugin start or reload, it will only load if another script tries to load it or if you load it using the load command
With this feature flag at the first few lines of your script, the script will yield/pause execution until the plugin has safely been initialized
This should be used if you call the "LoadScript(scriptName)" function while the plugin is being loaded, generally you should never load a script manually while the plugin is starting or being loaded
Code (Javascript):
LoadScript
("ScriptExample.js") // this will load or reload the specified script
Code (Javascript):
UnloadScript
("ScriptExample.js") // this will unload the script, if the script is active
Code (Javascript):
waitForScript
("ScriptExample.js") // this will yield the current script calling this function, until the specified script is loaded and running
setShared(key, value) Description: Stores a variable in a public list accessible by all scripts.
Parameters:
key (String): The unique identifier for the variable.
value (Any): The value to be stored. This can be a string, number, object, function, list, class instance, etc.
getShared(key) Description: Retrieves a variable from the public list. If the variable does not exist, the function will wait until it does or timeout after 500 milliseconds.
Parameters:
key (String): The unique identifier for the variable to be retrieved.
Returns:
The value associated with the given key, or null if the variable does not exist within the timeout period.
here are quick examples:
Code (Javascript):
setShared
("testVar","Hello world!")
To read a public variable from any script do the following:
Code (Javascript):
getShared
("testVar")
It'll return the variable "Hello world!"
You can share any type of variable! Here is an example with a function: script1.js (the script that contains the function)
Code (Javascript):
var Bukkit
= org.
bukkit.
Bukkit;
// Function to broadcast the total entity count function broadcastEntityCount
(){ // Get the OpenJS plugin instance var plugin
= Bukkit.
getPluginManager().
getPlugin("OpenJS");
// Run the task asynchronously Bukkit.
getScheduler().
runTask(plugin
,new java.
lang.
Runnable({ run
:function(){ // Get the server and initialize entity count var server
= plugin.
getServer(); var entityCount
=0;
// Get all worlds on the server var worlds
= server.
getWorlds(); for(var i
=0; i
< worlds.
size(); i
++){ var world
= worlds.
get(i
); // Add the number of entities in the current world to the total count entityCount
+= world.
getEntities().
size(); }
// Broadcast the total entity count to all players server.
broadcastMessage("Total entities: "+ entityCount
); } })); }
// Call the broadcastEntityCount function immediately broadcastEntityCount
();
// Set the broadcastEntityCount function as a public variable setShared
("broadcastEntities", broadcastEntityCount
);
script2.js (The script that will access and use the public function)
Code (Javascript):
// Retrieve the public variable "broadcastEntities" and store it in a variable var broadcastEntitiesFunction
= getShared
("broadcastEntities");
// Call the broadcastEntitiesFunction broadcastEntitiesFunction
();
Notes
The getShared function will block and wait for up to 500 milliseconds for the variable to become available. If it doesn't become available within this time, it returns null.
These functions allow for inter-script communication and sharing of data, making it easier to modularize your code and keep scripts clean and focused.
To listen to an event you need to use the custom "registerEvent" function: registerEvent(Event, Function/Listener)
Code (Javascript):
registerEvent
("org.bukkit.event.block.BlockPlaceEvent",{ handleEvent
:function(event
){ event.
getPlayer().
sendMessage("You tried to place "+ event.
getBlock().
getType().
toString()); event.
setCancelled(true); } });
// Access the imported classes var Bukkit
= org.
bukkit.
Bukkit; var PlayerJoinEvent
= org.
bukkit.
event.
player.
PlayerJoinEvent; var Logger
= java.
util.
logging.
Logger;
// Get a custom Logger var logger
= Logger.
getLogger("OpenJSPluginLogger");
// Register the event listener registerEvent
(PlayerJoinEvent.
class.
getName(),{ handleEvent
:function(event
){ var player
= event.
getPlayer(); var message
="Welcome "+ player.
getName()+" to the server!"; player.
sendMessage(message
); logger.
info(message
); } });
To load and store variables we have the following functions available: loadVar(varName, defaultVar, global) saveVar(varName, variable, global)
Example usage:
Code (Javascript):
var localvar
= loadVar
("Test",0,false) // this variable will only be accessible in its current script, because it is not being stored globally var globalvar
= loadVar
("Test",0,true) // this variable is accessible using loadVar on all scripts and will return the same result
AntiCooldown script:
Code (Javascript):
registerEvent
("org.bukkit.event.player.PlayerJoinEvent",{ handleEvent
:function(event
){ var player
= event.
getPlayer() var attribute
= org.
bukkit.
attribute.
Attribute.
GENERIC_ATTACK_SPEED; var attributeInstance
= player.
getAttribute(attribute
);
if(attributeInstance
){ attributeInstance.
setBaseValue(1024);// disable's cooldown Ui on the client } log.
info("Disabled hit cooldown for Player: "+ event.
getPlayer().
getName()); } });
registerEvent
("org.bukkit.event.entity.EntityDamageByEntityEvent",{ handleEvent
:function(event
){ var e
= event.
getEntity();
// allows hit-damage each tick on the target e.
setMaximumNoDamageTicks(1); e.
setNoDamageTicks(1); } });
NoBlockPlacing script:
Code (Javascript):
registerEvent
("org.bukkit.event.block.BlockPlaceEvent",{ handleEvent
:function(event
){ event.
getPlayer().
sendMessage("You tried to place "+ event.
getBlock().
getType().
toString()); event.
setCancelled(true); } });
Running a script in the main server thread (asynchronously):
Code (Javascript):
var Bukkit
= org.
bukkit.
Bukkit;
Bukkit.
getScheduler().
runTask(Bukkit.
getPluginManager().
getPlugin("OpenJS"),new java.
lang.
Runnable({ run
:function(){ // code you want to execute in the main thread here // example: Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command); // dispatchCommand only works in the main thread, allowing the script to execute commands } }));
WhileTrue do script (yes its possible):
Code (Javascript):
function broadcastMessage
(){ plugin.
getServer().
broadcastMessage("Hello, this is a broadcast message!"); }
// Function to start the broadcast loop function startBroadcastLoop
(){ while
(true){ broadcastMessage
(); // Sleep for 10 seconds (20 ticks = 1 second in Minecraft) java.
lang.
Thread.
sleep(2000);// 10 seconds = 10,000 milliseconds = 20 ticks * 1000 milliseconds } }
// Start the broadcast loop startBroadcastLoop
();