From 06d80171fb390bcdc636678180dc01490deac6a5 Mon Sep 17 00:00:00 2001 From: walterhiggins Date: Tue, 24 Mar 2015 19:37:44 +0000 Subject: [PATCH] Added inventory module for bukkit too --- docs/API-Reference.md | 2 -- src/main/js/modules/bukkit/inventory.js | 18 ++++++++++++++++++ src/main/js/modules/bukkit/items.js | 9 +++++---- src/main/js/modules/canary/inventory.js | 20 ++++++++++++++++++++ src/main/js/modules/inventory/index.js | 25 ++++--------------------- src/main/js/plugins/arrows.js | 1 - 6 files changed, 47 insertions(+), 28 deletions(-) create mode 100644 src/main/js/modules/bukkit/inventory.js create mode 100644 src/main/js/modules/canary/inventory.js diff --git a/docs/API-Reference.md b/docs/API-Reference.md index 2d7272b..a329ff7 100644 --- a/docs/API-Reference.md +++ b/docs/API-Reference.md @@ -4603,8 +4603,6 @@ The inventory module exposes a single function which when passed a player or NPC * remove : removes items from the inventory (Expects parameters of type `net.canarymod.api.inventory.Item` - I strongly recommend using the `items` module for constructing items) * contains : checks to see if there is the specified type and amount of item in the inventory (Expects parameters of type `net.canarymod.api.inventory.Item` - I strongly recommend using the `items` module for constructing items) -The Inventory module currently only works with CanaryMod. - ## Classroom Plugin The `classroom` object contains a couple of utility functions for use diff --git a/src/main/js/modules/bukkit/inventory.js b/src/main/js/modules/bukkit/inventory.js new file mode 100644 index 0000000..bba4fea --- /dev/null +++ b/src/main/js/modules/bukkit/inventory.js @@ -0,0 +1,18 @@ +function inventory(entity){ + var inv = entity.inventory; + var result = { + add: function(items){ + inv.addItem([items]); + return result; + }, + remove: function(items){ + inv.removeItem([items]); + return result; + }, + contains: function(items){ + return inv['contains(org.bukkit.inventory.ItemStack)'](items); + } + }; + return result; +} +module.exports = inventory; diff --git a/src/main/js/modules/bukkit/items.js b/src/main/js/modules/bukkit/items.js index 8631fb3..7415295 100644 --- a/src/main/js/modules/bukkit/items.js +++ b/src/main/js/modules/bukkit/items.js @@ -1,11 +1,12 @@ -var bkItemStack = org.bukkit.inventory.ItemStack; - +/*global require, module, Packages */ +var bkItemStack = Packages.org.bukkit.inventory.ItemStack; +var bkMaterial = Packages.org.bukkit.Material; var items = function( material, amount ) { material = material.toUpperCase(); - return new bkItemStack(bukkit.material[material],amount); + return new bkItemStack(bkMaterial[material],amount); }; -var materials = bukkit.material.values(); +var materials = bkMaterial.values(); for (var i = 0;i < materials.length; i++ ){ var name = (''+materials[i].name()).toLowerCase(); diff --git a/src/main/js/modules/canary/inventory.js b/src/main/js/modules/canary/inventory.js new file mode 100644 index 0000000..4a0d25c --- /dev/null +++ b/src/main/js/modules/canary/inventory.js @@ -0,0 +1,20 @@ +function inventory(entity){ + var inv = entity.inventory; + var result = { + add: function(items){ + inv['addItem(net.canarymod.api.inventory.Item)'](items); + return result; + }, + remove: function(items){ + inv['decreaseItemStackSize(int, int)'](items.id, items.amount); + return result; + }, + contains: function(items){ + var type = items.type; + var amount = items.amount; + return inv['hasItemStack(ItemType, int )'](type, amount); + } + }; + return result; +} +module.exports = inventory; diff --git a/src/main/js/modules/inventory/index.js b/src/main/js/modules/inventory/index.js index 4d17ade..72583dc 100644 --- a/src/main/js/modules/inventory/index.js +++ b/src/main/js/modules/inventory/index.js @@ -35,26 +35,9 @@ The inventory module exposes a single function which when passed a player or NPC * remove : removes items from the inventory (Expects parameters of type `net.canarymod.api.inventory.Item` - I strongly recommend using the `items` module for constructing items) * contains : checks to see if there is the specified type and amount of item in the inventory (Expects parameters of type `net.canarymod.api.inventory.Item` - I strongly recommend using the `items` module for constructing items) -The Inventory module currently only works with CanaryMod. - ***/ -function inventory(entity){ - var inv = entity.inventory; - var result = { - add: function(items){ - inv['addItem(net.canarymod.api.inventory.Item)'](items); - return result; - }, - remove: function(items){ - inv['decreaseItemStackSize(int, int)'](items.id, items.amount); - return result; - }, - contains: function(items){ - var type = items.type; - var amount = items.amount; - return inv['hasItemStack(ItemType, int )'](type, amount); - } - }; - return result; +if ( __plugin.canary ) { + module.exports = require('../canary/inventory'); +} else { + module.exports = require('../bukkit/inventory'); } -module.exports = inventory; diff --git a/src/main/js/plugins/arrows.js b/src/main/js/plugins/arrows.js index 1c841e4..4710dab 100644 --- a/src/main/js/plugins/arrows.js +++ b/src/main/js/plugins/arrows.js @@ -169,4 +169,3 @@ function onCanaryArrowHit( event ) { } } events.projectileHit( __plugin.bukkit ? onBukkitArrowHit : onCanaryArrowHit); -