Skip to content
Etternal edited this page Dec 7, 2024 · 3 revisions

Since ECMA Script can interact with Java code, it is possible to deal damage using Fabled damage classifiers like so:

var amount = 1;
var classification = "Piercing";
var Getskill = "Piercing-Base";

//---------------------------------------------------
var Bukkit = Java.type('org.bukkit.Bukkit');
var Class = Java.type('java.lang.Class');
var UUID = Java.type('java.util.UUID'); // Import the UUID class
var LivingEntity = Java.type('org.bukkit.entity.LivingEntity'); // Import the LivingEntity class

// Cooldown variable to prevent multiple attacks in succession
var lastAttackTime = {};

var Plugin = {
    type: function(pluginName, pluginClass) {
        var plugin = Bukkit.getPluginManager().getPlugin(pluginName);
        if (plugin == null) {
            print("Error: Plugin " + pluginName + " not found or is not loaded.");
            return null;
        }
        var classLoader = plugin.getClass().getClassLoader();
        return Class.forName(pluginClass, true, classLoader).static;
    }
};

function attack(e) {
    try {
        if (e.target == null) {
            print("Error: The target (e.target) is null.");
            return; 
        }

        var npcUuidString = e.target.getUUID(); // Get the UUID as a string
        if (npcUuidString == null || npcUuidString.isEmpty()) {
            print("Error: Could not obtain the target's UUID.");
            return; 
        }

        // Add cooldown per player
        var playerName = e.player.getName();
        var currentTime = new Date().getTime();
        if (lastAttackTime[playerName] && currentTime - lastAttackTime[playerName] < 1000) { // 1 second cooldown
            print("Active cooldown, attack ignored.");
            return;
        }
        lastAttackTime[playerName] = currentTime; // Update the last attack time

        var npcUuid = UUID.fromString(npcUuidString); // Convert the string to UUID
        if (npcUuid == null) {
            print("Error: Invalid UUID or conversion failed.");
            return; 
        }

        var Fabled = Plugin.type('Fabled', 'studio.magemonkey.fabled.Fabled'); // Get the Fabled class
        if (Fabled == null) {
            print("Error: Fabled plugin not found or loaded.");
            return; 
        }

        // Retrieve the Minecraft entity associated with the NPC using the UUID
        var mcNpc = Bukkit.getServer().getEntity(npcUuid);
        if (mcNpc == null) {
            print("Error: No entity found with the provided UUID.");
            return;
        }
        if (!(mcNpc instanceof LivingEntity)) {
            print("Error: The retrieved entity is not a LivingEntity.");
            return;
        }

        // Find the player by name using the Bukkit API
        var bukkitPlayer = Bukkit.getPlayer(e.player.getName());
        if (bukkitPlayer == null) {
            print("Error: Player not found.");
            return;
        }

        // Use Fabled.getSkill().damage to deal damage
        var skill = Fabled.getSkill(Getskill);
        if (skill == null) {
            print("Error: Skill 'Piercing-Base' not found.");
            return;
        }

        // Apply damage to the entity if everything is correct
        skill.damage(mcNpc, amount, bukkitPlayer, classification);
        print("Damage applied successfully.");
    } catch (err) {
        print("Unexpected error: " + err.message); // Catch any unexpected error and print the error message
    }
}

you can get a value from an attribute using this:

function interact(t) {
    // Import the necessary classes
    var Bukkit = Java.type("org.bukkit.Bukkit");
    var Class = Java.type("java.lang.Class");

    var attributeKey = "Vitality";

    // Function to dynamically load a class from the Fabled plugin
    function loadFabledClass(className) {
        var plugin = Bukkit.getPluginManager().getPlugin("Fabled");
        if (!plugin) {
            t.player.message("Error: Fabled plugin not found or not loaded.");
            return null;
        }
        var classLoader = plugin.getClass().getClassLoader();
        try {
            return Class.forName(className, true, classLoader).static;
        } catch (error) {
            t.player.message("Error loading class " + className + ": " + error.message);
            return null;
        }
    }

    // Load classes from the Fabled API
    var Fabled = loadFabledClass("studio.magemonkey.fabled.Fabled");
    var PlayerAttributeModifier = loadFabledClass("studio.magemonkey.fabled.api.player.PlayerAttributeModifier");
    var Operation = loadFabledClass("studio.magemonkey.fabled.api.enums.Operation");

    if (!Fabled || !PlayerAttributeModifier || !Operation) {
        t.player.message("Error: Some Fabled plugin classes could not be loaded.");
        return;
    }

    // Get the player who interacted with the NPC
    var player = Bukkit.getPlayer(t.player.getName());
    if (!player) {
        t.player.message("Error: Could not find the player.");
        return;
    }

    // Retrieve player data through the Fabled API
    var data = Fabled.getData(player);
    if (!data) {
        t.player.message("Error: Could not access player data.");
        return;
    }
    // Get and display the current attribute value
    var currentAttributeValue = data.getAttribute(attributeKey);
    if (currentAttributeValue !== null) {
        t.player.message("The current value of the attribute " + attributeKey + " is: " + currentAttributeValue);
    } else {
        t.player.message("Error: Attribute " + attributeKey + " not found.");
    }
}

and you can use this to increase, decrease or multiply an attribute

function interact(t) {
    var Bukkit = Java.type("org.bukkit.Bukkit");
    var Class = Java.type("java.lang.Class");
    
    var attributeKey = "Vitality";


    // Function to dynamically load a class from the Fabled plugin
    function loadFabledClass(className) {
        var plugin = Bukkit.getPluginManager().getPlugin("Fabled");
        return plugin ? Class.forName(className, true, plugin.getClass().getClassLoader()).static : null;
    }

    // Load classes from the Fabled API
    var Fabled = loadFabledClass("studio.magemonkey.fabled.Fabled");
    var PlayerAttributeModifier = loadFabledClass("studio.magemonkey.fabled.api.player.PlayerAttributeModifier");
    var Operation = loadFabledClass("studio.magemonkey.fabled.api.enums.Operation");

    if (!Fabled || !PlayerAttributeModifier || !Operation) return;

    // Get the player who interacted with the NPC
    var player = Bukkit.getPlayer(t.player.getName());
    if (!player) return;

    // Retrieve player data through the Fabled API
    var data = Fabled.getData(player);
    if (!data) return;

    // Add a modifier to the specified attribute
    var modifier = new PlayerAttributeModifier(attributeKey, 10, Operation.ADD_NUMBER, false);
    data.addAttributeModifier(attributeKey, modifier, true);
}