From c956a1b314074d030ac7cea9c9ff563e080eef63 Mon Sep 17 00:00:00 2001 From: deathcap Date: Sat, 9 Apr 2016 11:15:50 -0700 Subject: [PATCH 1/5] Rename main file to pickaxe.js --- package.json | 2 +- pickaxe.coffee => pickaxe.js | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename pickaxe.coffee => pickaxe.js (100%) diff --git a/package.json b/package.json index 429667e..a95df3d 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "voxel-pickaxe", "description": "pickaxe tools to help you mine (voxel.js plugin)", "version": "0.2.2", - "main": "pickaxe.coffee", + "main": "pickaxe.js", "repository": { "type": "git", "url": "git@github.com:deathcap/voxel-pickaxe.git" diff --git a/pickaxe.coffee b/pickaxe.js similarity index 100% rename from pickaxe.coffee rename to pickaxe.js From bf101f64fb01840b96a16c85bb7f638d1b52ed93 Mon Sep 17 00:00:00 2001 From: deathcap Date: Sat, 9 Apr 2016 11:25:25 -0700 Subject: [PATCH 2/5] Initial conversion to ES6 --- package.json | 8 +- pickaxe.js | 216 +++++++++++++++++++++++++++++---------------------- 2 files changed, 124 insertions(+), 100 deletions(-) diff --git a/package.json b/package.json index a95df3d..26aeb87 100644 --- a/package.json +++ b/package.json @@ -15,14 +15,8 @@ "tools" ], "dependencies": { - "coffeeify": "^1.0.0", "craftingrecipes": "^1.0.0", - "itempile": "^1.0.1" - }, - "browserify": { - "transform": [ - "coffeeify" - ] + "itempile": "^2.0.0" }, "license": "MIT" } diff --git a/pickaxe.js b/pickaxe.js index 3e0aa16..bbfaf93 100644 --- a/pickaxe.js +++ b/pickaxe.js @@ -1,126 +1,156 @@ -ItemPile = require 'itempile' -{Recipe, AmorphousRecipe, PositionalRecipe, CraftingThesaurus, RecipeLocator} = require 'craftingrecipes' +'use strict'; -module.exports = (game, opts) -> - return new ToolsPlugin(game, opts) +const ItemPile = require('itempile'); +const craftingrecipes = require('craftingrecipes'); -module.exports.pluginInfo = +const Recipe = craftingrecipes.Recipe; +const AmorphousRecipe = craftingrecipes.AmorphousRecipe; +const PositionalRecipe = craftingrecipes.PositionalRecipe; +const CraftingThesaurus = craftingrecipes.CraftingThesaurus; +const RecipeLocator = craftingrecipes.RecipeLocator; + +module.exports = (game, opts) => new ToolsPlugin(game, opts); + +module.exports.pluginInfo = { loadAfter: ['voxel-recipes', 'voxel-registry'] +}; -class ToolsPlugin - constructor: (@game, opts) -> - @registry = game.plugins?.get('voxel-registry') ? throw new Error('voxel-pickaxe requires "voxel-registry" plugin') +class ToolsPlugin { + constructor(game, opts) { + this.game = game; + + this.registry = game.plugins.get('voxel-registry'); + if (!this.registry) throw new Error('voxel-pickaxe requires "voxel-registry" plugin'); - # TODO: require/warning if voxel-mine missing? without, 'speed' property won't have any effect - @enable() + // TODO: require/warning if voxel-mine missing? without, 'speed' property won't have any effect + this.enable(); + } - enable: () -> - @registry.registerBlock 'plankOak', {displayName: 'Oak Planks', texture: 'planks_oak'} + enable() { + this.registry.registerBlock('plankOak', {displayName: 'Oak Planks', texture: 'planks_oak'}); - @registry.registerItem 'stick', {itemTexture: 'items/stick'} + this.registry.registerItem('stick', {itemTexture: 'items/stick'}); - @registry.registerItem 'pickaxeWood', {displayName: 'Wooden Pickaxe', itemTexture: 'items/wood_pickaxe', speed: 5.0, maxDamage:8, toolClass: 'pickaxe'} - @registry.registerItem 'pickaxeStone', {displayName: 'Stone Pickaxe', itemTexture: 'items/stone_pickaxe', speed: 10.0, maxDamage:128, toolClass: 'pickaxe'} - @registry.registerItem 'pickaxeIron', {displayName: 'Iron Pickaxe', itemTexture: 'items/iron_pickaxe', speed: 25.0, maxDamage:512, toolClass: 'pickaxe'} + this.registry.registerItem('pickaxeWood', {displayName: 'Wooden Pickaxe', itemTexture: 'items/wood_pickaxe', speed: 5.0, maxDamage:8, toolClass: 'pickaxe'}); + this.registry.registerItem('pickaxeStone', {displayName: 'Stone Pickaxe', itemTexture: 'items/stone_pickaxe', speed: 10.0, maxDamage:128, toolClass: 'pickaxe'}); + this.registry.registerItem('pickaxeIron', {displayName: 'Iron Pickaxe', itemTexture: 'items/iron_pickaxe', speed: 25.0, maxDamage:512, toolClass: 'pickaxe'}); - @registry.registerItem 'spadeWood', {displayName: 'Wooden Spade', itemTexture: 'items/wood_shovel', speed: 5.0, maxDamage:8, toolClass: 'spade'} - @registry.registerItem 'spadeStone', {displayName: 'Stone Spade', itemTexture: 'items/stone_shovel', speed: 10.0, maxDamage:128, toolClass: 'spade'} - @registry.registerItem 'spadeIron', {displayName: 'Iron Spade', itemTexture: 'items/iron_shovel', speed: 25.0, maxDamage:512, toolClass: 'spade'} + this.registry.registerItem('spadeWood', {displayName: 'Wooden Spade', itemTexture: 'items/wood_shovel', speed: 5.0, maxDamage:8, toolClass: 'spade'}); + this.registry.registerItem('spadeStone', {displayName: 'Stone Spade', itemTexture: 'items/stone_shovel', speed: 10.0, maxDamage:128, toolClass: 'spade'}); + this.registry.registerItem('spadeIron', {displayName: 'Iron Spade', itemTexture: 'items/iron_shovel', speed: 25.0, maxDamage:512, toolClass: 'spade'}); - @registry.registerItem 'axeWood', {displayName: 'Wooden Axe', itemTexture: 'items/wood_axe', speed: 5.0, maxDamage:8, toolClass: 'axe'} - @registry.registerItem 'axeStone', {displayName: 'Stone Axe', itemTexture: 'items/stone_axe', speed: 10.0, maxDamage:128, toolClass: 'axe'} - @registry.registerItem 'axeIron', {displayName: 'Iron Axe', itemTexture: 'items/iron_axe', speed: 25.0, maxDamage:512, toolClass: 'axe'} + this.registry.registerItem('axeWood', {displayName: 'Wooden Axe', itemTexture: 'items/wood_axe', speed: 5.0, maxDamage:8, toolClass: 'axe'}); + this.registry.registerItem('axeStone', {displayName: 'Stone Axe', itemTexture: 'items/stone_axe', speed: 10.0, maxDamage:128, toolClass: 'axe'}); + this.registry.registerItem('axeIron', {displayName: 'Iron Axe', itemTexture: 'items/iron_axe', speed: 25.0, maxDamage:512, toolClass: 'axe'}); - # recipes - recipes = @game.plugins?.get('voxel-recipes') - if recipes? - recipes.thesaurus.registerName 'wood.plank', 'plankOak' + // recipes + const recipes = this.game.plugins.get('voxel-recipes'); + if (recipes) { + recipes.thesaurus.registerName('wood.plank', 'plankOak'); - recipes.register new AmorphousRecipe(['wood.log'], new ItemPile('plankOak', 2)) - recipes.register new AmorphousRecipe(['wood.plank', 'wood.plank'], new ItemPile('stick', 4)) + recipes.register(new AmorphousRecipe(['wood.log'], new ItemPile('plankOak', 2))); + recipes.register(new AmorphousRecipe(['wood.plank', 'wood.plank'], new ItemPile('stick', 4))); - recipes.register @pickaxeRecipe('wood.plank', 'pickaxeWood') - recipes.register @pickaxeRecipe('cobblestone', 'pickaxeStone') - recipes.register @pickaxeRecipe('ingotIron', 'pickaxeIron') + recipes.register(this.pickaxeRecipe('wood.plank', 'pickaxeWood')); + recipes.register(this.pickaxeRecipe('cobblestone', 'pickaxeStone')); + recipes.register(this.pickaxeRecipe('ingotIron', 'pickaxeIron')); - recipes.register new RepairRecipe('pickaxeWood', 'plankOak', 4) - recipes.register new RepairRecipe('pickaxeStone', 'cobblestone', 20) - recipes.register new RepairRecipe('pickaxeIron', 'ingotIron', 200) + recipes.register(new RepairRecipe('pickaxeWood', 'plankOak', 4)); + recipes.register(new RepairRecipe('pickaxeStone', 'cobblestone', 20)); + recipes.register(new RepairRecipe('pickaxeIron', 'ingotIron', 200)); - recipes.register @spadeRecipe('wood.plank', 'spadeWood') - recipes.register @spadeRecipe('cobblestone', 'spadeStone') - recipes.register @spadeRecipe('ingotIron', 'spadeIron') + recipes.register(this.spadeRecipe('wood.plank', 'spadeWood')); + recipes.register(this.spadeRecipe('cobblestone', 'spadeStone')); + recipes.register(this.spadeRecipe('ingotIron', 'spadeIron')); - recipes.register @axeRecipe('wood.plank', 'axeWood') - recipes.register @axeRecipe('cobblestone', 'axeStone') - recipes.register @axeRecipe('ingotIron', 'axeIron') + recipes.register(this.axeRecipe('wood.plank', 'axeWood')); + recipes.register(this.axeRecipe('cobblestone', 'axeStone')); + recipes.register(this.axeRecipe('ingotIron', 'axeIron')); + } + } - - pickaxeRecipe: (headMaterial, toolMaterial, handleMaterial='stick') -> + pickaxeRecipe(headMaterial, toolMaterial, handleMaterial) { + if (handleMaterial === undefined) handleMaterial = 'stick'; return new PositionalRecipe([ [headMaterial, headMaterial, headMaterial], [undefined, handleMaterial, undefined], - [undefined, handleMaterial, undefined]], new ItemPile(toolMaterial, 1, {damage:0})) + [undefined, handleMaterial, undefined]], new ItemPile(toolMaterial, 1, {damage:0})); + } - spadeRecipe: (headMaterial, toolMaterial, handleMaterial='stick') -> + spadeRecipe(headMaterial, toolMaterial, handleMaterial) { + if (handleMaterial === undefined) handleMaterial = 'stick'; return new PositionalRecipe([ [headMaterial], [handleMaterial], [handleMaterial]], - new ItemPile(toolMaterial, 1, {damage:0})) + new ItemPile(toolMaterial, 1, {damage:0})); + } - axeRecipe: (headMaterial, toolMaterial, handleMaterial='stick') -> - # TODO: support mirrored recipe + axeRecipe(headMaterial, toolMaterial, handleMaterial) { + if (handleMaterial === undefined) handleMaterial = 'stick'; + // TODO: support mirrored recipe return new PositionalRecipe([ [headMaterial, headMaterial], [handleMaterial, headMaterial], - [handleMaterial, undefined]], new ItemPile(toolMaterial, 1, {damage:0})) - - - disable: () -> - # TODO - - -class RepairRecipe extends Recipe - constructor: (@toolItem, @repairingItem, @repairAmount) -> - - findMatchingSlots: (inventory) -> - # tool + repairing item, side by side in that order - foundIndices = {} - if inventory.get(0)?.item == @toolItem # TODO: thesaurus - foundIndices.tool = 0 - else - return undefined - - if inventory.get(1)?.item == @repairingItem # TODO: thesaurus - foundIndices.repairing = 1 - else - return undefined - - return foundIndices - - computeOutput: (inventory) -> - slots = @findMatchingSlots(inventory) - return undefined if not slots? - - tool = inventory.get(slots.tool) - repairedTool = tool.clone() - oldDamage = repairedTool.tags.damage ? 0 - newDamage = oldDamage - @repairAmount - newDamage = 0 if newDamage < 0 - repairedTool.tags.damage = newDamage - - return repairedTool - - # TODO: refactor with craftingrecipes module - craft: (inventory) -> - slots = @findMatchingSlots(inventory) - return undefined if !slots? - - inventory.takeAt slots.tool, 1 # TODO: check return - inventory.takeAt slots.repairing, 1 # TODO: check return + [handleMaterial, undefined]], new ItemPile(toolMaterial, 1, {damage:0})); + } + + disable() { + // TODO + } +} + +class RepairRecipe extends Recipe { + constructor(toolItem, repairingItem, repairAmount) { + this.toolItem = toolItem; + this.repairingItem = repairingItem; + this.repairAmount = repairAmount; + } + + findMatchingSlots(inventory) { + // tool + repairing item, side by side in that order + const foundIndices = {}; + if (inventory.get(0) !== undefined && inventory.get(0).item === this.toolItem) { // TODO: thesaurus + foundIndices.tool = 0; + } else { + return undefined; + } + + if (inventory.get(1) !== undefined && inventory.get(1).item === this.repairingItem) { // TODO: thesaurus + foundIndices.repairing = 1; + } else { + return undefined; + } + + return foundIndices; + } + + computeOutput(inventory) { + const slots = this.findMatchingSlots(inventory); + if (slots === undefined) return undefined; + + const tool = inventory.get(slots.tool); + const repairedTool = tool.clone(); + const oldDamage = repairedTool.tags.damage !== undefined ? repairedTool.tags.damage : 0; + let newDamage = oldDamage - this.repairAmount + if (newDamage < 0) newDamage = 0; + repairedTool.tags.damage = newDamage; + + return repairedTool; + } + + // TODO: refactor with craftingrecipes module + craft(inventory) { + const slots = this.findMatchingSlots(inventory); + if (slots === undefined) return undefined; + + inventory.takeAt(slots.tool, 1); // TODO: check return + inventory.takeAt(slots.repairing, 1); // TODO: check return + + return this.computeOutput(inventory); + } +} - return @computeOutput(inventory) From 0d739a6be9a5f1817ebbc1bb20b671a084ef120c Mon Sep 17 00:00:00 2001 From: deathcap Date: Mon, 9 May 2016 00:12:33 -0700 Subject: [PATCH 3/5] Update to craftingrecipes ^2.0.0, native ES6 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 26aeb87..fe9a81e 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "tools" ], "dependencies": { - "craftingrecipes": "^1.0.0", + "craftingrecipes": "^2.0.0", "itempile": "^2.0.0" }, "license": "MIT" From ffbc805620ff973952c4191b90c2933a45e0554e Mon Sep 17 00:00:00 2001 From: deathcap Date: Mon, 9 May 2016 01:13:52 -0700 Subject: [PATCH 4/5] Add missing super() call in RepairRecipe constructor, defines this --- pickaxe.js | 1 + 1 file changed, 1 insertion(+) diff --git a/pickaxe.js b/pickaxe.js index bbfaf93..70ec199 100644 --- a/pickaxe.js +++ b/pickaxe.js @@ -105,6 +105,7 @@ class ToolsPlugin { class RepairRecipe extends Recipe { constructor(toolItem, repairingItem, repairAmount) { + super(); this.toolItem = toolItem; this.repairingItem = repairingItem; this.repairAmount = repairAmount; From b056a223373a0c3bf3f9d1d05f20c407786b0c61 Mon Sep 17 00:00:00 2001 From: deathcap Date: Mon, 9 May 2016 01:14:08 -0700 Subject: [PATCH 5/5] 1.0.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index fe9a81e..b33485e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "voxel-pickaxe", "description": "pickaxe tools to help you mine (voxel.js plugin)", - "version": "0.2.2", + "version": "1.0.0", "main": "pickaxe.js", "repository": { "type": "git",