Added new items and dynamic select generator

This commit is contained in:
Stijn Bannink 2024-04-14 12:37:48 +02:00
parent c32577266b
commit 44624542e8
Signed by: SBDeveloper
GPG key ID: B730712F2C3A9D7A
8 changed files with 37 additions and 9 deletions

7
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,7 @@
{
"MinifyAll.PrefixOfNewMinifiedFiles": ".min",
"MinifyAll.minifyOnSaveToNewFile": true,
"MinifyAll.disableJson": true,
"MinifyAll.disableHtml": true,
"MinifyAll.openMinifiedDocument": false,
}

View file

@ -1 +1 @@
pre{outline:1px solid #ccc;padding:5px;margin:5px}.string{color:green}.number{color:#ff8c00}.boolean{color:#00f}.null{color:#ff00ff}.key{color:red}
pre{outline:1px solid #ccc;padding:5px;margin:5px;}.string{color:green;}.number{color:darkorange;}.boolean{color:blue;}.null{color:magenta;}.key{color:red;}

View file

@ -1 +1 @@
function syntaxHighlight(e){return"string"!=typeof e&&(e=JSON.stringify(e,void 0,4)),e=e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;"),e.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g,function(e){let t="number";return/^"/.test(e)?t=/:$/.test(e)?"key":"string":/true|false/.test(e)?t="boolean":/null/.test(e)&&(t="null"),'<span class="'+t+'">'+e+"</span>"})}
function syntaxHighlight(e){return"string"!=typeof e&&(e=JSON.stringify(e,void 0,4)),(e=e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")).replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g,(function(e){let t="number";return/^"/.test(e)?t=/:$/.test(e)?"key":"string":/true|false/.test(e)?t="boolean":/null/.test(e)&&(t="null"),'<span class="'+t+'">'+e+"</span>"}))}

11
dist/js/calculator.js vendored
View file

@ -1,6 +1,11 @@
// Damage values for items
// Can be found at: https://minecraft.fandom.com/wiki/
const itemDamages = {
'leather_boots': 64,
'netherite_hoe': 2030
'leather_helmet': 55, // Called a cap for new versions of the game
'leather_chestplate': 80,
'leather_leggings': 75,
'leather_boots': 65,
'netherite_hoe': 2031
};
/**
@ -33,7 +38,7 @@ function buildJSON(item, models) {
for (let i = 0; i < models.length; i++) {
const model = models[i];
const damage = (i + 1) / itemDamages[item];
const damage = (i + 1) / (itemDamages[item]-1);
json['overrides'][i + 1] = {
'predicate': {

View file

@ -1 +1 @@
function buildJSON(e,t){const a={parent:"item/handheld"};a.textures={layer0:"item/"+e},a.overrides=[],a.overrides[0]={predicate:{damaged:0,damage:0},model:"item/"+e};for(let d=0;d<t.length;d++){const r=t[d],i=(d+1)/itemDamages[e];a.overrides[d+1]={predicate:{damaged:0,damage:i},model:r}}return a.overrides[t.length+1]={predicate:{damaged:1,damage:0},model:"item/"+e},JSON.stringify(a,null,2)}const itemDamages={leather_boots:64,netherite_hoe:2030};
const itemDamages={leather_helmet:55,leather_chestplate:80,leather_leggings:75,leather_boots:65,netherite_hoe:2031};function buildJSON(e,t){const a={parent:"item/handheld"};a.textures={layer0:"item/"+e},a.overrides=[],a.overrides[0]={predicate:{damaged:0,damage:0},model:"item/"+e};for(let r=0;r<t.length;r++){const d=t[r],i=(r+1)/(itemDamages[e]-1);a.overrides[r+1]={predicate:{damaged:0,damage:i},model:d}}return a.overrides[t.length+1]={predicate:{damaged:1,damage:0},model:"item/"+e},JSON.stringify(a,null,2)}

18
dist/js/scripts.js vendored
View file

@ -15,6 +15,14 @@ $(document).ready(function () {
const plus = $("#plus");
const input = $("input[name='quant']");
const $select = $('#item');
// Append the items to the select
$.each(itemDamages, function(key, value) {
const itemName = capitalize(key);
$select.append($('<option></option>').attr('value', key).text(itemName));
});
minus.click(function (e) {
const currentVal = parseInt(input.attr('value'));
@ -103,6 +111,16 @@ $(document).ready(function () {
navigator.clipboard.writeText(output.text());
});
/**
* Capitalize a string.
*
* @param {string} str A string to capitalize
* @returns
*/
function capitalize(str) {
return str.split('_').map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(' ');
}
/**
* Get a random model.
*

View file

@ -1 +1 @@
$(document).ready(function(){function t(){const t=["red_car","blue_car","green_car","orange_car","blue_bicycle","red_bicycle","green_bicycle","orange_bicycle"];return t[Math.floor(Math.random()*t.length)]}const e=$("#calculatorForm"),a=$("#formItems"),n=$("#item"),r=$("#itemcount"),i=$("#items"),o=$("#content"),l=$("#copyButton"),c=$("#downloadButton"),s=$("#minus"),u=$("#plus"),d=$("input[name='quant']");s.click(function(t){const e=parseInt(d.attr("value"));isNaN(e)?d.attr("value","0"):(e>d.attr("min")&&d.attr("value",(e-1).toString()),e-1==d.attr("min")&&$(this).attr("disabled",!0),$("#iteminput"+e).remove())}),u.click(function(e){const a=parseInt(d.attr("value"));if(isNaN(a))d.attr("value","0");else{if(a<d.attr("max")&&d.attr("value",(a+1).toString()),a==d.attr("max"))return void $(this).attr("disabled",!0);parseInt(d.attr("value"))>1?s.prop("disabled",!1):s.prop("disabled",!0),i.append('<div id="iteminput'+(a+1)+'" class="mb-3 w-50"><label for="item'+(a+1)+'">Model for damage '+(a+1)+':</label><input type="text" class="form-control" id="item'+(a+1)+'" name="item'+(a+1)+'" placeholder="cars/'+t()+'" required></div>')}}),n.change(function(){if(""!==this.value){a.show();const t=itemDamages[this.value];r.prop("max",t),u.prop("disabled",!1)}else a.hide()}),e.on("submit",function(t){t.preventDefault();const e=n.val(),a=parseInt(d.attr("value")),r=[];for(let t=0;t<a;t++)r[t]=$("#item"+(t+1)).val();const i=buildJSON(e,r);o.html(syntaxHighlight(i));const l="data:text/json;charset=utf-8,"+encodeURIComponent(i);return c.attr("href",l),c.attr("download",e+".json"),!1}),l.click(function(t){t.preventDefault(),navigator.clipboard.writeText(o.text())})});
$(document).ready((function(){const t=$("#calculatorForm"),e=$("#formItems"),a=$("#item"),n=$("#itemcount"),i=$("#items"),r=$("#content"),o=$("#copyButton"),l=$("#downloadButton"),c=$("#minus"),s=$("#plus"),u=$("input[name='quant']"),d=$("#item");$.each(itemDamages,(function(t,e){const a=t.split("_").map((t=>t.charAt(0).toUpperCase()+t.slice(1))).join(" ");d.append($("<option></option>").attr("value",t).text(a))})),c.click((function(t){const e=parseInt(u.attr("value"));isNaN(e)?u.attr("value","0"):(e>u.attr("min")&&u.attr("value",(e-1).toString()),e-1==u.attr("min")&&$(this).attr("disabled",!0),$("#iteminput"+e).remove())})),s.click((function(t){const e=parseInt(u.attr("value"));if(isNaN(e))u.attr("value","0");else{if(e<u.attr("max")&&u.attr("value",(e+1).toString()),e==u.attr("max"))return void $(this).attr("disabled",!0);parseInt(u.attr("value"))>1?c.prop("disabled",!1):c.prop("disabled",!0),i.append('<div id="iteminput'+(e+1)+'" class="mb-3 w-50"><label for="item'+(e+1)+'">Model for damage '+(e+1)+':</label><input type="text" class="form-control" id="item'+(e+1)+'" name="item'+(e+1)+'" placeholder="cars/'+function(){const t=["red_car","blue_car","green_car","orange_car","blue_bicycle","red_bicycle","green_bicycle","orange_bicycle"];return t[Math.floor(Math.random()*t.length)]}()+'" required></div>')}})),a.change((function(){if(""!==this.value){e.show();const t=itemDamages[this.value];n.prop("max",t),s.prop("disabled",!1)}else e.hide()})),t.on("submit",(function(t){t.preventDefault();const e=a.val(),n=parseInt(u.attr("value")),i=[];for(let t=0;t<n;t++)i[t]=$("#item"+(t+1)).val();const o=buildJSON(e,i);r.html(syntaxHighlight(o));const c="data:text/json;charset=utf-8,"+encodeURIComponent(o);return l.attr("href",c),l.attr("download",e+".json"),!1})),o.click((function(t){t.preventDefault(),navigator.clipboard.writeText(r.text())}))}));

View file

@ -30,8 +30,6 @@
<label for="item">The item to use:</label>
<select class="form-control" id="item" name="item" required>
<option value="" selected>Select an item...</option>
<option value="leather_boots">Leather Boots</option>
<option value="netherite_hoe">Netherite Hoe</option>
</select>
</div>
<div id="formItems" style="display: none;">