Thief Script


Author: fullwall
Created: 2012-09-16T00:01:31-0400
Edited: 2013-11-16T01:18:59-0500
Denizen Version: -1
Views: 433
Downloads: 969
Likes: 2


WGET
Description: JS implementation of a simple Thief goal.

importPackage(Packages.net.citizensnpcs.api.ai);
importPackage(Packages.org.bukkit.entity);
importPackage(Packages.org.bukkit.inventory);
importPackage(Packages.net.citizensnpcs.api.ai.event);
importPackage(Packages.net.citizensnpcs.api.scripting);
importPackage(java.util);
function addGoals (goals, npc) {
    var thiefGoal = {
        npc : npc,
        reset : function() {
            this.player = null;
        },
        run : function(selector) {
            if (this.player == null || !this.player.isOnline()) {
                selector.finish();
                return;
            }
            if (this.npc.getLocation().distanceSquared(this.player.getLocation()) < 2) {
                this.player.damage(1);
                var inventory = this.player.getInventory();
                for (var i = 0; i < inventory.size(); i++) {
                    var item = inventory.getItem(i);
                    if (item == null || item.getType() == Material.AIR) {
                        continue;
                    }
                    if (item.getAmount() == 1) {
                        inventory.setItem(i, null);
                    } else {
                        item.setAmount(item.getAmount() - 1);
                        inventory.setItem(i, item);
                    }
                    break;
                }
                this.player.sendMessage(npc.getName() + " Ha! Stole an item!");
                allowed = false; // wait to get home.
                this.npc.getNavigator().setTarget(this.home);
                selector.finish();
            }
        },
        shouldExecute : function(selector) {
            if (!this.npc.isSpawned() || !allowed)
                return false;
            var nearby = this.npc.getBukkitEntity().getNearbyEntities(5,5,5);
            var size = nearby.size();
            for (var i = 0; i < size; i++) {
                if (entity instanceof Player) {
                    this.player = entity;
                    this.home = this.npc.getBukkitEntity().getLocation();
                    this.npc.getNavigator().setTarget(entity, false);
                    return true;
                }
            }
            return false;
        },
        allowed : true,
        equals : function(other) {
         return this == other;
        }
    };
    var goal = new Goal(thiefGoal);
    var eventHandler = {
        handle : function(event) {
            if (event == null || event.getNPC() != goal.npc)
               return;
            goal.allowed = true;
        },
        hashCode : function() {
            return 0;
        },
        equals : function(other) {
            return this == other;
        }
    };
    events.on(NavigationCompleteEvent, new EventHandler(eventHandler));
    goals.addGoal(1, goal);
}




Comments
2012-09-28 21:53:29 - tickleman:

Did /npc behaviours thief.js with success, but my citizen does nothing ! What else ?
2012-09-17 15:24:29 - instinx:

add to Citizens/scripts/behaviours use /npc behaviours filename.js
2012-09-28 21:54:07 - tickleman:

I had to remove "List " from line 40 else the script compilation fails with a java exception.