Skip to content

Commit

Permalink
Add script files
Browse files Browse the repository at this point in the history
  • Loading branch information
JasperLorelai committed Aug 6, 2021
0 parents commit b45a649
Show file tree
Hide file tree
Showing 13 changed files with 27,520 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.idea
4 changes: 4 additions & 0 deletions Config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
version: "1.16.5",
versionCompared: ""
}
40 changes: 40 additions & 0 deletions Util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const yaml = require("yaml");
const fs = require("fs");

module.exports = {
fileExists(path) {
return fs.existsSync(path);
},
loadYAML(path) {
return fs.readFileSync(path).toString();
},
loadYAMLasJSON(path) {
return yaml.parse(this.loadYAML(path));
},
saveJSONasYAML(path, json) {
fs.writeFileSync(path, yaml.stringify(json));
},
rename(path, name) {
fs.renameSync(path, name);
},
createBaseMenu(prefixTitle) {
return {
"spell-class": ".MenuSpell",
"helper-spell": true,
tags: ["NotSilenceable"],
delay: 1,
"stay-open-non-option": true,
title: "&9" + prefixTitle + " Sounds",
options: {}
};
},
createSoundSpell(sound, category) {
return {
"spell-class": ".instant.DummySpell",
"helper-spell": true,
tags: ["NotSilenceable"],
"variable-mods-cast": ["Sound =" + sound, "Menu =" + category],
modifiers: ["chance 100 cast sb-selectsound"]
};
}
}
23 changes: 23 additions & 0 deletions checkSoundConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const deepObjectDiff = require("deep-object-diff").detailedDiff;

const Util = require("./Util");
const Config = require("./Config");
const {version, versionCompared} = Config;

function getFile(version) {
const fileName = "./soundConfig/" + version + ".yml";

if (!Util.fileExists(fileName)) {
console.log("Final sound config for version '" + version + "' does not exit.");
return null;
}

return Util.loadYAMLasJSON(fileName);
}

const configFirst = getFile(version);
if (!configFirst) return;
const configSecond = getFile(versionCompared);
if (!configSecond) return;

Util.saveJSONasYAML("./soundConfig/difference-" + version + "-" + versionCompared + ".yml", deepObjectDiff(configSecond, configFirst));
51 changes: 51 additions & 0 deletions createSoundConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const Util = require("./Util");
const Config = require("./Config");
const {version} = Config;

const soundFileName = "./sounds/" + version + ".txt";

if (!Util.fileExists(soundFileName)) {
console.log("Sound list for version '" + version + "' does not exit.");
return;
}

function createObject(sound) {
let obj = {icon: ""};
if (!sound.includes(".")) {
// Store the singleton sound deep.
if (sound) obj.sounds = [sound];
return obj;
}
const index = sound.indexOf(".");
const key = sound.substring(0, index);
const value = sound.substr(index + 1);
obj[key] = createObject(value);
return obj;
}

const sounds = {};

const soundsTemp = Util.loadYAML(soundFileName)
.split("\n")
.sort()
.map(sound => createObject(sound.trim()));

function mergeConfig(config, sound) {
// We don't need to merge icons.
const key = Object.keys(sound).find(key => key !== "icon");
if (!key) return config;
// Array merging - for sounds.
if (key === "sounds" && config.sounds) config.sounds.push(sound.sounds[0]);
// Object merging.
else config[key] = config.hasOwnProperty(key) ? mergeConfig(config[key], sound[key]) : sound[key];
return config;
}

soundsTemp.filter((sound, index) => {
sound = JSON.stringify(sound);
// noinspection JSIncompatibleTypesComparison
return index === soundsTemp.findIndex(otherSound => JSON.stringify(otherSound) === sound);
})
.forEach(sound => mergeConfig(sounds, sound));

Util.saveJSONasYAML("./soundConfig/" + version + ".yml", sounds);
123 changes: 123 additions & 0 deletions createSoundboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
const Util = require("./Util");
const Config = require("./Config");
const {version} = Config;

String.prototype.toFormalCase = function() {
return this.length > 2 ? this.charAt(0).toUpperCase() + this.substr(1).toLowerCase() : this.toUpperCase();
}

String.prototype.toTitleCase = function() {
return this.replace(/_/g, " ").split(" ").map(e => e.toFormalCase()).join(" ");
}

const soundConfigFileName = "./soundConfig/final-" + version + ".yml";

if (!Util.fileExists(soundConfigFileName)) {
console.log("Final sound config for version '" + version + "' does not exit.");
return;
}

const spells = Util.loadYAMLasJSON("./soundboards/base.yml");
const soundConfig = Util.loadYAMLasJSON(soundConfigFileName);
const soundSpells = {};

function getCategories(config) {
return Object.keys(config).filter(key => !["icon", "sounds"].includes(key));
}


function createOption(options, categoryName, spell, item, slot) {
const option = {spell: spell, item: item};
if (slot) option.slot = slot;
options[categoryName.replace(/\s/g, "")] = option;
}

function paginateOptions(options, title, page = 0) {
let keys = Object.keys(options);
let spellName = title.substring(0, title.length - 1);
const hasPages = keys.length > 45 || page > 0;
if (hasPages) spellName += page + 1;
let titleName = spellName.substr(spellName.lastIndexOf("-") + 1);
if (hasPages) titleName = titleName.substring(0, titleName.length - 1);
spells[spellName] = Util.createBaseMenu(titleName.toFormalCase(), hasPages ? " - Page " + (page + 1) : "");

for (let i = 0; i < Math.min(45, keys.length); i++) {
const optionName = keys[i];
const option = options[optionName];
delete options[optionName];
option.slot = i;
spells[spellName].options[optionName] = option;
}

const hasNextPage = keys.length > 45;
const lastRow = Math.ceil(Math.min(45, keys.length) / 9) * 9;
const spellNamePageless = spellName.substr(0, spellName.length - 1);

// Add previous page button.
if (page > 0) createOption(spells[spellName].options, "Button_Previous_Page", spellNamePageless + (page ? page : ""), {type: "arrow", name: "&6Previous Page"}, lastRow + 2);

// Add back button.
let previousSpellName = spellName.substr(0, spellName.lastIndexOf("-"));
const spellNames = Object.keys(spells);
// If the back spell has pages, look for the paged version instead.
if (previousSpellName && !spellNames.includes(previousSpellName)) previousSpellName = spellNames.find(spell => spell.startsWith(previousSpellName));
if (previousSpellName) {
if (hasNextPage) createOption(spells[spellName].options, "Button_Home", previousSpellName, {type: "book",name: "&6Home"}, lastRow + 4);
else {
createOption(spells[spellName].options, "Button_Back", previousSpellName, {type: "book", name: "&6Back"}, lastRow + 4);
return;
}
}

// Set up next page.
if (!hasNextPage) return;
createOption(spells[spellName].options, "Button_Next_Page", spellNamePageless + (page + 2), {type: "arrow", name: "&6Next Page"}, lastRow + 6);
paginateOptions(options, title, page + 1);
}

function createCategorySpells(config, title, soundName) {
const categories = getCategories(config);
const {icon} = config;
const sounds = config.sounds || [];

// Collect options.
const options = {};

sounds.forEach(sound => {
const fullSound = (soundName ? soundName + "." : "") + sound;
const soundSpellName = "sound-" + fullSound.replace(/\./g, "-");
createOption(options, "Sound_" + sound.toTitleCase(), soundSpellName, {
type: icon,
name: "&e" + sound.toTitleCase() + " Sound",
lore: ["&7&o" + fullSound]
});
soundSpells[soundSpellName] = Util.createSoundSpell(fullSound, title.substring(3, title.length - 1).replace(/-/g, "_"));
});

categories.forEach(category => {
const categoryConfig = config[category];
const categoryName = category.toTitleCase();
const optionSpellName = title + category + (getCategories(categoryConfig).length > 45 ? "1" : "");
createOption(options, categoryName, optionSpellName, {type: categoryConfig.icon, name: "&6" + categoryName + " Sounds"});
if (!categoryConfig.sounds) return;
const menuName = (title + category).substr(3).replace(/-/g, "_");
spells["sb-back-edit"].modifiers.push("variablestringequals Menu:" + menuName + " cast " + optionSpellName);
});

paginateOptions(options, title);

// Go deeper.
categories.forEach(category => {
if (!config.hasOwnProperty(category)) return;
createCategorySpells(config[category], title + category + "-", (soundName ? soundName + "." : "") + category);
});
}

createCategorySpells(soundConfig, "sb-", "");

spells["sb"].title = "&9Soundboard";

// Add all the sound spells at the end.
Object.keys(soundSpells).forEach(key => spells[key] = soundSpells[key]);

Util.saveJSONasYAML("./soundboards/" + version + ".yml", spells);
16 changes: 16 additions & 0 deletions finaliseSoundConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const Util = require("./Util");
const Config = require("./Config");
const {version} = Config;

function makeName(prefix = "") {
return "./soundConfig/" + prefix + version + ".yml";
}

const soundConfigFileName = makeName();

if (!Util.fileExists(soundConfigFileName)) {
console.log("Sound config for version '" + version + "' does not exit.");
return;
}

Util.rename(soundConfigFileName, makeName("final-"));
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "soundboard",
"version": "1.0.0",
"description": "Minecraft soundboard.",
"main": "createSoundboard.js",
"scripts": {
"reinstall": "npm i",
"create_sooundboard": "node createSoundboard.js",
"create_sound_config": "node createSoundConfig.js",
"finalise_sound_config": "node finaliseSoundConfig.js",
"check_sound_config": "node checkSoundConfig.js"
},
"repository": {
"url": "https://github.com/JasperLorelai/minecraft-soundboard"
},
"engines": {
"node": "14.17.0",
"npm": "6.14.13"
},
"author": "JasperLorelai",
"license": "ISC",
"dependencies": {
"deep-object-diff": "^1.1.0",
"yaml": "^1.10.2"
}
}
Loading

0 comments on commit b45a649

Please sign in to comment.