Example #1
0
// get_craft_result(input)
int ModApiCraft::l_get_craft_result(lua_State *L)
{
	NO_MAP_LOCK_REQUIRED;

	int input_i = 1;
	std::string method_s = getstringfield_default(L, input_i, "method", "normal");
	enum CraftMethod method = (CraftMethod)getenumfield(L, input_i, "method",
				es_CraftMethod, CRAFT_METHOD_NORMAL);
	int width = 1;
	lua_getfield(L, input_i, "width");
	if(lua_isnumber(L, -1))
		width = luaL_checkinteger(L, -1);
	lua_pop(L, 1);
	lua_getfield(L, input_i, "items");
	std::vector<ItemStack> items = read_items(L, -1,getServer(L));
	lua_pop(L, 1); // items

	IGameDef *gdef = getServer(L);
	ICraftDefManager *cdef = gdef->cdef();
	CraftInput input(method, width, items);
	CraftOutput output;
	std::vector<ItemStack> output_replacements;
	bool got = cdef->getCraftResult(input, output, output_replacements, true, gdef);
	lua_newtable(L); // output table
	if (got) {
		ItemStack item;
		item.deSerialize(output.item, gdef->idef());
		LuaItemStack::create(L, item);
		lua_setfield(L, -2, "item");
		setintfield(L, -1, "time", output.time);
		push_items(L, output_replacements);
		lua_setfield(L, -2, "replacements");
	} else {
		LuaItemStack::create(L, ItemStack());
		lua_setfield(L, -2, "item");
		setintfield(L, -1, "time", 0);
		lua_newtable(L);
		lua_setfield(L, -2, "replacements");
	}
	lua_newtable(L); // decremented input table
	lua_pushstring(L, method_s.c_str());
	lua_setfield(L, -2, "method");
	lua_pushinteger(L, width);
	lua_setfield(L, -2, "width");
	push_items(L, input.items);
	lua_setfield(L, -2, "items");
	return 2;
}
Example #2
0
bool ScriptApiItem::item_CraftPredict(ItemStack &item, ServerActiveObject *user,
		const InventoryList *old_craft_grid, const InventoryLocation &craft_inv)
{
	SCRIPTAPI_PRECHECKHEADER

	int error_handler = PUSH_ERROR_HANDLER(L);

	lua_getglobal(L, "core");
	lua_getfield(L, -1, "craft_predict");
	LuaItemStack::create(L, item);
	objectrefGetOrCreate(L, user);

	//Push inventory list
	std::vector<ItemStack> items;
	for (u32 i = 0; i < old_craft_grid->getSize(); i++) {
		items.push_back(old_craft_grid->getItem(i));
	}
	push_items(L, items);

	InvRef::create(L, craft_inv);
	PCALL_RES(lua_pcall(L, 4, 1, error_handler));
	if (!lua_isnil(L, -1)) {
		try {
			item = read_item(L,-1, getServer());
		} catch (LuaError &e) {
			throw LuaError(std::string(e.what()) + ". item=" + item.name);
		}
	}
	lua_pop(L, 2);  // Pop item and error handler
	return true;
}
Example #3
0
bool ScriptApiItem::item_OnCraft(ItemStack &item, ServerActiveObject *user,
		const InventoryList *old_craft_grid, const InventoryLocation &craft_inv)
{
	SCRIPTAPI_PRECHECKHEADER

	lua_pushcfunction(L, script_error_handler);
	int errorhandler = lua_gettop(L);

	lua_getglobal(L, "minetest");
	lua_getfield(L, -1, "on_craft");
	LuaItemStack::create(L, item);
	objectrefGetOrCreate(user);
	
	//Push inventory list
	std::vector<ItemStack> items;
	for(u32 i=0; i<old_craft_grid->getSize(); i++)
		items.push_back(old_craft_grid->getItem(i));
	push_items(L, items);

	InvRef::create(L, craft_inv);
	if(lua_pcall(L, 4, 1, errorhandler))
		scriptError();
	if(!lua_isnil(L, -1)) {
		try {
			item = read_item(L,-1, getServer());
		} catch (LuaError &e) {
			throw LuaError(std::string(e.what()) + ". item=" + item.name);
		}
	}
	lua_pop(L, 2);  // Pop item and error handler
	return true;
}
void push_inventory_list(lua_State *L, Inventory *inv, const char *name)
{
	InventoryList *invlist = inv->getList(name);
	if(invlist == NULL){
		lua_pushnil(L);
		return;
	}
	std::vector<ItemStack> items;
	for(u32 i=0; i<invlist->getSize(); i++)
		items.push_back(invlist->getItem(i));
	push_items(L, items);
}