mc-achievements
Version:
✨ Quickly create custom Minecraft achievements for yourself. Choose the one you like among more than 100 icons.
59 lines (58 loc) • 2.96 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AchievementCreator = exports.ICONS = void 0;
const canvas_1 = require("canvas");
const path_1 = require("path");
const icons_1 = require("./icons");
Object.defineProperty(exports, "ICONS", { enumerable: true, get: function () { return icons_1.ICONS; } });
(0, canvas_1.registerFont)((0, path_1.resolve)(__dirname, "..", "assets", "font.ttf"), {
family: "Minecraft",
});
class AchievementCreator {
static _createCanvas() {
return __awaiter(this, void 0, void 0, function* () {
const background = yield (0, canvas_1.loadImage)((0, path_1.resolve)(__dirname, "..", "assets", "bg.png"));
const canvas = (0, canvas_1.createCanvas)(503, 100);
const ctx = canvas.getContext("2d");
ctx.drawImage(background, 0, 0, 503, 100);
return canvas;
});
}
static _fetchImage(name) {
return __awaiter(this, void 0, void 0, function* () {
const imagePath = (0, path_1.resolve)(__dirname, "..", "assets", "icons", `${name}.png`);
return (0, canvas_1.loadImage)(imagePath);
});
}
static create(_a) {
return __awaiter(this, arguments, void 0, function* ({ icon, title, content, }) {
if (!Object.values(icons_1.ICONS).includes(icon))
throw new Error(`${icon} not found in icons folder. See "ICONS" export for more information.`);
if (title.length > 28)
throw new Error("Title can not be longer than 28 characters");
if (content.length > 28)
throw new Error("Content can not be longer than 28 characters");
const canvas = yield this._createCanvas();
const ctx = canvas.getContext("2d");
ctx.font = "24px Minecraft";
ctx.textAlign = "left";
const image = yield this._fetchImage(icon);
ctx.drawImage(image, 35, 20, image.width, image.height);
ctx.fillStyle = "#f8f628";
ctx.fillText(title, 125, 40);
ctx.fillStyle = "#ffffff";
ctx.fillText(content, 125, 80);
return canvas.toBuffer();
});
}
}
exports.AchievementCreator = AchievementCreator;