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); }
Did /npc behaviours thief.js with success, but my citizen does nothing ! What else ?
add to Citizens/scripts/behaviours use /npc behaviours filename.js
I had to remove "List " from line 40 else the script compilation fails with a java exception.