Skip to content
This repository has been archived by the owner on Jan 21, 2021. It is now read-only.
hb432 edited this page Jul 9, 2020 · 4 revisions

Overview | Bar | Block | Item | Entity | Location | Modifier | Vector

What's JX?

JX is a utility library designed to make dealing with minecraft-y things in JS a much simpler task. Generally, if you want to do anything fancy in Minecraft with JS, you'll be dealing with Javadocs, classes, enums, and other relatively unknown factors on a day-to-day basis -- but not with JX. At least, that's the idea.

Wrappers

When it comes down to it, JX is a lot like jQuery in that it allows for chained calls. Take this for example:

/js $(self.getItemInHand()).name('Ultra Sword').enchantments('sharpness', 10);

This one will take the item in your hand, rename it to "Ultra Sword", and add sharpness X onto it. To learn more about individual wrappers, check out the other pages listed at the top. It's just too big of a topic to cover on one page.

Events

In vanilla grakkit, the event system can a bit confusing. JX's event system tries to make a few things simpler. For example, full names like org.bukkit.event.entity.EntityRegainHealthEvent are not needed. That event could be called with the following:

$('*entityRegainHealth');

Now, this call on its own does nothing. The real fun begins with .do:

$('*entityRegainHealth').do((event) => {
   const entity = $(event.getEntity());
   console.log(`An entity of type "${entity.lifeform()}" just regained some health.`);
});

Multiple calls to .do are possible in a chain-like manner, for example:

$('*entityRegainHealth').do((event) => {
   const entity = $(event.getEntity());
   console.log(`An entity of type "${entity.lifeform()}" just regained some health.`);
}).do((event) => {
   const entity = $(event.getEntity());
   const total = entity.health() + event.getAmount();
   console.log(`And it now has ${total/2} heart${entity.health === 2 ? '' : 's'}.`);
})

Gotta love that singular/plural grammar distinction. That was sarcastic, by the way. It sucks. Grammatical frustrations aside, this is all well and good. But things get awesome when you consider this example:

$('*entityRegainHealth').do((event, store) => {
   const entity = $(event.getEntity());
   if (entity.health() < 5 && entity.lifeform() === 'player') event.setCancelled(true);
   store.entity = entity;
}).if(true).do((event, store) => {
   console.log(`An entity of type "${store.entity.lifeform()}" just regained some health.`);
})

If a player tries to heal with less than 5 health already on their player, it won't work. Fatal injury system, boys. The console log message will only display if the player is able to heal, thanks to that .if(true) call. See, .if is the other method which can be called from the event object. It adds a filter to the event chain, meaning any listeners that come after it will only receive the event if it gets through the filter. The "true" or "false" option check for cancelled events, but more complex filters can be created by passing in a function:

$('*entityRegainHealth').do((event, store) => {
   const entity = $(event.getEntity());
   if (entity.health() < 5 && entity.lifeform() === 'player') event.setCancelled(true);
   store.entity = entity;
}).if((event, store) => {
   return store.entity.lifeform() !== 'witch';
}).if(true).do((event, store) => {
   console.log(`An entity of type "${store.entity.lifeform()}" just regained some health.`);
})

Now, when witches regain health, it won't be logged to the console. All other entities will continue to log normally, and the player at less than 5 hearts functionality will still work. No potion, food, or anything will save them now. You may have also noticed the store object -- this is just simply a way to send data to and from listeners and filters. As you could imagine, data set by a listener at the end of a chain is not accessible, at least synchronously, by the one at the beginning of it. To access this "async data" you'd need to do something like this:

$('*playerChat').do((event, store) => {
   // shows "undefined"
   console.log(store.message);

   // shows the message set in the other listener
   $('+').framework().timeout(() => console.log(store.message), 0);
}).do((event, store) => {
   store.message = event.getMessage();
})

Commands

Info about commands.

How does JX work?

Given everything on this wiki, you'd assume JX is pretty complicated internally. Well, you'd be dead-on with that assumption (given thats what you actually assumed.) Just take a look at the flow chart for this thing!

The Flowchart Need I say more?

Clone this wiki locally