Skip to content

Commit

Permalink
VanillaRecipe v5
Browse files Browse the repository at this point in the history
fixed bug on adding recipe ingredients with data 0 to modded workbench
  • Loading branch information
MineExplorer committed Jan 23, 2022
1 parent 1a6257f commit 253a563
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 64 deletions.
28 changes: 9 additions & 19 deletions declarations/VanillaRecipe.d.ts
@@ -1,30 +1,19 @@
declare namespace VanillaRecipe {
type ItemObj = {
item: string;
data?: number;
count?: number;
};
type RecipeFormat = {
type?: string;
tags?: string[];
priority?: number;
pattern?: string[];
key?: {
[key: string]: {
item: string;
data?: number;
count?: number;
};
[key: string]: ItemObj;
};
ingredients?: {
item: string;
data?: number;
count?: number;
}[];
result: {
item: string;
data?: number;
count?: number;
} | {
item: string;
data?: number;
count?: number;
}[];
ingredients?: ItemObj[];
result: ItemObj | ItemObj[];
};
export function setResourcePath(path: string): void;
export function setBehaviorPath(path: string): void;
Expand All @@ -40,4 +29,5 @@ declare namespace VanillaRecipe {
export function addShapelessRecipe(name: string, obj: RecipeFormat, addToWorkbench?: boolean): void;
export function deleteRecipe(name: string): void;
export function addStonecutterRecipe(name: string, obj: RecipeFormat): void;
export {};
}
31 changes: 17 additions & 14 deletions library/VanillaRecipe.js
Expand Up @@ -11,15 +11,15 @@ var __assign = (this && this.__assign) || function () {
};
LIBRARY({
name: "VanillaRecipe",
version: 4,
version: 5,
shared: true,
api: "CoreEngine"
});
var IS_OLD = getMCPEVersion().main === 28;
var MOD_PREFIX = "mod_";
var BEHAVIOR_NAME = "VanillaRecipe";
var VanillaRecipe;
(function (VanillaRecipe) {
var IS_OLD = getMCPEVersion().main === 28;
var MOD_PREFIX = "mod_";
var BEHAVIOR_NAME = "VanillaRecipe";
var resource_path;
var behavior_path;
var behavior_recipes_path;
Expand All @@ -28,8 +28,7 @@ var VanillaRecipe;
if (!IS_OLD)
return;
var resPath = path + "/definitions/recipe/";
if (!resource_path)
resource_path = resPath;
resource_path !== null && resource_path !== void 0 ? resource_path : (resource_path = resPath);
FileTools.mkdir(resPath);
resetRecipes(resPath);
}
Expand Down Expand Up @@ -68,8 +67,9 @@ var VanillaRecipe;
}
VanillaRecipe.resetRecipes = resetRecipes;
function recursiveDelete(file) {
if (!file.exists())
if (!file.exists()) {
return;
}
if (file.isDirectory()) {
var files = file.listFiles();
for (var i in files) {
Expand Down Expand Up @@ -145,6 +145,10 @@ var VanillaRecipe;
FileTools.WriteJSON(getFilePath(name), obj, true);
}
VanillaRecipe.generateJSONRecipe = generateJSONRecipe;
function parseItem(jsonItem) {
var _a;
return { id: getNumericID(jsonItem.item), data: (_a = jsonItem.data) !== null && _a !== void 0 ? _a : -1 };
}
function addWorkbenchRecipeFromJSON(obj) {
if (Array.isArray(obj.result)) {
Logger.Log("Recipes with multiple output are not supported in the modded workbench", "ERROR");
Expand All @@ -155,35 +159,34 @@ var VanillaRecipe;
count: obj.result.count || 1,
data: obj.result.data || 0
};
var ingredients = [];
if (obj.key) {
var ingredients = [];
for (var key in obj.key) {
ingredients.push(key);
var item = obj.key[key];
ingredients.push(getNumericID(item.item), item.data || -1);
var item = parseItem(obj.key[key]);
ingredients.push(item.id, item.data);
}
Recipes.addShaped(result, obj.pattern, ingredients);
}
else {
var ingredients = [];
obj.ingredients.forEach(function (item) {
ingredients.push({ id: getNumericID(item.item), data: item.data || 0 });
ingredients.push(parseItem(item));
});
Recipes.addShapeless(result, ingredients);
}
}
VanillaRecipe.addWorkbenchRecipeFromJSON = addWorkbenchRecipeFromJSON;
function addCraftingRecipe(name, obj, addToWorkbench) {
var _a;
var _b;
if (recipes[name])
return;
recipes[name] = true;
if (addToWorkbench)
addWorkbenchRecipeFromJSON(obj);
var type = obj.type;
obj.type = "crafting_" + obj.type;
if (!obj.tags)
obj.tags = ["crafting_table"];
(_b = obj.tags) !== null && _b !== void 0 ? _b : (obj.tags = ["crafting_table"]);
__isValid__ = true;
var items = obj.key || obj.ingredients;
for (var i in items) {
Expand Down
64 changes: 34 additions & 30 deletions source/VanillaRecipe/VanillaRecipe.ts
@@ -1,25 +1,26 @@
LIBRARY({
name: "VanillaRecipe",
version: 4,
version: 5,
shared: true,
api: "CoreEngine"
});

const IS_OLD = getMCPEVersion().main === 28;
const MOD_PREFIX = "mod_";
const BEHAVIOR_NAME = "VanillaRecipe";

namespace VanillaRecipe {
const IS_OLD = getMCPEVersion().main === 28;
const MOD_PREFIX = "mod_";
const BEHAVIOR_NAME = "VanillaRecipe";

type ItemObj = {item: string, data?: number, count?: number};
type RecipeFormat = {
type?: string,
tags?: string[],
priority?: number,
pattern?: string[],
key?: {
[key: string]: {item: string, data?: number, count?: number}
[key: string]: ItemObj
},
ingredients?: {item: string, data?: number, count?: number}[],
result: {item: string, data?: number, count?: number} | {item: string, data?: number, count?: number}[];
ingredients?: ItemObj[],
result: ItemObj | ItemObj[];
}

let resource_path: string;
Expand All @@ -30,7 +31,7 @@ namespace VanillaRecipe {
export function setResourcePath(path: string): void {
if (!IS_OLD) return;
const resPath = path + "/definitions/recipe/";
if (!resource_path) resource_path = resPath;
resource_path ??= resPath;
FileTools.mkdir(resPath);
resetRecipes(resPath);
}
Expand Down Expand Up @@ -58,21 +59,21 @@ namespace VanillaRecipe {
}

export function resetRecipes(path: string): void {
let files = FileTools.GetListOfFiles(path, "json");
const files = FileTools.GetListOfFiles(path, "json");
for (let i in files) {
let file = files[i];
const file = files[i];
if (file.getName().startsWith(MOD_PREFIX)) {
file.delete();
}
}
}

function recursiveDelete(file: java.io.File): void {
if (!file.exists())
if (!file.exists()) {
return;

}
if (file.isDirectory()) {
let files = file.listFiles();
const files = file.listFiles();
for (let i in files) {
recursiveDelete(files[i]);
}
Expand Down Expand Up @@ -113,7 +114,7 @@ namespace VanillaRecipe {
}

export function getNumericID(stringID: string): number {
let stringArray = stringID.split(":");
const stringArray = stringID.split(":");
if (stringArray.length == 1 || stringArray[0] == "minecraft") {
stringID = stringArray[stringArray.length - 1];
return VanillaBlockID[stringID] || VanillaItemID[stringID];
Expand All @@ -123,12 +124,12 @@ namespace VanillaRecipe {
return 0;
}

const nativeConvertNameID = ModAPI.requireGlobal("requireMethodFromNativeAPI('api.NativeAPI', 'convertNameId')");
const nativeConvertNameID: Function = ModAPI.requireGlobal("requireMethodFromNativeAPI('api.NativeAPI', 'convertNameId')");

let __isValid__ = true;
export function convertToVanillaID(stringID: string): string {
if (!getNumericID(stringID)) {
Logger.Log("ID " + stringID + " is invalid", "ERROR");
Logger.Log(`ID ${stringID} is invalid`, "ERROR");
__isValid__ = false;
return null;
}
Expand All @@ -138,7 +139,7 @@ namespace VanillaRecipe {
}

function generateBlankFile(recipeName: string): void {
let path = __packdir__ + "assets/definitions/recipe/" + getFileName(recipeName);
const path = __packdir__ + "assets/definitions/recipe/" + getFileName(recipeName);
FileTools.WriteText(path, '{"type": "crafting_shaped", "tags": []}');
}

Expand All @@ -147,29 +148,32 @@ namespace VanillaRecipe {
FileTools.WriteJSON(getFilePath(name), obj, true);
}

function parseItem(jsonItem: ItemObj): {id: number, data: number} {
return {id: getNumericID(jsonItem.item), data: jsonItem.data ?? -1}
}

export function addWorkbenchRecipeFromJSON(obj: RecipeFormat): void {
if (Array.isArray(obj.result)) {
Logger.Log("Recipes with multiple output are not supported in the modded workbench", "ERROR");
return;
}
var result = {
const result = {
id: getNumericID(obj.result.item),
count: obj.result.count || 1,
data: obj.result.data || 0
}
const ingredients = [];
if (obj.key) {
var ingredients = [];
for (let key in obj.key) {
ingredients.push(key);
let item = obj.key[key];
ingredients.push(getNumericID(item.item), item.data || -1);
const item = parseItem(obj.key[key]);
ingredients.push(item.id, item.data);
}
Recipes.addShaped(result, obj.pattern, ingredients)
}
else {
var ingredients = [];
obj.ingredients.forEach(function(item) {
ingredients.push({id: getNumericID(item.item), data: item.data || 0});
obj.ingredients.forEach(item => {
ingredients.push(parseItem(item))
});
Recipes.addShapeless(result, ingredients);
}
Expand All @@ -183,16 +187,16 @@ namespace VanillaRecipe {

const type = obj.type;
obj.type = "crafting_" + obj.type;
if (!obj.tags) obj.tags = [ "crafting_table" ];
obj.tags ??= [ "crafting_table" ];

__isValid__ = true;
let items = obj.key || obj.ingredients;
const items = obj.key || obj.ingredients;
for (let i in items) {
items[i].item = convertToVanillaID(items[i].item);
}
if (Array.isArray(obj.result)) {
for (let i in obj.result) {
let itemStack = obj.result[i];
const itemStack = obj.result[i];
itemStack.item = convertToVanillaID(itemStack.item);
}
}
Expand Down Expand Up @@ -232,9 +236,9 @@ namespace VanillaRecipe {
}

export function deleteRecipe(name: string): void {
let recipe = recipes[name];
const recipe = recipes[name];
if (recipe) {
let path = getFilePath(name);
const path = getFilePath(name);
new java.io.File(path).delete();
recipes[name] = false;
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Expand Up @@ -12,7 +12,7 @@
"exclude": [],
"include": [
"./library/**/*"
, "declarations/TileRender.d.ts" ],
],
"files": [
"declarations/core-engine.d.ts"
]
Expand Down

0 comments on commit 253a563

Please sign in to comment.