# Examples

{% tabs %}
{% tab title="Weapon" %}
![](https://imgur.com/txKmudn.gif)<br>

```javascript
/** 
 * @author Facuu16
 * @version 1.0.1
 * 
 * 11/11/23
 * Made for versions 1.9 onwards
 * Free use and modification!
*/

import("org.bukkit.event.block.Action");
import("org.bukkit.entity.EntityType");
import("org.bukkit.Effect");
import("org.bukkit.Sound");
import("org.bukkit.Material");

var weapon = API.newItemBuilder(API.getSkull("bb2abd66939f4cb7257a88cf52fbc6fdceec1433ec2a6ef16d62e34f6238781"))
                .setName("&bWeapon")
                .addLore("&8Right click &7to shoot!")
                .build();

var options = {
    visual: {
        particle: Effect.COLOURED_DUST,
        particleAmount: 1,
        sound: Sound.ENTITY_FIREWORK_BLAST,
        volume: 10,
        pitch: 1.8
    },

    stats: {
        damage: 5
    }
};

function build(build) {
    build.registerCommand(CommandInfo.builder()
        .name("weapon")
        .permission("mcjs.commands.weapon")
        .usage("weapon <get>")
        .target(CommandTarget.PLAYER)
        .build(), function (context) {
            var sender = context.getSender();
            var arg = context.getArg(0);

            if (arg == null) {
                API.message(sender, "&cUsage: /weapon <get>");
                return false;
            }

            switch (String(arg.toLowerCase())) {
                case "get":
                    sender.getInventory().addItem(weapon);
                    break;

                default:
                    API.message(sender, "&cUsage: /weapon <get>");
                    break;
            }

            return true;
        });

    build.registerListener("org.bukkit.event.player.PlayerInteractEvent", function (event) {
        if (!event.getAction().equals(Action.RIGHT_CLICK_AIR))
            return;
    
        var player = event.getPlayer();
    
        if (!player.getEquipment().getItemInMainHand().equals(weapon))
            return;
    
        var location = player.getLocation().clone();
        var world = location.getWorld();

        player.getWorld().playSound(location, options.visual.sound, options.visual.volume, options.visual.pitch);
        location.setY(location.getY() + 1.1);

        var vector = location.getDirection().multiply(0.35);

        for (var i = 0; i < 40; i++) {
            var current = location.add(vector);
            var block = current.getBlock();

            if (block == null)
                continue;

            if (block.getType() != Material.AIR)
                break;
            
            var entities = world.getNearbyEntities(current, 0.5, 0.5, 0.5);

            if (entities.isEmpty()) {
                world.playEffect(current, options.visual.particle, options.visual.particleAmount);
                continue;
            }

            for (var i in entities) {
                var entity = entities.get(i);

                if (entity.getType() == EntityType.PLAYER && entity.getName().equals(player.getName()))
                    continue;

                API.damage(entity, options.stats.damage);
            }
        }
    });

    build.registerListener("org.bukkit.event.block.BlockPlaceEvent", function (event) {
        if (event.getPlayer().getEquipment().getItemInMainHand().equals(weapon))
            event.setCancelled(true);
    });
}
```

\
This script adds a customizable weapon. It can be used in versions 1.9 onwards (with modifications it can be compatible with previous versions).\
\
COMMANDS:

* /weapon (mcjs.commands.weapon)
  {% endtab %}

{% tab title="Command" %}
Command Example

```javascript
function build(build) {
    build.registerCommand(CommandInfo.builder()
        .name("command")
        .aliases(["c", "cmd"])
        .description("Simple MCJS example command!")
        .permission("commands.command")
        .target(CommandTarget.ALL)
        .async(false)
        .build(), function (context) {
            API.message(context.getSender(), "&aHello World!");
            return true;
        });
}
```

{% endtab %}

{% tab title="Clickable Message" %}
Clickable Message Example

```javascript
import ("net.md_5.bungee.api.chat.ClickEvent");
import ("net.md_5.bungee.api.chat.HoverEvent");

function build(build) {
    build.registerCommand(CommandInfo.builder()
        .name("youtube")
        .aliases(["yt"])
        .permission("commands.youtube")
        .target(CommandTarget.PLAYER)
        .build(), function (context) {
            var player = context.getSender();

            API.newClickableMessage()
                .setMessage("&cClick to see my YouTube channel!")
                .setHoverEvent(HoverEvent.Action.SHOW_TEXT, "&cCLICK HERE")
                .setClickEvent(ClickEvent.Action.OPEN_URL, "https://www.youtube.com/watch?v=dQw4w9WgXcQ")
                .send(player);

            return true;
        });
}
```

{% endtab %}

{% tab title="Yaml" %}
Yaml Example

```javascript
import("java.nio.file.Paths");
import("com.facuu16.mcjs.flib.configuration.impl.Yaml");

function build(build) {
    var yaml = new Yaml(
        "yaml-example",
        Paths.get(API.getPlugin().getDataFolder().getPath(), "scripts", "storage").toString()
    );

    var data = yaml.getData();

    data.set("example", "example!");
    yaml.save();
}
```

{% endtab %}

{% tab title="Listener" %}
Listener Example

```javascript
import("org.bukkit.event.player.PlayerJoinEvent");

function build(build) {
    build.registerListener(PlayerJoinEvent, function (event) {
        API.message(event.getPlayer(), "&aWelcome to the server!");
    });
}
```

{% endtab %}

{% tab title="Prompt" %}
Prompt Example

```javascript
import("org.bukkit.Material");
import("org.bukkit.entity.Player");

function build(build) {
    build.registerCommand(CommandInfo.builder()
        .name("prompt")
        .target(CommandTarget.PLAYER)
        .build(), function (context) {
            var sender = context.getSender();

            API.newBlockPrompt()
                .setPrompt("&aBreak a block to select it!")
                .setFunction(function (player, block) {
                    API.message(sender, "&aBlock selected!");
                    return PromptResult.ACCEPT;
                })
                .then(function (player, block) {
                    API.newChatPrompt("java.lang.String")
                        .setPrompt("&aType a material name in the chat or 'cancel' to cancel...")
                        .setFunction(function (player, input) {
                            if (input.equalsIgnoreCase("cancel")) {
                                API.message(sender, "&cCanceling...");
                                return PromptResult.CANCEL;
                            }

                            try {
                                var material = Material.valueOf(input);

                                API.sendBlockChange(sender, block.getLocation(), material);
                                API.message(sender, "&aMaterial changed to: &e" + material.name());
                                return PromptResult.ACCEPT;
                            } catch (ex) {
                                API.message(sender, "&cInvalid material! Type another...");
                                return PromptResult.REJECT;
                            }
                        }).send(sender);
                }).send(sender);

            return true;
        });
}
```

{% endtab %}

{% tab title="Skull" %}
Skull Example

```javascript
function build(build) {
    build.registerCommand(CommandInfo.builder()
        .name("skull")
        .aliases(["s"])
        .description("Get a textured skull!")
        .permission("commands.skull")
        .usage("skull <minecraft-url>")
        .target(CommandTarget.PLAYER)
        .build(), function (context) {
            var sender = context.getSender();
            var url = context.getArg(0);

            if (url == null) {
                API.message(sender, "&cUsage: /skull <minecraft-url>");
                return false;
            }

            sender.getInventory().addItem(API.getSkull(url));
            API.message(sender, "&aSkull given!");
            return true;
        });
}
```

{% endtab %}

{% tab title="Task" %}
Task Example

```javascript
function build(build) {
    build.registerCommand(CommandInfo.builder()
        .name("task")
        .aliases(["t"])
        .description("MCJS task examples.")
        .permission("commands.task")
        .usage("task <task>")
        .target(CommandTarget.ALL)
        .build(), function (context) {
            var sender = context.getSender();
            var task = context.getArg(0);

            if (task == null) {
                API.message(sender, "&cUsage: /task <async, later, repeating, timer>");
                return false;
            }

            switch (String(task.toLowerCase())) {
                case "async":
                    API.runAsync(function() {
                        API.message(sender, "&aAsync task!");
                    });

                    break;

                case "later":
                    API.runLater(function() {
                        API.message(sender, "&aMessage sent after 5s!");
                    }, 100);

                    break;

                case "repeating":
                    API.runRepeating(function() {
                        API.message(sender, "&aMessage sent every 15s!");
                    }, 20, 300);

                    break;

                case "timer":
                    API.runTimer(function () {
                        API.message(sender, "&aMessage sent every 1s 5 times!");
                    }, 20, 5);

                    break;

                default:
                    API.message(sender, "&cInvalid option!");
                    break;
            }

            return true;
        });
}
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://facuu16.gitbook.io/mcjs/examples.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
