Added custom model data support

This commit is contained in:
Stijn Bannink 2024-04-14 17:36:28 +02:00
parent 3712035058
commit 56c3374b31
Signed by: SBDeveloper
GPG key ID: B730712F2C3A9D7A
5 changed files with 93 additions and 52 deletions

72
dist/js/calculator.js vendored
View file

@ -11,17 +11,14 @@ const itemDamages = {
/** /**
* Generate the JSON file. * Generate the JSON file.
* *
* @param supportsCMD Whether custom model data is supported.
* @param item The item to generate it for. * @param item The item to generate it for.
* @param namespace The namespace to use. * @param namespace The namespace to use.
* @param models An array of models to insert. * @param models An array of models to insert.
* @returns {String} The JSON for the texturepack. * @returns {String} The JSON for the texturepack.
*/ */
function buildJSON(item, namespace, models) { function buildJSON(supportsCMD, item, namespace, models) {
if (namespace == "minecraft") { namespace = (namespace == "minecraft") ? "" : namespace + ":";
namespace = "";
} else {
namespace += ":";
}
const json = {}; const json = {};
@ -35,36 +32,49 @@ function buildJSON(item, namespace, models) {
//Insert models //Insert models
json['overrides'] = []; json['overrides'] = [];
//Insert default model if (supportsCMD) {
json['overrides'][0] = { //Insert custom model data models
'predicate': { for (let i = 0; i < models.length; i++) {
'damaged': 0, json['overrides'][i + 1] = {
'damage': 0 'predicate': {
}, 'custom_model_data': i + 1
'model': namespace + 'item/' + item },
}; 'model': namespace + 'item/' + models[i]
};
for (let i = 0; i < models.length; i++) { }
const model = models[i]; } else {
const damage = (i + 1) / (itemDamages[item] - 1); //Insert not damaged item
json['overrides'][0] = {
json['overrides'][i + 1] = {
'predicate': { 'predicate': {
'damaged': 0, 'damaged': 0,
'damage': damage 'damage': 0
}, },
'model': model 'model': namespace + 'item/' + item
};
//Insert damaged models
for (let i = 0; i < models.length; i++) {
const model = models[i];
const damage = (i + 1) / (itemDamages[item] - 1);
json['overrides'][i + 1] = {
'predicate': {
'damaged': 0,
'damage': damage
},
'model': model
};
}
//Insert fully damaged item
json['overrides'][models.length + 1] = {
'predicate': {
'damaged': 1,
'damage': 0
},
'model': 'item/' + item
}; };
} }
//Insert damaged model
json['overrides'][models.length + 1] = {
'predicate': {
'damaged': 1,
'damage': 0
},
'model': 'item/' + item
};
return JSON.stringify(json, null, 2); return JSON.stringify(json, null, 2);
} }

View file

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

49
dist/js/scripts.js vendored
View file

@ -1,6 +1,8 @@
let namespace = "vp"; let namespace = "vp";
$(document).ready(function () { $(document).ready(function () {
const $cmd = $("#custommodeldata");
const form = $("#calculatorForm"); const form = $("#calculatorForm");
const formOptions = $("#formItems"); const formOptions = $("#formItems");
@ -20,12 +22,6 @@ $(document).ready(function () {
const $select = $('#item'); const $select = $('#item');
// Append the items to the select
$.each(itemDamages, function (key, value) {
const itemName = capitalize(key) + " (supports " + value + " models)";
$select.append($('<option></option>').attr('value', key).text(itemName));
});
minus.click(function (e) { minus.click(function (e) {
const currentVal = parseInt(input.attr('value')); const currentVal = parseInt(input.attr('value'));
@ -63,19 +59,51 @@ $(document).ready(function () {
minus.prop('disabled', true); minus.prop('disabled', true);
} }
items.append("<div id=\"iteminput" + (currentVal + 1) + "\" class=\"mb-3 w-50\"><label for=\"item" + (currentVal + 1) + "\">Model for damage " + (currentVal + 1) + ":</label><div class=\"input-group mb-3\"><span class=\"input-group-text\" id=\"namespace" + (currentVal + 1) + "\">" + namespace + ":</span><input type=\"text\" class=\"form-control\" id=\"item" + (currentVal + 1) + "\" name=\"item" + (currentVal + 1) + "\" placeholder=\"cars/" + randomModel() + "\" required></div></div>"); items.append("<div id=\"iteminput" + (currentVal + 1) + "\" class=\"mb-3 w-50\"><label for=\"item" + (currentVal + 1) + "\">Model for damage " + (currentVal + 1) + ":</label><div class=\"input-group mb-3\"><span class=\"input-group-text\" id=\"namespace" + (currentVal + 1) + "\">" + namespace + ":item/</span><input type=\"text\" class=\"form-control\" id=\"item" + (currentVal + 1) + "\" name=\"item" + (currentVal + 1) + "\" placeholder=\"cars/" + randomModel() + "\" required></div></div>");
} else { } else {
input.attr('value', '0'); input.attr('value', '0');
} }
}); });
// Append the items to the select on load (default for CMD is checked)
$.each(itemDamages, function (key, value) {
const itemName = capitalize(key);
$select.append($('<option></option>').attr('value', key).text(itemName));
});
$cmd.change(function () {
$select.find('option').each(function () {
const key = $(this).val();
const itemName = capitalize(key);
const hasSupportText = ($(this).text().includes('(supports'));
if (key === "") {
// Keep the default "Select an item..." option as is
return;
}
if ($cmd.is(':checked')) {
if (hasSupportText) {
const updatedText = itemName;
$(this).text(updatedText);
}
} else {
if (!hasSupportText) {
const updatedText = itemName + ' (supports ' + itemDamages[key] + ' models)';
$(this).text(updatedText);
}
}
});
});
nsp.change(function () { nsp.change(function () {
namespace = nsp.val(); namespace = nsp.val();
if (namespace == "") { if (namespace == "") {
namespace = "minecraft"; namespace = "minecraft";
} }
nsp.val(namespace); nsp.val(namespace);
$("span[id^='namespace']").text(namespace + ":"); $("span[id^='namespace']").text(namespace + ":item/");
}); });
item.change(function () { item.change(function () {
@ -95,6 +123,9 @@ $(document).ready(function () {
form.on("submit", function (e) { form.on("submit", function (e) {
e.preventDefault(); e.preventDefault();
//Check if custommodeldata is supported
const supportsCMD = $cmd.is(":checked");
//Get selected item and amount of models //Get selected item and amount of models
const selectedItem = item.val(); const selectedItem = item.val();
const currentVal = parseInt(input.attr('value')); const currentVal = parseInt(input.attr('value'));
@ -106,7 +137,7 @@ $(document).ready(function () {
} }
//Convert to JSON, and set in content field (with syntax highlighting) //Convert to JSON, and set in content field (with syntax highlighting)
const json = buildJSON(selectedItem, namespace, models); const json = buildJSON(supportsCMD, selectedItem, namespace, models);
output.html(syntaxHighlight(json)); output.html(syntaxHighlight(json));
//And fix the Download button //And fix the Download button

View file

@ -1 +1 @@
let namespace="vp";$(document).ready((function(){const t=$("#calculatorForm"),e=$("#formItems"),a=$("#item"),n=$("#itemcount"),i=$("#items"),r=$("#content"),o=$("#copyButton"),c=$("#downloadButton"),s=$("#minus"),l=$("#plus"),p=$("input[name='quant']"),m=$("#namespace"),u=$("#item");$.each(itemDamages,(function(t,e){const a=t.split("_").map((t=>t.charAt(0).toUpperCase()+t.slice(1))).join(" ")+" (supports "+e+" models)";u.append($("<option></option>").attr("value",t).text(a))})),s.click((function(t){const e=parseInt(p.attr("value"));isNaN(e)?p.attr("value","0"):(e>p.attr("min")&&p.attr("value",(e-1).toString()),e-1==p.attr("min")&&$(this).attr("disabled",!0),$("#iteminput"+e).remove())})),l.click((function(t){const e=parseInt(p.attr("value"));if(isNaN(e))p.attr("value","0");else{if(e<p.attr("max")&&p.attr("value",(e+1).toString()),e==p.attr("max"))return void $(this).attr("disabled",!0);parseInt(p.attr("value"))>1?s.prop("disabled",!1):s.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><div class="input-group mb-3"><span class="input-group-text" id="namespace'+(e+1)+'">'+namespace+':</span><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></div>')}})),m.change((function(){namespace=m.val(),""==namespace&&(namespace="minecraft"),m.val(namespace),$("span[id^='namespace']").text(namespace+":")})),a.change((function(){if(""!==this.value){e.show();const t=itemDamages[this.value];n.prop("max",t),l.prop("disabled",!1)}else e.hide()})),t.on("submit",(function(t){t.preventDefault();const e=a.val(),n=parseInt(p.attr("value")),i=[];for(let t=0;t<n;t++)i[t]=$("#item"+(t+1)).val();const o=buildJSON(e,namespace,i);r.html(syntaxHighlight(o));const s="data:text/json;charset=utf-8,"+encodeURIComponent(o);return c.attr("href",s),c.attr("download",e+".json"),!1})),o.click((function(t){t.preventDefault(),navigator.clipboard.writeText(r.text())}))})); let namespace="vp";$(document).ready((function(){const t=$("#custommodeldata"),e=$("#calculatorForm"),a=$("#formItems"),n=$("#item"),i=$("#itemcount"),c=$("#items"),o=$("#content"),s=$("#copyButton"),r=$("#downloadButton"),l=$("#minus"),p=$("#plus"),m=$("input[name='quant']"),u=$("#namespace"),d=$("#item");function f(t){return t.split("_").map((t=>t.charAt(0).toUpperCase()+t.slice(1))).join(" ")}l.click((function(t){const e=parseInt(m.attr("value"));isNaN(e)?m.attr("value","0"):(e>m.attr("min")&&m.attr("value",(e-1).toString()),e-1==m.attr("min")&&$(this).attr("disabled",!0),$("#iteminput"+e).remove())})),p.click((function(t){const e=parseInt(m.attr("value"));if(isNaN(e))m.attr("value","0");else{if(e<m.attr("max")&&m.attr("value",(e+1).toString()),e==m.attr("max"))return void $(this).attr("disabled",!0);parseInt(m.attr("value"))>1?l.prop("disabled",!1):l.prop("disabled",!0),c.append('<div id="iteminput'+(e+1)+'" class="mb-3 w-50"><label for="item'+(e+1)+'">Model for damage '+(e+1)+':</label><div class="input-group mb-3"><span class="input-group-text" id="namespace'+(e+1)+'">'+namespace+':item/</span><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></div>')}})),$.each(itemDamages,(function(t,e){const a=f(t);d.append($("<option></option>").attr("value",t).text(a))})),t.change((function(){d.find("option").each((function(){const e=$(this).val(),a=f(e),n=$(this).text().includes("(supports");if(""!==e)if(t.is(":checked")){if(n){const t=a;$(this).text(t)}}else if(!n){const t=a+" (supports "+itemDamages[e]+" models)";$(this).text(t)}}))})),u.change((function(){namespace=u.val(),""==namespace&&(namespace="minecraft"),u.val(namespace),$("span[id^='namespace']").text(namespace+":item/")})),n.change((function(){if(""!==this.value){a.show();const t=itemDamages[this.value];i.prop("max",t),p.prop("disabled",!1)}else a.hide()})),e.on("submit",(function(e){e.preventDefault();const a=t.is(":checked"),i=n.val(),c=parseInt(m.attr("value")),s=[];for(let t=0;t<c;t++)s[t]=$("#item"+(t+1)).val();const l=buildJSON(a,i,namespace,s);o.html(syntaxHighlight(l));const p="data:text/json;charset=utf-8,"+encodeURIComponent(l);return r.attr("href",p),r.attr("download",i+".json"),!1})),s.click((function(t){t.preventDefault(),navigator.clipboard.writeText(o.text())}))}));

View file

@ -22,24 +22,24 @@
<body> <body>
<div class="container"> <div class="container">
<div class="jumbotron my-4"> <div class="jumbotron my-4">
<img src="https://sbdevelopment.tech/wp-content/uploads/2023/09/logo2-light.png" alt="SBDevelopment logo" <h1 class="display-4 pb-1"><img src="https://sbdevelopment.tech/wp-content/uploads/2023/09/logo2-light.png"
class="mb-3" style="width: 15em;"> alt="SBDevelopment logo" class="pe-1 pb-2" style="width: 3em;"> Model File Generator</h1>
<h1 class="display-4">Model File Generator</h1>
<p class="lead">Generate the resourcepack asset files for your VehiclesPlus models.</p> <p class="lead">Generate the resourcepack asset files for your VehiclesPlus models.</p>
<hr class="my-4"> <hr class="mb-3">
<p class="fst-italic">You indicate which item you want to use and the path to the models models for each <p class="fst-italic">You indicate which item you want to use and the path to the models models for each
value. This tool then value. This tool then
creates the .json file for you.</p> creates the .json file for you.</p>
</div> </div>
<form id="calculatorForm"> <form id="calculatorForm">
<!-- <div class="mb-3 w-50" ?> <div class="mb-3 w-50" ?>
<label for="custommodeldata">Custom model data (1.14+):</label> <label for="custommodeldata">Custom model data (1.14+):</label>
<div class="form-check form-switch form-switch-lg"> <div class="form-check form-switch form-switch-lg">
<input class="form-check-input" type="checkbox" role="switch" id="custommodeldata" checked> <input class="form-check-input" type="checkbox" role="switch" id="custommodeldata" checked>
</div> </div>
<div class="form-text" id="namespace-help">1.14+ supports custom model data, which is preferred. If you <div class="form-text" id="namespace-help">1.14+ supports custom model data, which is preferred by
VehiclesPlus. If you
use 1.13 or lower, the calculator works with damage values.</div> use 1.13 or lower, the calculator works with damage values.</div>
</div> --> </div>
<div class="mb-3 w-50"> <div class="mb-3 w-50">
<label for="namespace">Namespace:</label> <label for="namespace">Namespace:</label>
<input type="text" class="form-control" id="namespace" name="namespace" required value="vp"> <input type="text" class="form-control" id="namespace" name="namespace" required value="vp">
@ -72,9 +72,9 @@
</div> </div>
<div id="items"> <div id="items">
<div id="iteminput1" class="mb-3 w-50"> <div id="iteminput1" class="mb-3 w-50">
<label for="item1" class="form-label">Model for damage 1:</label> <label for="item1" class="form-label">Model for value 1:</label>
<div class="input-group mb-3"> <div class="input-group mb-3">
<span class="input-group-text" id="namespace1">vp:</span> <span class="input-group-text" id="namespace1">vp:item/</span>
<input type="text" class="form-control" id="item1" name="item1" placeholder="cars/blue_car" <input type="text" class="form-control" id="item1" name="item1" placeholder="cars/blue_car"
required> required>
</div> </div>
@ -83,7 +83,7 @@
<button type="submit" class="btn btn-primary">Calculate</button> <button type="submit" class="btn btn-primary">Calculate</button>
</div> </div>
</form> </form>
<hr class="my-4"> <hr class="mb-3">
<h6>Output JSON:</h6> <h6>Output JSON:</h6>
<pre id="content" class="mb-3 w-50">Fill in the form above to get an output.</pre> <pre id="content" class="mb-3 w-50">Fill in the form above to get an output.</pre>
<button id="copyButton" class="btn btn-primary mb-3 mr-3">Copy</button> <button id="copyButton" class="btn btn-primary mb-3 mr-3">Copy</button>