Skip to content

Commit bfbdd65

Browse files
committed
Adds new import option to import item display settings to the display tab (currently doesn't refresh until you switch tabs)
1 parent bdf06f1 commit bfbdd65

2 files changed

Lines changed: 60 additions & 1 deletion

File tree

src/azurelib_utils/codec.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,52 @@ export function maybeExportItemJson(options = {}, as) {
456456
return this;
457457
}
458458

459+
export function maybeImportItemJson() {
460+
Blockbench.import({
461+
resource_id: 'model',
462+
type: 'json',
463+
extensions: ['json'],
464+
readtype: 'text',
465+
multiple: false
466+
}, (files) => {
467+
if (!files?.[0]) return;
468+
469+
let json;
470+
try {
471+
json = JSON.parse(files[0].content);
472+
} catch {
473+
return Blockbench.showQuickMessage('[AzureLib] Invalid JSON file.');
474+
}
475+
476+
if (json.parent !== 'builtin/entity' || typeof json.display !== 'object') {
477+
return Blockbench.showQuickMessage('[AzureLib] Not a valid AzureLib display file.');
478+
}
479+
480+
Project.display_settings = {};
481+
482+
for (const [slot, data] of Object.entries(json.display)) {
483+
if (!DisplayMode.slots.includes(slot)) continue;
484+
485+
const rotation = Array.isArray(data.rotation) ? data.rotation.slice() : [0, 0, 0];
486+
const translation = Array.isArray(data.translation) ? data.translation.slice() : [0, 0, 0];
487+
const scale = Array.isArray(data.scale) ? data.scale.slice() : [1, 1, 1];
488+
489+
const y = Number(translation[1]) || 0;
490+
translation[1] = Math.round((y + 1.5) * 100) / 100;
491+
const displaySlot = new DisplaySlot(slot);
492+
displaySlot.rotation = rotation;
493+
displaySlot.translation = translation;
494+
displaySlot.scale = scale;
495+
496+
Project.display_settings[slot] = displaySlot;
497+
}
498+
499+
Project.saved = false;
500+
501+
Blockbench.showQuickMessage('[AzureLib] Display settings imported successfully.');
502+
});
503+
}
504+
459505
var codec = Codecs.bedrock;
460506

461507
var format = new ModelFormat({

src/azurelib_utils/index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { loadAnimationUI, unloadAnimationUI } from './animationUi';
1313
import { removeMonkeypatches } from './utils';
1414
import { loadKeyframeOverrides, unloadKeyframeOverrides } from './keyframe';
1515
import azurelibSettings, { OBJ_TYPE_OPTIONS, onSettingsChanged } from './settings';
16-
import codec, { loadCodec, unloadCodec, maybeExportItemJson } from './codec';
16+
import codec, { loadCodec, unloadCodec, maybeExportItemJson, maybeImportItemJson } from './codec';
1717

1818
const SUPPORTED_BB_VERSION_RANGE = `${blockbenchConfig.min_version} - ${blockbenchConfig.max_version}`;
1919
if (!semverSatisfies(semverCoerce(Blockbench.version), SUPPORTED_BB_VERSION_RANGE)) {
@@ -23,6 +23,7 @@ if (!semverSatisfies(semverCoerce(Blockbench.version), SUPPORTED_BB_VERSION_RANG
2323
(function () {
2424
let exportAction;
2525
let exportDisplayAction;
26+
let importDisplayAction;
2627
let button;
2728

2829
Plugin.register("azurelib_utils", Object.assign(
@@ -102,6 +103,17 @@ if (!semverSatisfies(semverCoerce(Blockbench.version), SUPPORTED_BB_VERSION_RANG
102103
});
103104
MenuBar.addAction(exportDisplayAction, "file.export");
104105

106+
importDisplayAction = new Action({
107+
id: "import_AzureLib_display",
108+
name: "Import AzureLib Display Settings",
109+
icon: "icon-bow",
110+
description: "Import Display Settings into the display tab.",
111+
category: "file",
112+
condition: () => Format.id === "azure_model",
113+
click: maybeImportItemJson,
114+
});
115+
MenuBar.addAction(importDisplayAction, "file.import");
116+
105117
button = new Action('azurelib_settings', {
106118
name: 'AzureLib Model Settings',
107119
description: 'Change model type.',
@@ -130,6 +142,7 @@ if (!semverSatisfies(semverCoerce(Blockbench.version), SUPPORTED_BB_VERSION_RANG
130142
onunload() {
131143
exportAction.delete();
132144
exportDisplayAction.delete();
145+
importDisplayAction.delete();
133146
button.delete();
134147
unloadKeyframeOverrides();
135148
unloadAnimationUI();

0 commit comments

Comments
 (0)