-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
130 lines (111 loc) · 3.18 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const vscode = require('vscode');
const fs = require("fs");
const path = require("path");
const TEMPORARY_ITEM = "minecraft:knowledge_book";
function ensureDir(path) {
if (!fs.existsSync(path)) {
fs.mkdirSync(path);
}
}
function activate(context) {
let disposable = vscode.commands.registerCommand('recipe-creator.createRecipe', async function () {
// Recipe name
let recipeName = await vscode.window.showInputBox({
title: "Input the name of the recipe files, do not use any spaces or special characters."
});
// Verify name was inputted
if (!recipeName) {
return;
}
// Verify name is valid
let reg = /^[a-zA-Z0-9-+._]+$/;
if (!reg.test(recipeName)) {
vscode.window.showErrorMessage("Invalid file name!");
return;
}
// Select Workspace
let folders = vscode.workspace.workspaceFolders;
if (folders.length == 0) {
vscode.window.showErrorMessage("No workspaces opened!");
return;
}
let ws = folders[0];
if (folders.length > 1) {
let result = await vscode.window.showQuickPick(
folders.map(f => f.name),
{
canPickMany: false,
title: "Which workspace?"
}
);
ws = folders.find(w => w.name == result);
}
// Ensure "data" folder
let dataPath = path.join(ws.uri.fsPath, "data");
ensureDir(dataPath);
// Select namespace
let spaces = fs.readdirSync(dataPath, { withFileTypes: true }).filter(d => d.isDirectory()).map(d => d.name);
if (spaces.length == 0) {
vscode.window.showErrorMessage("Workspace contains no namespaces!");
return;
}
let namespace = spaces[0];
if (spaces.length > 1) {
let result = await vscode.window.showQuickPick(
spaces,
{
canPickMany: false,
title: "Which namespace?"
}
);
namespace = result;
}
// Update data path and create resource variable
dataPath = path.join(dataPath, namespace);
let resource = `${namespace}:${recipeName}`;
// Ensure advancements, functions and recipes folders
ensureDir(path.join(dataPath, "advancements"));
ensureDir(path.join(dataPath, "functions"));
ensureDir(path.join(dataPath, "recipes"));
let advancement = {
criteria: {
unlocked: {
trigger: "minecraft:recipe_unlocked",
conditions: {
recipe: resource
}
}
},
rewards: {
function: resource
}
}
let recipe = {
type: "minecraft:crafting_shaped",
pattern: [" A ", "AAA", " A "],
key: {
A: {
item: "minecraft:diamond"
}
},
result: {
item: TEMPORARY_ITEM,
count: 1
}
}
let command = `recipe take @a ${resource}
advancement revoke @s only ${resource}
clear @s ${TEMPORARY_ITEM} 1
# YOUR LOGIC HERE`;
fs.writeFileSync(path.join(dataPath, "advancements", `${recipeName}.json`), JSON.stringify(advancement, null, 4));
fs.writeFileSync(path.join(dataPath, "recipes", `${recipeName}.json`), JSON.stringify(recipe, null, 4));
fs.writeFileSync(path.join(dataPath, "functions", `${recipeName}.mcfunction`), command);
vscode.window.showInformationMessage(`Recipe ${resource} has been made. Update "recipe/${recipeName}.json" and "function/${recipeName}.json" accordingly.`);
});
context.subscriptions.push(disposable);
}
function deactivate() {}
module.exports = {
activate,
deactivate
}